Module:City: Difference between revisions
From Neodyland Wiki
More actions
m Protected Module:City: Widely used template ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) |
fix treatment of the default language; use more efficient mw.loadData |
||
| Line 18: | Line 18: | ||
return wikidata._getLabel(place, {lang = lang, link = link}) | return wikidata._getLabel(place, {lang = lang, link = link}) | ||
else | else | ||
local LookupTable = | -- if multiple calls to {{City}} from a single file, than mw.loadData should load [[Module:City/data]] only once | ||
local LookupTable = mw.loadData("Module:City/data") | |||
local item = LookupTable[mw.ustring.lower(place)] | local item = LookupTable[mw.ustring.lower(place)] | ||
if item then | if item then | ||
| Line 43: | Line 44: | ||
function p.city(frame) | function p.city(frame) | ||
local args = frame.args | local args = frame.args | ||
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then | if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then | ||
args.lang = | args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language | ||
end | end | ||
if (not args.link) or (mw.text.trim(args.link) == "") then | if (not args.link) or (mw.text.trim(args.link) == "") then | ||
args.link = "wikipedia" | args.link = "wikipedia" | ||
end | end | ||
args.place = mw.text.trim(args.place or '') | |||
return p._city(args.place, args.lang, args.link) | return p._city(args.place, args.lang, args.link) | ||
end | end | ||
return p | return p | ||
Revision as of 17:11, 19 September 2016
This documentation is transcluded from Module:City/doc.
local p = {}
function p._city(place, lang, link)
-- === STEP 1: if "place" is empty than do nothing ==============
if (not place) or (place == "") then
return ""
end
--=== STEP 2: Check if "place" already has a link and if so than skip the rest of the template ==============
if string.match(place, "[%{%{|%[%[].+[%}%}|%]%]]") then
return place
end
-- === STEP 3: Check if "place" holds a q-code or matches any of the hardwired names ==============
local wikidata = require("Module:Wikidata")
if string.match(place, "^Q%d+$") then
return wikidata._getLabel(place, {lang = lang, link = link})
else
-- if multiple calls to {{City}} from a single file, than mw.loadData should load [[Module:City/data]] only once
local LookupTable = mw.loadData("Module:City/data")
local item = LookupTable[mw.ustring.lower(place)]
if item then
return wikidata._getLabel(item, {lang = lang, link = link})
end
end
-- === STEP 4: Check if {{{1}}} matches existing template, gallery or category and if so provide the link ===
if #place>=3 and #place<40 then
local page = mw.title.new( place, '' )
if page and page.exists then
return "[[" .. place .. "]]"
end
page = mw.title.new( place, 'category' )
if page and page.exists then
return string.format("[[:Category:%s|%s]]", place,place)
end
end
-- return as is
return place
end
function p.city(frame)
local args = frame.args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
end
if (not args.link) or (mw.text.trim(args.link) == "") then
args.link = "wikipedia"
end
args.place = mw.text.trim(args.place or '')
return p._city(args.place, args.lang, args.link)
end
return p