Модуль:Песочница/CupIvan/wikidata/песочница

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
local debug = require('Модуль:Песочница/CupIvan/debug')
local p = {}

local MAX_LANGUAGES = 7 -- если больше - то сворачиваем

function p.languages(frame)
	local from = frame:getParent().args.from
	local entity = mw.wikibase.getEntity(from)
	if entity == nil then return "" end
	if entity['claims']['P407'] == nil then return "" end

	local n = 0; local all = {}
	local is_ru, is_en, is_multi, multi_n
	for k, a in pairs(entity['claims']['P407'])
	do
		if a['mainsnak'] ~= nil then
			local is_insert_to_all = true
			if a['mainsnak']['datavalue']['value']['id'] == 'Q7737' then is_ru = 1; is_insert_to_all = false end
			if a['mainsnak']['datavalue']['value']['id'] == 'Q1860' then is_en = 1; is_insert_to_all = false end
			if a['mainsnak']['datavalue']['value']['id'] == 'Q20923490' then
				is_multi = 1; is_insert_to_all = false
				if a.qualifiers and a.qualifiers['P1114'] then
					multi_n = tonumber(a.qualifiers['P1114'][1].datavalue.value.amount)
				end
			else
				n = n + 1
				if is_insert_to_all then
					table.insert(all, entityShortName(a.mainsnak.datavalue.value.id))
				end
			end
		end
	end

	local a = {}
	if multi_n then n = multi_n end
	if is_ru then n = n - 1; table.insert(a, entityShortName('Q7737')) end
	if is_en then n = n - 1; table.insert(a, entityShortName('Q1860')) end
	if table.getn(a) + n <= MAX_LANGUAGES then
		for k,v in pairs(all) do table.insert(a, v) end
		all = {}; n = 0
	end
	if is_multi and not multi_n then
		if is_ru or is_en then
			table.insert(a, "другие")
		else
			return "несколько языков"
		end
	else
		if n > 0 then
			local lang = mw.getLanguage('ru')
			local plural = lang:plural(n, {"", "а", "ов"})
			if is_ru or is_en then
				table.insert(a, "ещё&nbsp;"..n.."&nbsp;язык"..plural)
			else
				table.insert(a, n.."&nbsp;язык"..plural)
			end
		end
	end

	local st = mw.text.listToText(a)
	if table.getn(all) > 0 then
		local args = {header=st, content=mw.text.listToText(all)}
		args["шрифт заголовка"] = "normal"
		args["выравнивание заголовка"] = "left"
		args["тип"] = "transparent"
		st = frame:expandTemplate({title="Скрытый блок", args=args})
	end
	return st
end

-- FIXME: вынести в отдельный модуль
function entityShortName(Q)
	local entity = mw.wikibase.getEntity(Q)
	local shortName = _property(entity, 'P1813')
	local name = entityUrl(Q)
	if shortName == '' then shortName = entityTitle(Q) end
	if shortName ~= '' then
		if name == '' then name = shortName else name = name..'|'..shortName end
	end
	return '[['..name..']]'
end
-- заголовок записи
function entityTitle(Q)
	local entity = mw.wikibase.getEntity(Q)
	if not entity then return '' end
	if entity['labels']['ru'] ~= nil then return entity['labels']['ru']['value'] end
	if entity['labels']['en'] ~= nil then return entity['labels']['en']['value'] end
end
function entityUrl(Q)
	local entity = mw.wikibase.getEntity(Q)
	if not entity then return '' end
	if entity['sitelinks']['ruwiki'] ~= nil then return ':ru:'..entity['sitelinks']['ruwiki']['title'] end
	if entity['sitelinks']['enwiki'] ~= nil then return ':en:'..entity['sitelinks']['enwiki']['title'] end
	return ''
end
function _property(a, P)
	local claim = a.claims[P]
	if not claim then return '' end
	local v = claim[1]['mainsnak']['datavalue']['value']
	if v.text then return v.text end
	return entityTitle(v.id)
end
return p