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
function p.qCode(): adjustment for last edit: don't use redirected gallery page name for further search to avoid redirect targets like that of Susa on this page
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p._city(place, lang, link)
function p.qCode(place)
-- recover a q-code based on place name, also if one can link to a page on commons return such link
local item, link = nil, nil


     -- === STEP 1: if "place" is empty than do nothing ==============
     -- === STEP 1: if "place" is empty than return nothing ==============
if (not place) or (place == "") then
if (not place) or (place == "") then
return ""
return item, link
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
    -- === STEP 3: Check if "place"  holds a q-code or matches any of the hardwired names  ==============
  -- === STEP 2: Check if "place"  holds a q-code or matches any of the hardwired names  ==============
local wikidata = require("Module:Wikidata label")
if string.match(place, "^Q%d+$") then
if string.match(place, "^Q%d+$") then
return wikidata._getLabel(place, lang, link)
return place, link -- place string contains a q-code
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
local LookupTable = mw.loadData("Module:City/data")
local LookupTable = mw.loadData("Module:City/data")
local item = LookupTable[mw.ustring.lower(place)]
item = LookupTable[mw.ustring.lower(place)]
if item then
if item then
return wikidata._getLabel(item, lang, link)
return item, link
end
end
end
end


-- === STEP 4: Check if {{{1}}} matches existing template, gallery or category and if so provide the link  ===
-- === STEP 3: Check if "place" matches existing template, gallery or category and if so provide the link  ===
if #place>=3 and #place<40 then
if #place>=3 and #place<40 then
local page = mw.title.new( place, '' )
local page = mw.title.new( place, '' )
if page and page.exists then
if page and page.exists then
return "[[" .. place .. "]]"
local page_name = place
if page.redirectTarget then
page_name = page.redirectTarget.prefixedText
end
item = mw.wikibase.getEntityIdForTitle( page_name )
link = "[[" .. page_name .. "]]"
if item then
return item, link
end
end
end
page = mw.title.new( place, 'category' )
page = mw.title.new( place, 'category' )
if page and page.exists then
if page and page.exists then
return string.format("[[:Category:%s|%s]]", place,place)  
local resolve_redirect = require("Module:Resolve category redirect").rtarget
end  
place = resolve_redirect(place)
item = mw.wikibase.getEntityIdForTitle( 'Category:' .. place )
link = "[[:Category:" .. place .. "|" .. place .. "]]"
if item then
local entity = mw.wikibase.getEntityObject(item)
if entity then
local s = entity:getBestStatements( 'P31' )
if s[1] and s[1].mainsnak.datavalue.value.id=="Q4167836" then
-- if "instance of "(P31) = "Wikimedia category" (Q4167836)
s = entity:getBestStatements( 'P301' ) -- category's main topic
if s[1] then -- if property "category's main topic" (P301) is set
item = s[1].mainsnak.datavalue.value.id
else
item = nil
end
end
end
end
return item, link
end
end
-- === STEP 4: Check if "place" matches an interwiki link to a Wikipedia
-- page with the link label matching the full page name that is linked to a
-- Wikidata item ===
local langcode, pagename = mw.ustring.match(place, "^%[%[w?:(.+):(.+)|%2%]%]$")
if langcode and pagename then
item = mw.wikibase.getEntityIdForTitle( pagename, langcode .. 'wiki' )
link = place
end
return item, link
end
 
function p._city(place, lang, link)
if (not place) or (place == "") then
return "" --if "place" is empty than do nothing
end
if string.match(place, "[%{%{|%[%[].+[%}%}|%]%]]") then
return place --  if "place" already has a link and if so than skip the rest of the template
end
    -- Check if we can recover a q-code
local item, linkStr = p.qCode(place)
if item then
local wikidata = require("Module:Wikidata label")
return wikidata._getLabel(item, lang, link)
elseif linkStr then
return linkStr -- no q-code but we matched one of the galleries or categories
end
end



Latest revision as of 18:55, 26 October 2024

Module documentation[ create · purge ]
This documentation is transcluded from Module:City/doc.
local p = {}

function p.qCode(place)
	-- recover a q-code based on place name, also if one can link to a page on commons return such link
	local item, link = nil, nil

    -- === STEP 1: if "place" is empty than return nothing ==============
	if (not place) or (place == "") then
		return item, link
	end
	
  -- === STEP 2: Check if "place"  holds a q-code or matches any of the hardwired names  ==============
	if string.match(place, "^Q%d+$") then
		return place, link -- place string contains a q-code
	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")
		item = LookupTable[mw.ustring.lower(place)]
		if item then
			return item, link
		end
	end

	-- === STEP 3: Check if "place" 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
			local page_name = place
			if page.redirectTarget then
				page_name = page.redirectTarget.prefixedText
			end
			item = mw.wikibase.getEntityIdForTitle( page_name )
			link = "[[" .. page_name .. "]]"
			if item then
				return item, link
			end
		end
		
		page = mw.title.new( place, 'category' )
		if page and page.exists then
			local resolve_redirect = require("Module:Resolve category redirect").rtarget
			place = resolve_redirect(place)
			item = mw.wikibase.getEntityIdForTitle( 'Category:' .. place )
			link = "[[:Category:" .. place .. "|" .. place .. "]]"
			if item then
				local entity = mw.wikibase.getEntityObject(item)
				if entity then 
					local s = entity:getBestStatements( 'P31' )
					if s[1] and s[1].mainsnak.datavalue.value.id=="Q4167836" then 
						-- if "instance of "(P31) = "Wikimedia category" (Q4167836)
						s = entity:getBestStatements( 'P301' ) -- category's main topic 
						if s[1] then -- if property "category's main topic" (P301) is set
							item = s[1].mainsnak.datavalue.value.id 
						else
							item = nil
						end
					end
				end
			end
			return item, link
		end
	end
	
	-- === STEP 4: Check if "place" matches an interwiki link to a Wikipedia
	-- page with the link label matching the full page name that is linked to a
	-- Wikidata item ===
	local langcode, pagename = mw.ustring.match(place, "^%[%[w?:(.+):(.+)|%2%]%]$")
	if langcode and pagename then
		item = mw.wikibase.getEntityIdForTitle( pagename, langcode .. 'wiki' )
		link = place
	end
	return item, link
end

function p._city(place, lang, link)
	if (not place) or (place == "") then
		return "" --if "place" is empty than do nothing
	end
	
	if string.match(place, "[%{%{|%[%[].+[%}%}|%]%]]") then
		return place --  if "place" already has a link and if so than skip the rest of the template
	end
	
    -- Check if we can recover a q-code
	local item, linkStr = p.qCode(place)
	if item then
		local wikidata = require("Module:Wikidata label")
		return wikidata._getLabel(item, lang, link)
	elseif linkStr then
		return linkStr -- no q-code but we matched one of the galleries or categories
	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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.