Modulo:Controllo di autorità: differenze tra le versioni

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura
(rimuovo funzione duplicata: vcbaLink ora incorporata in bvaLink)
 
m (una versione importata)
 
(Nessuna differenza)

Versione attuale delle 22:52, 30 ago 2020

La documentazione per questo modulo può essere creata in Modulo:Controllo di autorità/man

--[[
* 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
]]

require('Module:No globals')

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

-------------------------------------------------------------------------------
--                  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, '^%d%d%d/%d%d?%d?%d?%d?%d?$') then
		return false
	end
	return string.format('[https://opac.vatlib.it/auth/detail/%s %s]', id:sub(1,3) .. '_' .. id:sub(5), 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 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 ipairs(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) .. '&nbsp;') 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

return p