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, entityId, 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
	else 
		return p.getLinkWhenNonexistingLabel(entityId)
	end
end

function p.getLinkWhenNonexistingLabel(entityId)
	local i18n = mw.loadData('Modul:Wikidata/i18n')
	local ImageFormatter = require 'Modul:ImageFormatter'
	return ImageFormatter.makeImage('Wikidata-edit.svg', {
		description = i18n['missing-label'],
		link = 'd:' .. entityId,
		size = '27x17px'
	}) .. '<code>[[d:' .. entityId .. '|' .. entityId .. ']]</code>' .. lib.category('missing-label')
end

function p.formatEntityWithGender(value, options)
	local entityId = lib.getEntityIdFromValue(value)
	local Module = require 'Modul:Wikidata'
	local gender = Module.getRawValueFromLua{
		entity = options.entity,
		property = 'P21'
	}

	local label, lang
	if gender == 'Q6581072' then
		label = Module.formatStatementsFromLua{
			id = entityId,
			limit = 1,
			property = 'P2521',
			rank = 'valid',
			sort = 'rank',
			withlang = 'cs' -- TODO: options.withlang or default_langs
		}
	end
	if not label then
		label, lang = lib.getLabelInLanguage(entityId, default_langs)
	end

	local link = mw.wikibase.sitelink(entityId)
	return constructLink(label, link, entityId, lang)
end

function p.formatUnit(unit)
	local entityId = p.getItemIdFromURI(unit)
	if not entityId then
		return nil
	end
	local Module = require 'Modul:Wikidata'
	local text, lang = Module.getRawValueFromLua{
		id = entityId,
		property = 'P558'
	} or lib.getLabelInLanguage(entityId, default_langs)
	local link = mw.wikibase.sitelink(entityId)
	return constructLink(text, link, entityId, lang)
end

function p.formatEntityId(entityId, options)
	local options = options or {}
	local label, lang = lib.getLabelInLanguage(entityId, options.withlang or default_langs)
	local link = mw.wikibase.sitelink(entityId)
	return constructLink(label, link, entityId, lang)
end

function p.getItemIdFromURI(uri)
	return mw.ustring.match(uri, '(Q%d+)')
end

return p