Module:OSMWikiBase

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 titleParser = require('Module:OsmPageTitleParser')
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

-- Given an entity, get key/tag/relation string with the optional localization,
-- or just get the label in the requested language
local getLabel = function(entity, lang)
	if entity then
		if entity.claims then
			for _, prop in ipairs({'P16', 'P19', 'P21', 'P52', 'P53'}) do
				local statement = entity:getBestStatements(prop)[1]
				if statement then
					local result = statement.mainsnak.datavalue.value
					if lang and lang ~= 'en' then
						-- get nativekey/nativevalue if available
						local native = entity:getLabel(lang)
						if native then
							result = result .. ' (' .. native .. ')'
						end
					end
					return result
				end
			end
		end
		return (
			entity:getLabel(lang) or
			entity:getLabel('en') or
			'(' .. entity:getId() .. ')'
		)
	else
		return "ERROR: Invalid ID"
	end
end

-- Given an entity, just get the label in the requested language
local getLabelonly = function(entity, lang)
	if entity then
		local label = entity:getLabel(lang) or entity:getLabel('en')
		return label
	end
end

-- Given an entity and a type of tag (key, value, prefix, suffix), just get the respective permanent ID
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

-- Given an entity, get the description in the requested language or fallback to EN
local getDescription = function(entity, lang)
	local res = ''
	if entity.descriptions then
		res = entity:getDescription(lang) or entity:getDescription('en') or ''
	end
	return res
end

-- Formats entity's label with the optional description
local formatEntity = function(entity, lang, anchor)
	local label = getLabel(entity, lang)
	if not entity then
		return label
	end
	local desc = getDescription(entity, lang)
	local ns = entity.id:sub(1, 1) == 'Q' and 'Item:' or 'Property:'
	local link = ns .. entity.id
	if anchor then
		link = link .. '#' .. anchor
	end
	local text = mw.html.create( 'span' )
	text:attr('title', desc)
		:wikitext(mw.text.nowiki(label) .. ' ')
		:tag('small')
    		:wikitext('(' .. entity.id .. ')')

	return '[[' .. link .. '|' .. tostring(text) .. ']]'
end

-- if lang is not given, tries to figure it out based on the current page title
local getLang = function(lang)
	if not lang then
		local parsed = titleParser.parseTitle(mw.title.getCurrentTitle())
		if parsed then
		 	return parsed.language:getCode()
	 	end
	end
	return lang
end

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

-- Debug console usage:    =p.formatEntity({'Q888'})
-- with lang               =p.formatEntity({'Q888', 'ru'})
function p.formatEntity(frame)
	local args = getArgs(frame)
	return wb() or formatEntity(mw.wikibase.getEntity(args[1]), getLang(args[2]), args[3])
end

-- Debug console usage:    =p.getLabel({'Q888'})
-- with lang               =p.getLabel({'Q888', 'ru'})
function p.getLabel(frame)
	local args = getArgs(frame)
	return wb() or getLabel(mw.wikibase.getEntity(args[1]), getLang(args[2]))
end

-- Debug console usage:    =p.getLabelonly({'Q888'})
-- with lang               =p.getLabelonly({'Q888', 'ru'})
function p.getLabelonly(frame)
	local args = getArgs(frame)
	return wb() or getLabelonly(mw.wikibase.getEntity(args[1]), getLang(args[2]))
end

-- Debug console usage:    =p.getPermanentID({'Q464', 'key'})
--                         =p.getPermanentID({'Q888', 'value'})
--                         =p.getPermanentID({'Q174', 'prefix'})
--                         =p.getPermanentID({'Q22132', 'suffix'})
function p.getPermanentID(frame)
	local args = getArgs(frame)
	return wb() or getPermanentID(mw.wikibase.getEntity(args[1]), args[2])
end

-- Debug console usage:    =p.getDescription({'Q888'})
-- with lang               =p.getDescription({'Q888', 'ru'})
function p.getDescription(frame)
	local args = getArgs(frame)
	return wb() or getDescription(mw.wikibase.getEntity(args[1]), getLang(args[2]))
end

return p