انتقل إلى المحتوى

وحدة:Pages with authority control identifiers

من ويكيبيديا، الموسوعة الحرة

require('Module:No globals')
local p = {}
local ac_module = require('Module:ضبط استنادي')
local ac_conf = ac_module.conf
local ar_sections = ac_module.ar_sections
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 {{فهرس تصنيف آلي}}
	return frame:expandTemplate{ title = 'فهرس تصنيف آلي', 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 red-linked authority control categories]]'
	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

--For use in [[Category:مقالات فيها معلومات ضبط استنادي]], i.e. on [[Category:Articles with VIAF identifiers]]
local function wp( 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 link    = '[['..linktarget..'|'..conf[1]..']]'
			local wdpl    = ':d:Property:P'..conf[2]
            local sect = ar_sections[conf[4]] or conf[4]
			local example = ' يظهر معرف ' .. rmCats(conf[3](conf[5])) .. ' ('.. conf[1] .. ') في قسم ' .. sect .. '.'
			local txCatExplain = frame:expandTemplate{ title = 'شرح تصنيف', args = {'مقالات فيها معرفات  '.. link ..'. '.. example ..' يرجى عدم إضافة تصنيفات فرعية.'} }
			local txCatMore    = frame:expandTemplate{ title = 'تصنيف أكثر', args = {'ضبط استنادي', wdpl} }
			local txEmptyCat   = frame:expandTemplate{ title = 'تصنيف فارغ' }
			local txWPCat      = frame:expandTemplate{ title = 'تصنيف صيانة', args = { hidden = 'yes', tracking = 'yes' } }
			local txTOC = whichTOC( frame )
			local wpCat = 'مقالات فيها معلومات ضبط استنادي'
			local outString = txCatExplain..'\n'..'تضاف الصفحات إلى هذا التصنيف عبر [[وحدة:ضبط استنادي]].'..txCatMore..txEmptyCat..txWPCat..txTOC..addCat(wpCat, id)
			return outString
		end
	end
	return ''
end

--For use in [[Category:صفحات متفرقة فيها معرفات ضبط استنادي]], i.e. on [[Category:صفحات متفرقة فيها معرفات 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 = 'تصنيف أكثر', args = {'ضبط استنادي', linktarget, wdpl} }
			local txEmptyCat = frame:expandTemplate{ title = 'تصنيف فارغ' }
			local txWPCat    = frame:expandTemplate{ title = 'تصنيف ويكيبيديا', args = { hidden = 'yes', tracking = 'yes' } }
			local txDirtyCat = frame:expandTemplate{ title = 'تصنيف ملوث' }
			local txTOC = whichTOC( frame )
			local idCat = 'مقالات فيها معرفات '..id..''
			local wpfCat = 'صفحات متفرقة فيها معرفات ضبط استنادي'
			local outString = txCatMore..'\n'..
							'تضاف الصفحات إلى هذا التصنيف عبر [[وحدة:ضبط استنادي]].'..
							txEmptyCat..txWPCat..txDirtyCat..txTOC..'\n'..
							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 wpfaultyID = mw.ustring.match(title, 'صفحات متفرقة فيها معرفات ([%w%.%- ]+)')
		local wpID       = mw.ustring.match(title, 'مقالات فيها معرفات ([%w%.%- ]+)')
		if 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