Přeskočit na obsah

Modul:Wikidata/Formatters/wikibase-entityid

Tato stránka je zamčena
Z Wikipedie, otevřené encyklopedie

require 'Modul:No globals'

local p = {}

local lib = require 'Modul:Wikidata/lib'

-- @const
local default_langs = { 'cs', 'en', 'sk' }

local function constructLink(label, link, lang)
	if link then
		if label then
			if lang and lang ~= 'cs' 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 ~= 'cs' 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 = {'cs', 'mul'},
		}
	elseif options.label == 'gender' then
		local gender = Module.getRawValueFromLua{
			entity = options.entity,
			property = 'P21'
		}
		if gender == 'Q6581072' then
			label, lang = Module.formatStatementsFromLua{
				addclass = false,
				id = entityId,
				limit = 1,
				property = 'P2521',
				rank = 'valid',
				sort = {'rank'},
				withlang = 'cs',
			}, 'cs'
		end
	elseif options.label == 'unitsymbol' then
		label = Module.getRawValueFromLua{
			id = entityId,
			property = 'P558'
		}
	end
	if not label then
		label, lang = lib.getLabelInLanguage(entityId, options.withlang or default_langs)
		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