Module:City: Difference between revisions
From Neodyland Wiki
More actions
fix treatment of the default language; use more efficient mw.loadData |
remove dependence on Module:Wikidata |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p._getLabel(item, lang, link) | |||
local entity, langList, sitelink, label | |||
entity = mw.wikibase.getEntity(item); | |||
langList = mw.language.getFallbacksFor(lang) | |||
table.insert(langList, 1, lang) | |||
-- choose label | |||
for i, language in ipairs(langList) do | |||
label = entity:getLabel(language) | |||
if label then break end | |||
end | |||
if not label then | |||
label = item | |||
end | |||
-- allow different link formats | |||
if link=='-' then | |||
return label | |||
elseif link=='wikidata' then | |||
return mw.ustring.format('[[d:%s|%s]]', item, label) | |||
elseif link=='commons' then | |||
sitelink = entity:getSitelink('commonswiki') | |||
if sitelink==label then | |||
return mw.ustring.format('[[%s]]', sitelink) | |||
elseif sitelink then | |||
return mw.ustring.format('[[%s|%s]]', sitelink, label) | |||
end | |||
end | |||
-- apply default "Wikipadia" link format | |||
for i, language in ipairs(langList) do | |||
sitelink = entity:getSitelink(language .. 'wiki') | |||
if sitelink then | |||
lang = language | |||
break | |||
end | |||
end | |||
if not sitelink then | |||
return mw.ustring.format('[[d:%s|%s]]', item, label) -- no wiki sitelink, so link to wikidata | |||
else | |||
return mw.ustring.format('[[:%s:%s|%s]]', lang, sitelink, label) | |||
end | |||
end | |||
function p._city(place, lang, link) | function p._city(place, lang, link) | ||
| Line 13: | Line 57: | ||
end | end | ||
-- === STEP 3: Check if "place" holds a q-code or matches any of the hardwired names ============== | |||
if string.match(place, "^Q%d+$") then | if string.match(place, "^Q%d+$") then | ||
return | return p._getLabel(place, lang, link) | ||
else | else | ||
-- if multiple calls to {{City}} from a single file, than mw.loadData should load [[Module:City/data]] only once | -- if multiple calls to {{City}} from a single file, than mw.loadData should load [[Module:City/data]] only once | ||
| Line 22: | Line 65: | ||
local item = LookupTable[mw.ustring.lower(place)] | local item = LookupTable[mw.ustring.lower(place)] | ||
if item then | if item then | ||
return | return p._getLabel(item, lang, link) | ||
end | end | ||
end | end | ||
Revision as of 20:27, 19 September 2016
This documentation is transcluded from Module:City/doc.
local p = {}
function p._getLabel(item, lang, link)
local entity, langList, sitelink, label
entity = mw.wikibase.getEntity(item);
langList = mw.language.getFallbacksFor(lang)
table.insert(langList, 1, lang)
-- choose label
for i, language in ipairs(langList) do
label = entity:getLabel(language)
if label then break end
end
if not label then
label = item
end
-- allow different link formats
if link=='-' then
return label
elseif link=='wikidata' then
return mw.ustring.format('[[d:%s|%s]]', item, label)
elseif link=='commons' then
sitelink = entity:getSitelink('commonswiki')
if sitelink==label then
return mw.ustring.format('[[%s]]', sitelink)
elseif sitelink then
return mw.ustring.format('[[%s|%s]]', sitelink, label)
end
end
-- apply default "Wikipadia" link format
for i, language in ipairs(langList) do
sitelink = entity:getSitelink(language .. 'wiki')
if sitelink then
lang = language
break
end
end
if not sitelink then
return mw.ustring.format('[[d:%s|%s]]', item, label) -- no wiki sitelink, so link to wikidata
else
return mw.ustring.format('[[:%s:%s|%s]]', lang, sitelink, label)
end
end
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 ==============
if string.match(place, "^Q%d+$") then
return p._getLabel(place, lang, 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 p._getLabel(item, lang, 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