Modul:Wikidata/Formatters/wikibase-entityid

Z Wikipedie, otevřené encyklopedie
Skočit na: Navigace, Hledání

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Wikidata/Formatters/wikibase-entityid/Dokumentace

require 'Modul:No globals'

local p = {}

local lib = require 'Modul:Wikidata/lib'
local i18n = mw.loadData('Modul:Wikidata/i18n')

local function constructLink(label, link, lang)
	if link then
		if label then
			if lang and lang ~= i18n.lang then
				return mw.ustring.format('<span lang="%s">[[%s|%s]]</span>', lang, link, label)
			else
				return '[[' .. link .. '|' .. label .. ']]'
			end
		else
			return '[[' .. link .. ']]'
		end
	end
	if label then
		if lang and lang ~= i18n.lang then
			return mw.ustring.format('<span lang="%s">%s</span>', lang, label)
		else
			return label
		end
	end
	return nil
end

-- @deprecated
p.getLinkWhenNonexistingLabel = lib.getLinkWhenNonexistingLabel

-- @deprecated
function p.formatEntityWithGender(value, options)
	options.label = 'gender'
	return p.formatValue(value, options)
end

local function getLabel(entityId, options)
	local label, lang
	local Module = require 'Modul:Wikidata'
	--if options.label and lib.isPropertyId(options.label) then
	if options.label == 'short' then
		label = Module.formatStatementsFromLua{
			addclass = false,
			id = entityId,
			limit = 1,
			property = 'P1813',
			rank = 'valid',
			sort = {'rank'},
			withlang = {i18n.lang, 'mul'},
		} or Module.getRawValueFromLua{
			id = entityId, property = 'P835',
		} or Module.getRawValueFromLua{
			id = entityId, property = 'P428',
		}
	elseif options.label == 'gender' then
		local gender = Module.getRawValueFromLua{
			id = options.id,
			property = 'P21'
		}
		local property = ({ Q6581072 = 'P2521', --[[ Q6581097 = 'P3321' ]] })[gender]
		if property then
			label, lang = Module.formatStatementsFromLua{
				addclass = false,
				id = entityId,
				limit = 1,
				property = property,
				rank = 'valid',
				sort = {'rank'},
				withlang = i18n.lang,
			}, i18n.lang
		end
	elseif options.label == 'unitsymbol' then
		label = Module.getRawValueFromLua{
			id = entityId,
			property = 'P558'
		}
	end
	if not label then
		if options.withlang then
			label, lang = mw.wikibase.getLabelByLang(entityId, options.withlang), options.withlang
		else
			label, lang = mw.wikibase.getLabelWithLang(entityId)
		end
		if label then
			label = mw.text.nowiki(label)
		end
	end
	return label, lang
end

local function getSitelink(entityId, options)
	if not lib.IsOptionTrue(options, 'nolink') then
		return mw.wikibase.sitelink(entityId)
	end
	return nil
end

function p.formatEntityId(entityId, options)
	local options = options or {}
	local label, lang = getLabel(entityId, options)
	local link = getSitelink(entityId, options)
	return constructLink(label, link, lang) or lib.getLinkWhenNonexistingLabel(entityId)
end

function p.getRawValue(value, options)
	if lib.IsOptionTrue(options or {}, 'numeric') then
		return value['numeric-id']
	end
	return value.id
end

p.formatRawValue = p.formatEntityId

function p.formatValue(value, options)
	return p.formatEntityId(p.getRawValue(value, {}), options)
end

return p