Module:OSMWikiBase/sandbox

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

getLabel

Usage
{{#invoke: OSMWikiBase|getLabel}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
Returns
if any of these properties are available, returns property value together with localized label (non-English): permanent key ID (P16), permanent tag ID (P19), permanent relation role ID (P21). Otherwise, the label in the requested language or English.

getDescription

Usage
{{#invoke: OSMWikiBase|getDescription}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
Returns
the description of the given entity in the specified language if available, otherwise the description in English if available, otherwise an empty string.

formatEntity

Usage
{{#invoke:OSMWikiBase|formatEntity}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
3 - Anchor in the data item page, e.g. P5 (optional)
Returns
a wiki markup link to the Item or Property page, with the label with Q-code as text, and the description as the tooltip.
local getArgs = require('Module:Arguments').getArgs
local p = {}

local wb = function()
	if mw and mw.wikibase and mw.wikibase.getEntity then
		return nil
	else
		return "<span class='error'>DataItems are having issues</span>"
	end
end


local getPermanentID = function(entity, tagType)
	if entity and entity.claims then
		if tagType == 'key' then
			if entity.claims['P16'] then 
				return entity.claims['P16'][1].mainsnak.datavalue.value 
			else return "-"
			end 
		elseif tagType == 'value' then
			if entity.claims['P19'] then 
				return entity.claims['P19'][1].mainsnak.datavalue.value 
			else return "-"
			end 
		elseif tagType == 'prefix' then
			if entity.claims['P52'] then 
				return entity.claims['P52'][1].mainsnak.datavalue.value 
			else return "-"
			end 
		elseif tagType == 'suffix' then
			if entity.claims['P53'] then 
				return entity.claims['P53'][1].mainsnak.datavalue.value 
			else return "-"
			end 
		else return "Invalid or not specified type of tag: Use parameters key, value, prefix, or suffix."
		end
	end
	return "Invalid ID" 
end

--------------------------------------------------------------------------------
-- Public functions
--------------------------------------------------------------------------------

function p.getLabel(frame) 
	local entityObject = mw.wikibase.getEntity(frame.args.entity) 
	if entityObject and entityObject.claims then 
		if entityObject.claims['P16'] then 
			return entityObject.claims['P16'][1].mainsnak.datavalue.value 
		else 
			return entityObject.labels.en.value 
		end 
	else 
		return entityObject.labels.en.value 
	end 
	return "Invalid ID" 
end

function p.getPermanentID(frame)
	local args = getArgs(frame)
	return wb() or getPermanentID(mw.wikibase.getEntity(args[1]), args[2])
end

function p.getDescription(frame)
	if frame.args.entity then
		local entity = frame.arg.entity
	else
		local entity = mw.wikibase.getEntityIdForCurrentPage()
	end
	local entity = frame.args.entity 
	local lang = frame.args.lang 
	local language = string.lower(string.sub(lang,1, 3)) 
	if (string.find(language, ":")) then 
		language = string.lower(string.sub(language, 1, 2)) 
	end 
	if string.len(language) > 2 then 
		language = "en" 
	end 
	local entityObject = mw.wikibase.getEntity(entity) 
	if entityObject.descriptions then 
		if entityObject.descriptions[language] then 
			local s = string.gsub(entityObject.descriptions[language].value, "\"", "'") 
			return s 
		else 
			local s = string.gsub(entityObject.descriptions.en.value, "\"", "'") 
			return s 
		end 
	else 
		return "no description found" 
	end 
end

function p.getElement(frame) 
	local elementObject = frame.args.element 
	if string.match(frame.args.template_value, "") then 
		local entityObject = p.getRelevantEntity(frame.args.entity) 
		local response = "" 
		if entityObject and entityObject.claims and entityObject.claims['P5'] then 
			for k, v in pairs(entityObject.claims['P5']) do 
				local elementInEntity = v.mainsnak.datavalue.value.id 
				if string.match(elementObject, elementInEntity) then 
					response = "yes" 
				else response = "no" 
				end 
				if string.match(response, "yes") then 
					return "yes" 
				end 
			end 
		end 
		return "no" 
	end 
	return frame.args.template_value 
end

function p.getStatement(frame) 
	if string.match(frame.args.template_value, "") then 
		local entityObject = p.getRelevantEntity(frame.args.entity) 
		local property = frame.args.property 
		return entityObject.claims[property][1].mainsnak.datavalue.value 
	else 
		return frame.args.template_value 
	end 
end

function p.getRelevantEntity(entity) 
	if entity then 
		return mw.wikibase.getEntity(entity) 
	else 
		return mw.wikibase.getEntity(mw.wikibase.getEntityIdForCurrentPage()) 
	end 
end

function p.getStatusProposal(frame)
	local entityObject = p.getRelevantEntity(frame.args.entity)
	if entityObject and entityObject.claims["P6"] and entityObject.claims["P6"][1].references[1].snaks["P11"][1] then
		return entityObject.claims["P6"][1].references[1].snaks["P11"][1].datavalue.value
	end
end

return p