Modulo:Controllo di autorità/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m fix indentazione con tab
semplificato rispetto alla versione enwiki con il Modulo:Wikidata
Riga 5:
]]
 
local mWikidata = require( 'Modulo:Wikidata' )
local modlingue = require( 'Modulo:Linguaggi' )
require( 'Module:No globals' )
Riga 268 ⟶ 269:
end
return '[http://thesaurus.cerl.org/record/' .. id .. ' ' .. id .. ']' .. getCatForId( 'CERL' )
end
 
local function getIdsFromWikidata( item, property )
local ids = {}
if not item.claims[property] then
return ids
end
for _, statement in pairs( item.claims[property] ) do
if statement.mainsnak.datavalue then
table.insert( ids, statement.mainsnak.datavalue.value )
end
end
return ids
end
 
local function matchesWikidataRequirements( item, reqs )
for _, group in pairs( reqs ) do
local property = 'p' .. group[1]
local qid = group[2]
if item.claims[property] ~= nil then
for _, statement in pairs ( item.claims[property] ) do
if statement.mainsnak.datavalue ~= nil then
if statement.mainsnak.datavalue.value['numeric-id'] == qid then
return true
end
end
end
end
end
return false
end
 
-- ritorna true se la proprietà P31 (instance of) ha il valore Q5 (human)
local function isInstanceOfHuman( entity )
local ret = false
if entity.claims and entity.claims.P31 and #entity.claims.P31 > 0 then
for _, claim in pairs( entity.claims.P31 ) do
if claim.mainsnak and
claim.mainsnak.snaktype == 'value' and
claim.mainsnak.datavalue and
claim.mainsnak.datavalue.type == 'wikibase-entityid' and
claim.mainsnak.datavalue.value and
claim.mainsnak.datavalue.value['numeric-id'] == 5 then
ret = true
break
end
end
end
return ret
end
 
Riga 368 ⟶ 320:
{ 'CERL', '[[Consortium of European Research Libraries|CERL]]', 1871, cerlLink }
}
 
-- Check that the Wikidata item has this property-->value before adding it
local reqs = {}
 
local p = {}
 
function p.authorityControl( frame )
local elements, categories = {}, {}
-- Su it.wiki è stata disabilitata la lettura dei parametri, per usare solo Wikidata
local parentArgsret = {} --frame:getParent().args ''
--Create rows
local elements = {}
 
--redirect PND to GND
if (parentArgs.GND == nil or parentArgs.GND == '') and parentArgs.PND ~= nil and parentArgs.PND ~= '' then
parentArgs.GND = parentArgs.PND
end
 
--Wikidata fallback if requested
local item = mw.wikibase.getEntityObject()
if item ~= nil and item.claims ~= nil then
for _, params in pairs( conf ) do
if params[3] ~= 0 then
local val = parentArgs[params[1]]
if not val or val == '' then
local canUseWikidata = nil
if reqs[params[1]] ~= nil then
canUseWikidata = matchesWikidataRequirements( item, reqs[params[1]] )
else
canUseWikidata = true
end
if canUseWikidata then
local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
if wikidataIds[1] then
parentArgs[params[1]] = wikidataIds[1]
end
end
end
end
end
end
 
--Worldcat
--if parentArgs['WORLDCATID'] and parentArgs['WORLDCATID'] ~= '' then
-- table.insert( elements, createRow( 'WORLDCATID', '', parentArgs['WORLDCATID'], '[//www.worldcat.org/identities/' .. parentArgs['WORLDCATID'] .. ' WorldCat]', false ) ) --Validation?
--elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~= '' then
-- local lccnParts = splitLccn( parentArgs['LCCN'] )
-- if lccnParts then
-- table.insert( elements, createRow( 'LCCN', '', parentArgs['LCCN'], '[//www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3] .. ' WorldCat]', false ) )
-- end
--end
 
for _, params in pairs( conf ) do
--Configured rows
local val = mWikidata._getProperty({ 'P' .. params[3], n = 1 })
local rct = 0
if val then
for k, params in pairs( conf ) do
local val = parentArgs[params[1]]
if val and val ~= '' then
local lingue = params['lingue'] and ( modlingue.lingue( params['lingue'] ) .. ' ' ) or ''
table.insert( elements, createRow( params[1], params[2] .. ':', val, lingue, params[4]( val ), true ) )
rct = rct + 1
end
end
 
if #elements > 0 then
--local elementscats = ''
ret = string.format( '<table class="CdA"><tr><th>%s</th><td>%s</td></tr></table>',
--if rct > 13 then
-- elementscats = '[[Category:AC with ' .. rct .. ' elements]]'
--end
local paramcat = ''
for k, v in pairs( frame:getParent().args ) do
paramcat = '[[Categoria:Voci con template Controllo di autorità con parametri]]'
break
end
 
local ret = ''
if #elements ~= 0 then
ret = string.format( '<table class="CdA"><tr><th>%s</th><td>%s</td></tr></table>%s',
'[[Aiuto:Controllo di autorità|Controllo di autorità]]',
table.concat( elements, '<span style="font-weight:bold;">&nbsp;·</span> ' ), )
if mWikidata._instanceOf( { 'Q5' } ) then
paramcat )
table.insert( categories, '[[Categoria:Voci biografiche con codici di controllo di autorità]]' )
end
if mw.title.getCurrentTitle().namespace == 0 then
if #elements ~= 0 then
if isInstanceOfHuman( item ) then
ret = ret .. '[[Categoria:Voci biografiche con codici di controllo di autorità]]'
else
ret = ret .. '[[Categoria:Voci non biografiche con codici di controllo di autorità]]'
end
else
rettable.insert( = ret ..categories, '[[Categoria:Voci non biografiche con templatecodici Controllodi controllo di autorità ma senza codici]]' )
end
else
table.insert( categories, '[[Categoria:Voci con template Controllo di autorità ma senza codici]]' )
end
 
return ret
return ret .. ( mw.title.getCurrentTitle().namespace == 0 and table.concat( categories ) or '' )
end