Module:OSMWikiBase

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  .. 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