Difference between revisions of "Module:Authority control"

From Betting Wiki
Jump to navigation Jump to search
en>Tpt
(fix errors)
en>Tpt
Line 1: Line 1:
function outputError( message, cat )
+
function getCatForId( id )
     local str = '<span class="error">' .. message .. '</span>'
+
     local title = mw.title.getCurrentTitle()
     if cat then
+
    local namespace = title.namespace
         str = str .. '[[Category:' .. cat .. ']]'
+
    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
    return str
 
 
end
 
end
  
 
function viafLink( id )
 
function viafLink( id )
 
     if not string.match( id, '^%d+$' ) then
 
     if not string.match( id, '^%d+$' ) then
         return outputError( 'The VIAF id ' .. id .. ' is not valid.', 'Wikipedia articles with faulty authority control identifiers (VIAF)' )
+
         return false
 
     end
 
     end
     return '<span class="uid">[http://viaf.org/viaf/' .. id .. ' ' .. id .. ']</span>'
+
     return '[http://viaf.org/viaf/' .. id .. ' ' .. id .. ']'
 
end
 
end
  
Line 25: Line 29:
 
     local elements = {}
 
     local elements = {}
 
     for k, params in pairs( conf ) do
 
     for k, params in pairs( conf ) do
         if parentArgs[params[1]] and parentArgs[params[1]] ~= '' then
+
         local val = parentArgs[params[1]]
             table.insert( elements, params[2] .. ': ' .. params[4]( 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] ) )
 +
            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] .. ')]]' )
 +
            end
 +
           
 
         end
 
         end
 
     end
 
     end

Revision as of 07:21, 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

local conf = {
    { 'VIAF', '[[Virtual International Authority File|VIAF]]', 0, viafLink }
}
    
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] ) )
            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] .. ')]]' )
            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