Modulo:Controllo di autorità

'A Wikisource.

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

--la tabella dei codici si trova più sotto

--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 isniLink( id )
    id = validateIsni( id )
    if not id then
        return '[Codice ISNI non valido]'
    end
    return '[http://isni.org/isni/' .. id .. ' ' .. id:sub( 1, 4 ) .. ' ' .. id:sub( 5, 8 ) .. ' '  .. id:sub( 9, 12 ) .. ' '  .. id:sub( 13, 16 ) .. ']'
end

-- principali codici di controllo di autorità
-- si veda [[d:Property:P1630]]
local codici = {
	{ 213, '[[w:International Standard Name Identifier|ISNI]]', 'http://isni.org/%s/', isniLink},
	{ 214, '[[w:Virtual International Authority File|VIAF]]', '//viaf.org/viaf/%s/' },
	{ 227, '[[w:Gemeinsame Normdatei|GND]]', 'http://d-nb.info/gnd/%s' },
	{ 244, '[[w:Library of Congress Control Number|LCCN]]', 'http://lccn.loc.gov/%s' },
	{ 268, '[[w:Bibliothèque nationale de France|BNF]]', 'http://catalogue.bnf.fr/ark:/12148/cb%s/PUBLIC' },
	{ 396, '[[w:Servizio bibliotecario nazionale|SBN]]', 'http://id.sbn.it/af/%s' },
	{ 1871, '[[w:de:Consortium of European Research Libraries|CERL]]', 'http://thesaurus.cerl.org/record/%s' }
}

function val(item, prop)
	local claims = item.claims
	local res = {}
	if claims and claims['P'..prop[1]] then
		for index, claim in pairs(claims['P'..prop[1]]) do
			local dv = claim.mainsnak.datavalue
			if dv and dv.value and type(dv.value) == 'string' and mw.text.trim(dv.value) ~= '' then
				if prop[4] then 
					table.insert(res, prop[4](dv.value))
				else
					table.insert(res, '['..mw.ustring.format(prop[3], mw.uri.encode(dv.value, 'PATH'))..' '..dv.value..']')
				end
			end
		end
	end
	if #res > 0 then
		return prop[2] .. mw.message.new('colon-separator'):plain() .. table.concat(res, mw.message.new('comma-separator'):plain())
	else
		return nil
	end
end

local p = {}

-- per l'uso da parte di altri moduli
function p.box(item)
	if mw.wikibase and not item then
		item = mw.wikibase.getEntityObject()
	end
	if not item then
		return ''
	end
	local res = {}
	for i, v in ipairs(codici) do
		local x = val(item, v)
		if x then
			table.insert(res, x)
		end
	end

	if #res > 0 then
		return tostring(
			mw.html.create('div')
			:addClass('controlloAutorita')
			:css{
				float = 'right',
				clear = 'right',
				border = '1px solid #a2a9b1;',
				['background-color'] = '#f8f9fa;',
				padding = '5px',
				['margin-bottom'] = '3.3em',
				['margin-left'] = '1.4em',
				['width'] = '198px'
			}
			:tag('div')
				:css{
					background = '#ccf',
					['padding-left'] = '0.5em',
					['font-weight'] = 'bold',
					['text-align'] = 'center',
					['white-space'] = 'nowrap'
				}
				:wikitext('[[w:Controllo di autorità|Schede di autorità]]')
				:done()
			:tag('div')
				:wikitext('<div>'..table.concat(res, '</div><div>')..'</div>')
			:allDone()
		)
	else
		return ''
	end
end

-- da invocare nel wikicodice
function p.ext(frame)
	return p.box()
end

return p