« Module:Autorité » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
corrige une erreur quand l'item Wikidata ne contient aucun "statement"
0x010D (discussion | contributions)
Redirection des identifiants de la Bibliothèque Nationale d'Allemagne vers GND
Ligne 52 : Ligne 52 :
end
end


function createRow( id, label, rawValue, link, isDepreciated )
function createRow( id, label, rawValue, link )
     if link then
     if link then
         local text = '<span class="nowrap uid">[' .. link .. ' ' .. label .. ']</span>'
         local text = '<span class="nowrap uid">[' .. link .. ' ' .. label .. ']</span>'
        if isDepreciated then
            text = text .. '[[Catégorie:Page utilisant le modèle Autorité avec un paramètre obsolète]]'
        end
         return text
         return text
     else
     else
Ligne 75 : Ligne 72 :
end
end


--In this order: name of the parameter, label, propertyId in Wikidata, formatting function, boolean if the parameter is depreciated
--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
local conf = {
     { 'SUDOC', 'Système universitaire de documentation', 269, sudocLink },
     { 'SUDOC', 'Système universitaire de documentation', 269, sudocLink },
Ligne 81 : Ligne 78 :
     { 'VIAF', 'Fichier d’autorité international virtuel', 214, viafLink },
     { 'VIAF', 'Fichier d’autorité international virtuel', 214, viafLink },
     { 'LCCN', 'Bibliothèque du Congrès', 244, lccnLink },
     { 'LCCN', 'Bibliothèque du Congrès', 244, lccnLink },
    { 'DNB', 'Bibliothèque nationale allemande', 0, gndLink },
     { 'GND', 'Gemeinsame Normdatei', 227, gndLink },
     { 'GND', 'Gemeinsame Normdatei', 227, gndLink },
    { 'PND', 'Personennamendatei', 0, gndLink, true },
    { 'GKD', 'Gemeinsame Körperschaftsdatei', 0, gndLink, true },
    { 'GKD-V1', 'Gemeinsame Körperschaftsdatei', 0, gndLink, true },
     { 'SWD', 'Schlagwortnormdatei', 0, swdLink }
     { 'SWD', 'Schlagwortnormdatei', 0, swdLink }
}
}
Ligne 95 : Ligne 88 :
     --Create rows
     --Create rows
     local elements = {}
     local elements = {}
    local isDepreciated = false --S'il y a un id déprécié


    --Redirection des identifiants de la Bibliothèque Nationale d'Allemagne vers GND
    if parentArgs.GND == nil or parentArgs.GND == '' then
        if parentArgs.DNB ~= nil and parentArgs.DNB ~= '' then
            parentArgs.GND = parentArgs.DNB
            isDepreciated = true
        elseif parentArgs.PND ~= nil and parentArgs.PND ~= '' then
            parentArgs.GND = parentArgs.PND
            isDepreciated = true
        elseif parentArgs.GKD ~= nil and parentArgs.GKD ~= '' then
            parentArgs.GND = parentArgs.GKD
            isDepreciated = true
        elseif parentArgs['GKD-V1'] ~= nil and parentArgs['GKD-V1'] ~= '' then
            parentArgs.GND = parentArgs['GKD-V1']
            isDepreciated = true
        end
    end
   
     --Wikidata fallback if requested
     --Wikidata fallback if requested
     local item = mw.wikibase.getEntity()
     local item = mw.wikibase.getEntity()
Ligne 117 : Ligne 128 :
   
   
         if val and val ~= '' then
         if val and val ~= '' then
             table.insert( elements, createRow( params[1], params[2], val, params[4]( val ), params[5] ) )
             table.insert( elements, createRow( params[1], params[2], val, params[4]( val ) ) )
         end
         end
     end
     end
Ligne 123 : Ligne 134 :
     --Worldcat
     --Worldcat
     if parentArgs['WORLDCATID'] and parentArgs['WORLDCATID'] ~= '' then
     if parentArgs['WORLDCATID'] and parentArgs['WORLDCATID'] ~= '' then
         table.insert( elements, createRow( 'WORLDCATID', 'WorldCat', parentArgs['WORLDCATID'], 'http://www.worldcat.org/identities/' .. parentArgs['WORLDCATID'], false ) ) --Validation?
         table.insert( elements, createRow( 'WORLDCATID', 'WorldCat', parentArgs['WORLDCATID'], 'http://www.worldcat.org/identities/' .. parentArgs['WORLDCATID'] ) ) --Validation?
     elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~= '' then
     elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~= '' then
         local lccnParts = splitLccn( parentArgs['LCCN'] )
         local lccnParts = splitLccn( parentArgs['LCCN'] )
         if lccnParts then
         if lccnParts then
             table.insert( elements, createRow( 'LCCN', 'WorldCat', parentArgs['LCCN'], 'http://www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3], false ) )
             table.insert( elements, createRow( 'LCCN', 'WorldCat', parentArgs['LCCN'], 'http://www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3] ) )
         end
         end
     end
     end


    local str = ''
    if isDepreciated then
        str = str .. '[[Catégorie:Page utilisant le modèle Autorité avec un paramètre obsolète]]\n'
    end
     return "* ''[[Autorité (sciences de l'information)|Notices d’autorité]]'' : " .. table.concat( elements, '&nbsp;• ' )
     return "* ''[[Autorité (sciences de l'information)|Notices d’autorité]]'' : " .. table.concat( elements, '&nbsp;• ' )
end
end


return p
return p

Version du 16 mai 2013 à 19:26

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

end

function bnfLink( id )

   --Add cb prefix if it has been removed
   if not string.match( id, '^cb.+$' ) then
       id = 'cb' .. id
   end
   return 'http://catalogue.bnf.fr/ark:/12148/' .. id

end

function viafLink( id )

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

end

function lccnLink( id )

   local parts = splitLccn( id )
   if not parts then
       return false
   end
   id = parts[1] .. parts[2] .. append( parts[3], '0', 6 )
   return 'http://id.loc.gov/authorities/names/' .. id

end

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

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

end

function createRow( id, label, rawValue, link )

   if link then
       local text = '[' .. link .. ' ' .. label .. ']'
       return text
   else
       return '* L\'indentifiant ' .. id .. ' ' .. rawValue .. ' n\'est pas valide.'
   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
       table.insert( ids, statement.mainsnak.datavalue.value )
   end
   return ids

end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function local conf = {

   { 'SUDOC', 'Système universitaire de documentation', 269, sudocLink },
   { 'BNF', 'Bibliothèque nationale de France', 268, bnfLink },
   { 'VIAF', 'Fichier d’autorité international virtuel', 214, viafLink },
   { 'LCCN', 'Bibliothèque du Congrès', 244, lccnLink },
   { 'GND', 'Gemeinsame Normdatei', 227, gndLink },
   { 'SWD', 'Schlagwortnormdatei', 0, swdLink }

}

local p = {}

function p.authorityControl( frame )

   local parentArgs = frame:getParent().args
   --Create rows
   local elements = {}
   local isDepreciated = false --S'il y a un id déprécié
   --Redirection des identifiants de la Bibliothèque Nationale d'Allemagne vers GND
   if parentArgs.GND == nil or parentArgs.GND ==  then
       if parentArgs.DNB ~= nil and parentArgs.DNB ~=  then
           parentArgs.GND = parentArgs.DNB
           isDepreciated = true
       elseif parentArgs.PND ~= nil and parentArgs.PND ~=  then
           parentArgs.GND = parentArgs.PND
           isDepreciated = true
       elseif parentArgs.GKD ~= nil and parentArgs.GKD ~=  then
           parentArgs.GND = parentArgs.GKD
           isDepreciated = true
       elseif parentArgs['GKD-V1'] ~= nil and parentArgs['GKD-V1'] ~=  then
           parentArgs.GND = parentArgs['GKD-V1']
           isDepreciated = true
       end
   end
   
   --Wikidata fallback if requested
   local item = mw.wikibase.getEntity()
   if item ~= nil and item.claims ~= nil then
       for _, params in pairs( conf ) do
           if params[3] ~= 0 then
               local val = parentArgs[params[1]]
               if not val or val ==  then
                   local wikidataIds = getIdsFromWikidata( item, 'p' .. params[3] )
                   if wikidataIds[1] then
                       parentArgs[params[1]] = wikidataIds[1]
                   end
               end
           end
       end
   end
   --Configured rows
   for _, params in pairs( conf ) do
       local val = parentArgs[params[1]]

       if val and val ~=  then
           table.insert( elements, createRow( params[1], params[2], val, params[4]( val ) ) )
       end
   end
   --Worldcat
   if parentArgs['WORLDCATID'] and parentArgs['WORLDCATID'] ~=  then
       table.insert( elements, createRow( 'WORLDCATID', 'WorldCat', parentArgs['WORLDCATID'], 'http://www.worldcat.org/identities/' .. parentArgs['WORLDCATID'] ) ) --Validation?
   elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~=  then
       local lccnParts = splitLccn( parentArgs['LCCN'] )
       if lccnParts then
           table.insert( elements, createRow( 'LCCN', 'WorldCat', parentArgs['LCCN'], 'http://www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3] ) )
       end
   end
   local str = 
   if isDepreciated then
       str = str .. '\n'
   end
   return "* Notices d’autorité : " .. table.concat( elements, ' • ' )

end

return p