Module:Controllu de autoridade

Dae Wikipedia, s'entziclopedia lìbera.
Jump to navigation Jump to search

Documentation for this module may be created at Module:Controllu de autoridade/doc

--[[
* Mòdulu chi implementat su template Controllu de autoridade.
* Su mòdulu est istadu importadu initzialmente dae:
* https://en.wikipedia.org/w/index.php?title=Module:Authority_control&oldid=633242817
]]

local mWikidata = require( 'Module:Wikidata' )
local modlimbas = require( 'Module:Limbàgios' )
require( 'Module:No globals' )

local function viafLink( id )
	if not string.match( id, '^%d+$' ) then
		return false
	end
	return string.format( '[https://viaf.org/viaf/%s %s]', id, id ),
		   string.format( '[https://www.worldcat.org/identities/containsVIAFID/%s %s]', id, id)
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
	for i = 1, 15 do
		local digit = isni:byte( i ) - 48 -- Get integer value
		total = (total + digit) * 2
	end
	local remainder = total % 11
	local result = (12 - remainder) % 11
	if result == 10 then
		return "X"
	end
	return tostring( result )
end

-- Validate ISNI (and ORCID) and retuns it as a 16 characters string or returns false if it's invalid
-- See http://support.orcid.org/knowledgebase/articles/116780-structure-of-the-orcid-identifier
local function validateIsni( id )
	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

local function isniLink( id )
	id = validateIsni( id )
	if not id then
		return false
	end
	return string.format( '[http://isni.org/isni/%s %s %s %s %s]', id, id:sub( 1, 4 ), id:sub( 5, 8 ), id:sub( 9, 12 ), id:sub( 13, 16 ) )
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 string.format( '[https://opac.sbn.it/opacsbn/opac/iccu/scheda_authority.jsp?bid=%s %s]', id, id )
end

local function europeanaLink( id )
	if not string.match( id, '^[a-z]+/base/[1-9]%d*$') then
		return false
	end
	return string.format( '[https://data.europeana.eu/%s %s]', id, id )
end

local function append(str, c, length)
	while str:len() < length do
		str = c .. str
	end
	return str
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 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 )
	local worldCatId
	if parts[1] ~= 'sh' then
		worldCatId = parts[1] .. parts[2] .. '-' .. parts[3]
	end
	return string.format( '[http://id.loc.gov/authorities/%s/%s %s]', lccnType, id, id ),
		   worldCatId and string.format( '[https://www.worldcat.org/identities/lccn-%s %s]', worldCatId, worldCatId)
end

local function orcidLink( id )
	id = validateIsni( id )
	if not id then
		return false
	end
	id = id:sub( 1, 4 ) .. '-' .. id:sub( 5, 8 ) .. '-'  .. id:sub( 9, 12 ) .. '-'  .. id:sub( 13, 16 )
	return string.format( '[https://orcid.org/%s %s]', id, id )
end

local function bavLink( id ) 
	if not string.match( id, 'ADV%d%d%d%d%d%d%d%d$' ) then 
 		return false
	end 
	return id
end

local function cerlLink( id )
	if not string.match( id, 'cn[cilp]%d%d%d%d%d%d%d%d$' ) then 
	 	return false
	end 
	return string.format( '[https://thesaurus.cerl.org/record/%s %s]', id, id )
end

local function aagLink( id )
	--P3372's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[http://www.aucklandartgallery.com/explore-art-and-ideas/artist/%s %s]', id, id )
end

local function acmLink( id )
	--P864's format regex: \d{11} (e.g. 12345678901)
	if not id:match( '^%d%d%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://dl.acm.org/author_page.cfm?id=%s %s]', id, id )
end

local function adbLink( id )
	--P1907's format regex: [a-z][-a-z]+-([1-2]\d|[1-9])\d{0,3} (e.g. barton-sir-edmund-toby-71)
	if not id:match( '^[a-z][-a-z]+-[1-2]%d%d?%d?%d?$' ) and
	   not id:match( '^[a-z][-a-z]+-[1-9]%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[http://adb.anu.edu.au/biography/%s %s]', id, id )
end

local function agsaLink( id )
	--P6804's format regex: [1-9]\d* (e.g. 3625)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[https://www.agsa.sa.gov.au/collection-publications/collection/creators/_/%s %s]', id, id )
end

local function autoresuyLink( id )
	--P2558's format regex: [1-9]\d{0,4} (e.g. 12345)
	if not id:match( '^[1-9]%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://autores.uy/autor/%s %s]', id, id )
end

local function awrLink( id )
	--P4186's format regex: (([A-Z]{3}\d{4})|([A-Z]{2}\d{5}))[a-z] (e.g. PR00768b)
	if not id:match( '^[A-Z][A-Z][A-Z]%d%d%d%d[a-z]$' ) and
	   not id:match( '^[A-Z][A-Z]%d%d%d%d%d[a-z]$' ) then
		return false
	end
	return string.format( '[http://www.womenaustralia.info/biogs/%s %s]', id, id )
end

local function balatLink( id )
	--P3293's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[http://balat.kikirpa.be/object/104257%s %s]', id, id )--no https as of 9/2019
end

local function bibsysLink( id )
	--P1015's format regex: [1-9]\d* or [1-9](\d{0,8}|\d{12}) (e.g. 1234567890123)
	if not id:match( '^[1-9]%d?%d?%d?%d?%d?%d?%d?%d?$' ) and
	   not id:match( '^[1-9]%d%d%d%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://authority.bibsys.no/authority/rest/authorities/html/%s %s]', id, id )
end

local function bildLink( id )
	--P2092's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[https://www.bildindex.de/document/obj%s %s]', id, id )
end

local function bncLink( id )
	--P1890's format regex: \d{9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://www.bildindex.de/document/obj%s %s]', id, id )
end

local function bneLink( id )
	--P950's format regex: (XX|FF|a)\d{4,7}|(bima|bimo|bica|bis[eo]|bivi|Mise|Mimo|Mima)\d{10} (e.g. XX1234567)
	if not id:match( '^[XF][XF]%d%d%d%d%d?%d?%d?$' ) and
	   not id:match( '^a%d%d%d%d%d?%d?%d?$' ) and
	   not id:match( '^bi[mcsv][aoei]%d%d%d%d%d%d%d%d%d%d$' ) and
	   not id:match( '^Mi[sm][eoa]%d%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[http://catalogo.bne.es/uhtbin/authoritybrowse.cgi?action=display&authority_id=%s %s] [http://datos.bne.es/resource/%s (data)]', id, id, id )
end

local function bnfLink( id )
	--P268's format regex: \d{8}[0-9bcdfghjkmnpqrstvwxz] (e.g. 123456789)
	if not id:match( '^c?b?%d%d%d%d%d%d%d%d[0-9bcdfghjkmnpqrstvwxz]$' ) then
		return false
	end
	--Add cb prefix if it has been removed
	if not id:match( '^cb.+$' ) then
		id = 'cb'..id
	end
	return string.format( '[https://catalogue.bnf.fr/ark:/12148/%s %s] [https://data.bnf.fr/ark:/12148/%s (data)]', id, id, id )
end

local function botanistLink( id )
	--P428's format regex: ('t )?(d')?(de )?(la )?(van (der )?)?(Ma?c)?(De)?(Di)?\p{Lu}?C?['\p{Ll}]*([-'. ]*(van )?(y )?(d[ae][nr]?[- ])?(Ma?c)?[\p{Lu}bht]?C?['\p{Ll}]*)*\.? ?f?\.? (e.g. L.)
	--not easily/meaningfully implementable in Lua's regex since "(this)?" is not allowed...
	if not mw.ustring.match( id, "^[%u%l%d%. '-]+$" ) then --better than nothing
		return false
	end
	local id2 = id:gsub(' +', '%%20')
	return string.format( '[https://www.ipni.org/ipni/advAuthorSearch.do?find_abbreviation=%s %s]', id, id )
end

local function bpnLink( id )
	--P651's format regex: \d{6,8} (e.g. 00123456)
	if not id:match( '^%d%d%d%d%d%d%d%d$' ) and --original format regex, changed 8/2019 to
	   not id:match( '^0?%d%d%d%d%d%d%d$' ) and --allow 1-2 leading 0s, allowed by the website
	   not id:match( '^0?0?%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[http://www.biografischportaal.nl/en/persoon/%s %s]', id, id )
end

local function canticLink( id )
	--P1273's format regex: a\d{7}[0-9x] (e.g. a10640745)
	if not id:match( '^a%d%d%d%d%d%d%d[%dx]$' ) then
		return false
	end
	return string.format( '[http://cantic.bnc.cat/registres/CUCId/%s %s]', id, id )
end

local function ciniiLink( id )
	--P271's format regex: DA\d{7}[\dX] (e.g. DA12345678)
	if not id:match( '^DA%d%d%d%d%d%d%d[%dX]$' ) then
		return false
	end
	return string.format( '[https://ci.nii.ac.jp/author/%s %s]', id, id )
end

local function daaoLink( id )
	--P1707's format regex: [a-z\-]+\d* (e.g. rolf-harris)
	if not id:match( '^[a-z%-]+%d*$' ) then
		return false
	end
	return string.format( '[https://www.daao.org.au/bio/%s %s]', id, id )
end

local function dblpLink( id )
	--P2456's format regex: \d{2,3} /\d+(-\d+)?|[a-z] /[a-zA-Z][0-9A-Za-z]*(-\d+)? (e.g. 123/123)
	if not id:match( '^%d%d%d?/%d+$' ) and
	   not id:match( '^%d%d%d?/%d+%-%d+$' ) and
	   not id:match( '^[a-z]/[a-zA-Z][0-9A-Za-z]*$' ) and
	   not id:match( '^[a-z]/[a-zA-Z][0-9A-Za-z]*%-%d+$' ) then
		return false
	end
	return string.format( '[https://dblp.org/pid/%s %s]', id, id )
end

local function dsiLink( id )
	--P2349's format regex: [1-9]\d* (e.g. 1538)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[http://www.uni-stuttgart.de/hi/gnt/dsi2/index.php?table_name=dsi&function=details&where_field=id&where_value=%s %s]', id, id )
end

local function fnzaLink( id )
	--P6792's format regex: [1-9]\d* (e.g. 9785)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[https://findnzartists.org.nz/artist/%s %s]', id, id )
end

local function gndLink( id )                  
	--P227's format regex: 1[012]?\d{7}[0-9X]|[47]\d{6}-\d|[1-9]\d{0,7}-[0-9X]|3\d{7}[0-9X] (e.g. 4079154-3)
	if not id:match( '^1[012]?%d%d%d%d%d%d%d[0-9X]$' ) and
	   not id:match( '^[47]%d%d%d%d%d%d%-%d$' ) and
	   not id:match( '^[1-9]%d?%d?%d?%d?%d?%d?%d?%-[0-9X]$' ) and
	   not id:match( '^3%d%d%d%d%d%d%d[0-9X]$' ) then
		return false
	end
	return string.format( '[https://d-nb.info/gnd/%s %s]', id, id )
end

local function hdsLink( id )
	--P902's format regex: \d{6} (e.g. 050123)
	if not id:match( '^%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://hls-dhs-dss.ch/fr/articles/%s %s]', id, id )
end

local function iaafLink( id )
	--P1146's format regex: [0-9][0-9]* (e.g. 012)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[https://www.iaaf.org/athletes/_/%s %s]', id, id )
end

local function iciaLink( id )
	--P1736's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[https://www.imj.org.il/artcenter/newsite/en/?artist=%s %s]', id, id )
end

local function jocondeLink( id )
	--P347's format regex: [\-0-9A-Za-z]{11} (e.g. 12345678901)
	local regex = '^'..string.rep('[%-0-9A-Za-z]', 11)..'$'
	if not id:match( regex ) then
		return false
	end
	return string.format( '[https://www.pop.culture.gouv.fr/notice/joconde/%s %s]', id, id )
end

local function kulturnavLink( id )
	--P1248's format regex: [0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[http://kulturnav.org/%s %s]', id, id )
end

local function lirLink( id )
	--P886's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[http://www.e-lir.ch/e-LIR___Lexicon.%s %s]', id, id ) --no https as of 9/2019
end

local function lnbLink( id )
	--P1368's format regex: \d{9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://kopkatalogs.lv/F?func=direct&local_base=lnc10&doc_number=%s %s]', id, id )
end

local function leonoreLink( id )
	--P640's format regex: LH/\d{1,4}/\d{1,3}|19800035/\d{1,4}/\d{1,5}(Bis)?|C/0/\d{1,2} (e.g. LH/2064/18)
	if not id:match( '^LH/%d%d?%d?%d?/%d%d?%d?$' ) and             --IDs from       LH/1/1 to         LH/2794/54 (legionaries)
	   not id:match( '^19800035/%d%d?%d?%d?/%d%d?%d?%d?%d?$' ) and --IDs from 19800035/1/1 to 19800035/385/51670 (legionnaires who died 1954-1977 & some who died < 1954)
	   not id:match( '^C/0/%d%d?$' ) then                          --IDs from        C/0/1 to             C/0/84 (84 famous legionaries)
		return false
	end
	return string.format( '[http://www.culture.gouv.fr/public/mistral/leonore_fr?ACTION=CHERCHER&FIELD_1=COTE&VALUE_1=%s %s]', id, id ) --no https as of 9/2019
end

local function mbaLink( id )
	--P434's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/artist/%s %s]', id, id ) --special category name
end

local function mbareaLink( id )
	--P982's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/area/%s %s]', id, id ) --special category name
end

local function mbiLink( id )
	--P1330's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/instrument/%s %s]', id, id ) --special category name
end

local function mblLink( id )
	--P966's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/label/%s %s]', id, id ) --special category name
end

local function mbpLink( id )
	--P1004's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/place/%s %s]', id, id ) --special category name
end

local function mbrgLink( id )
	--P436's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/release-group/%s %s]', id, id ) --special category name
end

local function mbsLink( id )
	--P1407's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/series/%s %s]', id, id ) --special category name
end

local function mbwLink( id )
	--P435's format regex: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} (e.g. 12345678-1234-1234-1234-1234567890AB)
	if not id:match( '^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$' ) then
		return false
	end
	return string.format( '[https://musicbrainz.org/work/%s %s]', id, id ) --special category name
end

local function mgpLink( id )
	--P549's format regex: \d{1,6} (e.g. 123456)
	if not id:match( '^%d%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://genealogy.math.ndsu.nodak.edu/id.php?id=%s %s]', id, id )
end

local function naraLink( id )
	--P1225's format regex: ^([1-9]\d{0,8})$ (e.g. 123456789)
	if not id:match( '^[1-9]%d?%d?%d?%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://catalog.archives.gov/id/%s %s]', id, id )
end

local function nclLink( id )
	--P1048's format regex: \d+ (e.g. 1081436)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[http://aleweb.ncl.edu.tw/F/?func=accref&acc_sequence=%s %s]', id, id ) --no https as of 9/2019
end

local function ndlLink( id )
	--P349's format regex: 0?\d{8} (e.g. 012345678)
	if not id:match( '^0?%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://id.ndl.go.jp/auth/ndlna/%s %s]', id, id ) 
end

local function ngvLink( id )
	--P2041's format regex: \d+ (e.g. 12354)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[https://www.ngv.vic.gov.au/explore/collection/artist/%s %s]', id, id )
end

local function nkcLink( id )
	--P691's format regex: [a-z]{2,4}[0-9]{2,14} (e.g. abcd12345678901234)
	if not id:match( '^[a-z][a-z][a-z]?[a-z]?%d%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://aleph.nkp.cz/F/?func=find-c&local_base=aut&ccl_term=ica=%s %s]', id, id )
end

local function nlaLink( id )
	--P409's format regex: [1-9][0-9]{0,11} (e.g. 123456789012)
	if not id:match( '^[1-9]%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://nla.gov.au/anbd.aut-an%s %s]', id, id )
end

local function nlgLink( id )
	--P3348's format regex: [1-9]\d* (e.g. 1)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[http://data.nlg.gr/resource/authority/record%s %s]', id, id )
end

local function nliLink( id )
	--P949's format regex: \d{9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[http://uli.nli.org.il/F/?func=direct&doc_number=%s %s]', id, id )
end

local function nlkLink( id )
	--P5034's format regex: KA.(19|20).{7} (e.g. KAC201501465)
	if not id:match( '^KA.19.......$' ) and
	   not id:match( '^KA.20.......$' ) then
		return false
	end
	return string.format( '[https://nl.go.kr/authorities/resource/%s %s]', id, id )
end

local function nlpLink( id )
	--P1695's format regex: 9810[0-9]\d* or A[0-9]{7}[0-9X] (e.g. 9810123456789012345 or A10414836)
	if not id:match( '^9810%d+$' ) and
	   not id:match( '^A%d%d%d%d%d%d%d[%dX]$' ) then
		return false
	end
	return string.format( '[https://tools.wmflabs.org/wikidata-externalid-url?p=1695&id=%s %s]', id, id )
end

local function nlrLink( id )
	--P1003's format regex: \d{9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[http://alephnew.bibnat.ro:8991/F?func=find-b&request=%s %s]', id, id )
end

local function nskLink( id )
	--P1375's format regex: \d{9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[http://katalog.nsk.hr/F/?func=direct&doc_number=%s %s]', id, id ) --no https as of 9/2019
end

local function ntaLink( id )
	--P1006's format regex: \d{8}[\dX] (e.g. 12345678X)
	if not id:match( '^%d%d%d%d%d%d%d%d[%dX]$' ) then
		return false
	end
	return string.format( '[http://data.bibliotheken.nl/id/thes/p%s %s]', id, id )
end

local function picLink( id )
	--P2750's format regex: [1-9]\d* (e.g. 1)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[https://pic.nypl.org/constituents/%s %s]', id, id )
end

local function ridLink( id )
	--P1053's format regex: [A-Z]-\d{4}-(19|20)\d\d (e.g. A-1234-1934)
	if not id:match( '^[A-Z]%-%d%d%d%d%-19%d%d$' ) and
	   not id:match( '^[A-Z]%-%d%d%d%d%-20%d%d$' ) then
		return false
	end
	return string.format( '[https://www.researcherid.com/rid/%s %s]', id, id )
end

local function reroLink( id )
	--P3065's format regex: 0[1-2]-[A-Z0-9]{1,10} (e.g. 02-A012345678)
	if not id:match( '^0[1-2]%-[A-Z%d][A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?[A-Z%d]?$' ) then
		return false
	end
	return string.format( '[http://data.rero.ch/%s %s]', id, id )
end

local function rkdartistsLink( id )
	--P650's format regex: [1-9]\d{0,5} (e.g. 123456)
	if not id:match( '^[1-9]%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://rkd.nl/en/explore/artists/%s %s]', id, id )
end

local function rkdidLink( id )
	--P350's format regex: [1-9]\d{0,5} (e.g. 123456)
	if not id:match( '^[1-9]%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[https://rkd.nl/en/explore/artists/%s %s]', id, id )
end

local function rslLink( id )
	--P947's format regex: \d{1,9} (e.g. 123456789)
	if not id:match( '^%d%d?%d?%d?%d?%d?%d?%d?%d?$' ) then
		return false
	end
	return string.format( '[http://aleph.rsl.ru/F?func=find-b&find_code=SYS&adjacent=Y&local_base=RSL11&request=%s %s]', id, id ) --no https as of 9/2019
end

local function selibrLink( id )
	--P906's format regex: [1-9]\d{4,5} (e.g. 123456)
	if not id:match( '^[1-9]%d%d%d%d%d?$' ) then
		return false
	end
	return string.format( '[https://libris.kb.se/auth/%s %s]', id, id )
end

local function sikartLink( id )
	--P781's format regex: \d{7,9} (e.g. 123456789)
	if not id:match( '^%d%d%d%d%d%d%d%d?%d?$' ) then
		return false
	end
	return string.format( '[http://www.sikart.ch/KuenstlerInnen.aspx?id=%s %s]', id, id ) --no https as of 9/2019
end

local function snacLink( id )
	--P3430's format regex: \d*[A-Za-z][0-9A-Za-z]* (e.g. A)
	if not id:match( '^%d*[A-Za-z][0-9A-Za-z]*$' ) then
		return false
	end
	return string.format( '[https://snaccooperative.org/ark:/99166/%s %s]', id, id )
end

local function sudocLink( id )
	--P269's format regex: (\d{8}[\dX]|) (e.g. 026927608)
	if not id:match( '^%d%d%d%d%d%d%d%d[%dxX]$' ) then --legacy: allow lowercase 'x'
		return false
	end
	return string.format( '[https://www.idref.fr/%s %s]', id, id )
end

local function s2authoridLink( id )
	--P4012's format regex: [1-9]\d* (e.g. 1796130)
	if not id:match( '^[1-9]%d*$' ) then
		return false
	end
	return string.format( '[https://www.semanticscholar.org/author/%s %s]', id, id ) --special category name
end

local function ta98Link( id )
	--P1323's format regex: A\d{2}\.\d\.\d{2}\.\d{3}[FM]? (e.g. A12.3.45.678)
	if not id:match( '^A%d%d%.%d%.%d%d%.%d%d%d[FM]?$' ) then
		return false
	end
	return string.format( '[http://tools.wmflabs.org/wikidata-externalid-url/?p=1323&url_prefix=https:%2F%2Fwww.unifr.ch%2Fifaa%2FPublic%2FEntryPage%2FTA98%20Tree%2FEntity%20TA98%20EN%2F&url_suffix=%20Entity%20TA98%20EN.htm&id=%s %s]', id, id )
end

local function tdviaLink( id )
	--P7314's format regex: [a-z/-]+] (e.g. barkan-omer-lutfi)
	if not id:match( '^[a-z/-]+$' ) then
		return false
	end
	return string.format( '[https://islamansiklopedisi.org.tr/%s %s]', id, id )
end

local function teLink( id )
	--P1693's format regex: E[1-8]\.\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1}\.\d{1}\.\d{1,3} (e.g. E1.23.45.67.8.9.0)
	local e1, e2 = id:match( '^E([1-8])%.(%d%d?)%.%d%d?%.%d%d?%.%d%.%d%.%d%d?%d?$' )
	if not e1 then
		return false
	end
	local TEnum = 'TEe0'..e1 --no formatter URL in WD, probably due to this complexity
	if e1 == '5' or e1 == '7' then
		if #e2 == 1 then e2 = '0'..e2 end
		TEnum = TEnum..e2
	end
	return string.format( '[http://www.unifr.ch/ifaa/Public/EntryPage/ViewTE/%s %s]', id, id )
end

local function tepapaLink( id )
	--P3544's format regex: \d+ (e.g. 1)
	if not id:match( '^%d+$' ) then
		return false
	end
	return string.format( '[https://collections.tepapa.govt.nz/agent/%s %s]', id, id )
end

local function thLink( id )
	--P1694's format regex: H\d\.\d{2}\.\d{2}\.\d\.\d{5} (e.g. H1.23.45.6.78901)
	local h1, h2 = id:match( '^H(%d)%.(%d%d)%.%d%d%.%d%.%d%d%d%d%d$' )
	if not h1 then
		return false
	end
	local THnum = 'THh'..h1..h2 --no formatter URL in WD, probably due to this complexity
	return string.format( '[http://www.unifr.ch/ifaa/Public/EntryPage/ViewTH/'..THnum..'.html %s %s]', id, id )
end

local function tlsLink( id )
	local id2 = id:gsub(' +', '_')
	--P1362's format regex: \p{Lu}[\p{L}\d_',\.\-\(\)\*/–]{3,59} (e.g. Abcd)
	local class = "[%a%d_',%.%-%(%)%*/–]"
	local regex = "^%u"..string.rep(class, 3)..string.rep(class.."?", 56).."$"
	if not mw.ustring.match( id2, regex ) then
		return false
	end
	return string.format( '[http://tls.theaterwissenschaft.ch/wiki/%s %s]', id, id ) --no https as of 9/2019
end

local function troveLink( id )
	--P1315's format regex: [1-9]\d{5,7} (e.g. 12345678)
	if not id:match( '^[1-9]%d%d%d%d%d%d?%d?$' ) then
		return false
	end
	return string.format( '[https://trove.nla.gov.au/people/%s %s]', id, id )
end

local function ulanLink( id )
	--P245's format regex: 500\d{6} (e.g. 500123456)
	if not id:match( '^500%d%d%d%d%d%d$' ) then
		return false
	end
	return string.format( '[https://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=%s %s]', id, id )
end

local function uscongressLink( id )
	--P1157's format regex: [A-Z]00[01]\d{3} (e.g. A000123)
	if not id:match( '^[A-Z]00[01]%d%d%d$' ) then
		return false
	end
	return string.format( '[http://bioguide.congress.gov/scripts/biodisplay.pl?index=%s %s]', id, id ) --no https as of 9/2019
end

-- In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
	{ 'VIAF', '[[Virtual International Authority File|VIAF]]', 214, viafLink, limbas = { 'en' } },
	{ 'ISNI', '[[International Standard Name Identifier|ISNI]]', 213, isniLink, limbas = { 'en' } },
	{ 'AAG', '[[Auckland Art Gallery Toi o Tāmaki|AAG]]', 3372, aagLink },
	{ 'ACM-DL', '[[ACM Digital Library|ACM DL]]', 864, acmLink },
	{ 'ADB', '[[Australian Dictionary of Biography|ADB]]', 1907, adbLink },
	{ 'AGSA', '[[Art Gallery of South Australia|AGSA]]', 6804, agsaLink },
	{ 'autores.uy', '[[autores.uy]]', 2558, autoresuyLink },
	{ 'AWR', '[[Australian Women\'s Register|AWR]]', 4186, awrLink },
	{ 'BALaT', '[[Royal Institute for Cultural Heritage#Online artworks pages|BALaT]]', 3293, balatLink },
	{ 'BIBSYS', '[[Bibsys|BIBSYS]]', 1015, bibsysLink, limbas = { 'en' } },
	{ 'Bildindex', '[[Marburg Picture Index|Bildindex]]', 2092, bildLink },
	{ 'BNC', '[[Biblioteca Nacional de Chile|BNC]]', 1890, bncLink },
	{ 'BNE', '[[Biblioteca natzionale de s\'Ispagna|BNE]]', 950, bneLink, limbas = { 'es' } },
	{ 'BNF', '[[Bibliothèque nationale de France|BNF]]', 268, bnfLink, limbas = { 'fr' } },
	{ 'Botanist', '[[Author citation (botany)|Botanist]]', 428, botanistLink, limbas = { 'en' } },
	{ 'BPN', '[[Biografisch Portaal|BPN]]', 651, bpnLink },
	{ 'BAV', '[[Biblioteca apostòlica vaticana|BAV]]', 1017, bavLink, limbas = { 'it' } },
	{ 'CANTIC', '[[Catàlogos de autoridade de nùmenes e tìtulos de Catalugna|CANTIC]]', 1273, canticLink, limbas = { 'ca' } },
	{ 'CERL', '[[Consortium of European Research Libraries|CERL]]', 1871, cerlLink, limbas = { 'it' } },
	{ 'CINII', '[[CiNii]]', 271, ciniiLink, limbas = { 'ja', 'en' } },
	{ 'DAAO', '[[Dictionary of Australian Artists|DAAO]]', 1707, daaoLink, limbas = { 'en' } },
	{ 'DBLP', '[[DBLP]]', 2456, dblpLink },
	{ 'DSI', '[[Stuttgart Database of Scientific Illustrators 1450–1950|DSI]]', 2349, dsiLink, limbas = { 'en' } },
	{ 'Europeana', '[[Europeana]]', 7704, europeanaLink },
	{ 'FNZA', '[[:d:Property:P6792|FNZA]]', 6792, fnzaLink },
	{ 'GND', '[[Gemeinsame Normdatei|GND]]', 227, gndLink, limbas = { 'de' } },
	{ 'HDS', '[[Ditzionàriu Istòricu de s\'Isvìtzera|HDS]]', 902, hdsLink, limbas = { 'de', 'fr', 'it' } },
	{ 'IAAF', '[[World Athletics]]', 1146, iaafLink },
	{ 'ICIA', '[[Information Center for Israeli Art|ICIA]]', 1736, iciaLink },
	{ 'Joconde', '[[Joconde]]' , 347, jocondeLink },
	{ 'KULTURNAV', '[[KulturNav]]', 1248, kulturnavLink },
	{ 'LCCN', '[[Library of Congress Control Number|LCCN]]', 244, lccnLink, limbas = { 'en' } },
	{ 'LIR', '[[Historical Dictionary of Switzerland#Lexicon_Istoric_Retic|LIR]]', 886, lirLink },
	{ 'LNB', '[[National Library of Latvia|LNB]]', 1368, lnbLink, limbas = { 'lv', 'en' } },
	{ 'Léonore', '[[Base Léonore|Léonore]]', 640, leonoreLink, limbas = { 'fr' } },
	{ 'MBA', '[[MusicBrainz]]', 434, mbaLink, category = 'MusicBrainz' }, --special category name
	{ 'MBAREA', '[[MusicBrainz]]', 982, mbareaLink, category = 'Area in MusicBrainz' }, --special category name
	{ 'MBI', '[[MusicBrainz]]', 1330, mbiLink, category = 'Istrumentu in MusicBrainz' }, --special category name
	{ 'MBL', '[[MusicBrainz]]', 966, mblLink, category = 'Eticheta in MusicBrainz' }, --special category name
	{ 'MBP', '[[MusicBrainz]]', 1004, mbpLink, category = 'Logu in MusicBrainz' }, --special category name
	{ 'MBRG', '[[MusicBrainz]] release group', 436, mbrgLink, category = 'Grupu de intregada in MusicBrainz' }, --special category name
	{ 'MBS', '[[MusicBrainz]]', 1407, mbsLink, category = 'Sèrie in MusicBrainz' }, --special category name
	{ 'MBW', '[[MusicBrainz]] work', 435, mbwLink, category = 'Traballu in MusicBrainz' }, --special category name
	{ 'MGP', '[[Mathematics Genealogy Project|MGP]]', 549, mgpLink },
	{ 'NARA', '[[National Archives and Records Administration|NARA]]', 1225, naraLink },
	{ 'NCL', '[[National Central Library|NCL]]', 1048, nclLink },
	{ 'NDL', '[[Biblioteca de sa Dieta natzionale de su Giapone|NDL]]', 349, ndlLink, limbas = { 'en', 'ja' } },
	{ 'NGV', '[[National Gallery of Victoria|NGV]]', 2041, ngvLink },
	{ 'NKC', '[[National Library of the Czech Republic|NKC]]', 691, nkcLink, limbas = { 'en', 'cs' } },
	{ 'NLA', '[[National Library of Australia|NLA]]', 409, nlaLink, limbas = { 'en' } },
	{ 'NLG', '[[National Library of Greece|NLG]]', 3348, nlgLink, limbas = { 'en' } },
	{ 'NLI', '[[National Library of Israel|NLI]]', 949, nliLink, limbas = { 'en', 'he' } },
	{ 'NLK', '[[National Library of Korea|NLK]]', 5034, nlkLink, limbas = { 'ko' } },
	{ 'NLP', '[[National Library of Poland|NLP]]', 1695, nlpLink, limbas = { 'pl' } },
	{ 'NLR', '[[National Library of Romania|NLR]]', 1003, nlrLink, limbas = { 'ro' } },
	{ 'NSK', '[[National and University Library in Zagreb|NSK]]', 1375, nskLink, limbas = { 'hr' } },
	{ 'NTA', '[[Royal Library of the Netherlands|NTA]]', 1006, ntaLink, limbas = { 'en' } },
	{ 'ORCID', '[[Open Researcher and Contributor ID|ORCID]]', 496, orcidLink, limbas = { 'en' } },
	{ 'PIC', '[[:d:Q23892012|PIC]]', 2750, picLink },
	{ 'RID', '[[ResearcherID]]', 1053, ridLink },
	{ 'RERO', '[[RERO (Library Network of Western Switzerland)|RERO]]', 3065, reroLink },
	{ 'RKDartists', '[[Netherlands Institute for Art History#Online artist pages|RKD]]', 650, rkdartistsLink, limbas = { 'en' } },
	{ 'RKDID', '[[:d:Q17299580|RKDimages ID]]', 350, rkdidLink },
	{ 'RSL', '[[Russian State Library|RSL]]', 947, rslLink, limbas = { 'en', 'ru' } },
	{ 'SBN', '[[Servìtziu bibliotecàriu natzionale italianu|SBN]]', 396, sbnLink, limbas = { 'it' } },
	{ 'SELIBR', '[[LIBRIS|SELIBR]]', 906, selibrLink, limbas = { 'se' } },
	{ 'SIKART', '[[SIKART]]', 781, sikartLink, limbas = { 'en' } },
	{ 'SNAC-ID', '[[SNAC]]', 3430, snacLink, limbas = { 'en' } },
	{ 'SUDOC', '[[Système universitaire de documentation|SUDOC]]', 269, sudocLink, limbas = { 'fr' } },
	{ 'S2AuthorId', '[[Semantic Scholar|S2AuthorId]]', 4012, s2authoridLink, category = 'Autore de Semantic Scholar' }, --special category name
	{ 'TA98', '[[Terminologia Anatomica|TA98]]', 1323, ta98Link },
	{ 'TDVİA', '[[Türkiye Diyanet Vakfı İslâm Ansiklopedisi|TDVİA]]', 7314, tdviaLink, limbas = { 'tr' } },
	{ 'TE', '[[Terminologia Embryologica|TE]]', 1693, teLink },
	{ 'TePapa', '[[Museum of New Zealand Te Papa Tongarewa|TePapa]]', 3544, tepapaLink, limbas = { 'en' } },
	{ 'TH', '[[Terminologia Histologica|TH]]', 1694, thLink },
	{ 'TLS', '[[Theaterlexikon der Schweiz|TLS]]', 1362, tlsLink },
	{ 'Trove', '[[Trove]]', 1315, troveLink, limbas = { 'en' } }, --formerly NLA-person
	{ 'ULAN', '[[Union List of Artist Names|ULAN]]', 245, ulanLink, limbas = { 'en' } },
	{ 'USCongress', '[[Biographical Directory of the United States Congress|US Congress]]', 1157, uscongressLink, limbas = { 'en' } },
}

local p = {}

-- Funzione per il template {{Controllo di autorità}}.
function p.authorityControl( frame )
	local elements, categories = {}, {}
	local ret = ''
	local styles = 'Module:Controllu de autoridade/styles.css'

	for _, params in pairs( conf ) do
		local val = mWikidata._getProperty({ 'P' .. params[3], n = 1 })
		if val then
			local el, cat
			local link, worldCatLink = params[4]( val )
			if link then
				local limbas = params['limbas'] and ( modlimbas.limbas( params['limbas'] ) .. '&nbsp;' ) or ''
				el = string.format( '%s <span class="uid">%s%s</span>', params[2], limbas, link )
				cat = string.format( '[[Categoria:Artìculos cun còdighe %s]]', params[1] )
				if worldCatLink then elements.worldCatLink = worldCatLink end
			else
				el = string.format( '<span class="error">Su valore %s de %s no est vàlidu.</span>', val, params[1] )
				cat = string.format( '[[Categoria:Artìculos cun còdighes de controllu de autoridade non vàlidos (%s)]]', params[1] )
			end
			table.insert( elements, el )
			table.insert( categories, cat )
		end
	end

	if elements.worldCatLink then
		table.insert( elements, '[[WorldCat|WorldCat Identities]] ' .. modlimbas.limbas({ 'en' }) .. '&nbsp;' .. elements.worldCatLink )
	end

	if #elements > 0 then
		ret = mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = {src = styles} }
		ret = ret .. string.format( '<table class="CdA"><tr><th>%s</th><td>%s</td></tr></table>',
							 '[[Agiudu:Controllu de autoridade|Controllu de autoridade]]',
							 table.concat( elements, '<span style="font-weight:bold;">&nbsp;·</span> ' ) )
		if mWikidata._instanceOf( { 'Q5' } ) then
			table.insert( categories, '[[Categoria:Artìculos biogràficos cun còdighes de controllu de autoridade]]' )
		else
			table.insert( categories, '[[Categoria:Artìculos non biogràficos cun còdighes de controllu de autoridade]]' )
		end
	else
		table.insert( categories, '[[Categoria:Artìculos cun su template Controllu de autoridade ma chene còdighes]]' )
	end

	return ret .. ( mw.title.getCurrentTitle().namespace == 0 and table.concat( categories ) or '' )
end

return p