Modulo:Senato.it

Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Senato.it/man (modifica · cronologia)
Sandbox: Modulo:Senato.it/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Senato.it/test (modifica · cronologia · Esegui)
Modulo che implementa il template {{Senato.it}}. È usato anche dal modulo:Collegamenti esterni per generare automaticamente uno o più link alle schede di un senatore su senato.it a seconda dei dati su Wikidata.
local getArgs = require('Modulo:Arguments').getArgs
local mWikidataCheck = require('Modulo:Controllo Wikidata')._main
local mEditAtWikidata = require('Modulo:Modifica su Wikidata')._showMessage
local mPipetrick = require('Modulo:Pipetrick')._pipetrick
local mRoman = require('Modulo:Numero romano').main
local mCitazione = require('Modulo:Citazione')
local mWikidata = require('Modulo:Wikidata')
-- costanti
local ERROR_CAT = '[[Categoria:Errori di compilazione del template Senato.it]]'
local PROP_CARICA = 'P39'
local PROP_ID_SENATORE = 'P2549'
local PROP_LEGISLATURA = 'P2937'
local romanToNumber = {
['I'] = 1, ['II'] = 2, ['III'] = 3, ['IV'] = 4, ['IIII'] = 4,
['V'] = 5, ['VI'] = 6, ['VII'] = 7, ['VIII'] = 8, ['IX'] = 9,
['X'] = 10, ['XI'] = 11, ['XII'] = 12, ['XIII'] = 13, ['XIV'] = 14,
['XV'] = 15, ['XVI'] = 16, ['XVII'] = 17, ['XVIII'] = 18, ['XIX'] = 19
}
local function formatError(error_msg)
return string.format('<strong class="error">Errore: %s.</strong>%s', error_msg,
mw.title.getCurrentTitle().namespace == 0 and ERROR_CAT or '')
end
local function paramError(param_name, no_wikidata)
local error_msg = "il parametro <kbd>" .. param_name .. "</kbd> non è compilato"
if no_wikidata == false then
error_msg = error_msg .. " e non è ricavabile da Wikidata"
end
return formatError(error_msg)
end
-- La funzione ricava da Wikidata l'id del senatore
-- (restituisce nil se è impossibile ricavare l'id del senatore)
local function getIdFromWikidata(item)
local claims = mWikidata._getClaims(PROP_ID_SENATORE, { from = item })
-- ritorna il primo id (possiamo assumere che ve ne sia uno soltanto)
if claims ~= nil and #claims > 0 then return mWikidata._formatStatement(claims[1]) end
end
-- La funzione converte il numero di legislatura in una stringa numerica (es. da "xiv" a "14")
-- (restituisce nil se è impossibile trasformare l'input in un numero naturale)
local function formatLegislatura(leg)
return leg:match("^%d+$") and leg or romanToNumber[leg:upper()]
end
-- La funzione ricava da Wikidata tutte le legislature da senatore
local function getAllLegsFromWikidata(item)
local legs = {}
local claims = mWikidata._getClaims(PROP_CARICA, { from = item })
if claims ~= nil then
for _, claim in ipairs(claims) do
-- Se la carica ricoperta è "senatore della Repubblica Italiana" (Q13653224)
if claim.mainsnak.datavalue.value['id'] == 'Q13653224' then
-- allora ricava la/e legislatura/e (P2937)
local leg = mWikidata._formatQualifiers(claim, PROP_LEGISLATURA)
if leg then
for l in string.gmatch(leg, "(%w+)") do
table.insert(legs, formatLegislatura(l))
break
end
end
end
end
end
return legs
end
local p = {}
-- La funzione ritorna i collegamenti ad una o più legislature per il senatore
function p._main(args)
local item = args.from
-- argomenti in input
local id = args[1] or args['id']
local leg = args[2] or args['legislatura']
local name = mPipetrick(args[3] or args['nome'])
local access_date = args['accesso']
-- costanti
local site = 'Senato.it'
local publisher = 'Parlamento italiano'
local id_from_WD = not id and getIdFromWikidata(item) or nil
local legs = leg and { formatLegislatura(leg) } or getAllLegsFromWikidata(item)
-- gestione messaggi e categorie di errore
local err
if id == nil and id_from_WD == nil then
err = paramError('id', leg and true or false)
elseif #legs == 0 and leg then
err = formatError("il numero di legislatura indicato è errato")
elseif #legs == 0 then
err = paramError('legislatura', id and true or false)
end
if err then
return args['mostra errori'] ~= 'no' and err or nil
end
-- icona e categoria Wikidata
local wd_icon = mEditAtWikidata({ pid = PROP_ID_SENATORE, qid = item, id }) or ''
local wd_tracking_cat = mWikidataCheck({ PROP_ID_SENATORE, id }) or ''
-- generazione dei link
local url_pattern = "https://www.senato.it/loc/link.asp?tipodoc=sattsen&id=%s&leg=%s"
local altri_link = {}
local first_url, first_title
-- link disposti sempre in ordine crescente di legislatura
table.sort(legs, function(n, m) return tonumber(n) < tonumber(m) end)
for i = 1, #legs do
local leg = legs[i]
local url = string.format(url_pattern, id or id_from_WD, leg)
local roman = mRoman({ leg }):upper()
local short_name = roman .. " legislatura"
local long_name = short_name .. " della Repubblica Italiana"
if i == 1 then
first_url = url
if #legs == 1 then
first_title = name
site = string.format("%s - [[%s|%s]]", site, long_name, short_name)
else
first_title = string.format("%s (%s)", name, long_name)
end
else
table.insert(altri_link, { url, short_name })
end
end
return mCitazione.cita_da_modulo('args', {
url = first_url,
titolo = first_title,
altrilink = altri_link,
sito = site,
editore = publisher,
accesso = access_date
}) .. wd_icon .. wd_tracking_cat
end
-- Entry-point per il template Senato.it
function p.getLinks(frame)
local args = getArgs(frame)
args['mostra errori'] = frame.args['mostra errori']
return p._main(args)
end
return p