Jump to content

Module:AfC submission catcheck

Permanently protected module
From Wikipedia, the free encyclopedia

local p = {}
local yesno = require('Module:Yesno')

local function removeFalsePositives(str)
	if not str then
		return ''
	end
	str = mw.ustring.gsub(str, "<!%-%-.-%-%->", "")
	str = mw.ustring.gsub(str, "<!%-%-.*$", "") --remove comments
	str = mw.ustring.gsub(str, "<nowiki>.-</nowiki>", "") --remove nowiki tags
	str = mw.ustring.gsub(str, "<pre[^>]*>.-</pre>", "") --remove pre tags
	str = mw.ustring.gsub(str, "<syntaxhighlight[^>]*>.-</syntaxhighlight>", "") --remove syntaxhighlight tags
	str = mw.ustring.gsub(str, "<includeonly>.-</includeonly>", "") --remove includeonly tags
	str = mw.ustring.gsub(str, "{{[Dd]raft[ _][Cc]ategories[^{}]-{{[^{}]-}}[^{}]-}}", "") --remove draft categories, with children templates
	str = mw.ustring.gsub(str, "{{[Dd]raft[ _][Cc]ategories.-}}", "") --remove draft categories (again)
	str = mw.ustring.gsub(str, "{{[Dd]raft[ _]?cat[^{}]-{{[^{}]-}}[^{}]-}}", "") --redirect
	str = mw.ustring.gsub(str, "{{[Dd]raft[ _]?cat.-}}", "")
	str = mw.ustring.gsub(str, "%[%[[ _]-[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy][ _]-:[ _]-([^|%]]*)[ _]-%|?[^%]]-%]%]", "[[Category:%1]]") --normalize categories
	str = mw.ustring.gsub(str, "%[%[Category:[^%]]-[Dd]raft[^%]]-%]%]","") --remove all categories containing "draft"
	str = mw.ustring.gsub(str, "%[%[Category:[^%]]-[Ww]ikipedia[^%]]-%]%]", "") --remove all categories containing "wikipedia"
	str = mw.ustring.gsub(str, "%[%[Category:[^%]]-[Uu]ser[^%]]-%]%]", "") --remove all categories containing "user"
	return str
end

function p.checkforcats(frame)
	local args = frame.args
	local t = mw.title.getCurrentTitle()
	local tc = t:getContent()
	if tc == nil or yesno(args.demo) then
		return ""
	end

	tc = removeFalsePositives(tc)
	if mw.ustring.match(tc, "%[%[Category:[^%[%]{}]+%]%]" ) == nil then
		return ""
	else
		local result = "[[Category:Drafts with categories]]"

		local notAfCParam = args.notAfCsubmission
		if notAfCParam == nil or not yesno(notAfCParam) then
			result = result .. "[[Category:AfC submissions with categories]]"
		end

		return result
	end
end

function p.submitted(frame)
	local text = removeFalsePositives(mw.title.getCurrentTitle():getContent())
	if mw.ustring.find(text, '{{AfC submission||', 1, true) or mw.ustring.find(text, '{{AFC submission||', 1, true) then
		return frame.args[1]
	else
		return frame.args[2]
	end
end

return p