মডিউল:কর্তৃপক্ষ নিয়ন্ত্রণ (নতুন)

এই মডিউলের জন্য মডিউল:কর্তৃপক্ষ নিয়ন্ত্রণ (নতুন)/নথি-এ নথিপত্র তৈরি করা হয়ে থাকতে পারে

--[[  
  __  __           _       _           _         _   _                _ _                           _             _ 
 |  \/  | ___   __| |_   _| | ___ _   / \  _   _| |_| |__   ___  _ __(_) |_ _   _    ___ ___  _ __ | |_ _ __ ___ | |
 | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __| '_ \ / _ \| '__| | __| | | |  / __/ _ \| '_ \| __| '__/ _ \| |
 | |  | | (_) | (_| | |_| | |  __/_ / ___ \ |_| | |_| | | | (_) | |  | | |_| |_| | | (_| (_) | | | | |_| | | (_) | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_/   \_\__,_|\__|_| |_|\___/|_|  |_|\__|\__, |  \___\___/|_| |_|\__|_|  \___/|_|
                                                                            |___/                                    
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 

]]

local properties = {

	P212 = { -- ISBN-13
		item = "Q33057", -- P1629: International Standard Book Number
		issuedBy = "", -- P2378:
		URL_format = "https://www.wikidata.org/wiki/Special:BookSources/$1",
	},

	P213 = { -- ISNI
		item = "Q423048", -- P1629: International Standard Name Identifier
		issuedBy = "", -- P2378:
		URL_format = "https://isni.oclc.org/xslt/DB=1.2/CMD?ACT=SRCH&IKT=8006&TRM=ISN%3A$1",
	},

	P214 = { -- VIAF ID
		item = "Q19832964", -- P1629: VIAF ID
		issuedBy = "Q190593", -- P2378: OCLC, Inc.
		URL_format = "https://viaf.org/viaf/$1/",
	},

	P227 = { -- GND ID
		item = "Q54506313", -- P1629: GND ID
		issuedBy = "Q27302", -- P2378: German National Library
		URL_format = "https://d-nb.info/gnd/$1",
	},

	P236 = { -- ISSN
		item = "Q131276", -- P1629: International Standard Serial Number
		issuedBy = "", -- P2378:
		URL_format = "https://issn.org/resource/issn/$1",
	},

	P243 = { -- OCLC control number
		item = "Q846596", -- P1629: WorldCat
		issuedBy = "", -- P2378:
		URL_format = "https://www.worldcat.org/oclc/$1",
	},

	P244 = { -- Library of Congress authority ID
		item = "Q13219454", -- P1629: Library of Congress Authorities, Library of Congress Name Authority File, Library of Congress Subject Headings, Library of Congress Linked Data Service and Library of Congress
		issuedBy = "Q131454", -- P2378: Library of Congress
		URL_format = "https://id.loc.gov/authorities/$1",
	},

	P245 = { -- Union List of Artist Names ID
		item = "Q2494649", -- P1629: Union List of Artist Names
		issuedBy = "Q11203476", -- P2378: Getty Research Institute
		URL_format = "https://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=$1",
	},

	P268 = { -- Bibliothèque nationale de France ID
		item = "Q19938912", -- P1629: BnF authorities, Bibliothèque nationale de France and general catalog of BnF
		issuedBy = "Q193563", -- P2378: Bibliothèque nationale de France
		URL_format = "https://catalogue.bnf.fr/ark:/12148/cb$1",
	},

	P269 = { -- IdRef ID
		item = "Q47757534", -- P1629: IdRef
		issuedBy = "Q2826570", -- P2378: Agence bibliographique de l'enseignement supérieur
		URL_format = "https://www.idref.fr/$1",
	},

	P270 = { -- CALIS ID
		item = "Q9384291", -- P1629: China Academic Library and Information System
		issuedBy = "", -- P2378:
		URL_format = "http://opac.calis.edu.cn/aopac/ajsp/detail.jsp?actionfrom=1&actl=CAL++$1",
	},

	P271 = { -- CiNii author ID (books)
		item = "Q10726338", -- P1629: CiNii
		issuedBy = "Q4346622", -- P2378: National Institute of Informatics
		URL_format = "https://ci.nii.ac.jp/author/$1",
	},

	P347 = { -- Joconde work ID
		item = "Q809825", -- P1629: Joconde
		issuedBy = "", -- P2378:
		URL_format = "https://www.pop.culture.gouv.fr/notice/joconde/$1",
	},

	P349 = { -- National Diet Library ID
		item = "Q2553334", -- P1629: Web NDL Authorities and National Diet Library
		issuedBy = "Q477675", -- P2378: National Diet Library
		URL_format = "https://id.ndl.go.jp/auth/ndlna/$1",
	},

	P359 = { -- Rijksmonument ID
		item = "Q916333", -- P1629: Rijksmonument
		issuedBy = "", -- P2378:
		URL_format = "https://monumentenregister.cultureelerfgoed.nl/monumenten/$1",
	},

	P396 = { -- SBN author ID
		item = "Q105086090", -- P1629: OPAC SBN
		issuedBy = "Q3803707", -- P2378: Istituto Centrale per il Catalogo Unico
		URL_format = "https://opac.sbn.it/opacsbn/opac/iccu/scheda_authority.jsp?bid=$1",
	},

	P402 = { -- OpenStreetMap relation ID
		item = "Q936", -- P1629: OpenStreetMap
		issuedBy = "", -- P2378:
		URL_format = "https://www.openstreetmap.org/relation/$1",
	},

	P409 = { -- Libraries Australia ID
		item = "Q106691619", -- P1629: Libraries Australia Authorities
		issuedBy = "Q623578", -- P2378: National Library of Australia
		URL_format = "https://librariesaustralia.nla.gov.au/search/display?dbid=auth&id=$1",
	},

	P428 = { -- botanist author abbreviation
		item = "Q922063", -- P1629: International Plant Names Index
		issuedBy = "", -- P2378:
		URL_format = "https://www.ipni.org/ipni/advAuthorSearch.do?find_abbreviation=$1",
	},

	P434 = { -- MusicBrainz artist ID
		item = "Q14005", -- P1629: MusicBrainz
		issuedBy = "Q14005", -- P2378: MusicBrainz
		URL_format = "https://musicbrainz.org/artist/$1",
	},

	P458 = { -- IMO ship number
		item = "Q1970938", -- P1629: IMO number
		issuedBy = "", -- P2378:
		URL_format = "https://www.marinetraffic.com/ais/details/ships/$1",
	},

	P496 = { -- ORCID iD
		item = "Q51044", -- P1629: ORCID iD
		issuedBy = "Q19861084", -- P2378: ORCID, Inc.
		URL_format = "https://orcid.org/$1",
	},

	P508 = { -- BNCF Thesaurus ID
		item = "Q16583225", -- P1629: Nuovo soggettario
		issuedBy = "Q460907", -- P2378: National Central Library of Florence
		URL_format = "https://thes.bncf.firenze.sbn.it/termine.php?id=$1",
	},

	P539 = { -- Museofile
		item = "Q13481868", -- P1629: Muséofile
		issuedBy = "Q384602", -- P2378: Ministry of Culture (France)
		URL_format = "https://pop.culture.gouv.fr/notice/museo/$1",
	},

	P549 = { -- Mathematics Genealogy Project ID
		item = "Q829984", -- P1629: Mathematics Genealogy Project
		issuedBy = "", -- P2378:
		URL_format = "https://mathgenealogy.org/id.php?id=$1",
	},

	P587 = { -- MMSI
		item = "Q1795701", -- P1629: Maritime Mobile Service Identity
		issuedBy = "", -- P2378:
		URL_format = "https://www.marinetraffic.com/ais/details/ships/$1",
	},

	P640 = { -- Léonore ID
		item = "Q2886420", -- P1629: Léonore database
		issuedBy = "Q384602", -- P2378: Ministry of Culture (France) and Archives nationales
		URL_format = "",
	},

	P648 = { -- Open Library ID
		item = "Q1201876", -- P1629: Open Library
		issuedBy = "Q1201876", -- P2378: Open Library
		URL_format = "https://openlibrary.org/works/$1",
	},

	P649 = { -- NRHP reference number
		item = "Q3719", -- P1629: National Register of Historic Places and National Register of Historic Places listed place
		issuedBy = "", -- P2378:
		URL_format = "https://npgallery.nps.gov/AssetDetail/NRIS/$1",
	},

	P650 = { -- RKDartists ID
		item = "Q17299517", -- P1629: RKDartists
		issuedBy = "Q758610", -- P2378: Netherlands Institute for Art History
		URL_format = "https://rkd.nl/explore/artists/$1",
	},

	P651 = { -- Biografisch Portaal van Nederland ID
		item = "Q1868372", -- P1629: Biografisch Portaal
		issuedBy = "", -- P2378:
		URL_format = "http://www.biografischportaal.nl/persoon/$1",
	},

	P691 = { -- NKCR AUT ID
		item = "Q13550863", -- P1629: Czech National Authority Database
		issuedBy = "Q1967876", -- P2378: National Library of the Czech Republic
		URL_format = "https://aleph.nkp.cz/F/?func=find-c&local_base=aut&ccl_term=ica=$1",
	},

	P723 = { -- Digitale Bibliotheek voor de Nederlandse Letteren author ID
		item = "Q2451336", -- P1629: Digital Library for Dutch Literature (DBNL)
		issuedBy = "Q2451336", -- P2378: Digital Library for Dutch Literature (DBNL)
		URL_format = "http://www.dbnl.org/auteurs/auteur.php?id=$1",
	},

	P757 = { -- World Heritage Site ID
		item = "Q9259", -- P1629: UNESCO World Heritage Site
		issuedBy = "", -- P2378:
		URL_format = "https://whc.unesco.org/en/list/$1",
	},

	P781 = { -- SIKART ID
		item = "Q683543", -- P1629: SIKART
		issuedBy = "", -- P2378:
		URL_format = "https://www.sikart.ch/KuenstlerInnen.aspx?id=$1&lng=en",
	},

	P808 = { -- Bien de Interés Cultural (BIC) code
		item = "Q19832983", -- P1629: Bien de Interés Cultural code and Bien de Interés Cultural
		issuedBy = "", -- P2378:
		URL_format = "https://heritage.toolforge.org/api/api.php?action=search&srcountry=es&format=html&srid=$1",
	},

	P886 = { -- Lexicon istoric retic ID
		item = "Q14566772", -- P1629: Lexicon istoric retic
		issuedBy = "", -- P2378:
		URL_format = "http://www.e-lir.ch/e-LIR___Lexicon.$1.450.0.html",
	},

	P902 = { -- HDS ID
		item = "Q642074", -- P1629: Historical Dictionary of Switzerland
		issuedBy = "", -- P2378:
		URL_format = "https://hls-dhs-dss.ch/de/articles/$1",
	},

	P906 = { -- SELIBR ID
		item = "Q1798125", -- P1629: LIBRIS
		issuedBy = "Q953058", -- P2378: National Library of Sweden
		URL_format = "https://libris.kb.se/auth/$1",
	},

	P947 = { -- RSL ID (person)
		item = "Q1048694", -- P1629: Russian State Library
		issuedBy = "Q1048694", -- P2378: Russian State Library
		URL_format = "http://aleph.rsl.ru/F?func=find-b&find_code=SYS&adjacent=Y&local_base=RSL11&request=$1&CON_LNG=ENG",
	},

	P950 = { -- Biblioteca Nacional de España ID
		item = "Q73504744", -- P1629: BNE authority file and datos.bne.es
		issuedBy = "Q750403", -- P2378: Biblioteca Nacional de España
		URL_format = "https://datos.bne.es/resource/$1",
	},

	P957 = { -- ISBN-10
		item = "Q33057", -- P1629: International Standard Book Number
		issuedBy = "", -- P2378:
		URL_format = "https://wikidata.org/wiki/Special:BookSources/$1",
	},

	P1003 = { -- National Library of Romania ID
		item = "Q622012", -- P1629: National Library of Romania
		issuedBy = "Q622012", -- P2378: National Library of Romania
		URL_format = "http://aleph.bibnat.ro/F/?func=direct&local_base=NLR10&doc_number=$1",
	},

	P1006 = { -- Nationale Thesaurus voor Auteurs ID
		item = "Q105488572", -- P1629: data.bibliotheken.nl and Nationale Thesaurus voor Auteurs
		issuedBy = "Q1526131", -- P2378: Koninklijke Bibliotheek
		URL_format = "https://data.bibliotheken.nl/doc/thes/p$1",
	},

	P1015 = { -- NORAF ID
		item = "Q16889143", -- P1629: Norwegian Authority File: Persons and Corporate Bodies
		issuedBy = "Q55097783", -- P2378: Unit and National Library of Norway
		URL_format = "https://authority.bibsys.no/authority/rest/authorities/html/$1",
	},

	P1048 = { -- NCL ID
		item = "Q618340", -- P1629: National Central Library
		issuedBy = "Q618340", -- P2378: National Central Library
		URL_format = "http://aleweb.ncl.edu.tw/F/?func=accref&acc_sequence=$1",
	},

	P1053 = { -- ResearcherID
		item = "Q7315186", -- P1629: ResearcherID
		issuedBy = "", -- P2378:
		URL_format = "https://www.researcherid.com/rid/$1",
	},

	P1157 = { -- US Congress Bio ID
		item = "Q20205343", -- P1629: US Congress Bio identifier and Biographical Directory of the United States Congress
		issuedBy = "Q11268", -- P2378: United States Congress
		URL_format = "https://bioguide.congress.gov/search/bio/$1",
	},

	P1216 = { -- National Heritage List for England number
		item = "Q6973052", -- P1629: National Heritage List for England
		issuedBy = "Q19604421", -- P2378: Historic England
		URL_format = "https://www.historicengland.org.uk/listing/the-list/list-entry/$1",
	},

	P1225 = { -- U.S. National Archives Identifier
		item = "Q29896325", -- P1629: National Archives Identifier and National Archives and Records Administration
		issuedBy = "Q518155", -- P2378: National Archives and Records Administration
		URL_format = "https://catalog.archives.gov/id/$1",
	},

	P1248 = { -- KulturNav-ID
		item = "Q16323066", -- P1629: KulturNav
		issuedBy = "Q16323066", -- P2378: KulturNav
		URL_format = "https://kulturnav.org/$1",
	},

	P1362 = { -- Theaterlexikon der Schweiz ID
		item = "Q2416247", -- P1629: Theaterlexikon der Schweiz
		issuedBy = "", -- P2378:
		URL_format = "http://tls.theaterwissenschaft.ch/wiki/$1",
	},

	P1415 = { -- Oxford Dictionary of National Biography ID
		item = "Q17565097", -- P1629: Oxford Dictionary of National Biography
		issuedBy = "Q34433", -- P2378: University of Oxford
		URL_format = "https://doi.org/10.1093/ref:odnb/$1",
	},

	P1440 = { -- FIDE player ID
		item = "Q27038151", -- P1629: ratings.fide.com
		issuedBy = "Q102178", -- P2378: FIDE
		URL_format = "https://ratings.fide.com/profile/$1",
	},

	P1483 = { -- kulturnoe-nasledie.ru ID
		item = "Q50339681", -- P1629: kulturnoe-nasledie.ru
		issuedBy = "", -- P2378:
		URL_format = "https://ru_monuments.toolforge.org/wikivoyage.php?id=$1",
	},

	P1600 = { -- Inventari del Patrimoni Arquitectònic de Catalunya code
		item = "Q1393661", -- P1629: Inventari del Patrimoni Arquitectònic de Catalunya
		issuedBy = "Q8022", -- P2378: Generalitat of Catalonia
		URL_format = "http://invarquit.cultura.gencat.cat/Cerca/Fitxa?index=0&consulta=&codi=$1",
	},

	P1665 = { -- Chessgames.com player ID
		item = "Q534035", -- P1629: chessgames.com
		issuedBy = "", -- P2378:
		URL_format = "https://www.chessgames.com/perl/chessplayer?pid=$1",
	},

	P1695 = { -- NLP ID (unique)
		item = "Q856423", -- P1629: National Library of Poland
		issuedBy = "", -- P2378:
		URL_format = "https://wikidata-externalid-url.toolforge.org/?p=1695&id=$1",
	},

	P1882 = { -- Web Gallery of Art ID
		item = "Q1464063", -- P1629: Web Gallery of Art
		issuedBy = "", -- P2378:
		URL_format = "https://mix-n-match.toolforge.org/api.php?query=redirect&catalog=11&ext_id=$1",
	},

	P2558 = { -- autores.uy ID
		item = "Q22975461", -- P1629: Autores.uy
		issuedBy = "", -- P2378:
		URL_format = "http://autores.uy/entidad/$1",
	},

	P2917 = { -- COAM structure ID
		item = "Q5777096", -- P1629: Official Architects' Association of Madrid
		issuedBy = "", -- P2378:
		URL_format = "https://guia-arquitectura-madrid.coam.org/#inm.$1",
	},

	P3083 = { -- SIMBAD ID
		item = "Q654724", -- P1629: SIMBAD
		issuedBy = "", -- P2378:
		URL_format = "https://simbad.u-strasbg.fr/simbad/sim-id?Ident=$1",
	},

	P3177 = { -- Patrimonio Web JCyL ID
		item = "", -- P1629:
		issuedBy = "", -- P2378:
		URL_format = "https://servicios.jcyl.es/pweb/datos.do?numero=$1&tipo=Inmuebles&ruta=",
	},

	P3178 = { -- Zaragoza monument ID
		item = "", -- P1629:
		issuedBy = "", -- P2378:
		URL_format = "https://www.zaragoza.es/ciudad/turismo/en/que-visitar/sitios-interes/detalle_Monumento?id=$1",
	},

	P3318 = { -- Guía Digital del Patrimonio Cultural de Andalucía identifier
		item = "Q5758805", -- P1629: Catálogo General del Patrimonio Histórico Andaluz, Andalusian Heritage Institute and Q97319067
		issuedBy = "Q5917182", -- P2378: Andalusian Heritage Institute
		URL_format = "http://www.iaph.es/patrimonio-inmueble-andalucia/resumen.do?id=$1",
	},

	P3580 = { -- SIPCA code
		item = "Q28836387", -- P1629: SIPCA
		issuedBy = "", -- P2378:
		URL_format = "http://www.sipca.es/censo/$1/.html",
	},

	P3758 = { -- DOCOMOMO Ibérico ID
		item = "", -- P1629:
		issuedBy = "Q28740267", -- P2378: Foundation DOCOMOMO Ibérico
		URL_format = "http://www.docomomoiberico.com/index.php?option=com_k2&view=item&id=$1&lang=en",
	},

	P4075 = { -- Czech Monument Catalogue Number
		item = "Q41593640", -- P1629: Památkový katalog
		issuedBy = "", -- P2378:
		URL_format = "https://pamatkovykatalog.cz/soupis/podle-relevance/1/seznam/?katCislo=$1",
	},

	P4157 = { -- MEG ID
		item = "Q1955752", -- P1629: Musée d'ethnographie de Genève
		issuedBy = "", -- P2378:
		URL_format = "https://www.ville-ge.ch/meg/musinfo_public.php?id=$1",
	},

	P4399 = { -- Itaú Cultural ID
		item = "Q41599984", -- P1629: Enciclopédia Itaú Cultural
		issuedBy = "Q41599984", -- P2378: Enciclopédia Itaú Cultural
		URL_format = "https://enciclopedia.itaucultural.org.br/$1",
	},

	P4360 = { -- Monumentos de São Paulo ID
		item = "", -- P1629:
		issuedBy = "", -- P2378:
		URL_format = "http://web.archive.org/web/2017/http://www.monumentos.art.br/monumento/$1",
	},

	P4372 = { -- iPatrimônio ID
		item = "Q64745161", -- P1629: iPatrimônio
		issuedBy = "", -- P2378:
		URL_format = "http://www.ipatrimonio.org/$1",
	},

	P4659 = { -- Musée d'Orsay artwork ID
		item = "Q23402", -- P1629: Musée d'Orsay
		issuedBy = "Q23402", -- P2378: Musée d'Orsay
		URL_format = "http://www.musee-orsay.fr/en/collections/index-of-works/notice.html?nnumid=$1",
	},

	P4721 = { -- MuBE Virtual ID
		item = "", -- P1629:
		issuedBy = "", -- P2378:
		URL_format = "http://mubevirtual.com.br/pt_br?Dados&area=ver&id=$1",
	},

	P4868 = { -- Hispania Nostra Red List code
		item = "Q69873044", -- P1629: Red List of Endangered Heritage item and geographic location
		issuedBy = "", -- P2378:
		URL_format = "http://listarojapatrimonio.org/ficha/$1/",
	},

	P5034 = { -- National Library of Korea ID
		item = "Q56640487", -- P1629: Korean Authority File
		issuedBy = "Q495005", -- P2378: National Library of Korea
		URL_format = "https://nl.go.kr/authorities/resource/$1",
	},

	P8189 = { -- National Library of Israel J9U ID
		item = "Q106509962", -- P1629: National Library of Israel Names and Subjects Authority File
		issuedBy = "Q188915", -- P2378: National Library of Israel
		URL_format = "http://uli.nli.org.il/F/?func=find-b&local_base=NLX10&find_code=UID&request=$1",
	},

	P9394 = { -- Louvre Museum ARK ID
		item = "Q19675", -- P1629: Louvre Museum
		issuedBy = "Q19675", -- P2378: Louvre Museum
		URL_format = "https://collections.louvre.fr/ark:/53355/cl$1",
	},

}

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 '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|%s]]", id_name_URL, label) -- link to wikipedia
	else    
		return string.format("[[d:%s|%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("<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

-- ==================================================
-- 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 "<span style=\"color:red\">[last digit should be " .. lastDigit .. "]</span>"
	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

--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]{1,3}-\d{4}-(19|20)\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='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='^%d%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='J9U'         , property='P8189', lang='he', regexp='' },  --National Library of Israel J9U 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='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='Louvre'      , property='P9394', lang='', regexp='' }, -- Louvre ID
	
	{label='OCLC'        , property='P243',  lang='', regexp='' }, -- OCLC
	{label='ISBN-13'     , property='P212',  lang='', regexp='' }, -- ISBN-13
	{label='ISBN-10'     , property='P957',  lang='', regexp='' }, -- ISBN-10
	{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
	{label='NLK'            , property='P5034', 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 = "[[w:en:Help:Authority control|Authority control]]" -- hardwire the default
	if lang~='en' then
		field_name = core.getLabel("Q36524", lang)
	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 {{Authority control|...|bare=1 }} 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 = '[[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

	--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://quickstatements.toolforge.org/#/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

			-- combine them all
			local lineStr = string.format("\n*%s:&thinsp;<span class=\"uid\">%s</span>%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*[[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

	-- return results
	if results~='' then -- if there are any results than wrap them in <div> tag
		results  = string.format('<div class="hlist">%s\n</div>', 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 wikidata
	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.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
	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('<tr><td class="type fileinfo-paramfield">%s</td><td>\n%s\n</td></tr>', 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('<table %s>\n%s\n</table>\n', style, results)
	else
		results = string.format('\n%s\n', results)
	end
	return results..cats
end

return p