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

Contenuto cancellato Contenuto aggiunto
+
aggiungo funzione
Riga 5:
]]
 
local mWikidata = require( 'ModuloModule:WikidataNo globals' )
 
local modlingue = require( 'Modulo:Linguaggi' )
local cfgmWikidata = mw.title.newrequire( 'Modulo:Controllo di autorità/Configurazione.jsonWikidata' ):getContent()
local modlingue = require( 'ModuleModulo:No globalsLinguaggi' )
local cfg = mw.title.new('Modulo:Controllo di autorità/Configurazione.json'):getContent()
 
-------------------------------------------------------------------------------
Riga 15 ⟶ 16:
 
-- Returns the ISNI check digit isni must be a string where the 15 first elements are digits
local function getIsniCheckDigit( isni )
local total = 0
for i = 1, 15 do
local digit = isni:byte( i ) - 48 -- Get integer value
total = (total + digit) * 2
end
Riga 26 ⟶ 27:
return "X"
end
return tostring( result )
end
 
-- Validate ISNI (and ORCID) and retuns it as a 16 characters string or returns false if it's invalid
-- See http://support.orcid.org/knowledgebase/articles/116780-structure-of-the-orcid-identifier
local function validateIsni( id )
id = id:gsub( '[ %-]', '' ):upper()
if not id:match( '^%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d[%dX]$' ) then
return false
end
if getIsniCheckDigit( id ) ~= string.char( id:byte( 16 ) ) then
return false
end
Riga 42 ⟶ 43:
end
 
local function splitLccn( id )
if id:match( '^%l%l?%l?%d%d%d%d%d%d%d%d%d?%d?$' ) then
id = id:gsub( '^(%l+)(%d+)(%d%d%d%d%d%d)$', '%1/%2/%3' )
end
if id:match( '^%l%l?%l?/%d%d%d?%d?/%d+$' ) then
return mw.text.split( id, '/' )
end
return false
Riga 65 ⟶ 66:
local f = {}
 
function f.viafLink( id )
if not string.match( id, '^%d+$' ) then
return false
end
return string.format( '[https://viaf.org/viaf/%s %s]', id, id )
end
 
function f.isniLink( id )
id = validateIsni( id )
if not id then
return false
end
return string.format( '[http://isni.org/isni/%s %s %s %s %s]', id, id:sub( 1, 4 ), id:sub( 5, 8 ), id:sub( 9, 12 ), id:sub( 13, 16 ) )
end
 
function f.sbnLink( id )
if not string.match( id, '^IT\\ICCU\\%d%d%d%d%d%d%d%d%d%d$' ) and not string.match( id, '^IT\\ICCU\\%u%u[%d%u]%u\\%d%d%d%d%d%d$' ) then
return false
end
return string.format( '[https://opac.sbn.it/opacsbn/opac/iccu/scheda_authority.jsp?bid=%s %s]', id, id )
end
 
function f.bncfLink( id )
if not tonumber( id ) then
return false
end
return string.format( '[https://thes.bncf.firenze.sbn.it/termine.php?id=%s %s]', id, id )
end
 
function f.europeanaLink( id )
if not string.match( id, '^[a-z]+/base/[1-9]%d*$') then
return false
end
return string.format( '[https://data.europeana.eu/%s %s]', id, id )
end
 
function f.lccnLink( id )
local parts = splitLccn( id )
if not parts then
return false
end
local lccnType = parts[1] ~= 'sh' and 'names' or 'subjects'
id = parts[1] .. parts[2] .. append( parts[3], '0', 6 )
return string.format( '[http://id.loc.gov/authorities/%s/%s %s]', lccnType, id, id )
end
 
function f.orcidLink( id )
id = validateIsni( id )
if not id then
return false
end
id = id:sub( 1, 4 ) .. '-' .. id:sub( 5, 8 ) .. '-' .. id:sub( 9, 12 ) .. '-' .. id:sub( 13, 16 )
return string.format( '[https://orcid.org/%s %s]', id, id )
end
 
function f.gndLink( id )
return string.format( '[https://d-nb.info/gnd/%s %s]', id, id )
end
 
function f.bnfLink( id )
-- Add cb prefix if it has been removed
if not string.match( id, '^cb.+$' ) then
id = 'cb' .. id
end
return string.format( '[https://catalogue.bnf.fr/ark:/12148/%s %s] [https://data.bnf.fr/ark:/12148/%s (data)]', id, id, id )
end
 
function f.bneLink( id )
return string.format( '[http://catalogo.bne.es/uhtbin/authoritybrowse.cgi?action=display&authority_id=%s %s] [http://datos.bne.es/resource/%s (data)]', id, id, id )
end
 
function f.ulanLink( id )
return string.format( '[https://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=%s %s]', id, id )
end
 
function f.nlaLink( id )
return string.format( '[https://nla.gov.au/anbd.aut-an%s %s]', id, id )
end
 
function f.bavLink( id )
if not string.match( id, 'ADV%d%d%d%d%d%d%d%d$' ) then
return false
end
Riga 151 ⟶ 152:
end
 
function f.cerlLink( id )
if not string.match( id, 'cn[cilp]%d%d%d%d%d%d%d%d$' ) then
return false
end
return string.format( '[https://thesaurus.cerl.org/record/%s %s]', id, id )
end
 
function f.ndlLink( id )
if not string.match( id, '^0?%d%d%d%d%d%d%d%d$' ) then
return false
end
return string.format( '[https://id.ndl.go.jp/auth/ndlna/%s %s]', id, id )
end
 
function f.worldcatLink( id )
return string.format( '[https://www.worldcat.org/identities/%s %s]', id, id )
end
 
Riga 174 ⟶ 175:
 
local p = {}
 
-- Funzione di utilità per il manuale, restituisce un elenco degli identificativi.
function p.identifiers(frame)
local ret = {}
for _, params in ipairs(mw.text.jsonDecode(cfg)) do
local value, n = params.etichetta:gsub('|%u%u+', '')
if n > 0 then
value = string.format('%s (%s)', value, params.codice)
end
if frame.args.wikidata then
value = string.format('%s, su Wikidata [[d:P:P%s|P%s]]', value, params.P, params.P)
end
table.insert(ret, '* ' .. value)
end
return table.concat(ret, '\n')
end
 
-- Funzione per il template {{Controllo di autorità}}.
function p.authorityControl( frame )
local elements, categories = {}, {}
local ret = ''
local styles = 'Modulo:Controllo di autorità/styles.css'
 
for _, params in pairsipairs( mw.text.jsonDecode( cfg ) ) do
local val = mWikidata._getProperty({ 'P' .. params.P, n = 1 })
if val then
local el, cat
local link = f[params.funzione]( val )
if link then
local lingue = params.lingue and ( modlingue.lingue( params.lingue ) .. ' ' ) or ''
el = string.format( '%s <span class="uid">%s%s</span>', params.etichetta, lingue, link )
cat = string.format( '[[Categoria:Voci con codice %s]]', params.codice )
else
el = string.format( '<span class="error">Il valore %s di %s non è valido.</span>', val, params.codice )
cat = string.format( '[[Categoria:Voci con codici controllo di autorità non validi (%s)]]', params.codice )
end
table.insert( elements, el )
table.insert( categories, cat )
end
end
 
if #elements > 0 then
ret = mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = styles } }
ret = ret .. string.format( '<table class="CdA"><tr><th>%s</th><td>%s</td></tr></table>',
'[[Aiuto:Controllo di autorità|Controllo di autorità]]',
table.concat( elements, '<span style="font-weight:bold;">&nbsp;·</span> ' ) )
if mWikidata._instanceOf( { 'Q5' } ) then
table.insert( categories, '[[Categoria:Voci biografiche con codici di controllo di autorità]]' )
else
table.insert( categories, '[[Categoria:Voci non biografiche con codici di controllo di autorità]]' )
end
else
table.insert( categories, '[[Categoria:Voci con template Controllo di autorità ma senza codici]]' )
end
 
return ret .. ( mw.title.getCurrentTitle().namespace == 0 and table.concat( categories ) or '' )
end