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), 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 = {}


-- 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'}) 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, 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 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 getLabel(mw.wikibase.getEntity(args[1]), getLang(args[2]))
end

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

return p