Jump to content

Modulus:Scriptor/Auctoritas

E Wikisource

Documentation for this module may be created at Modulus:Scriptor/Auctoritas/doc

--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:en:International Standard Name Identifier|ISNI]]', 'http://isni.org/%s/', isniLink},
	{ 214, '[[w:en:Virtual International Authority File|VIAF]]', '//viaf.org/viaf/%s/' },
	{ 227, '[[w:en:Gemeinsame Normdatei|GND]]', 'http://d-nb.info/gnd/%s' },
	{ 244, '[[w:en:Library of Congress Control Number|LCCN]]', 'http://lccn.loc.gov/%s' },
	{ 268, '[[w:en:Bibliothèque nationale de France|BNF]]', 'http://catalogue.bnf.fr/ark:/12148/cb%s/PUBLIC' },
	{ 396, '[[w:en:Servizio bibliotecario nazionale|SBN]]', 'http://id.sbn.it/af/%s' },
	{ 1871, '[[w:en:Consortium of European Research Libraries|CERL]]', 'http://thesaurus.cerl.org/record/%s' },
	{ 7859, '[[w:en:WorldCat|WorldCat]]', 'https://www.worldcat.org/identities/%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')
			:tag('div')
				:addClass('controlloAutoritaTitle')
				:wikitext("'''[[w:en:Authority control|Auctoritas]]'''")
				:done()
			:tag('div')
				:wikitext('<div>'..table.concat(res, ' • ')..'</div>')
			:allDone()
		)
	else
		return ''
	end
end

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

return p