Modulo:Motto

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
require('strict')

local mWikidata = require('Modulo:Wikidata')

-- Restituisce la categoria Wikidata appropriata in base al valore utente e a quello su Wikidata.
--
-- @param {table}
-- @return {string}
local function getWikidataCategory(t)
	local pattern = '^%[?%[?([^|%]]*).*$'
	local cat = ''
	local retrieved = false
	local addCat = function (v)
		cat = cat .. string.format('[[Categoria:%s]]', v)
	end
	-- rimuove i wikilink per il confronto
	for i, v in pairs(t) do
		t[i] = v and v:match(pattern)
	end

	for i = 2, 3 do
		local userval, wdval = t[1], t[i]
		local wdprop = i == 2 and 'P1546' or 'P1451'
		if userval then
			if not wdval then
				addCat(wdprop .. ' assente su Wikidata')
			elseif wdval == mw.language.getContentLanguage():ucfirst(userval) then
				addCat(wdprop .. ' uguale su Wikidata')
			else
				addCat(wdprop .. ' differente su Wikidata')
			end
		elseif wdval and not retrieved then
			retrieved = true
			addCat(wdprop .. ' letta da Wikidata')
		end
	end
	return cat
end

-- Restituisce il motto nella P1546 o nella P1451 su Wikidata.
--
-- @return {string} motto ricavato dalla P1546
-- @return {string} motto ricavato dalla P1451
local function getWikidataMotto()
	local mottoP1546
	local mottoId = mWikidata._getProperty({ 'P1546', n = 1, formatting = 'raw' })
	local mottoP1451 = mWikidata._getProperty({ 'P1451', n = 1, formatting = 'raw' })

	if mottoId and mw.wikibase.isValidEntityId(mottoId) then
		local sitelink = mw.wikibase.getSitelink(mottoId)
		local label = mw.wikibase.getLabel(mottoId) or sitelink
		if sitelink then
			mottoP1546 = string.format('[[%s|%s]]', sitelink, label)
		else
			mottoP1546 = label
		end
	end

	return mottoP1546, mottoP1451
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per {{#invoke:Motto|valore}}.
function p.valore(frame)
	local cat = ''
	local userval = mw.text.trim(frame:getParent().args.motto or '')
	if userval == '' then userval = nil end
	local wdP1546, wdP1451 = getWikidataMotto()

	if mw.title.getCurrentTitle().namespace == 0 then
		cat = getWikidataCategory({ userval, wdP1546, wdP1451 })
	end

	return (userval or wdP1546 or wdP1451 or '') .. cat
end

return p