Module:OSMWikiBase/sandbox

From OpenStreetMap Wiki
Jump to navigation Jump to search
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