Modulo:Controllo di autorità/sandbox

Versione del 24 mar 2020 alle 20:42 di Sakretsu (discussione | contributi) (ripulisco; carico configurazione json)
--[[
* Modulo che implementa il template Controllo di autorità.
* Il modulo è stato importato inizialmente da:
* https://en.wikipedia.org/w/index.php?title=Module:Authority_control&oldid=633242817
]]

local mWikidata = require( 'Modulo:Wikidata' )
local modlingue = require( 'Modulo:Linguaggi' )
local cfg = mw.title.new( 'Modulo:Controllo di autorità/Configurazione.json' ):getContent()
require( 'Module:No globals' )

-------------------------------------------------------------------------------
--                  Funzioni di utilità
-------------------------------------------------------------------------------

-- 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
	local remainder = total % 11
	local result = (12 - remainder) % 11
	if result == 10 then
		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
	return id
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
end

local function append(str, c, length)
	while str:len() < length do
		str = c .. str
	end
	return str
end

-------------------------------------------------------------------------------
--                  Funzioni per generare i link
-------------------------------------------------------------------------------

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 
	return id
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

-------------------------------------------------------------------------------
--                  Funzioni esportate
-------------------------------------------------------------------------------

local p = {}

-- 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 identifier, params in pairs( mw.text.jsonDecode( cfg ) ) do
		local val = mWikidata._getProperty({ 'P' .. params.P, n = 1 })
		if val then
			local el, cat
			local link = f[params['function']]( val )
			if link then
				local lingue = params.languages and ( modlingue.lingue( params.languages ) .. '&nbsp;' ) or ''
				el = string.format( '%s <span class="uid">%s%s</span>', params.label, lingue, link )
				cat = string.format( '[[Categoria:Voci con codice %s]]', identifier )
			else
				el = string.format( '<span class="error">Il valore %s di %s non è valido.</span>', val, identifier )
				cat = string.format( '[[Categoria:Voci con codici controllo di autorità non validi (%s)]]', identifier )
			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

return p