Difference between revisions of "Module:Authority control"

From Betting Wiki
Jump to navigation Jump to search
en>Tpt
m
en>Tpt
Line 30: Line 30:
 
     end
 
     end
 
     return '[http://id.loc.gov/authorities/names/' .. cleanedId .. ' ' .. cleanedId .. ']'
 
     return '[http://id.loc.gov/authorities/names/' .. cleanedId .. ' ' .. cleanedId .. ']'
 +
end
 +
 +
function orcidLink( id )
 +
    return '[http://orcid.org/' .. id .. ' ' .. id .. ']'
 +
end
 +
 +
function gndLink( id )
 +
    if not string.match( id, '^%d+$' ) then
 +
        return false
 +
    end
 +
    return '[http://d-nb.info/gnd/' .. id .. ' ' .. id .. ']'
 
end
 
end
  
Line 60: Line 71:
 
local conf = {
 
local conf = {
 
     { 'VIAF', '[[Virtual International Authority File|VIAF]]', 0, viafLink },
 
     { 'VIAF', '[[Virtual International Authority File|VIAF]]', 0, viafLink },
     { 'LCCN', '[[Library of Congress Control Number|LCCN]]', 0, lccnLink }
+
     { 'LCCN', '[[Library of Congress Control Number|LCCN]]', 0, lccnLink },
 +
    { 'ORCID', '[[ORCID]]', 0, orcidLink },
 +
    { 'GND', '[[Universal Authority File|GND]]', 0, gndLink }
 
}
 
}
 
      
 
      
Line 74: Line 87:
 
             local link = params[4]( val )
 
             local link = params[4]( val )
 
             if link then
 
             if link then
                 table.insert( elements, params[2] .. ': <span class="uid">' .. link .. '</span>' .. getCatForId( params[1] ) )
+
                 table.insert( elements, '* ' .. params[2] .. ': <span class="uid">' .. link .. '</span>' .. getCatForId( params[1] ) .. '\n' )
 
             else
 
             else
                 table.insert( elements, '<span class="error">The ' .. params[1] .. ' id ' .. val .. ' is not valid.</span>[[Category:Wikipedia articles with faulty authority control identifiers (' .. params[1] .. ')]]' )
+
                 table.insert( elements, '* <span class="error">The ' .. params[1] .. ' id ' .. val .. ' is not valid.</span>[[Category:Wikipedia articles with faulty authority control identifiers (' .. params[1] .. ')]]\n' )
 
             end
 
             end
 
              
 
              
Line 86: Line 99:
 
         bodyclass = 'hlist',
 
         bodyclass = 'hlist',
 
         group1 = '[[Authority control]]',
 
         group1 = '[[Authority control]]',
         list1 = table.concat( elements, ' * ' )
+
         list1 = table.concat( elements )
 
     } )
 
     } )
 
end
 
end
  
 
return p
 
return p

Revision as of 08:37, 6 April 2013

Documentation for this module may be created at Module:Authority control/doc

function getCatForId( id )
    local title = mw.title.getCurrentTitle()
    local namespace = title.namespace
    if namespace == 0 then
        return '[[Category:Wikipedia articles with ' .. id .. ' identifiers]]'
    elseif namespace == 2 and not title.isSubpage then
        return '[[Category:User pages with ' .. id .. ' identifiers]]'
    else
        return '[[Category:Miscellaneous pages with ' .. id .. ' identifiers]]'
    end
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 )
    cleanedId = ''
    if string.match( id, '^%l%l?%l?/%d%d%d?%d?/%d+$' ) then
        local parts = split( id, '/' )
        cleanedId = parts[1] .. parts[2] .. append( parts[3], '0', 6 )
    elseif string.match( id, '^%l%l?%l?%d%d%d%d%d%d%d%d%d?%d?$' ) then
        cleanedId = id
    end
    if cleanedId == '' then
        return false
    end
    return '[http://id.loc.gov/authorities/names/' .. cleanedId .. ' ' .. cleanedId .. ']'
end

function orcidLink( id )
    return '[http://orcid.org/' .. id .. ' ' .. id .. ']'
end

function gndLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return '[http://d-nb.info/gnd/' .. id .. ' ' .. id .. ']'
end

--Do split before deployment of mw.text.split
function split( str, c )
    local parts = {}
    local current = ''
    local length = string.len( str )
    for i = 1,length do
        local ch = string.char( string.byte( str, i ) )
        if ch == c then
            table.insert( parts, current )
            current = ''
        else
            current = current .. ch
        end
    end
    table.insert( parts, current )
    return parts
end

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

--In this order: name of the parameter, label, propertyId in Wikidata, formattting function
local conf = {
    { 'VIAF', '[[Virtual International Authority File|VIAF]]', 0, viafLink },
    { 'LCCN', '[[Library of Congress Control Number|LCCN]]', 0, lccnLink },
    { 'ORCID', '[[ORCID]]', 0, orcidLink },
    { 'GND', '[[Universal Authority File|GND]]', 0, gndLink }
}
    
local p = {}

function p.authorityControl( frame )
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}
    for k, params in pairs( conf ) do
        local val = parentArgs[params[1]]
        if val and val ~= '' then
            local link = params[4]( val )
            if link then
                table.insert( elements, '* ' .. params[2] .. ': <span class="uid">' .. link .. '</span>' .. getCatForId( params[1] ) .. '\n' )
            else
                table.insert( elements, '* <span class="error">The ' .. params[1] .. ' id ' .. val .. ' is not valid.</span>[[Category:Wikipedia articles with faulty authority control identifiers (' .. params[1] .. ')]]\n' )
            end
            
        end
    end
    local Navbox = require('Module:Navbox')
    return Navbox._navbox( {
        name  = 'Authority control',
        bodyclass = 'hlist',
        group1 = '[[Authority control]]',
        list1 = table.concat( elements )
    } )
end

return p