Модуль:Wikibase

Материал из Wikivoyage

Для документации этого модуля может быть создана страница Модуль:Wikibase/doc

local wiki = 
{
	langcode = mw.language.getContentLanguage().code
}

-- internationalisation
local i18n = {
    ["errors"] = {
        ["entity-not-found"] = "Не найдена запись в Викиданных",
    }
}

local p = { }

local function printError(code)
	return '<span class="error">' .. (i18n.errors[code] or code) .. '</span>'
end

function p.descriptionIn(frame)
	local langcode = frame.args[1]
	local id = frame.args[2]
	-- return description of a Wikidata entity in the given language or the default language of this Wikipedia site
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.descriptions then
		local desc = entity.descriptions[langcode or wiki.langcode]
		if desc then return desc.value end
	end
end

function p.labelIn(frame)
	local langcode = frame.args[1]
	local id = frame.args[2]
	-- return label of a Wikidata entity in the given language or the default language of this Wikipedia site
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.labels then
		local label = entity.labels[langcode or wiki.langcode]
		if label then return label.value end
	end
end

function p.pageId(frame)
	local id = frame.args[1]
	local entity = mw.wikibase.getEntity(id)
	if not entity then return nil else return entity.id end
end

function p.labelOf(frame)
	local id = frame.args[1]
	-- returns the label of the given entity/property id
	-- if no id is given, the one from the entity associated with the calling Wikipedia article is used
	if not id then
		local entity = mw.wikibase.getEntity()
		if not entity then return printError("entity-not-found") end
		id = entity.id
	end
	return mw.wikibase.label(id)
end

function p.sitelinkOf(frame)
	local id = frame.args[1]
	-- returns the Wikipedia article name of the given entity
	-- if no id is given, the one from the entity associated with the calling Wikipedia article is used
	if not id then
		local entity = mw.wikibase.getEntity()
		if not entity then return printError("entity-not-found") end
		id = entity.id
	end
	return mw.wikibase.sitelink(id)
end

function p.label(frame)
	local id = frame.args[1]
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.labels then
		local label = entity.labels['ru']
		if label then
			return '[[:d:'..id..'|'..label.value..' <small>('..id..')</small>]]'
		     else
			return '[[:d:'..id..'|'..entity.labels['en'].value..' <small>(in English; '..id..')</small>]]'
		end
	end
end

function p.labelp(frame)
	local id = frame.args[1]
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.labels then
		local label = entity.labels['ru']
		if label then
			return '[[:d:Property:'..id..'|'..label.value..' <small>('..id..')</small>]]'
		     else
			return '[[:d:Property:'..id..'|'..entity.labels['en'].value..' <small>(in English; '..id..')</small>]]'
		end
	end
end

return p