Modulo:Wikidata/sandbox: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m uso funzione già esistente |
Nessun oggetto della modifica |
||
(13 versioni intermedie di 3 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 89 ⟶ 90:
local siteLink = mw.wikibase.getSitelink(entityId)
local ret
if entityId == mw.wikibase.getEntityIdForCurrentPage() then
ret = siteLink
elseif siteLink and label then
ret = mw.getContentLanguage():ucfirst(label) == siteLink and
string.format('[[%s]]', label) or
Line 174 ⟶ 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 322 ⟶ 325:
qualifiers = (n and n <= #qualifiers) and { qualifiers[n] } or {}
end
--
args.snaktype = args.snaktype or 'value'
for _, qualifier in ipairs(qualifiers) do
Line 362 ⟶ 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 384 ⟶ 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 408 ⟶ 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 414 ⟶ 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 663 ⟶ 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 668 ⟶ 744:
end
return false, args.loadedEntities
end
Line 690 ⟶ 766:
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
Line 783 ⟶ 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 817 ⟶ 907:
function p.checkProperty(frame)
return select(2, xpcall(function()
return p._N(getArgs(frame, { parentOnly = true })) > 0 and
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
|