Mô đun:Pages with authority control identifiers

require('strict')
local p = {}
local ac_conf = require('Module:Authority control').conf
local currentTitle = mw.title.getCurrentTitle()
local title = currentTitle.text
local isCat = (currentTitle.namespace == 14)
--======================================================================**
--                         Local Utility Functions                          
--======================================================================**
local function whichTOC( frame )
	--local pageCount = mw.site.stats.pagesInCategory(title, 'pages')
	--if pageCount > 1200 then
	--	return frame:expandTemplate{ title = 'Large category TOC' }
	--elseif pageCount > 100 then
	--	return frame:expandTemplate{ title = 'Category TOC', args = { align = 'center' } }
	--end
	--return 
	
	-- standardize TOC behavior via 
	return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
end
local function redCatCheck( catName ) --catName == 'Blah', not 'Category:Blah', not 
	if catName and catName ~=  and mw.title.new(catName, 14).exists == false then
		return 
	end
	return 
end
--======================================================================**
--                    Local Category-Specific Functions                     
--======================================================================**
--For use in ,
--   i.e. on 
local function pages( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf1 == id then
			local link = conf2 --not used locally yet
			local txCatMore = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control'} }
			local txWPCat   = frame:expandTemplate{ title = 'Wikipedia category' }
			local pagesCat = 'Pages with authority control information'
			local outString = txCatMore..txWPCat..'\n'..
					..redCatCheck(pagesCat)
			return outString
		end
	end
	return 
end
--For use in ,
--   i.e. on 
local function misc( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf1 == id then
			local link = conf2
			local txCatExplain = frame:expandTemplate{ title = 'Category explanation', 
					args = { 'pages, other than main user pages or Wikipedia articles, using Authority control with '..link..' identifiers.' } }
			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 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.'..
					..redCatCheck(idCat)..
					..redCatCheck(miscCat)
			return outString
		end
	end
	return 
end
--For use in ,
--   i.e. on 
local function user( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf1 == id then
			local link = conf2 --not used locally yet
			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 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.'..
					..redCatCheck(idCat)..
					..redCatCheck(userCat)
			return outString
		end
	end
	return 
end
--For use in ,
--   i.e. on 
local function wp( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf1 == id then
			local link = conf2
			local txCatExplain = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..link..' identifiers. Please do not add subcategories.'} }
			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 txTOC = whichTOC( frame )
			local idCat = 'Pages with '..id..' identifiers'
			local wpCat = 'Wikipedia 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.'..
					..redCatCheck(idCat)..
					..redCatCheck(wpCat)
			return outString
		end
	end
	return 
end
--For use in ,
--   i.e. on 
local function wpfaulty( frame, id )
	for _, conf in pairs( ac_conf ) do
		if conf.category == id or conf1 == id then
			local link = conf2 --not used locally yet
			local param = conf3
			local txCatMore  = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', 'd:Property:P'..param} }
			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 = 'Wikipedia 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.'..
					..redCatCheck(idCat)..
					..redCatCheck(wpfCat)
			return outString
		end
	end
	return 
end
--======================================================================**
--                            Main/External Call                            
--======================================================================**
function p.autoDetect( frame )
	if isCat then
		local pagesID    = mw.ustring.match(title, 'Trang chứa nhận dạng (%w%.%- +)')
		local miscID     = mw.ustring.match(title, 'Trang khác chứa nhận dạng (%w%.%- +)')
		local userID     = mw.ustring.match(title, 'Trang thành viên chứa nhận dạng (%w%.%- +)')
		local wpfaultyID = mw.ustring.match(title, 'Bài viết Wikipedia chứa nhận dạng (%w%.%- +) sai')
		local wpID       = mw.ustring.match(title, 'Bài viết Wikipedia chứa nhận dạng (%w%.%- +)')
		
		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 'Bản mẫu:Bài viết chứa nhận dạng kiểm soát tính nhất quán không rõ thể loại'
		end
	end
	return 
end
return p