Difference between revisions of "Module:Authority control"

From Dharmawiki
Jump to navigation Jump to search
m (1 revision imported)
 
en>Jarekt
m
Line 1: Line 1:
require('Module:No globals')
+
--[[ 
 +
  __  __          _      _          _        _  _                _ _                          _            _
 +
|  \/  | ___  __| |_  _| | ___ _  / \  _  _| |_| |__  ___  _ __(_) |_ _  _    ___ ___  _ __ | |_ _ __ ___ | |
 +
| |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __| '_ \ / _ \| '__| | __| | | |  / __/ _ \| '_ \| __| '__/ _ \| |
 +
| |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | |_| | | | (_) | |  | | |_| |_| | | (_| (_) | | | | |_| | | (_) | |
 +
|_|  |_|\___/ \__,_|\__,_|_|\___(_)_/  \_\__,_|\__|_| |_|\___/|_|  |_|\__|\__, |  \___\___/|_| |_|\__|_|  \___/|_|
 +
                                                                            |___/                                   
 +
This module is intended to be the engine behind "Template:Authority control".
  
local function getCatForId( id )
+
Please do not modify this code without applying the changes first at "Module:Authority control/sandbox" and testing
    local title = mw.title.getCurrentTitle()
+
at "Module:Authority control/testcases".
    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 viafLink( id )
+
Authors and maintainers:
    if not string.match( id, '^%d+$' ) then
+
* User:Jarekt - original version
        return false
 
    end
 
    return '[https://viaf.org/viaf/' .. id .. ' ' .. id .. ']' .. getCatForId( 'VIAF' )
 
end
 
  
local function kulturnavLink( id )
+
]]
    return '[http://kulturnav.org/language/en/' .. id .. ' id]'
 
end
 
  
local function sikartLink( id )
+
-- ==================================================
    return '[http://www.sikart.ch/KuenstlerInnen.aspx?id=' .. id .. '&lng=en ' .. id .. ']'
+
-- === Internal functions ===========================
end
+
-- ==================================================
  
local function tlsLink( id )
+
local function getSitelink(item, lang)
local id2 = mw.ustring.gsub(id, '%s', function(s) return mw.uri.encode(s, 'WIKI') end)
+
local langList = mw.language.getFallbacksFor(lang)
    return '[http://tls.theaterwissenschaft.ch/wiki/' .. id2 .. ' ' .. id .. ']'
+
table.insert(langList, 1, lang)
 +
for _, language in ipairs(langList) do
 +
local sitelink = mw.wikibase.sitelink( item, language .. 'wiki' )
 +
if sitelink then
 +
return 'w:'.. language ..':'.. sitelink
 +
end
 +
end
 +
return nil
 
end
 
end
  
 
+
local function getIdentifierNameLink( lang, item1, item2, label )
local function ciniiLink( id )
+
-- Identifier names, like VIAF, LCCN, ISNI, need to be linked to the articles about them if possible
    return '[http://ci.nii.ac.jp/author/' .. id .. '?l=en ' .. id .. ']'
+
-- Alternativly they can be linked to the articles for institutions that issue them
 +
local id_name_URL = nil
 +
-- 1) try wikipedia sitelink for the identifier in users language and in English
 +
if item1 and item1 ~='' then
 +
id_name_URL = getSitelink(item1, lang)
 +
end
 +
-- 2) try wikipedia sitelink for the issuedBy property in users language and in English
 +
if id_name_URL==nil and item2 and item2 ~='' then -- if no link than
 +
id_name_URL = getSitelink(item2, lang)
 +
end
 +
-- 3) if still no links than link to wikidata
 +
if id_name_URL then
 +
return string.format("[[%s|%s]]", id_name_URL, label) -- link to wikipedia
 +
else   
 +
return string.format("[[d:%s|%s]]", item1, label) -- link to wikidata
 +
end
 
end
 
end
  
local function bneLink( id )
+
-- ==================================================
    return '[http://catalogo.bne.es/uhtbin/authoritybrowse.cgi?action=display&authority_id=' .. id .. ' ' .. id .. ']'  
+
-- Create link to a single identifier
end
+
-- INPUTS:
 +
--  * val - value of the identifier
 +
--  * URL_format - string used to create URL
 +
--  * params - additional parameters related to this type of identifiers. Single item from "conf"
 +
--  * color - color of the link
 +
local function getIdentifierValLink(val, URL_format, params, color)
 +
if not val or val=='' then
 +
return ''
 +
end
 +
-- check if identifier is in the right format
 +
local mismatchStr = ''
 +
local val_ = val:gsub( ' ', '' ) -- remove spaces
 +
if (params.regexp and not val:match( params.regexp )) then
 +
mismatchStr  = string.format("<span style=\"color:red\">[does not match %s pattern]</span>", params.regexp)
 +
elseif (params.verify) then -- check if special "Verify" function is present
 +
mismatchStr = params.verify(val_) -- add error message if any
 +
end
 +
-- identifier_value_URL
 +
local val_URL = URL_format:gsub('$1', val_)-- URL part of the link for the identifier value
 +
if color~="blue" then
 +
val = string.format('<span style=\"color:%s\">%s</span>', color, val)
 +
end
 +
return string.format("<span class=\"plainlinks\">[%s %s]</span>%s", val_URL, val, mismatchStr) -- link to the identifier's external website
 +
end  
  
 +
-- ==================================================
 +
-- Convert between 2 formats of LCCN: "n/79/63767" -> "n79063767"
 +
-- "n/79/63767" format was used as input by {{Authority Control}} templates
 +
-- "n79063767" format is used by wikidata
 +
local function fixLCCN(id)
 +
  if id then
 +
local a, b, c = string.match(id, "([%a%d]*)/([%a%d]*)/([%a%d]*)")
 +
if c then
 +
local pad = 6 - string.len(c)
 +
if pad > 0 then
 +
c = string.rep("0", pad)..c
 +
end
 +
id = a..b..c
 +
end
 +
end
 +
return id
 +
end -- fixLCCN
  
local function uscongressLink( id )
+
-- ==================================================
    return '[http://bioguide.congress.gov/scripts/biodisplay.pl?index=' .. id .. ' ' .. id .. ']'
+
-- Verify last "check" digit is correct. ISNI and several other
end
+
-- identifiers use last digit as a verification digit
 
+
local function verifyLastDigit( id )
local function narapersonLink( id )
 
    return '[https://research.archives.gov/person/' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function naraorganizationLink( id )
 
    return '[https://research.archives.gov/organization/' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function botanistLink( id )
 
local id2 = mw.ustring.gsub(id, '%s', function(s) return mw.uri.encode(s, 'PATH') end)
 
    return '[http://www.ipni.org/ipni/advAuthorSearch.do?find_abbreviation=' .. id2 .. ' ' .. id .. ']'
 
end
 
 
 
local function mgpLink( id )
 
    -- TODO Implement some sanity checking regex
 
    return '[http://www.genealogy.ams.org/id.php?id=' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function rslLink( id )
 
    -- TODO Implement some sanity checking regex
 
    return '[http://aleph.rsl.ru/F?func=find-b&find_code=SYS&adjacent=Y&local_base=RSL11&request=' .. id .. '&CON_LNG=ENG ' .. id .. ']'
 
end
 
 
 
local function leonoreLink( id )
 
-- Identifiants allant de LH/1/1 à LH/2794/54 (légionnaires)
 
-- Identifiants allant de C/0/1 à C/0/84 (84 légionnaires célèbres)
 
-- Identifiants allant de 19800035/1/1 à 19800035/385/51670 (légionnaires décédés entre 1954 et 1977, et quelques dossiers de légionnaires décédés avant 1954)
 
    if not string.match( id, '^LH/%d%d?%d?%d?/%d%d?%d?$' ) and
 
      not string.match( id, '^C/0/%d%d?$' ) and
 
  not string.match( id, '^19800035/%d%d?%d?%d?/%d%d?%d?%d?%d?$' ) then
 
        return false
 
    end
 
    return '[http://www.culture.gouv.fr/public/mistral/leonore_fr?ACTION=CHERCHER&FIELD_1=COTE&VALUE_1=' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function sbnLink( id )
 
    if not string.match( id, '^IT\\ICCU\\%d%d%d%d%d%d%d%d%d%d$' ) and not string.match( id, '^IT\\ICCU\\%u%u[%d%u]%u\\%d%d%d%d%d%d$' ) then
 
        return false
 
    end
 
    return '[http://opac.sbn.it/opacsbn/opac/iccu/scheda_authority.jsp?bid=' .. id .. ' ' .. id .. ']' .. getCatForId( 'SBN' )
 
end
 
 
 
local function nkcLink( id )
 
return '[http://aleph.nkp.cz/F/?func=find-c&local_base=aut&ccl_term=ica=' .. id .. '&CON_LNG=ENG ' .. id .. ']'
 
end
 
 
 
local function nclLink( id )
 
    if not string.match( id, '^%d+$' ) then
 
        return false
 
    end
 
    return '[http://aleweb.ncl.edu.tw/F/?func=accref&acc_sequence=' .. id .. '&CON_LNG=ENG ' .. id .. ']'
 
end
 
 
 
local function ndlLink( id )
 
return '[http://id.ndl.go.jp/auth/ndlna/' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function sudocLink( id )
 
    if not string.match( id, '^%d%d%d%d%d%d%d%d[%dxX]$' ) then
 
        return false
 
    end
 
    return '[https://www.idref.fr/' .. id .. ' ' .. id .. ']'
 
end
 
 
 
local function hlsLink( id )
 
    if not string.match( id, '^%d+$' ) then
 
        return false
 
    end
 
    return '[http://www.hls-dhs-dss.ch/textes/f/F' .. id .. '.php ' .. id .. ']'
 
end
 
 
 
local function lirLink( id )
 
    if not string.match( id, '^%d+$' ) then
 
        return false
 
    end
 
    return '[http://www.e-lir.ch/e-LIR___Lexicon.' .. id .. '.450.0.html ' .. id .. ']'
 
end
 
 
 
local 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
 
 
 
local function append(str, c, length)
 
    while str:len() < length do
 
        str = c .. str
 
    end
 
    return str
 
end
 
 
 
local function lccnLink( id )
 
    local parts = splitLccn( id )
 
    if not parts then
 
        return false
 
    end
 
    local lccnType = parts[1] ~= 'sh' and 'names' or 'subjects'
 
    id = parts[1] .. parts[2] .. append( parts[3], '0', 6 )
 
    return '[http://id.loc.gov/authorities/' .. lccnType .. '/' .. id .. ' ' .. id .. ']' .. getCatForId( 'LCCN' )
 
end
 
 
 
local function mbLink( id )
 
    -- TODO Implement some sanity checking regex
 
    return '[//musicbrainz.org/artist/' .. id .. ' ' .. id .. ']' .. getCatForId( 'MusicBrainz' )
 
end
 
 
 
--Returns the ISNI check digit isni must be a string where the 15 first elements are digits
 
local function getIsniCheckDigit( isni )
 
 
     local total = 0
 
     local total = 0
     for i = 1, 15 do
+
     for i = 1, #id-1 do
         local digit = isni:byte( i ) - 48 --Get integer value
+
         local digit = id:byte( i ) - 48 --Get integer value
 
         total = (total + digit) * 2
 
         total = (total + digit) * 2
 
     end
 
     end
     local remainder = total % 11
+
     local result = (12 - remainder) % 11
+
     --local remainder = total % 11
     if result == 10 then
+
     local lastDigit = tostring((12 - total % 11) % 11)
         return "X"
+
     if lastDigit == '10' then
 +
         lastDigit = "X"
 
     end
 
     end
     return tostring( result )
+
end
+
     if (lastDigit == string.sub( id, -1)) then
 
+
return ''
--Validate ISNI (and ORCID) and retuns it as a 16 characters string or returns false if it's invalid
+
else
--See http://support.orcid.org/knowledgebase/articles/116780-structure-of-the-orcid-identifier
+
return "<span style=\"color:red\">[last digit should be " .. lastDigit .. "]</span>"
local function validateIsni( id )
+
end
    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
 
end
  
local function isniLink( id )
+
-- ==================================================
    id = validateIsni( id )
+
-- === Settings =====================================
    if not id then
+
-- ==================================================
        return false
+
-- In order to add a new identifier associated with Wikidata property do the following
    end
+
-- 1) go to [[Template:Authority control/IdentifierList]] and verify that the property number is on the list, if not than edit the page to add it
    return '[http://isni.org/isni/' .. id .. ' ' .. id:sub( 1, 4 ) .. ' ' .. id:sub( 5, 8 ) .. ' '  .. id:sub( 9, 12 ) .. ' '  .. id:sub( 13, 16 ) .. ']' .. getCatForId( 'ISNI' )
+
-- 2) copy code generated at [[Template:Authority control/IdentifierList]] to protected [[Module:Authority control/conf]]
end
+
-- 3) add the property to the "conf" list below
  
local function orcidLink( id )
+
-- load 'Module:Authority control/conf' which holds hardwired data derived from Wikidata's properties of
    id = validateIsni( id )
+
-- properties
    if not id then
+
local properties = require('Module:Authority control/conf')
        return false
 
    end
 
    id = id:sub( 1, 4 ) .. '-' .. id:sub( 5, 8 ) .. '-'  .. id:sub( 9, 12 ) .. '-'  .. id:sub( 13, 16 )
 
    return '[https://orcid.org/' .. id .. ' ' .. id .. ']' .. getCatForId( 'ORCID' )
 
end
 
  
local function gndLink( id )
+
--conf  holds list of identifiers to be displayed
     return '[http://d-nb.info/gnd/' .. id .. ' ' .. id .. ']' .. getCatForId( 'GND' )
+
local conf = {
end
+
    -- people
 +
{label='VIAF'        , property='P214' , lang=''  , regexp='^%d+$' },
 +
{label='ISNI'        , property='P213' , lang=''  , regexp='^%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d[%dX]$', verify=verifyLastDigit },
 +
{label='ORCID'      , property='P496' , lang=''  , regexp='^0000%-000[1-3]%-%d%d%d%d%-%d%d%d[%dX]$' },
 +
{label='ULAN'        , property='P245' , lang=''  , regexp='^500%d%d%d%d%d%d$' }, -- 'Union List of Artist Names' by Getty Research Institute
 +
{label='ResearcherID', property='P1053', lang=''  , regexp='^[A-Z]%-%d%d%d%d%-[12][90]%d%d$' },
 +
{label='LCCN'        , property='P244' , lang='en', regexp='^[ns][broshj]?%d%d%d%d%d%d%d%d%d?%d?$' }, -- Library of Congress Authorities
 +
{label='GND'        , property='P227' , lang='de', regexp='^[%dX%-]+$'},
 +
{label='SELIBR'      , property='P906' , lang='se', regexp='^%d+$' }, -- National Library of Sweden
 +
{label='SUDOC'      , property='P269' , lang='fr', regexp='^%d%d%d%d%d%d%d%d[%dxX]$' },   
 +
{label='BNF'        , property='P268' , lang='fr', regexp='^%d+%w?$' }, -- Bibliothèque nationale de France
 +
{label='BPN'        , property='P651' , lang='nl', regexp='^%d%d%d%d%d%d%d%d$' }, -- Biografisch Portaal number
 +
{label='NAID'        , property='P1225', lang='en', regexp='^%d+$' }, -- NARA ID (redirect for US National Archives Identifier (P1225))
 +
{label='NARA'        , property='P1225', lang='en', regexp='' }, -- US National Archives Identifier
 +
{label='Museofile'  , property='P539' , lang='fr', regexp='^M%d%d%d%d%-?%d?%d?$' }, --Ministry of Culture (France)
 +
{label='NDL'        , property='P349' , lang='ja', regexp='^0?%d%d%d%d%d%d%d%d$' }, -- National Diet Library (of Japan)
 +
{label='NLA'        , property='P409' , lang='en', regexp='^[1-9]%d*$' }, -- National Library of Australia
 +
{label='BIBSYS'      , property='P1015', lang='no', regexp='^%d+$' }, -- Norwegian information system BIBSYS
 +
{label='HDS'        , property='P902' , lang='de', regexp='^[1-9]%d%d?%d?%d?%d?$' },  -- Historical Dictionary of Switzerland
 +
{label='MusicBrainz' , property='P434' , lang='en', regexp='^[-%x]+$' },
 +
{label='MGP'        , property='P549' , lang='en', regexp='^%d%d?%d?%d?%d?%d?$' },  -- Mathematics Genealogy Project
 +
{label='NCL'        , property='P1048', lang='zh', regexp='^%d+$' },  --National Central Library (Taiwan)
 +
{label='NKC'        , property='P691' , lang='cs', regexp='^%l%l%l?%l?%d%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?$' },  --National Library of the Czech Republic
 +
{label='Léonore'     , property='P640' , lang='fr', regexp='^[LHC%/%d]+$' },
 +
{label='SBN'        , property='P396' , lang='it'},  -- Istituto Centrale per il Catalogo Unico / National Library Service (SBN) of Italy
 +
{label='RSL'        , property='P947' , lang='ru', regexp='^%d%d%d%d%d%d%d%d%d$' },  --Russian State Library
 +
{label='Botanist'    , property='P428' , lang='en' },
 +
{label='US Congress' , property='P1157', lang='en', regexp='^%u00[01]%d%d%d' },
 +
{label='BNE'        , property='P950' , lang='es', regexp='' }, --Biblioteca Nacional de España
 +
{label='CALIS'      , property='P270' , lang='zh'}, --China Academic Library and Information
 +
{label='CiNii'      , property='P271' , lang='jp', regexp='^DA%d%d%d%d%d%d%d[%dX]$' },
 +
{label='TLS'        , property='P1362', lang='de', regexp='' }, -- Theaterlexikon der Schweiz
 +
{label='SIKART'      , property='P781' , lang='de', regexp='^%d%d%d%d%d%d%d%d?%d?%d?$' }, -- Swiss
 +
{label='NLP'        , property='P1695', lang='pl', regexp='' }, -- National Library of Poland
 +
{label='WGA'        , property='P1882', lang='en', regexp='' }, -- Web Gallery of Art
 +
{label='KulturNav'  , property='P1248', lang='no', regexp='' },
 +
{label='RKD'        , property='P650' , lang='nl', regexp='^[1-9]%d%d?%d?%d?%d?$' }, --Netherlands Institute for Art History#Online artist pages
 +
{label='autores.uy'  , property='P2558', lang='es', regexp='^[1-9]%d?%d?%d?%d?$' },  --autores.uy
 +
{label='NLI'        , property='P949' , lang='he', regexp='^%d%d%d%d%d%d%d%d%d$' },  --National Library of Israel ID
  
local function selibrLink( id )
+
{label='FIDE'        , property='P1440', lang='en', regexp='' }, -- FIDE database for chess players
if not string.match( id, '^%d+$' ) then
+
{label='Chess Games' , property='P1665', lang='en', regexp='' }, -- Chess Games
        return false
 
    end
 
    return '[//libris.kb.se/auth/' .. id .. ' ' .. id .. ']' .. getCatForId( 'SELIBR' )
 
end
 
  
local function bnfLink( id )
+
{label='ISSN'        , property='P236',  lang='', regexp='' }, -- P1629: International Standard Serial Number
     --Add cb prefix if it has been removed
+
{label='OSM'        , property='P402',  lang='', regexp='' },  -- P1629: OpenStreetMap
    if not string.match( id, '^cb.+$' ) then
+
{label='Joconde'     , property='P347',  lang='fr', regexp='' }, -- Joconde ID
        id = 'cb' .. id
+
{label='Rijksmonument',property='P359',  lang='nl', regexp='' }, -- Rijksmonument ID
    end
+
{label='IMO'        , property='P458',  lang='', regexp='' }, --IMO ship number
 +
{label='BNCF'        , property='P508',  lang='it', regexp='' }, -- BNCF Thesaurus ID
 +
{label='MMSI'        , property='P587',  lang='', regexp='' }, -- P1629: Maritime Mobile Service Identity
 +
{label='Open Library', property='P648',  lang='', regexp='' }, -- P1629: Open Library
 +
{label='NRHP'        , property='P649',  lang='en', regexp='' }, -- NRHP reference number
 +
{label='DBNL'        , property='P723',  lang='', regexp='' }, -- DBNL author ID
 +
{label='Europeana'  , property='P727',  lang='', regexp='' }, -- Europeana ID
 +
{label='UNESCO'      , property='P757',  lang='', regexp='' }, -- World Heritage Site ID
 +
{label='BIC'        , property='P808',  lang='', regexp='' }, -- Bien de Interés Cultural (BIC) code
 +
{label='LIR'        , property='P886',  lang='', regexp='' }, -- LIR
 +
{label='BNR'        , property='P1003', lang='ro', regexp='' }, -- NLR (Romania) ID
 +
{label='Koninklijke' , property='P1006', lang='nl', regexp='' }, -- National Thesaurus for Author Names ID
 +
{label='Atlas'      , property='P1212', lang='', regexp='' }, -- Atlas ID
 +
{label='Historic England', property='P1216', lang='en', regexp='' }, -- National Heritage List for England number
  
    return '[http://catalogue.bnf.fr/ark:/12148/' .. id .. ' ' .. id .. '] [http://data.bnf.fr/ark:/12148/' .. id .. ' (data)]' .. getCatForId( 'BNF' )
+
{label='Oxford Dict.', property='P1415', lang='en', regexp='' }, -- Oxford Dictionary of National Biography ID
end
+
{label='kulturnoe-nasledie', property='P1483', lang='ru', regexp='' },  -- kulturnoe-nasledie.ru ID
 +
{label='Catalunya'  , property='P1600', lang='ca', regexp='' }, -- Inventari del Patrimoni Arquitectònic de Catalunya code
 +
{label='COAM'        , property='P2917', lang='es', regexp='' }, -- COAM structure ID
 +
{label='SIMBAD'      , property='P3083', lang='fr', regexp='' }, -- SIMBAD ID
 +
{label='JCyL'        , property='P3177', lang='es', regexp='' }, -- Patrimonio Web JCyL ID
 +
{label='Zaragoza'    , property='P3178', lang='es', regexp='' },  -- Zaragoza monument ID
 +
{label='BDI'        , property='P3318', lang='es', regexp='' }, -- Patrimonio Inmueble de Andalucía ID
 +
{label='SIPCA'      , property='P3580', lang='es', regexp='' }, -- SIPCA code
 +
{label='DOCOMOMO'    , property='P3758', lang='', regexp='' }, -- DOCOMOMO Ibérico ID
 +
{label='Czech Monument', property='P4075', lang='cz', regexp='' }, -- Czech Monument Catalogue Number
 +
{label='MEG'        , property='P4157', lang='ch', regexp='' }, -- P1629: Musée d'ethnographie de Genève
 +
{label='Enciclopédia Itaú Cultural' , property='P4399', lang='pt_br', regexp='' }, -- Enciclopédia Itaú Cultural ID
 +
{label='Monumentos de São Paulo'    , property='P4360', lang='pt_br', regexp='' }, -- Monumentos de São Paulo ID
 +
{label='Infopatrimônio'            , property='P4372', lang='pt_br', regexp='' }, -- Infopatrimônio ID
 +
{label="Musée d'Orsay"              , property='P4659', lang='fr'   , regexp='' }, -- Musée d'Orsay artwork ID
 +
{label='MuBE'                      , property='P4721', lang='pt_br', regexp='' }, -- MuBE Virtual ID
 +
{label='Hispania Nostra'            , property='P4868', lang='es'   , regexp='' }, -- Hispania Nostra Red List ID
 +
}
  
local function bpnLink( id )
+
-- ==================================================
    if not string.match( id, '^%d+$' ) then
+
-- === External functions ===========================
        return false
+
-- ==================================================
    end
+
local p = {}
    return '[http://www.biografischportaal.nl/en/persoon/' .. id .. ' ' .. id .. ']' .. getCatForId( 'BPN' )
 
end
 
  
local function ridLink( id )
+
function p.getAuthorityControlTag( lang )
    return '[http://www.researcherid.com/rid/' .. id .. ' ' .. id .. ']' .. getCatForId( 'RID' )
+
-- get a localized interwiki link to article "Authority Control"
 +
local field_name = "[[w:en:Help:Authority control|Authority control]]" -- hardwire the default
 +
if lang~='en' then
 +
local Wikidata = require("Module:Wikidata label")    -- used for creation of name based on wikidata
 +
field_name = Wikidata._getLabel("Q36524", lang, "wikipedia")
 +
end
 +
return field_name
 
end
 
end
  
local function bibsysLink( id )
+
-- ==================================================
    return '[https://authority.bibsys.no/authority/rest/authorities/html/' .. id .. ' ' .. id .. ']' .. getCatForId( 'BIBSYS' )
+
function p._authorityControl(entity, args, lang, length)
end
+
-- INPUTS:
 +
--  * entity - wikidata entity if already created or nil. If provided than you should still provide args.Wikidata
 +
--  * args  - structure with identifier fields: args.VIAF, args.LCCN, args.Wikidata, etc.
 +
--  * lang  - language code
 +
--  * length - maximum length of the identifier array, or number of identifiers to display
 +
-- OUTPUTS:
 +
--  * results - wikicode string equivalent to {{Authority control|...|bare=1 }} call
 +
--  * cats    - wikicode with maintenance categories
  
local function ulanLink( id )
+
  -- count custom parameters (not pulled from Wikidata)
    return '[//www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=' .. id .. ' ' .. id .. ']' .. getCatForId( 'ULAN' )
+
local nCustomParam = 0
end
+
for _,params in ipairs( conf ) do
 +
if (args[params.label]~=nil) then
 +
nCustomParam = nCustomParam + 1
 +
end
 +
end
 +
 +
-- Get entity - record of wikidata related to a single item
 +
local q = args.wikidata
 +
if not entity and q then
 +
entity = mw.wikibase.getEntity(q)
 +
end
 +
 +
-- Check if this is category item
 +
local cats = '' -- categories (mismatching and missing)
 +
if entity and entity.claims and entity.claims.P31 then
 +
for _, statement in pairs( entity.claims.P31) do
 +
if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q4167836')  then -- P31 == Wikimedia category
 +
cats = '[[Category:Wrong Wikidata ID in authority control data: category item]]'
 +
end
 +
if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q4167410') then -- P31 == Wikimedia disambiguation page
 +
cats = '[[Category:Wrong Wikidata ID in authority control data: disambiguation item]]'
 +
end
 +
end
 +
end
  
local function nlaLink( id )
+
--compare provided arguments with Wikidata identifiers
return '[//nla.gov.au/anbd.aut-an' .. id .. ' ' .. id .. ']' .. getCatForId( 'NLA' )
+
local data = {} -- structure similar to "args" but filled with wikidata data
end
+
for _,params in ipairs( conf ) do
 +
local label = string.lower(params.label)
 +
data[label] = nil
 +
if entity and entity.claims and params.property and entity.claims[params.property] then -- if we have wikidata item and item has the property
 +
-- capture all Wikidata values for the identifier
 +
--for _, statement in pairs( entity.claims[params.property]) do
 +
for _, statement in pairs( entity:getBestStatements( params.property )) do
 +
if (statement.mainsnak.snaktype == "value") then -- or if statement.mainsnak.datavalue then
 +
local v = statement.mainsnak.datavalue.value
 +
if data[label]==nil then
 +
data[label] = v        -- save the first value
 +
end
 +
if args[label] == v then  -- match between template and wikidata identifiers
 +
data[label] = ''       -- ignore identifier from wikidata
 +
break 
 +
end
 +
end
 +
end
 +
end
 +
end
  
local function rkdartistsLink( id )
+
--Create string with all the identifiers listed
return '[https://rkd.nl/en/explore/artists/' .. id .. ' ' .. id .. ']' .. getCatForId( 'RKDartists' )
+
local results1 = {} -- high priority list
end
+
local results2 = {} -- low  priority list
 +
properties.P214.item = 'Q54919';  -- hardwire link to VIAF
 +
local today = '+' .. os.date('!%F') .. 'T00:00:00Z/11'
 +
local TransStr = 'https://tools.wmflabs.org/quickstatements/index_old.html#v1=%s|%s|%%22%s%%22|S143|Q565|S813|'.. today -- QuickStatementts URL
 +
TransStr = '<span class=\"plainlinks\" title=\"Click (+) to copy to wikidata\">['.. TransStr .. ' (+)]</span>'
 +
for _,params in ipairs( conf ) do
 +
local label = string.lower(params.label)
 +
local val1 = args[label] -- identifier value provided to the template
 +
local val2 = data[label] -- identifier value pulled from wikidata
 +
if val1 or val2 then
 +
local P = properties[params.property] -- properties of wikidata identifier propertyc
 +
-- name_link - link for the identifier name
 +
local name_link = getIdentifierNameLink( lang, P.item, P.issuedBy, params.label )
 +
 +
-- val_link - identifier value or values
 +
local transfer = ''
 +
local val3 = string.gsub(val1 or '', ' ', '' ) -- remove spaces
 +
local val_link
 +
if not val1 then
 +
val_link = getIdentifierValLink(val2, P.URL_format, params, 'blue') -- wikidata only no local identifier
 +
elseif val2=='' then
 +
val_link = getIdentifierValLink(val1, P.URL_format, params, 'magenta') -- match was found
 +
elseif val2 then
 +
val_link = getIdentifierValLink(val1, P.URL_format, params, 'darkgreen') .. "/"..getIdentifierValLink(val2, P.URL_format, params, 'blue')
 +
cats  = string.format("%s[[Category:Pages using authority control with identifiers mismatching Wikidata]]\n", cats)
 +
transfer  = string.format(TransStr, q, params.property, val3)
 +
elseif not val2 and entity then
 +
val_link = getIdentifierValLink(val1, P.URL_format, params, 'darkgreen')
 +
cats  = string.format("%s[[Category:Pages using authority control with identifiers missing from Wikidata]]\n", cats)
 +
transfer  = string.format(TransStr, q, params.property, val3)
 +
else
 +
val_link = getIdentifierValLink(val1, P.URL_format, params, 'blue') -- local identifier and no wikidata q-code
 +
end
  
local function getIdsFromWikidata( item, property )
+
-- combine them all
    local ids = {}
+
local lineStr = string.format("\n*%s:&thinsp;<span class=\"uid\">%s</span>%s", name_link, val_link, transfer)
    if not item.claims[property] then
+
if (params.lang==lang) or (params.lang=='') then
        return ids
+
table.insert(results1, lineStr) -- add to high priority list
    end
+
else
    for _, statement in pairs( item:getBestStatements( property )) do
+
table.insert(results2, lineStr) -- add to low priority list
if statement.mainsnak.datavalue then
+
end
table.insert( ids, statement.mainsnak.datavalue.value )
+
end
 +
end -- for all sources
 +
 +
-- merge high and low priority lists, trim them if needed and convert to string
 +
--table.insert(results1, "\n*End list 1") -- for debuging
 +
--table.insert(results2, "\n*End list 2")
 +
for _,v in pairs(results2) do table.insert(results1, v) end
 +
local results = table.concat(results1, "", 1, math.min(#results1, length or #results1))
 +
 +
-- Add Link to wikidata
 +
if q then
 +
results = string.format("\n*[[File:Wikidata-logo.svg|20px|wikidata:%s|link=wikidata:%s]]: [[d:%s|%s]]%s",q,q,q,q,results)
 +
end
 +
 +
-- Add link to Worldcat
 +
if (args.worldcatid==nil and (args.lccn or data.lccn)) then
 +
args.worldcatid = 'lccn-' .. (args.lccn or data.lccn)
 +
end
 +
if args.worldcatid  then
 +
results = string.format("%s\n*<span class=\"uid\">[//www.worldcat.org/identities/%s WorldCat]</span>", results, args.worldcatid)
 +
end
 +
 +
-- Add maintenance categories
 +
if q == nil then
 +
cats = string.format("%s[[Category:Pages using authority control without Wikidata link]]\n", cats)
 +
end
 +
if nCustomParam>0 then
 +
if cats=='' and entity ~= nil then
 +
cats = string.format("%s[[Category:Pages using authority control with all identifiers matching Wikidata]]\n", cats)
 +
end
 +
if string.find(results, "<span style=\"color:red\">") then
 +
cats = string.format("%s[[Category:Pages using authority control with badly formated identifier]]\n", cats)
 
end
 
end
    end
+
end
    return ids
 
end
 
  
local function matchesWikidataRequirements( item, reqs )
+
-- return results
    for _, group in pairs( reqs ) do
+
if results~='' then -- if there are any results than wrap them in <div> tag
        local property = 'p' .. group[1]
+
results  = string.format('<div class="hlist">%s\n</div>', results)
        local qid = group[2]
+
end
        if item.claims[property] ~= nil then
+
return results, cats
            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
 
end
  
local function createRow( id, label, rawValue, link, withUid )
+
--================================================
    if link then
+
function p.authorityControl(frame)
        if withUid then
+
-- prepare arguments
            return '*<span style="white-space:nowrap;">' .. label .. ' <span class="uid">' .. link .. '</span></span>\n'
+
local args = {}
        else
+
for name, value in pairs( frame.args ) do
            return '*<span style="white-space:nowrap;">' .. label .. ' ' .. link .. '</span>\n'
+
if value ~= '' then -- nuke empty strings
        end
+
args[string.lower(name)] = value -- make it case independent
    else
+
end
        return '* <span class="error">The ' .. id .. ' id ' .. rawValue .. ' is not valid.</span>[[Category:Wikipedia articles with faulty authority control identifiers (' .. id .. ')]]\n'
+
end
    end
+
for name, value in pairs( frame:getParent().args ) do
end
+
if value ~= '' then -- nuke empty strings
 
+
args[string.lower(name)] = value
--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
+
end
local conf = {
+
end
    { 'VIAF', '[[Virtual International Authority File|VIAF]]', 214, viafLink },
+
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
    { 'LCCN', '[[Library of Congress Control Number|LCCN]]', 244, lccnLink },
+
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
    { 'ISNI', '[[International Standard Name Identifier|ISNI]]', 213, isniLink },
+
end
    { 'ORCID', '[[ORCID]]', 496, orcidLink },
+
local yesno = require('Module:Yesno')
    { 'GND', '[[Integrated Authority File|GND]]', 227, gndLink },
+
local bare  = yesno(args.bare,false)       
    { 'SELIBR', '[[LIBRIS|SELIBR]]', 906, selibrLink },
+
    { 'SUDOC', '[[Système universitaire de documentation|SUDOC]]', 269, sudocLink },
+
-- Convert template arguments to the same format as used on wikidata
    { 'BNF', '[[Bibliothèque nationale de France|BNF]]', 268, bnfLink },
+
if (args.gnd == nil or args.gnd == '') and args.pnd ~= nil and args.pnd ~= '' then
    { 'BPN', '[[Biografisch Portaal|BPN]]', 651, bpnLink },
+
args.gnd = args.pnd --redirect PND to GND
    { 'RID', '[[ResearcherID]]', 1053, ridLink },
+
end
    { 'BIBSYS', '[[BIBSYS]]', 1015, bibsysLink },
+
if (args.bnf and args.bnf ~= '') then
    { 'ULAN', '[[Union List of Artist Names|ULAN]]', 245, ulanLink },
+
args.bnf = string.sub(args.bnf, 3) -- trim first 2 characters
    { 'HDS', '[[Historical Dictionary of Switzerland|HDS]]', 902, hlsLink },
+
end
    { 'LIR', '[[Historical Dictionary of Switzerland#Lexicon_Istoric_Retic|LIR]]', 886, lirLink },
+
if (args.isni and args.isni ~= '') then -- group in sets of 4
    { 'MBA', '[[MusicBrainz]]', 434, mbLink },
+
args.isni = string.sub(args.isni, 1, 4).." "..string.sub(args.isni,5,8).." "..string.sub(args.isni,9,12).." "..string.sub(args.isni,13,16)
    { 'MGP', '[[Mathematics Genealogy Project|MGP]]', 549, mgpLink },
+
end
    { 'NLA', '[[National Library of Australia|NLA]]', 409, nlaLink },
+
args.lccn = fixLCCN(args.lccn)
    { 'NDL', '[[National Diet Library|NDL]]', 349, ndlLink },
+
args.wikidata = args.wikidata or args.q or nil
    { 'NCL', '[[National Central Library|NCL]]', 1048, nclLink },
+
    { 'NKC', '[[National Library of the Czech Republic|NKC]]', 691, nkcLink },
+
-- call the inner "core" function
    { 'Léonore', '[[Base Léonore|Léonore]]', 640, leonoreLink },
+
local results, cats = p._authorityControl(nil, args, args.lang, args.length)
    { 'SBN', '[[Istituto Centrale per il Catalogo Unico|ICCU]]', 396, sbnLink },
+
local namespace = mw.title.getCurrentTitle().namespace
    { 'RLS', '[[Russian State Library|RLS]]', 947, rslLink },
+
if (namespace == 2 or namespace == 6 or namespace == 10 or namespace == 828 or math.fmod(namespace,2)==1) then
    { 'Botanist', '[[Author citation (botany)|Botanist]]', 428, botanistLink },
+
cats = '' -- lets not add categories to user pages, files, templates, modules or talk pages and concentrate on templates and categories instead
    { 'NARA-person', '[[National Archives and Records Administration|NARA]]', 1222, narapersonLink },
 
    { 'NARA-organization', '[[National Archives and Records Administration|NARA]]', 1223, naraorganizationLink },
 
    { 'USCongress', '[[Biographical Directory of the United States Congress|US Congress]]', 1157, uscongressLink },
 
    { 'BNE', '[[Biblioteca Nacional de España|BNE]]', 950, bneLink },
 
    { 'CINII', '[[CiNii]]', 271, ciniiLink },
 
    { 'TLS', '[[Theaterlexikon der Schweiz|TLS]]', 1362, tlsLink },
 
    { 'SIKART', '[[SIKART]]', 781, sikartLink },
 
    { 'KULTURNAV', '[[KulturNav]]', 1248, kulturnavLink },
 
    { 'RKDartists', '[[Netherlands Institute for Art History#Online artist pages|RKD]]', 650, 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 Identities]', false ) ) --Validation?
 
    elseif parentArgs['VIAF'] and string.match( parentArgs['VIAF'], '^%d+$' ) then -- Hackishly copy the validation code; this should go away when we move to using P1793 and P1630
 
        table.insert( elements, createRow( 'VIAF', '', parentArgs['VIAF'], '[//www.worldcat.org/identities/containsVIAFID/' .. parentArgs['VIAF'] .. ' WorldCat Identities]', false ) )
 
    elseif parentArgs['LCCN'] and parentArgs['LCCN'] ~= '' then
 
        local lccnParts = splitLccn( parentArgs['LCCN'] )
 
        if lccnParts and lccnParts[1] ~= 'sh' then
 
            table.insert( elements, createRow( 'LCCN', '', parentArgs['LCCN'], '[//www.worldcat.org/identities/lccn-' .. lccnParts[1] .. lccnParts[2] .. '-' .. lccnParts[3] .. ' WorldCat Identities]', 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
 
end
 
 
if #elements ~= 0 then
+
--package results as a infobox if not "bare"
return Navbox._navbox( {
+
if not bare then
name  = 'Authority control',
+
-- Get field name for authority control
bodyclass = 'hlist',
+
local field_name = p.getAuthorityControlTag(args.lang)
group1 = '[[Help:Authority control|Authority control]]' .. elementscats,
+
 
list1 = table.concat( elements )
+
-- build table
} )
+
results = string.format('<tr><td class="type fileinfo-paramfield">%s</td><td>\n%s\n</td></tr>', field_name, results)
 +
local style = frame:expandTemplate{ title="Infobar-Layout", args={ ["lang"] = args.lang, ["class"] = 'commons-file-information-table' } }
 +
results = string.format('<table %s>%s</table>\n', style, results)
 
else
 
else
return ""
+
results = string.format('\n%s\n', results)
 
end
 
end
 +
return results..cats
 
end
 
end
  
 
return p
 
return p

Revision as of 05:37, 24 January 2019

--[[

 __  __           _       _           _         _   _                _ _                           _             _ 
|  \/  | ___   __| |_   _| | ___ _   / \  _   _| |_| |__   ___  _ __(_) |_ _   _    ___ ___  _ __ | |_ _ __ ___ | |
| |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __| '_ \ / _ \| '__| | __| | | |  / __/ _ \| '_ \| __| '__/ _ \| |
| |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | |_| | | | (_) | |  | | |_| |_| | | (_| (_) | | | | |_| | | (_) | |
|_|  |_|\___/ \__,_|\__,_|_|\___(_)_/   \_\__,_|\__|_| |_|\___/|_|  |_|\__|\__, |  \___\___/|_| |_|\__|_|  \___/|_|
                                                                           |___/                                    

This module is intended to be the engine behind "Template:Authority control".

Please do not modify this code without applying the changes first at "Module:Authority control/sandbox" and testing at "Module:Authority control/testcases".

Authors and maintainers:

  • User:Jarekt - original version

]]

-- ================================================== -- === Internal functions =========================== -- ==================================================

local function getSitelink(item, lang) local langList = mw.language.getFallbacksFor(lang) table.insert(langList, 1, lang) for _, language in ipairs(langList) do local sitelink = mw.wikibase.sitelink( item, language .. 'wiki' ) if sitelink then return 'w:'.. language ..':'.. sitelink end end return nil end

local function getIdentifierNameLink( lang, item1, item2, label ) -- Identifier names, like VIAF, LCCN, ISNI, need to be linked to the articles about them if possible -- Alternativly they can be linked to the articles for institutions that issue them local id_name_URL = nil -- 1) try wikipedia sitelink for the identifier in users language and in English if item1 and item1 ~= then id_name_URL = getSitelink(item1, lang) end -- 2) try wikipedia sitelink for the issuedBy property in users language and in English if id_name_URL==nil and item2 and item2 ~= then -- if no link than id_name_URL = getSitelink(item2, lang) end -- 3) if still no links than link to wikidata if id_name_URL then return string.format("%s", id_name_URL, label) -- link to wikipedia else return string.format("%s", item1, label) -- link to wikidata end end

-- ================================================== -- Create link to a single identifier -- INPUTS: -- * val - value of the identifier -- * URL_format - string used to create URL -- * params - additional parameters related to this type of identifiers. Single item from "conf" -- * color - color of the link local function getIdentifierValLink(val, URL_format, params, color) if not val or val== then return end -- check if identifier is in the right format local mismatchStr = local val_ = val:gsub( ' ', ) -- remove spaces if (params.regexp and not val:match( params.regexp )) then mismatchStr = string.format("[does not match %s pattern]", params.regexp) elseif (params.verify) then -- check if special "Verify" function is present mismatchStr = params.verify(val_) -- add error message if any end -- identifier_value_URL local val_URL = URL_format:gsub('$1', val_)-- URL part of the link for the identifier value if color~="blue" then val = string.format('%s', color, val) end return string.format("[%s %s]%s", val_URL, val, mismatchStr) -- link to the identifier's external website end

-- ================================================== -- Convert between 2 formats of LCCN: "n/79/63767" -> "n79063767" -- "n/79/63767" format was used as input by Template:Authority Control templates -- "n79063767" format is used by wikidata local function fixLCCN(id)

 if id then

local a, b, c = string.match(id, "([%a%d]*)/([%a%d]*)/([%a%d]*)") if c then local pad = 6 - string.len(c) if pad > 0 then c = string.rep("0", pad)..c end id = a..b..c end end return id end -- fixLCCN

-- ================================================== -- Verify last "check" digit is correct. ISNI and several other -- identifiers use last digit as a verification digit local function verifyLastDigit( id )

   local total = 0
   for i = 1, #id-1 do
       local digit = id:byte( i ) - 48 --Get integer value
       total = (total + digit) * 2
   end
   --local remainder = total % 11
   local lastDigit = tostring((12 - total % 11) % 11)
   if lastDigit == '10' then
       lastDigit = "X"
   end
   if (lastDigit == string.sub( id, -1)) then

return else return "[last digit should be " .. lastDigit .. "]" end end

-- ================================================== -- === Settings ===================================== -- ================================================== -- In order to add a new identifier associated with Wikidata property do the following -- 1) go to Template:Authority control/IdentifierList and verify that the property number is on the list, if not than edit the page to add it -- 2) copy code generated at Template:Authority control/IdentifierList to protected Module:Authority control/conf -- 3) add the property to the "conf" list below

-- load 'Module:Authority control/conf' which holds hardwired data derived from Wikidata's properties of -- properties local properties = require('Module:Authority control/conf')

--conf holds list of identifiers to be displayed local conf = {

   -- people 

{label='VIAF' , property='P214' , lang= , regexp='^%d+$' }, {label='ISNI' , property='P213' , lang= , regexp='^%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d[%dX]$', verify=verifyLastDigit }, {label='ORCID' , property='P496' , lang= , regexp='^0000%-000[1-3]%-%d%d%d%d%-%d%d%d[%dX]$' }, {label='ULAN' , property='P245' , lang= , regexp='^500%d%d%d%d%d%d$' }, -- 'Union List of Artist Names' by Getty Research Institute {label='ResearcherID', property='P1053', lang= , regexp='^[A-Z]%-%d%d%d%d%-[12][90]%d%d$' }, {label='LCCN' , property='P244' , lang='en', regexp='^[ns][broshj]?%d%d%d%d%d%d%d%d%d?%d?$' }, -- Library of Congress Authorities {label='GND' , property='P227' , lang='de', regexp='^[%dX%-]+$'}, {label='SELIBR' , property='P906' , lang='se', regexp='^%d+$' }, -- National Library of Sweden {label='SUDOC' , property='P269' , lang='fr', regexp='^%d%d%d%d%d%d%d%d[%dxX]$' }, {label='BNF' , property='P268' , lang='fr', regexp='^%d+%w?$' }, -- Bibliothèque nationale de France {label='BPN' , property='P651' , lang='nl', regexp='^%d%d%d%d%d%d%d%d$' }, -- Biografisch Portaal number {label='NAID' , property='P1225', lang='en', regexp='^%d+$' }, -- NARA ID (redirect for US National Archives Identifier (P1225)) {label='NARA' , property='P1225', lang='en', regexp= }, -- US National Archives Identifier {label='Museofile' , property='P539' , lang='fr', regexp='^M%d%d%d%d%-?%d?%d?$' }, --Ministry of Culture (France) {label='NDL' , property='P349' , lang='ja', regexp='^0?%d%d%d%d%d%d%d%d$' }, -- National Diet Library (of Japan) {label='NLA' , property='P409' , lang='en', regexp='^[1-9]%d*$' }, -- National Library of Australia {label='BIBSYS' , property='P1015', lang='no', regexp='^%d+$' }, -- Norwegian information system BIBSYS {label='HDS' , property='P902' , lang='de', regexp='^[1-9]%d%d?%d?%d?%d?$' }, -- Historical Dictionary of Switzerland {label='MusicBrainz' , property='P434' , lang='en', regexp='^[-%x]+$' }, {label='MGP' , property='P549' , lang='en', regexp='^%d%d?%d?%d?%d?%d?$' }, -- Mathematics Genealogy Project {label='NCL' , property='P1048', lang='zh', regexp='^%d+$' }, --National Central Library (Taiwan) {label='NKC' , property='P691' , lang='cs', regexp='^%l%l%l?%l?%d%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?$' }, --National Library of the Czech Republic {label='Léonore' , property='P640' , lang='fr', regexp='^[LHC%/%d]+$' }, {label='SBN' , property='P396' , lang='it'}, -- Istituto Centrale per il Catalogo Unico / National Library Service (SBN) of Italy {label='RSL' , property='P947' , lang='ru', regexp='^%d%d%d%d%d%d%d%d%d$' }, --Russian State Library {label='Botanist' , property='P428' , lang='en' }, {label='US Congress' , property='P1157', lang='en', regexp='^%u00[01]%d%d%d' }, {label='BNE' , property='P950' , lang='es', regexp= }, --Biblioteca Nacional de España {label='CALIS' , property='P270' , lang='zh'}, --China Academic Library and Information {label='CiNii' , property='P271' , lang='jp', regexp='^DA%d%d%d%d%d%d%d[%dX]$' }, {label='TLS' , property='P1362', lang='de', regexp= }, -- Theaterlexikon der Schweiz {label='SIKART' , property='P781' , lang='de', regexp='^%d%d%d%d%d%d%d%d?%d?%d?$' }, -- Swiss {label='NLP' , property='P1695', lang='pl', regexp= }, -- National Library of Poland {label='WGA' , property='P1882', lang='en', regexp= }, -- Web Gallery of Art {label='KulturNav' , property='P1248', lang='no', regexp= }, {label='RKD' , property='P650' , lang='nl', regexp='^[1-9]%d%d?%d?%d?%d?$' }, --Netherlands Institute for Art History#Online artist pages {label='autores.uy' , property='P2558', lang='es', regexp='^[1-9]%d?%d?%d?%d?$' }, --autores.uy {label='NLI' , property='P949' , lang='he', regexp='^%d%d%d%d%d%d%d%d%d$' }, --National Library of Israel ID

{label='FIDE' , property='P1440', lang='en', regexp= }, -- FIDE database for chess players {label='Chess Games' , property='P1665', lang='en', regexp= }, -- Chess Games

{label='ISSN' , property='P236', lang=, regexp= }, -- P1629: International Standard Serial Number {label='OSM' , property='P402', lang=, regexp= }, -- P1629: OpenStreetMap {label='Joconde' , property='P347', lang='fr', regexp= }, -- Joconde ID {label='Rijksmonument',property='P359', lang='nl', regexp= }, -- Rijksmonument ID {label='IMO' , property='P458', lang=, regexp= }, --IMO ship number {label='BNCF' , property='P508', lang='it', regexp= }, -- BNCF Thesaurus ID {label='MMSI' , property='P587', lang=, regexp= }, -- P1629: Maritime Mobile Service Identity {label='Open Library', property='P648', lang=, regexp= }, -- P1629: Open Library {label='NRHP' , property='P649', lang='en', regexp= }, -- NRHP reference number {label='DBNL' , property='P723', lang=, regexp= }, -- DBNL author ID {label='Europeana' , property='P727', lang=, regexp= }, -- Europeana ID {label='UNESCO' , property='P757', lang=, regexp= }, -- World Heritage Site ID {label='BIC' , property='P808', lang=, regexp= }, -- Bien de Interés Cultural (BIC) code {label='LIR' , property='P886', lang=, regexp= }, -- LIR {label='BNR' , property='P1003', lang='ro', regexp= }, -- NLR (Romania) ID {label='Koninklijke' , property='P1006', lang='nl', regexp= }, -- National Thesaurus for Author Names ID {label='Atlas' , property='P1212', lang=, regexp= }, -- Atlas ID {label='Historic England', property='P1216', lang='en', regexp= }, -- National Heritage List for England number

{label='Oxford Dict.', property='P1415', lang='en', regexp= }, -- Oxford Dictionary of National Biography ID {label='kulturnoe-nasledie', property='P1483', lang='ru', regexp= }, -- kulturnoe-nasledie.ru ID {label='Catalunya' , property='P1600', lang='ca', regexp= }, -- Inventari del Patrimoni Arquitectònic de Catalunya code {label='COAM' , property='P2917', lang='es', regexp= }, -- COAM structure ID {label='SIMBAD' , property='P3083', lang='fr', regexp= }, -- SIMBAD ID {label='JCyL' , property='P3177', lang='es', regexp= }, -- Patrimonio Web JCyL ID {label='Zaragoza' , property='P3178', lang='es', regexp= }, -- Zaragoza monument ID {label='BDI' , property='P3318', lang='es', regexp= }, -- Patrimonio Inmueble de Andalucía ID {label='SIPCA' , property='P3580', lang='es', regexp= }, -- SIPCA code {label='DOCOMOMO' , property='P3758', lang=, regexp= }, -- DOCOMOMO Ibérico ID {label='Czech Monument', property='P4075', lang='cz', regexp= }, -- Czech Monument Catalogue Number {label='MEG' , property='P4157', lang='ch', regexp= }, -- P1629: Musée d'ethnographie de Genève {label='Enciclopédia Itaú Cultural' , property='P4399', lang='pt_br', regexp= }, -- Enciclopédia Itaú Cultural ID {label='Monumentos de São Paulo' , property='P4360', lang='pt_br', regexp= }, -- Monumentos de São Paulo ID {label='Infopatrimônio' , property='P4372', lang='pt_br', regexp= }, -- Infopatrimônio ID {label="Musée d'Orsay" , property='P4659', lang='fr' , regexp= }, -- Musée d'Orsay artwork ID {label='MuBE' , property='P4721', lang='pt_br', regexp= }, -- MuBE Virtual ID {label='Hispania Nostra' , property='P4868', lang='es' , regexp= }, -- Hispania Nostra Red List ID }

-- ================================================== -- === External functions =========================== -- ================================================== local p = {}

function p.getAuthorityControlTag( lang ) -- get a localized interwiki link to article "Authority Control" local field_name = "Authority control" -- hardwire the default if lang~='en' then local Wikidata = require("Module:Wikidata label") -- used for creation of name based on wikidata field_name = Wikidata._getLabel("Q36524", lang, "wikipedia") end return field_name end

-- ================================================== function p._authorityControl(entity, args, lang, length) -- INPUTS: -- * entity - wikidata entity if already created or nil. If provided than you should still provide args.Wikidata -- * args - structure with identifier fields: args.VIAF, args.LCCN, args.Wikidata, etc. -- * lang - language code -- * length - maximum length of the identifier array, or number of identifiers to display -- OUTPUTS: -- * results - wikicode string equivalent to

call

-- * cats - wikicode with maintenance categories

 -- count custom parameters (not pulled from Wikidata)

local nCustomParam = 0 for _,params in ipairs( conf ) do if (args[params.label]~=nil) then nCustomParam = nCustomParam + 1 end end

-- Get entity - record of wikidata related to a single item local q = args.wikidata if not entity and q then entity = mw.wikibase.getEntity(q) end

-- Check if this is category item local cats = -- categories (mismatching and missing) if entity and entity.claims and entity.claims.P31 then for _, statement in pairs( entity.claims.P31) do if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q4167836') then -- P31 == Wikimedia category cats = end if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q4167410') then -- P31 == Wikimedia disambiguation page cats = end end end

--compare provided arguments with Wikidata identifiers local data = {} -- structure similar to "args" but filled with wikidata data for _,params in ipairs( conf ) do local label = string.lower(params.label) data[label] = nil if entity and entity.claims and params.property and entity.claims[params.property] then -- if we have wikidata item and item has the property -- capture all Wikidata values for the identifier --for _, statement in pairs( entity.claims[params.property]) do for _, statement in pairs( entity:getBestStatements( params.property )) do if (statement.mainsnak.snaktype == "value") then -- or if statement.mainsnak.datavalue then local v = statement.mainsnak.datavalue.value if data[label]==nil then data[label] = v -- save the first value end if args[label] == v then -- match between template and wikidata identifiers data[label] = -- ignore identifier from wikidata break end end end end end

--Create string with all the identifiers listed local results1 = {} -- high priority list local results2 = {} -- low priority list properties.P214.item = 'Q54919'; -- hardwire link to VIAF local today = '+' .. os.date('!%F') .. 'T00:00:00Z/11' local TransStr = 'https://tools.wmflabs.org/quickstatements/index_old.html#v1=%s%7C%s%7C%%22%s%%22%7CS143%7CQ565%7CS813%7C'.. today -- QuickStatementts URL TransStr = '['.. TransStr .. ' (+)]' for _,params in ipairs( conf ) do local label = string.lower(params.label) local val1 = args[label] -- identifier value provided to the template local val2 = data[label] -- identifier value pulled from wikidata if val1 or val2 then local P = properties[params.property] -- properties of wikidata identifier propertyc -- name_link - link for the identifier name local name_link = getIdentifierNameLink( lang, P.item, P.issuedBy, params.label )

-- val_link - identifier value or values local transfer = local val3 = string.gsub(val1 or , ' ', ) -- remove spaces local val_link if not val1 then val_link = getIdentifierValLink(val2, P.URL_format, params, 'blue') -- wikidata only no local identifier elseif val2== then val_link = getIdentifierValLink(val1, P.URL_format, params, 'magenta') -- match was found elseif val2 then val_link = getIdentifierValLink(val1, P.URL_format, params, 'darkgreen') .. "/"..getIdentifierValLink(val2, P.URL_format, params, 'blue') cats = string.format("%s\n", cats) transfer = string.format(TransStr, q, params.property, val3) elseif not val2 and entity then val_link = getIdentifierValLink(val1, P.URL_format, params, 'darkgreen') cats = string.format("%s\n", cats) transfer = string.format(TransStr, q, params.property, val3) else val_link = getIdentifierValLink(val1, P.URL_format, params, 'blue') -- local identifier and no wikidata q-code end

-- combine them all local lineStr = string.format("\n*%s: %s%s", name_link, val_link, transfer) if (params.lang==lang) or (params.lang==) then table.insert(results1, lineStr) -- add to high priority list else table.insert(results2, lineStr) -- add to low priority list end end end -- for all sources

-- merge high and low priority lists, trim them if needed and convert to string --table.insert(results1, "\n*End list 1") -- for debuging --table.insert(results2, "\n*End list 2") for _,v in pairs(results2) do table.insert(results1, v) end local results = table.concat(results1, "", 1, math.min(#results1, length or #results1))

-- Add Link to wikidata if q then results = string.format("\n*wikidata:%s: %s%s",q,q,q,q,results) end

-- Add link to Worldcat if (args.worldcatid==nil and (args.lccn or data.lccn)) then args.worldcatid = 'lccn-' .. (args.lccn or data.lccn) end if args.worldcatid then results = string.format("%s\n*WorldCat", results, args.worldcatid) end

-- Add maintenance categories if q == nil then cats = string.format("%s\n", cats) end if nCustomParam>0 then if cats== and entity ~= nil then cats = string.format("%s\n", cats) end if string.find(results, "") then cats = string.format("%s\n", cats) end end

-- return results

if results~= then -- if there are any results than wrap them in

tag results = string.format('
%s\n
', results)

end return results, cats end

--================================================ function p.authorityControl(frame) -- prepare arguments local args = {} for name, value in pairs( frame.args ) do if value ~= then -- nuke empty strings args[string.lower(name)] = value -- make it case independent end end for name, value in pairs( frame:getParent().args ) do if value ~= then -- nuke empty strings args[string.lower(name)] = value end end if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language end local yesno = require('Module:Yesno') local bare = yesno(args.bare,false)

-- Convert template arguments to the same format as used on wikidata if (args.gnd == nil or args.gnd == ) and args.pnd ~= nil and args.pnd ~= then args.gnd = args.pnd --redirect PND to GND end if (args.bnf and args.bnf ~= ) then args.bnf = string.sub(args.bnf, 3) -- trim first 2 characters end if (args.isni and args.isni ~= ) then -- group in sets of 4 args.isni = string.sub(args.isni, 1, 4).." "..string.sub(args.isni,5,8).." "..string.sub(args.isni,9,12).." "..string.sub(args.isni,13,16) end args.lccn = fixLCCN(args.lccn) args.wikidata = args.wikidata or args.q or nil

-- call the inner "core" function local results, cats = p._authorityControl(nil, args, args.lang, args.length) local namespace = mw.title.getCurrentTitle().namespace if (namespace == 2 or namespace == 6 or namespace == 10 or namespace == 828 or math.fmod(namespace,2)==1) then cats = -- lets not add categories to user pages, files, templates, modules or talk pages and concentrate on templates and categories instead end

--package results as a infobox if not "bare" if not bare then -- Get field name for authority control local field_name = p.getAuthorityControlTag(args.lang)

-- build table

results = string.format('%s\n%s\n', field_name, results)

local style = frame:expandTemplate{ title="Infobar-Layout", args={ ["lang"] = args.lang, ["class"] = 'commons-file-information-table' } }

results = string.format('%s
\n', style, results)

else results = string.format('\n%s\n', results) end return results..cats end

return p