Modul:Wikidata/Formatters/wikibase-entityid

Z Wikizdrojů, volně dostupné knihovny

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Wikidata/Formatters/wikibase-entityid

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 lib.formatTextInLanguage(mw.ustring.format('[[%s|%s]]', link, label), lang)
			else
				return '[[' .. link .. '|' .. label .. ']]'
			end
		else
			return '[[' .. link .. ']]'
		end
	end
	if label then
		if lang and lang ~= i18n.lang then
			return lib.formatTextInLanguage(label, lang)
		else
			return label
		end
	end
	return nil
end

local function getLabel(entityId, options)
	local label, lang
	local Module = require 'Modul:Wikidata-WP'
	--if options.label and lib.isPropertyId(options.label) then
	if options.label == 'short' then
		local fallback
		if options.withlang then
			fallback = {options.withlang, 'mul'}
		else
			fallback = {i18n.lang, 'mul'}
		end
		for _, withlang in ipairs(fallback) do
			label, lang = Module.getRawValueFromLua{
				id = entityId,
				property = 'P1813',
				rank = 'valid',
				sort = {'rank'},
				withlang = withlang,
			}, withlang
			if label then
				break
			end
		end
		if not label then
			label = Module.getRawValueFromLua{
				id = entityId, property = 'P835',
			} or Module.getRawValueFromLua{
				id = entityId, property = 'P428',
			}
			lang = nil
		end
	elseif options.label == 'gender' then
		local gender = Module.getRawValueFromLua{
			id = options.id,
			property = 'P21'
		}
		local property = ({ Q6581072 = 'P2521', --[[ Q6581097 = 'P3321' ]] })[gender]
		if property then
			local fallback
			if options.withlang then
				fallback = {options.withlang}
			else
				fallback = {i18n.lang}
			end
			for _, withlang in ipairs(fallback) do
				label, lang = Module.getRawValueFromLua{
					id = entityId,
					property = property,
					rank = 'valid',
					sort = {'rank'},
					withlang = withlang,
				}, withlang
				if label then
					break
				end
			end
		end
	elseif options.label == 'unitsymbol' then
		local fallback
		if options.withlang then
			fallback = {options.withlang, 'en', 'mul'}
		else
			fallback = {i18n.lang, 'en', 'mul'}
		end
		for _, withlang in ipairs(fallback) do
			label = Module.getRawValueFromLua{
				id = entityId,
				property = 'P5061',
				withlang = withlang,
			}
			if label then
				break
			end
		end
	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 lib.IsOptionTrue(options, 'nolink') then
		return nil
	end
	local link
	if options.link then
		if options.link == 'wikidata' then
			link = entityId
		else
			link = mw.wikibase.getSitelink(entityId, options.link)
		end
		if link then
			link = lib.getInterwikiPrefix(options.link) .. link
		end
	else
		link = mw.wikibase.getSitelink(entityId)
	end
	return link
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