Modulo:Wikidata/sandbox: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m nome variabile |
Nessun oggetto della modifica |
||
(25 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('
local getArgs = require('Module:Arguments').getArgs
local mConvert = require('Module:Conversione')
local mLanguages = require('Module:
local mCitation = require('Module:Citazione')
-- Categoria per le pagine con errori
Line 79 ⟶ 80:
if success and uri.protocol and protocols[uri.protocol] then
local dest = tostring(uri)
return string.format('<
else
return url
Line 86 ⟶ 87:
local function formatEntityId(entityId)
local label = mw.wikibase.
local
local ret
if entityId == mw.wikibase.getEntityIdForCurrentPage() then
ret =
elseif siteLink and label then
ret = mw.getContentLanguage():ucfirst(label) == siteLink and
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 =
end
return ret
Line 170 ⟶ 177:
end
local function formatFromPattern(str, args, refs)
local pattern = args.pattern
pattern = mw.ustring.gsub(pattern, '\\{', '{')
pattern = mw.ustring.gsub(pattern, '\\}', '}')
return mw.getCurrentFrame():preprocess(mw.message.newRawMessage(pattern, str, refs or ''):plain())
end
Line 197 ⟶ 204:
local function formatUnitSymbol(entityId, args)
local ret
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.
if link then
ret = string.format('[[%s|%s]]', link, ret)
Line 230 ⟶ 245:
else
-- se è richiesto solo il simbolo dell'unità
-- senza la conversione lo ottiene da
ret = args.rounding and round(ret, args.rounding) or ret
if args.formatnum then
Line 310 ⟶ 325:
qualifiers = (n and n <= #qualifiers) and { qualifiers[n] } or {}
end
-- qualifier filtrati per snaktype, default "value"
args.snaktype = args.snaktype or 'value'
for _, qualifier in ipairs(qualifiers) do
if qualifier.snaktype == args.snaktype or args.snaktype == 'all' then
if
if args.pattern then
if formattedQualifier ~= '' then
table.insert(formattedQualifiers, formattedQualifier)
end
else
table.insert(formattedQualifiers, formattedQualifier)
end
end
end
Line 346 ⟶ 365:
end
return text
end
local function formatReferences(references, args)
local formattedReferences = {}
local refArgs = {}
local parameters = {
titolo = 'P1476', data = 'P577', url = 'P854', dataaccesso = 'P813',
editore = 'P123', urlarchivio = 'P1065', dataarchivio = 'P2960'
}
if references[1].snaks.P854 then
local snaks = references[1].snaks
for parameter, property in pairs(parameters) do
if snaks[property] then
refArgs[parameter] = formatSnak(snaks[property][1], args)
end
end
refArgs.titolo = refArgs.titolo or refArgs.url
if snaks.P407 then
local langs = {}
for _, value in ipairs(snaks.P407) do
local lang = formatSnak(value, { formatting = 'raw' })
table.insert(langs, mw.wikibase.getLabel(lang) or lang)
end
refArgs.lingua = table.concat(langs, ',')
end
if refArgs.urlarchivio then refArgs.urlmorto = 'sì' end
local formattedReference = mw.getCurrentFrame():extensionTag{
name = 'ref',
content = mCitation.cita_da_modulo('web', refArgs),
args = { name = references[1].hash }
}
table.insert(formattedReferences, formattedReference)
end
return table.concat(formattedReferences)
end
Line 368 ⟶ 421:
local formattedStatement = formatStatement(claim, args)
if formattedStatement ~= '' then
local formattedReferences
if args.showreferences and claim.references then
formattedReferences = formatReferences(claim.references, args)
end
-- eventuale pattern
if args.pattern then
formattedStatement = formatFromPattern(formattedStatement, args, formattedReferences)
if formattedStatement ~= '' then
table.insert(formattedStatements, formattedStatement)
end
Line 392 ⟶ 449:
-------------------------------------------------------------------------------
-- 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
Line 398 ⟶ 455:
local isItem = qualifier.snaktype == 'value' and
qualifier.datavalue.type == 'wikibase-entityid'
local qualifierValues = mw.text.split(qualifierValue, ',')
for _, qualifierHas in ipairs(qualifierValues) do
-- per le proprietà di tipo item il confronto è eseguito sull'id
if formatSnak(qualifier, isItem and { formatting = 'raw' } or {}) == qualifierHas then
ret = true
break
end
end
end
Line 418 ⟶ 478:
end
-- Restituisce una
--
-- ("rank", "qualifier", "qualifiertype", "noqualifier", ...).
-- Restituisce nil solo se la pagina non è collegata a un elemento Wikidata e non è indicato il from.
local function getClaims(propertyId, args)
local entityId, claims, filteredClaims
Line 438 ⟶ 498:
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
Line 612 ⟶ 684:
-- Restituisce il numero di statement di una proprietà di Wikidata.
function p._N(args)
local propertyId, claims
Line 635 ⟶ 707:
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
Line 640 ⟶ 744:
end
return false, args.loadedEntities
end
Line 660 ⟶ 764:
ret = mw.wikibase.getLabelByLang(entityId, args[2])
else
ret = mw.wikibase.
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
Line 755 ⟶ 866:
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
Line 783 ⟶ 901:
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 0 or ''
end, errhandler))
end
-- Funzione per il template {{WikidataClasse}}
function p.propertyHasEntity(frame)
local args = getArgs(frame, { parentOnly = true })
local propertyId = args[1]
args.recursion = tonumber(args.prof) or 8
return select(2, xpcall(function()
return p._propertyHasEntity(propertyId, args) and 1 or ''
end, errhandler))
end
|