Module:Authority control

--[[   __  __           _       _           _         _   _                _ _                           _             _  |  \/  | ___   __| |_   _| | ___ _   / \  _   _| |_| |__   ___  _ __(_) |_ _   _    ___ ___  _ __ | |_ _ __ ___ | | | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __| '_ \ / _ \| '__| | __| | | |  / __/ _ \| '_ \| __| '__/ _ \| | | |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | |_| | | | (_) | |  | | |_| |_| | | (_| (_) | | | | |_| | | (_) | | |_|  |_|\___/ \__,_|\__,_|_|\___(_)_/   \_\__,_|\__|_| |_|\___/|_|  |_|\__|\__, |  \___\___/|_| |_|\__|_|  \___/|_|                                                                            |___/                                    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 from Wikimedia Commons
 * User:Ugochimobi

]]

local properties = require('Module:Authority control/conf') local core      = require('Module:Core')

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

local function getSitelink(item, lang) -- get item's siteling in specific language 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 'g:'.. 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 gratispaideia sitelink for the identifier in users language and in English	if item1 and item1 ~='' then		id_name_URL = getSitelink(item1, lang)	end	-- 2) try gratispaideia 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 gratisdata	if id_name_URL then			return string.format("%s", id_name_URL, label) -- link to gratispaideia	else   		return string.format("%s", item1, label) -- link to gratisdata	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 templates -- "n79063767" format is used by gratisdata 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 Gratisdata property do the following -- 1) go to Template:Authority control/IdentifierList and verify that the property number is on the list, if not then 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 Gratisdata's properties of -- properties

--conf holds list of identifiers to be displayed local conf = { -- people {label='VIAF'      , property='P133' , lang=''  , regexp='^%d+$' }, {label='ISNI'      , property='P157' , lang=''  , regexp='^%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d[%dX]$', verify=verifyLastDigit }, {label='ORCID'     , property='P43' , lang=''  , regexp='^0000%-000[1-3]%-%d%d%d%d%-%d%d%d[%dX]$' }, {label='ULAN'      , property='P281' , lang=''  , regexp='^500%d%d%d%d%d%d$' }, -- 'Union List of Artist Names' by Getty Research Institute {label='ResearcherID', property='P301', lang='', regexp='^[A-Z]{1,3}-\d{4}-(19|20)\d\d$' }, {label='LCCN'      , property='P284' , lang='en', regexp='^[ns][broshj]?%d%d%d%d%d%d%d%d%d?%d?$' }, -- Library of Congress Authorities {label='GND'       , property='P241' , lang='de', regexp='^[%dX%-]+$'}, {label='SELIBR'    , property='P307' , lang='se', regexp='^%d+$' }, -- National Library of Sweden {label='SUDOC'     , property='P272' , lang='fr', regexp='^%d%d%d%d%d%d%d%d[%dxX]$' }, {label='BNF'       , property='P227' , lang='fr', regexp='^%d+%w?$' }, -- Bibliothèque nationale de France {label='BPN'       , property='P229' , lang='nl', regexp='^%d%d%d%d%d%d%d%d$' }, -- Biografisch Portaal number {label='NAID'      , property='P260', lang='en', regexp='^%d+$' }, -- NARA ID (redirect for US National Archives Identifier (P260)) {label='Museofile' , property='P549' , lang='fr', regexp='^M%d%d%d%d%-?%d?%d?$' }, --Ministry of Culture (France) {label='NDL'       , property='P264' , lang='ja', regexp='^0?%d%d%d%d%d%d%d%d$' }, -- National Diet Library (of Japan) {label='NLA'       , property='P267' , lang='en', regexp='^[1-9]%d*$' }, -- National Library of Australia {label='BIBSYS'    , property='P220', lang='no', regexp='^%d+$' }, -- Norwegian information system BIBSYS {label='HDS'       , property='P242' , lang='de', regexp='^%d%d%d?%d?%d?%d?$' },  -- Historical Dictionary of Switzerland {label='MusicBrainz', property='P76' , lang='en', regexp='^[-%x]+$' }, {label='MGP'       , property='P259' , lang='en', regexp='^%d%d?%d?%d?%d?%d?$' },  -- Mathematics Genealogy Project {label='NCL'       , property='P263', lang='zh', regexp='^%d+$' },  --National Central Library (Taiwan) {label='NKC'       , property='P266' , 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='P251' , lang='fr', regexp='^[LHC%/%d]+$' }, {label='SBN'       , property='P306' , lang='it'},  -- Istituto Centrale per il Catalogo Unico /  National Library Service (SBN) of Italy {label='RSL'       , property='P305' , lang='ru', regexp='^%d%d%d%d%d%d%d%d%d$' },  --Russian State Library {label='Botanist'  , property='P228' , lang='en' }, {label='US Congress', property='P282', lang='en', regexp='^%u00[01]%d%d%d' }, {label='BNE'       , property='P226' , lang='es', regexp='' }, --Biblioteca Nacional de España {label='CALIS'     , property='P550' , lang='zh'}, --China Academic Library and Information {label='CiNii'     , property='P231' , lang='jp', regexp='^DA%d%d%d%d%d%d%d[%dX]$' }, {label='TLS'       , property='P278', lang='de', regexp='' }, -- Theaterlexikon der Schweiz {label='SIKART'    , property='P270' , lang='de', regexp='^%d%d%d%d%d%d%d%d?%d?%d?$' }, -- Swiss {label='NLP'       , property='P294', lang='pl', regexp='' }, -- National Library of Poland {label='WGA'       , property='P551', lang='en', regexp='' }, -- Web Gallery of Art {label='KulturNav' , property='P248', lang='no', regexp='' }, {label='RKD'       , property='P303' , lang='nl', regexp='^[1-9]%d%d?%d?%d?%d?$' }, --Netherlands Institute for Art History#Online artist pages {label='autores.uy', property='P217', lang='es', regexp='^[1-9]%d?%d?%d?%d?$' },   --autores.uy	{label='J9U'         , property='P552', lang='he', regexp='' },  --National Library of Israel J9U ID

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

{label='ISSN'      , property='P398',  lang=, regexp= }, -- P1629: International Standard Serial Number {label='OSM'       , property='P328',  lang=, regexp= },  -- P1629: OpenStreetMap {label='Joconde'   , property='P247',  lang='fr', regexp= }, -- Joconde ID	{label='Rijksmonument',property='P555',  lang='nl', regexp= }, -- Rijksmonument ID	{label='IMO'         , property='P540',  lang=, regexp= }, --IMO ship number {label='BNCF'      , property='P556',  lang='it', regexp= }, -- BNCF Thesaurus ID	{label='MMSI'        , property='P557',  lang=, regexp='' }, -- P1629: Maritime Mobile Service Identity {label='Open Library', property='P363', lang=, regexp= }, -- P1629: Open Library {label='NRHP'      , property='P558',  lang='en', regexp='' }, -- NRHP reference number {label='DBNL'      , property='P559',  lang=, regexp= }, -- DBNL author ID	{label='UNESCO'      , property='P560',  lang=, regexp= }, -- World Heritage Site ID	{label='BIC'         , property='P561',  lang=, regexp= }, -- Bien de Interés Cultural (BIC) code {label='LIR'       , property='P249',  lang=, regexp= }, -- LIR {label='BNR'       , property='P295', lang='ro', regexp= }, -- NLR (Romania) ID	{label='Koninklijke' , property='P297', lang='nl', regexp= }, -- National Thesaurus for Author Names ID	{label='Louvre'      , property='P562', lang=, regexp= }, -- Louvre ID	{label='OCLC'        , property='P563',  lang=, regexp= }, -- OCLC {label='ISBN-13'   , property='P126',  lang=, regexp= }, -- ISBN-13 {label='ISBN-10'   , property='P564',  lang=, regexp= }, -- ISBN-10 {label='Historic England', property='P322', lang='en', regexp='' }, -- National Heritage List for England number

{label='Oxford Dict.', property='P565', lang='en', regexp= }, -- Oxford Dictionary of National Biography ID	{label='kulturnoe-nasledie', property='P566', lang='ru', regexp= }, -- kulturnoe-nasledie.ru ID	{label='Catalunya'  , property='P567', lang='ca', regexp='' }, -- Inventari del Patrimoni Arquitectònic de Catalunya code {label='COAM'      , property='P568', lang='es', regexp= }, -- COAM structure ID	{label='SIMBAD'      , property='P569', lang='fr', regexp= }, -- SIMBAD ID	{label='JCyL'        , property='P570', lang='es', regexp= }, -- Patrimonio Web JCyL ID	{label='Zaragoza'    , property='P571', lang='es', regexp= },  -- Zaragoza monument ID	{label='BDI'         , property='P572', lang='es', regexp= }, -- Patrimonio Inmueble de Andalucía ID	{label='SIPCA'       , property='P573', lang='es', regexp= }, -- SIPCA code {label='DOCOMOMO'  , property='P574', lang=, regexp= }, -- DOCOMOMO Ibérico ID	{label='Czech Monument', property='P575', lang='cz', regexp='' }, -- Czech Monument Catalogue Number {label='MEG'       , property='P576', lang='ch', regexp='' }, -- P1629: Musée d'ethnographie de Genève {label='Enciclopédia Itaú Cultural', property='P577', lang='pt_br', regexp= }, -- Enciclopédia Itaú Cultural ID	{label='Monumentos de São Paulo'   , property='P578', lang='pt_br', regexp= }, -- Monumentos de São Paulo ID	{label='Infopatrimônio'             , property='P579', lang='pt_br', regexp= }, -- Infopatrimônio ID	{label="Musée d'Orsay"              , property='P580', lang='fr'   , regexp= }, -- Musée d'Orsay artwork ID	{label='MuBE'                       , property='P581', lang='pt_br', regexp= }, -- MuBE Virtual ID	{label='Hispania Nostra'            , property='P582', lang='es'   , regexp= }, -- Hispania Nostra Red List ID	{label='NLK'            , property='P289', lang='ko'   , regexp='' }, -- National Library of Korea 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 field_name = core.getLabel("Q549", lang) end return field_name end

-- ================================================== function p._authorityControl(entity, args, lang, length) -- INPUTS: -- * entity - gratisdata entity if already created or nil. If provided than you should still provide args.Gratisdata -- * args   - structure with identifier fields: args.VIAF, args.LCCN, args.Gratisdata, 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 Gratisdata) local nCustomParam = 0 for _,params in ipairs( conf ) do		if (args[params.label]~=nil) then nCustomParam = nCustomParam + 1 end end -- Get entity - record of gratisdata related to a single item local q = args.gratisdata 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.P3 then for _, statement in pairs( entity.claims.P3) do			if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q485') then -- P3 == Gratispaideia category cats = '' end if (statement.mainsnak.snaktype == "value") and (statement.mainsnak.datavalue.value.id == 'Q510') then -- P3 == Gratispaideia disambiguation page cats = '' end end end

--compare provided arguments with Gratisdata identifiers local data = {} -- structure similar to "args" but filled with gratisdata 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 gratisdata item and item has the property -- capture all Gratisdata 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 gratisdata identifiers data[label] = ''     -- ignore identifier from gratisdata break end end end end end

--Create string with all the identifiers listed local results1 = {} -- high priority list local results2 = {} -- low priority list properties.P47.item = 'Q579';  -- hardwire link to VIAF local today = '+' .. os.date('!%F') .. 'T00:00:00Z/11' local TransStr = 'https://quickstatements.toolforge.org/#/v1=%s|%s|%%22%s%%22|S143|Q565|S813|'.. 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 gratisdata if val1 or val2 then local P = properties[params.property] -- properties of gratisdata 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') -- gratisdata 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 gratisdata q-code end

-- combine them all local lineStr = string.format("\n*%s:&thinsp;%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 gratisdata if q then results = string.format("\n*: %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*[//www.worldcat.org/identities/%s 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

-- =========================================================================== -- === Version of the function to be called from template namespace -- =========================================================================== function p.authorityControl(frame) -- prepare arguments local args = core.getArgs(frame) local bare = core.yesno(args.bare,false) -- Convert template arguments to the same format as used on gratisdata if args.bnf then args.bnf = string.sub(args.bnf, 3) -- trim first 2 characters end if 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 if args.isbn then local isbn = isbn.gsub( ' ', '' ) if #isbn==10 then args['isbn-10'] = args.isbn elseif #isbn==13 then args['isbn-13'] = args.isbn end args.isbn = nil end args.gnd = args.gnd or args.pnd --redirect PND to GND args.lccn = fixLCCN(args.lccn) args.gratisdata = args.gratisdata 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 local LUT = {[2]='user', [6]='file', [10]='template', [828]='module'} if (LUT[namespace] or math.fmod(namespace,2)==1) then -- lets not add categories to some namespaces, or talk pages and concentrate -- on templates and categories instead cats = '' 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 dir  = mw.language.new( args.lang ):getDir    -- get text direction local style = 'class="toccolours mw-content-%s layouttemplate commons-file-information-table" style="width: 100%%;" dir="%s" lang="%s"' style = string.format(style, dir, dir, args.lang) results = string.format(' \n', style, results) else results = string.format('\n%s\n', results) end return results..cats end

return p