Documentation for this module may be created at Module:Pages with authority control identifiers/doc

require('Module:No globals')

local p = {}
local ac_conf = require('Module:Authority control').conf
local rmCats = require('Module:Suppress categories').main
local currentTitle = mw.title.getCurrentTitle()
local title = currentTitle.text

--[[==========================================================================]]
--[[                         Local Utility Functions                          ]]
--[[==========================================================================]]

local function whichTOC( frame )
	-- standardize TOC behavior via {{CatAutoTOC}}
	return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
end

local function redCatCheck( cat ) --cat == 'Blah' (not 'Category:Blah', '[[Category:Blah]]', etc.)
	if cat and cat ~= '' and mw.title.new(cat, 14).exists == false then
		return '[[Category:Pages with authority control identifiers red-linked category]]'
	end
	return ''
end

local function addCat( cat, id )
	if id and id ~= '' then
		return '[[Category:'..cat..'|'..id..']]'..redCatCheck(cat)
	else
		return '[[Category:'..cat..']]'..redCatCheck(cat)
	end
end

local function getExample(conf, id)
	if id == 'WORLDCATID' then
		return '[https://www.worldcat.org/identities/lccn-n78039510 WorldCat]'
	else
		return rmCats(conf[3](conf[5]))
	end
end

--[[==========================================================================]]
--[[                    Local Category-Specific Functions                     ]]
--[[==========================================================================]]

--For indirect use on [[Category:<catType> with multiple identifiers]],
--   i.e. on [[Category:Articles with multiple identifiers]]
local function multiple( frame, catType, pre, post )
	local txCatMore  = frame:expandTemplate{ title = 'Cat more', args = { 'Wikipedia:Authority control' } }
	local txEmptyCat = frame:expandTemplate{ title = 'Possibly empty category' }
	local txWPCat    = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
	local txQ        = frame:expandTemplate{ title = 'Q', args = { 'Q7245' } }
	local txWdpl1    = frame:expandTemplate{ title = 'Wikidata property link', args = { 'P213' } }
	local txWdpl2    = frame:expandTemplate{ title = 'Wikidata property link', args = { 'P244' } }
	local txTOC      = whichTOC( frame )
	local ex = 'For example, '..txQ..' contains multiple IDs for '..txWdpl1..', '..txWdpl2..', etc.<br/>'
	local only = 'Pages in this category should only be added by [[Module:Authority control]].'
	if catType and catType == 'pages' then
		txEmptyCat = ''
		txWPCat = frame:expandTemplate{ title = 'Wikipedia category' }
		ex = ''
		only = ''
	end
	local outString = txCatMore..txEmptyCat..txWPCat..txTOC..'\n'..ex..only
	return (pre or '')..outString..(post or '')
end

--For use in [[Category:Pages with authority control information]],
--   i.e. on [[Category:Pages with VIAF identifiers]]
local function pages( frame, id )
	if id == 'multiple' then
		local pagesCat = 'Pages with authority control information'
		local pagesCatFull = addCat(pagesCat)
		return multiple( frame, 'pages', '', pagesCatFull )
	end
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf[1] == id then
			local linktarget = conf.link or conf[1]..' (identifier)'
			local wdpl      = ':d:Property:P'..conf[2]
			local txCatMore = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', linktarget, wdpl} }
			local txWPCat   = frame:expandTemplate{ title = 'Wikipedia category' }
			local pagesCat  = 'Pages with authority control information'
			local outString = txCatMore..txWPCat..'\n'..addCat(pagesCat, id)
			return outString
		end
	end
	return ''
end

--For use in [[Category:Miscellaneous pages with authority control information]],
--   i.e. on [[Category:Miscellaneous pages with VIAF identifiers]]
local function misc( frame, id )
	if id == 'multiple' then
		local link = id
		local txCatExplain = frame:expandTemplate{ title = 'Category explanation', 
				args = { 'pages, other than main user pages or Wikipedia articles, using {{[[Template:Authority control|Authority control]]}} with '..link..' identifiers.' } }
		local idCat   = 'Pages with '..id..' identifiers'
		local miscCat = 'Miscellaneous pages with authority control information'
		local idCatFull   = addCat(idCat)
		local miscCatFull = addCat(miscCat, id)
		return multiple( frame, 'misc', txCatExplain, idCatFull..miscCatFull )
	end
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf[1] == id then
			local linktarget = conf.link or conf[1]..' (identifier)'
			local link    = '[['..linktarget..'|'..conf[1]..']]'
			local example = 'The '..conf[1]..' identifier appears as '..getExample(conf,id)..' in the '..conf[4]..' section.'
			local wdpl    = ':d:Property:P'..conf[2]
			local txCatExplain = frame:expandTemplate{ title = 'Category explanation', 
					args = { 'pages, other than main user pages or Wikipedia articles, using {{[[Template:Authority control|Authority control]]}} with '..link..' identifiers. '..example } }
			local txCatMore  = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', wdpl} }
			local txEmptyCat = frame:expandTemplate{ title = 'Possibly empty category' }
			local txWPCat    = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
			local txTOC = whichTOC( frame )
			local idCat = 'Pages with '..id..' identifiers'
			local miscCat = 'Miscellaneous pages with authority control information'
			local outString = txCatExplain..txCatMore..txEmptyCat..txWPCat..txTOC..'\n'..
							'Pages in this category should only be added by [[Module:Authority control]].'..
							addCat(idCat)..addCat(miscCat, id)
			return outString
		end
	end
	return ''
end

--For use in [[Category:User pages with authority control information]],
--   i.e. on [[Category:User pages with VIAF identifiers]]
local function user( frame, id )
	if id == 'multiple' then
		local idCat   = 'Pages with '..id..' identifiers'
		local userCat = 'User pages with authority control information'
		local idCatFull   = addCat(idCat)
		local userCatFull = addCat(userCat, id)
		return multiple( frame, 'user', '', idCatFull..userCatFull )
	end
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf[1] == id then
			local linktarget = conf.link or conf[1]..' (identifier)'
			local wdpl       = ':d:Property:P'..conf[2]
			local txCatMore  = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', linktarget, wdpl} }
			local txEmptyCat = frame:expandTemplate{ title = 'Possibly empty category' }
			local txWPCat    = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
			local txTOC      = whichTOC( frame )
			local idCat   = 'Pages with '..id..' identifiers'
			local userCat = 'User pages with authority control information'
			local outString = txCatMore..txEmptyCat..txWPCat..txTOC..'\n'..
							'Pages in this category should only be added by [[Module:Authority control]].'..
							addCat(idCat)..addCat(userCat, id)
			return outString
		end
	end
	return ''
end

--For use in [[Category:Articles with authority control information]],
--   i.e. on [[Category:Articles with VIAF identifiers]]
local function wp( frame, id )
	if id == 'multiple' then
		local link = id
		local txCatExplain = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..link..' identifiers. Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].'} }
		local idCat = 'Pages with '..id..' identifiers'
		local wpCat = 'Articles with authority control information'
		local idCatFull = addCat(idCat)
		local wpCatFull = addCat(wpCat, id)
		return multiple( frame, 'wp', txCatExplain, idCatFull..wpCatFull )
	end
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf[1] == id then
			local linktarget = conf.link or conf[1]..' (identifier)'
			local link    = '[['..linktarget..'|'..conf[1]..']]'
			local wdpl    = ':d:Property:P'..conf[2]
			local example = 'The '..conf[1]..' identifier appears as '..getExample(conf, id)..' in the '..conf[4]..' section.'
			local txCatExplain = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..link..' identifiers.'..example..' Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].'} }
			local txCatMore    = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', wdpl} }
			local txEmptyCat   = frame:expandTemplate{ title = 'Possibly empty category' }
			local txWPCat      = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
			local txTOC = whichTOC( frame )
			local idCat = 'Pages with '..id..' identifiers'
			local wpCat = 'Articles with authority control information'
			local outString = txCatExplain..txCatMore..txEmptyCat..txWPCat..txTOC..'\n'..
							'Pages in this category should only be added by [[Module:Authority control]].'..
							addCat(idCat)..addCat(wpCat, id)
			return outString
		end
	end
	return ''
end

--For use in [[Category:Articles with faulty authority control information]],
--   i.e. on [[Category:Articles with faulty VIAF identifiers]]
local function wpfaulty( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf[1] == id then
			local linktarget = conf.link or conf[1]..' (identifier)'
			local wdpl       = ':d:Property:P'..conf[2]
			local txCatMore  = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', linktarget, wdpl} }
			local txEmptyCat = frame:expandTemplate{ title = 'Possibly empty category' }
			local txWPCat    = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
			local txDirtyCat = frame:expandTemplate{ title = 'Polluted category' }
			local txTOC = whichTOC( frame )
			local idCat = 'Pages with '..id..' identifiers'
			local wpfCat = 'Articles with faulty authority control information'
			local outString = txCatMore..txEmptyCat..txWPCat..txDirtyCat..txTOC..'\n'..
							'Pages in this category should only be added by [[Module:Authority control]].'..
							addCat(idCat)..addCat(wpfCat, id)
			return outString
		end
	end
	return ''
end

--[[==========================================================================]]
--[[                            Main/External Call                            ]]
--[[==========================================================================]]
function p.autoDetect( frame )
	if currentTitle.namespace == 14 then --cat space
		local pagesID    = mw.ustring.match(title, 'Pages with ([%w%.%- ]+) identifiers')
		local miscID     = mw.ustring.match(title, 'Miscellaneous pages with ([%w%.%- ]+) identifiers')
		local userID     = mw.ustring.match(title, 'User pages with ([%w%.%- ]+) identifiers')
		local wpfaultyID = mw.ustring.match(title, 'Articles with faulty ([%w%.%- ]+) identifiers')
		local wpID       = mw.ustring.match(title, 'Articles with ([%w%.%- ]+) identifiers')
		
		if     pagesID    then return pages( frame, pagesID )
		elseif miscID     then return misc( frame, miscID )
		elseif userID     then return user( frame, userID )
		elseif wpfaultyID then return wpfaulty( frame, wpfaultyID ) --must be before wpID check, in case they both match
		elseif wpID       then return wp( frame, wpID )             --to keep the regex simple
		else   return '[[Category:Pages with authority control identifiers unknown category]]'
		end
	end
	return ''
end

return p