Module:Autorité

La bibliothèque libre.
Aller à la navigation Aller à la recherche
Documentation du module [voir] [modifier] [purger]
La documentation de ce module Scribunto écrit en Lua est incluse depuis sa sous-page de documentation.

Ce module contient le code de {{Autorité}}.

function sbnLink( id )
    if not string.match( id, '^%u%u[%d%u]%u%d%d%d%d%d%d$' ) then
        return false
    end
    return '[https://opac.sbn.it/nome/' .. id .. ' ' .. id .. ']'
end

function ndlLink( id )
    if not string.match( id, '^0?%d%d%d%d%d%d%d%d$' ) then
        return false
    end
    return '[http://id.ndl.go.jp/auth/ndlna/' .. id .. ' ' .. id .. ']'
end


function isniLink( id )
    id = validateIsni( id )
    if not id then
        return false
    end
    return '[http://isni.org/isni/' .. id .. ' ' .. id:sub( 1, 4 ) .. ' ' .. id:sub( 5, 8 ) .. ' '  .. id:sub( 9, 12 ) .. ' '  .. id:sub( 13, 16 ) .. ']'
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
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
 
--Returns the ISNI check digit isni must be a string where the 15 first elements are digits
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

function ulanLink( id )
    if not string.match( id, '^%d%d?%d?%d?%d?%d?%d?%d?%d?$' ) then
        return false
    end
    return '[//www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=' .. id .. ' ' .. id .. ']'
end

function sudocLink( id )
    if not string.match( id, '^%d%d%d%d%d%d%d%d[%dxX]$' ) then
        return false
    end
    return '[http://www.idref.fr/' .. id .. ' ' .. id .. ']'
end

function bnfLink( id )
    if not id:match( '^%d%d%d%d%d%d%d%d[0-9bcdfghjkmnpqrstvwxz]?$' ) then
        return false
    end
    return '[http://catalogue.bnf.fr/ark:/12148/cb' .. id .. ' cb' .. id .. ']'
end

function viafLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return '[http://viaf.org/viaf/' .. id .. ' ' .. id .. ']'
end

function lccnLink( id )
    if not id:match( '^%l%l?%l?%d%d%d%d%d%d%d%d%d?%d?$' ) then
        return false
    end

    return '[http://id.loc.gov/authorities/' .. id .. ' ' .. id .. ']'
end

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

function gndLink( id )
    return '[http://d-nb.info/gnd/' .. id .. ' ' .. id .. ']'
end

function nlaLink( id )
    return '[//nla.gov.au/anbd.aut-an' .. id .. ' ' .. id .. ']'
end

function leonoreLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return '[https://www.leonore.archives-nationales.culture.gouv.fr/ui/notice/' .. id .. ' ' .. id .. ']'
end

function canadianaLink( id )
  if not string.match( id,  '^%d%d%d%d[A-Z]%d%d%d%d[EF]?$' ) then
    return false
  end
  return '[https://www.collectionscanada.gc.ca/canadiana-authorities/index/view?cdnAutNbr=' .. id  .. '&format=full&index_name=cdnAutNbr&search_text=' .. id .. '&page=1&filter= ' .. id .. ']'
end

function banqLink( id )
  if not string.match( id,  '^000%d%d%d%d%d%d%d$' ) and not string.match( id,  '^ncf%d%d%d%d%d%d%d%d$' ) then
    return false
  end
  return id
end

function imslpLink( id )
    return '[https://imslp.org/wiki/' .. mw.uri.encode(id, 'WIKI') .. ' ' .. id:gsub( '_', ' ' ) .. ']'
end

function worldcatLink( id )
    if not id:match( '^%l%l?%l?%d%d%d%d%d%d%d%d%d?%d?$' ) then
        return false
    end

    id = id:gsub( '^(%l+%d+)(%d%d%d%d%d%d)$', '%1-%2' )
    return '[//www.worldcat.org/identities/lccn-' .. id .. ' lccn-' .. id .. ']'
end

function createRow( label, rawValue, link )
    if link then
    	return '<li>' .. label .. ' : ' .. link .. '</li>'
    else
        return '<li><span class="error">L\'identifiant ' .. label .. ' ' .. rawValue .. ' n\'est pas valide.</span>[[Catégorie:Pages utilisant le modèle Autorité avec un paramètre erroné]]</li>'
    end
end

function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
    	if statement.mainsnak.snaktype == 'value' then
            table.insert( ids, statement.mainsnak.datavalue.value )
        end
    end
    return ids
end

--In this order: label, propertyId in Wikidata, formatting function, parameter name
local conf = {
    { '[[w:Fichier d\'autorité international virtuel|VIAF]]', 214, viafLink, 'VIAF' },
    { '[[w:International Standard Name Identifier|ISNI]]', 213, isniLink, 'ISNI' }, 
    { '[[w:Union List of Artist Names|ULAN]]', 245, ulanLink, 'ULAN' },
    { '[[w:Bibliothèque nationale de France|BNF]]', 268, bnfLink, 'BNF' },
    { '[[w:Système universitaire de documentation|SUDOC]]', 269, sudocLink, 'SUDOC' },
    { '[[w:Bibliothèque et Archives Canada|BAC]]', 1670, canadianaLink, 'BAC' },
    { '[[w:Bibliothèque et Archives nationales du Québec|BAnQ]]', 3280, banqLink, 'BANQ' },
    { '[[w:Numéro de contrôle de la Bibliothèque du Congrès|LCCN]]', 244, lccnLink, 'LCCN' },
    { '[[w:Gemeinsame Normdatei|GND]]', 227, gndLink, 'GND' },
    { '[[w:Institut central du catalogue unique|SBN]]', 396, sbnLink, 'SBN' }, 
    { '[[w:Bibliothèque nationale de la Diète|NDL]]', 349, ndlLink, 'NDL' }, 
    { '[[w:Bibliothèque nationale d\'Australie|NLA]]', 409, nlaLink, 'NLA' },
    { '[[w:Base Léonore|Léonore]]', 11152, leonoreLink, 'Léonore' },
    { '[[w:International Music Score Library Project|IMSLP]]', 839, imslpLink, 'IMSLP' },
    { '[[w:WorldCat|WorldCat]]', 244, worldcatLink, 'LCCN' }
}
    
local p = {}

-- Rendu depuis Lua
function p.autorite( item, args )
    if item == nil then
    	item = {}
    end
    if item.claims == nil then
    	item.claims = {}
    end

	local str = ''
    local elements = {}

    for _, params in pairs( conf ) do
        local wikidataIds = getIdsFromWikidata( item, 'P' .. params[2] )
        if args[params[4]] ~= nil then
            local id = args[params[4]]
            table.insert( elements, createRow( params[1], id, params[3]( id ) ) )
            str = str .. '[[Catégorie:Pages utilisant le modèle Autorité avec un paramètre local]]\n'
            mw.addWarning('Il est préférable de définir les identifiants d\'autorité sur Wikidata, ils seront affichés automatiquement par le modèle Autorité.')
        else
            for _, id in pairs( wikidataIds ) do
                table.insert( elements, createRow( params[1], id, params[3]( id ) ) )
            end
        end
    end

    if mw.title.getCurrentTitle().namespace == 102 then
        str = str .. '[[Catégorie:Pages « Auteur » utilisant le modèle Autorité]]\n'
    end
    if #elements == 0 then
        str = str .. '[[Catégorie:Pages utilisant le modèle Autorité sans aucun identifiant]]\n'
        return str
    end

    return '<div id="authorityControl" class="catlinks" style="font-size: 0.9em; padding: 1px;"><table><tr><th style="white-space: nowrap; background-color: #ddf; padding: 0 5px;">Notices d’autorité</th><td style="padding-left: 5px"><ul style="margin-left: 0">' .. table.concat( elements ) .. '</ul></td></tr></table></div>\n' .. str
end

function p.authorityControl( frame )
	return p.autorite( mw.wikibase.getEntityObject(), frame:getParent().args )
end

return p