Module:Wikidata/Formatters/wikibase-entityid

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Documentation icon Τεκμηρίωση module[δημιουργία]
--require 'Module:No globals'

local p = {}

local lib = require 'Module:Wikidata/lib'
local i18n = mw.loadData('Module: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 'Module: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'},
		}
	elseif options.label == 'gender' then
		local gender = Module.getRawValueFromLua{
			id = options.id,
			property = 'P21'
		}
		if gender == 'Q6581072' then
			label, lang = Module.formatStatementsFromLua{
				addclass = false,
				id = entityId,
				limit = 1,
				property = 'P2521',
				rank = 'valid',
				sort = {'rank'},
				withlang = i18n.lang,
			}, i18n.lang
		end
	elseif options.label == 'unitsymbol' then
		label = Module.getRawValueFromLua{
			id = entityId,
			property = 'P5061',
			withlang = i18n.lang
		}
	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) or ('d:' .. 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