Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:City: Difference between revisions

From Neodyland Wiki
define default link to Wikipedia instead of Commons for empty strings
expand
Line 1: Line 1:
local p = {}
local p = {}
local data = mw.loadData("Module:City/data")
local wikidata = require "Module:Wikidata"


function p._city(place, lang, link)
function p._city(place, lang, link)


local item
    -- === STEP 1: if "place" is empty than do nothing ==============
 
if (not place) or (place == "") then
if (not place) or (place == "") then
return nil
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
end
 
if string.match(place, "Q%d+") then
    -- === STEP 3: Check if "place"  holds a q-code or matches any of the hardwired names  ==============
item = place
local wikidata = require("Module:Wikidata")
if string.match(place, "^Q%d+$") then
return wikidata._getLabel(place, {lang = lang, link = link})
else
else
item = data[mw.ustring.lower(place)]
local LookupTable = require("Module:City/data")
local item = LookupTable[mw.ustring.lower(place)]
if item then
return wikidata._getLabel(item, {lang = lang, link = link})
end
end
end
if not item then
 
return place
-- === 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 mw.title.new( place, '' ).exists then
return "[[" .. place .. "]]"
elseif mw.title.new( place, 'category' ).exists then
return string.format("[[:Category:%s|%s]]", place,place)
end
end
end
if (not link) or (mw.text.trim(link) == "") then
 
link = "wikipedia"
-- return as is
end
return place
return wikidata._getLabel(item, {lang = lang, link = link})
end
end


function p.city(frame)
function p.city(frame)
local args = frame.args
local args = frame.args
return p._city(args[1], args.lang, args.link)
args.place = mw.text.trim(args.place)
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = mw.message.new( "lang" ):plain() -- get user's chosen language
end
if (not args.link) or (mw.text.trim(args.link) == "") then
args.link = "wikipedia"
end
return p._city(args.place, args.lang, args.link)
end
end


return p
return p

Revision as of 03:54, 12 September 2016

Module documentation[ create · purge ]
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
		local LookupTable = require("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 mw.title.new( place, '' ).exists then
			return "[[" .. place .. "]]"
		elseif mw.title.new( place, 'category' ).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
	args.place = mw.text.trim(args.place)
	if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then 
		args.lang = mw.message.new( "lang" ):plain() -- get user's chosen language 
	end
	if (not args.link) or (mw.text.trim(args.link) == "") then
		args.link = "wikipedia"
	end
	return p._city(args.place, args.lang, args.link)
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.