跳转到内容

模組:Kovl/Authority control

维基百科,自由的百科全书

require('strict')
local links = require('Module:Kovl/Authority control/links')

local 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

local 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.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
    end
    return ids
end

local function matchesWikidataRequirements( item, reqs )
    for _, group in pairs( reqs ) do
        local property = 'p' .. group[1]
        local qid = group[2]
        if item.claims[property] ~= nil then
            for _, statement in pairs ( item.claims[property] ) do
            	if statement.mainsnak.datavalue ~= nil then
	                if statement.mainsnak.datavalue.value['numeric-id'] == qid then
    	                return true
        	        end
        	    end
            end
        end
    end
    return false
end

local function createRow( id, label, rawValue, link, withUid )
    if link then
        if withUid then
            return '* ' .. label .. ' <span class="uid">' .. link .. '</span>\n'
        else
            return '* ' .. label .. ' ' .. link .. '\n'
        end
    else
        return '* <span class="error">The ' .. id .. ' id ' .. rawValue .. ' is not valid.</span>[[Category:Wikipedia articles with faulty authority control identifiers (' .. id .. ')]]\n'
    end
end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
    { 'VIAF', '[[Virtual International Authority File|VIAF]]', 214, links.viafLink },
    { 'LCCN', '[[Library of Congress Control Number|LCCN]]', 244, links.lccnLink },
    { 'ISNI', '[[International Standard Name Identifier|ISNI]]', 213, links.isniLink },
    { 'ORCID', '[[ORCID]]', 496, links.orcidLink },
    { 'GND', '[[Integrated Authority File|GND]]', 227, links.gndLink },
    { 'SELIBR', '[[LIBRIS|SELIBR]]', 906, links.selibrLink },
    { 'SUDOC', '[[Système universitaire de documentation|SUDOC]]', 269, links.sudocLink },    
    { 'BNF', '[[Bibliothèque nationale de France|BNF]]', 268, links.bnfLink },
    { 'BPN', '[[Biografisch Portaal|BPN]]', 651, links.bpnLink },
    { 'RID', '[[ResearcherID]]', 1053, links.ridLink },
    { 'BIBSYS', '[[BIBSYS]]', 1015, links.bibsysLink },
    { 'ULAN', '[[Union List of Artist Names|ULAN]]', 245, links.ulanLink },
    { 'HDS', '[[Historical Dictionary of Switzerland|HDS]]', 902, links.hlsLink },
    { 'LIR', '[[Historical Dictionary of Switzerland#Lexicon_Istoric_Retic|LIR]]', 886, links.lirLink },
    { 'MBA', '[[MusicBrainz]]', 434, links.mbLink },
    { 'MGP', '[[Mathematics Genealogy Project|MGP]]', 549, links.mgpLink },    
    { 'NLA', '[[National Library of Australia|NLA]]', 409, links.nlaLink },
    { 'NDL', '[[National Diet Library|NDL]]', 349, links.ndlLink },
    { 'NCL', '[[National Central Library|NCL]]', 1048, links.nclLink },
    { 'NKC', '[[National Library of the Czech Republic|NKC]]', 691, links.nkcLink },
    { 'Léonore', '[[:fr:Base Léonore|Léonore]]', 640, links.leonoreLink }, 
    { 'SBN', '[[Istituto Centrale per il Catalogo Unico|ICCU]]', 396, links.sbnLink },     
    { 'RLS', '[[Russian State Library|RLS]]', 947, links.rslLink },
    { 'Botanist', '[[Author citation (botany)|Botanist]]', 428, links.botanistLink },
    { 'NARA-person', '[[National Archives and Records Administration|NARA]]', 1222, links.narapersonLink },
    { 'NARA-organization', '[[National Archives and Records Administration|NARA]]', 1223, links.naraorganizationLink },
    { 'USCongress', '[[Biographical Directory of the United States Congress|US Congress]]', 1157, links.uscongressLink },
    { 'BNE', '[[Biblioteca Nacional de España|BNE]]', 950, links.bneLink },
    { 'CINII', '[[CiNii]]', 271, links.ciniiLink },
    { 'TLS', '[[Theaterlexikon der Schweiz|TLS]]', 0, links.tlsLink },
    { 'SIKART', '[[SIKART]]', 781, links.sikartLink },
    { 'KULTURNAV', '[[KulturNav]]', 1248, links.kulturnavLink },
    { 'RKDartists', '[[Netherlands Institute for Art History#Online artist pages|RKD]]', 650, links.rkdartistsLink },
}

-- Check that the Wikidata item has this property-->value before adding it
local reqs = {}

local p = {}

function p.authorityControl( frame )
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}

    --redirect PND to GND
    if (parentArgs.GND == nil or parentArgs.GND == '') and parentArgs.PND ~= nil and parentArgs.PND ~= '' then
        parentArgs.GND = parentArgs.PND
    end

    --Wikidata fallback if requested
    local item = mw.wikibase.getEntityObject()
    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 canUseWikidata = nil
                    if reqs[params[1]] ~= nil then
                        canUseWikidata = matchesWikidataRequirements( item, reqs[params[1]] )
                    else
                        canUseWikidata = true
                    end
                    if canUseWikidata then
                        local wikidataIds = getIdsFromWikidata( item, 'p' .. params[3] )
                        if wikidataIds[1] then
                            parentArgs[params[1]] = wikidataIds[1]
                        end
                    end
                end
            end
        end
    end

    --Worldcat
    if parentArgs['WORLDCATID'] and parentArgs['WORLDCATID'] ~= '' then
        table.insert( elements, createRow( 'WORLDCATID', '', parentArgs['WORLDCATID'], '[//www.worldcat.org/identities/' .. parentArgs['WORLDCATID'] .. ' WorldCat]', false ) ) --Validation?
    elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~= '' then
        local lccnParts = splitLccn( parentArgs['LCCN'] )
        if lccnParts then
            table.insert( elements, createRow( 'LCCN', '', parentArgs['LCCN'], '[//www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3] .. ' WorldCat]', false ) )
        end
    end

    --Configured rows
    local rct = 0
    for k, 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 ), true ) )
            rct = rct + 1
        end
    end
    local Navbox = require('Module:Navbox')
    local elementscats = ''
    if rct > 13 then
    	elementscats  = '[[Category:AC with ' .. rct .. ' elements]]'
	end
	
	if #elements ~= 0 then
		return Navbox._navbox( {
			name  = 'Authority control',
			bodyclass = 'hlist',
			group1 = '[[Authority control]]' .. elementscats,
			list1 = table.concat( elements )
			} )
	else
		return ""
	end
end

return p