Modulo:Wikidata: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m uniformato a ipairs
m inverto logica del parametro per retrocompatibilità
 
(24 versioni intermedie di 5 utenti non mostrate)
Riga 1:
--[[
* Modulo per implementare le funzionalità dei template:
* {{Wikidata}}, {{WikidataQ}}, {{WikidataIdx}}, {{WikidataN}}, {{WikidataLabel}}, {{WikidataDescription}}
* {{WikidataLink}}, {{WikidataId}}, {{WikidataTipo}} e {{WikidataIstanza}}.
* Permette di accedere a Wikidata in modo più avanzato rispetto a {{#property}}.
Riga 16:
-- =============================================================================
 
require('Module:No globalsstrict')
 
local getArgs = require('Module:Arguments').getArgs
local mConvert = require('Module:Conversione')
local mLanguages = require('Module:LinguaggiLingue')
 
-- Categoria per le pagine con errori
Riga 36:
['unknown-snak-type'] = 'Tipo di snak sconosciuto',
['unknown-datavalue-type'] = 'Tipo di dato sconosciuto',
['unknown-entity-type'] = 'Tipo di entità sconosciuta',
['unknown-output-format'] = 'Formato di output sconosciuto'
},
somevalue = "''valore sconosciuto''",
Riga 79 ⟶ 80:
if success and uri.protocol and protocols[uri.protocol] then
local dest = tostring(uri)
return string.format('<divspan style="word-break: break-all;">[%s %s]</divspan>', dest, dest:gsub(uri.protocol .. '://', ''))
else
return url
Riga 86 ⟶ 87:
 
local function formatEntityId(entityId)
local label = mw.wikibase.labelgetLabel(entityId)
local linksiteLink = mw.wikibase.sitelinkgetSitelink(entityId)
local ret
if entityId == mw.wikibase.getEntityIdForCurrentPage() then
if link then
ret = (label and label ~= link) andsiteLink
elseif siteLink and label then
string.format('[[%s|%s]]', link, label) or
ret = mw.getContentLanguage():ucfirst(label) == siteLink and
string.format('[[%s]]', link)
string.format('[[%s]]', label) or
string.format('[[%s|%s]]', siteLink, label)
elseif siteLink then
ret = string.format('[[%s]]', siteLink)
elseif label then
ret = label
else
ret = label or ''
end
return ret
Riga 197 ⟶ 204:
 
local function formatUnitSymbol(entityId, args)
local ret
local ret = p._getProperty({ 'P558', n = 1, from = entityId })
for _, lang in ipairs({ 'mul', 'it', 'en' }) do
ret = p._getProperty({ 'P5061', includelang = lang, from = entityId })
if ret and ret ~= '' then
break
else
ret = nil
end
end
local space = ret == '°' and '' or ' '
if ret and args.showunitlink then
local link = mw.wikibase.sitelinkgetSitelink(entityId)
if link then
ret = string.format('[[%s|%s]]', link, ret)
Riga 230 ⟶ 245:
else
-- se è richiesto solo il simbolo dell'unità
-- senza la conversione lo ottiene da P558P5061
ret = args.rounding and round(ret, args.rounding) or ret
if args.formatnum then
Riga 256 ⟶ 271:
local entityId = getEntityIdFromValue(datavalue.value)
if args.showprop then
ret = p._getProperty({ args.showprop, n = 1, from = entityId, formatting = args.formatting }) or ''
elseif args.formatting then
local formatting = args.formatting:lower()
ret = (formatting == 'raw' or formatting == 'id') and entityId or
formatting == 'label' and mw.wikibase.getLabel(entityId) or
formatting == 'title' and (mw.wikibase.getSitelink(entityId) or '') or
error(i18n.errors['unknown-output-format'])
else
ret = args.formatting == 'raw' and entityId or formatEntityId(entityId)
end
elseif datavalue.type == 'string' then
Riga 300 ⟶ 321:
-- È al plurale perché anche i qualifier possono avere più di un valore
-- (si ottiene inserendo due volte lo stesso qualifier)
local function formatQualifiers(claim, qualifierqualifierId, args, rawTable, retTable)
local formattedQualifiers = retTable or {}
 
if claim.qualifiers and claim.qualifiers[qualifierqualifierId] then
local qualifiers = claim.qualifiers[qualifierqualifierId]
-- con args.nq seleziona solo l'n-esimo qualifier
if args.nq then
Riga 310 ⟶ 331:
qualifiers = (n and n <= #qualifiers) and { qualifiers[n] } or {}
end
-- qualifier filtrati per snaktype, default "value"
for _, q in ipairs(qualifiers) do
args.snaktype = args.snaktype or 'value'
local formattedQualifier = formatSnak(q, args)
for _, qualifier in ipairs(qualifiers) do
if formattedQualifier ~= '' then
if qualifier.snaktype == args.patternsnaktype or args.snaktype == 'all' then
local formattedQualifier = formatFromPatternformatSnak(formattedQualifierqualifier, args)
if formattedQualifier ~= '' then
if args.pattern then
formattedQualifier = formatFromPattern(formattedQualifier, args)
if formattedQualifier ~= '' then
table.insert(formattedQualifiers, formattedQualifier)
end
else
table.insert(formattedQualifiers, formattedQualifier)
end
else
table.insert(formattedQualifiers, formattedQualifier)
end
end
Riga 335 ⟶ 360:
local function appendQualifiers(statement, text, args)
local formattedQualifiers = {}
local qualifiersqualifierIds = mw.text.split(args.showqualifiers, ',')
for _, qualifierqualifierId in ipairs(qualifiersqualifierIds) do
if statement.qualifiers[qualifierqualifierId] then
local formattedQualifier = formatQualifiers(statement, qualifierqualifierId, args)
table.insert(formattedQualifiers, formattedQualifier)
end
Riga 392 ⟶ 417:
-------------------------------------------------------------------------------
 
-- Restituisce true se lo statement contiene il qualifier richiesto con un dato valore (o uno tra più valori separati da virgola)
local function hasQualifierValue(statement, qualifierId, qualifierValue)
local ret = false
Riga 398 ⟶ 423:
local isItem = qualifier.snaktype == 'value' and
qualifier.datavalue.type == 'wikibase-entityid'
local qualifierValues = mw.text.split(qualifierValue, ',')
-- per le proprietà di tipo item il confronto è eseguito sull'id
for _, qualifierHas in ipairs(qualifierValues) do
if formatSnak(qualifier, isItem and { formatting = 'raw' } or {}) == qualifierValue then
-- per le proprietà di tipo item il confronto è eseguito sull'id
ret = true
if formatSnak(qualifier, isItem and { formatting = 'raw' } or {}) == qualifierHas then
break
ret = true
break
end
end
end
Riga 418 ⟶ 446:
end
 
-- Restituisce una tablesequence Lua contenente gli statement per la property richiesta,
-- oppureanche nilvuota se l'entity o la proprietà non esistono.esiste, o non ci sono valori che soddisfano i criteri
-- ("rank", "qualifier", "qualifiertype", "noqualifier", ...).
-- Gli statement restituiti sono eventualmente filtrati in base ai parametri:
-- Restituisce nil solo se la pagina non è collegata a un elemento Wikidata e non è indicato il from.
-- "rank", "qualifier", "qualifiertype" e "n"
local function getClaims(propertyId, args)
local entityId, claims, filteredClaims
Riga 438 ⟶ 466:
claims = mw.wikibase.getAllStatements(entityId, propertyId)
claims = filterRankValue(claims, args.rank)
end
 
-- statements filtrati per snaktype, default "value"
args.snaktype = args.snaktype or 'value'
if args.snaktype and args.snaktype ~= 'all' then
filteredClaims = {}
for _, claim in ipairs(claims) do
if claim.mainsnak.snaktype == args.snaktype then
table.insert(filteredClaims, claim)
end
end
claims = filteredClaims
end
 
Riga 522 ⟶ 562:
end
 
function p._formatQualifiers(claim, qualifierqualifierId, args, rawTable, retTable)
return formatQualifiers(claim, qualifierqualifierId, args or {}, rawTable, retTable)
end
 
Riga 553 ⟶ 593:
-- o nil se l'entity o la proprietà non esistono, o se per parametri di selezione non ci sono risultati.
function p._getQualifier(args)
local propertyId, qualifierqualifierId, value, claims, ret
 
-- parametri posizionali
Riga 560 ⟶ 600:
error(i18n.errors['property-param-not-provided'], 2)
end
qualifierqualifierId = args[2] and string.upper(args[2])
if not qualifierqualifierId then
error(i18n.errors['qualifier-param-not-provided'], 2)
end
Riga 573 ⟶ 613:
local formattedQualifiers = {}
for _, claim in ipairs(claims) do
formattedQualifiers = formatQualifiers(claim, qualifierqualifierId, args, true, formattedQualifiers)
end
ret = #formattedQualifiers > 0 and
Riga 612 ⟶ 652:
 
-- Restituisce il numero di statement di una proprietà di Wikidata.
function p._N(args)
local propertyId, claims
 
Riga 635 ⟶ 675:
if statement == entityId then
return true
end
end
end
 
-- Se non è stato trovato alcun valore, controlla se questo sia ereditato
-- tramite la proprietà "sottoclasse di" (P279) scavando in profondità
-- fino all'esaurirsi del numero specificato in args.recursion.
--[[ TODO: Valutare se sia opportuna una ricerca ricorsiva potenzialmente infinita.
Per farlo si può aggiungere un parametro (opzionale) maxDepth
che svolga l'attuale funzione di recursion e cambiare quest'ultimo
in un parametro booleano.
]]
args.recursion = tonumber(args.recursion) or 0
if args.recursion > 0 then
local recursion = args.recursion
if type(args.loadedEntities) ~= 'table' then
args.loadedEntities = setmetatable({}, {
__newindex = function(t, k, v)
rawset(t, k, v)
rawset(t, #t+1, k)
end })
args.loadedEntities[args.from or mw.wikibase.getEntityIdForCurrentPage()] = true
end
for _, statement in ipairs(statements) do
if not args.loadedEntities[statement] then
args.loadedEntities[statement] = true
args.recursion = args.recursion - 1
args.from = statement
if p._propertyHasEntity('P279', args) then
return true, args.loadedEntities
end
args.recursion = recursion
end
end
Riga 640 ⟶ 712:
end
 
return false, args.loadedEntities
end
 
Riga 660 ⟶ 732:
ret = mw.wikibase.getLabelByLang(entityId, args[2])
else
ret = mw.wikibase.labelgetLabel(entityId)
end
return ret
end
 
-- Restituisce la descrizione di un item o di una proprietà Wikidata.
function p._getDescription(args)
local entityId = args[1] and string.upper(args[1])
local ret = mw.wikibase.getDescription(entityId)
return ret
end
Riga 699 ⟶ 778:
 
-- Restituisce l'ID dell'item Wikidata collegato alla pagina corrente o a una pagina specificata
-- (nota: se il parametro followRedirects è valorizzato con "no", segue i redirect fermandosi al primo redirect collegato a un elemento)
function p._getId(args)
local ret
local followRedirects = not args.followRedirects or args.followRedirects ~= "no"
if args[1] then
local title = mw.title.new(args[1])
Riga 710 ⟶ 790:
break
else
title = followRedirects and title.redirectTarget or nil
end
end
Riga 755 ⟶ 835:
return select(2, xpcall(function()
return p._getLabel(getArgs(frame, { parentOnly = true }))
end, errhandler))
end
 
-- Funzione per il template {{WikidataDescription}}
function p.getDescription(frame)
return select(2, xpcall(function()
return p._getDescription(getArgs(frame, { parentOnly = true }))
end, errhandler))
end
Riga 783 ⟶ 870:
return select(2, xpcall(function()
return p._getId(getArgs(frame, { parentOnly = true }))
end, errhandler))
end
 
-- Funzione per il template {{WikidataValido}}
function p.checkProperty(frame)
return select(2, xpcall(function()
return p._N(getArgs(frame, { parentOnly = true })) > 0 and 1 or ''
end, errhandler))
end
 
-- Funzione per il template {{WikidataClasse}}
function p.propertyHasEntity(frame)
local args = getArgs(frame)
local propertyId = args[1]
return select(2, xpcall(function()
return p._propertyHasEntity(propertyId, args) and 1 or ''
end, errhandler))
end