Modulo:Wikidata: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
introduco ricerca ricorsiva come da discussione; codice sviluppato in Modulo:Sandbox/Horcrux/Wikidata con Utente:Horcrux
m inverto logica del parametro per retrocompatibilità
 
(15 versioni intermedie di 4 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 89 ⟶ 90:
local siteLink = mw.wikibase.getSitelink(entityId)
local ret
if entityId == mw.wikibase.getEntityIdForCurrentPage() then
if siteLink and label then
ret = truesiteLink
ifelseif siteLink and label then
ret = mw.getContentLanguage():ucfirst(label) == siteLink and
string.format('[[%s]]', label) or
Riga 268 ⟶ 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 408 ⟶ 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 414 ⟶ 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 {}) == qualifierValuequalifierHas then
break
ret = true
break
end
end
end
Riga 722 ⟶ 734:
ret = mw.wikibase.getLabel(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 759 ⟶ 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 770 ⟶ 790:
break
else
title = followRedirects and title.redirectTarget or nil
end
end
Riga 815 ⟶ 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 850 ⟶ 877:
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