Jump to content

Module:Noinclude tfd

From Wikipedia, the free encyclopedia

local redr = require("Module:Redirect")
local function stringCount(string, pattern)
	local _dump, cnt = mw.ustring.gsub(string, pattern, "")
	return cnt
end
local function parse(frame, links)
	local missing = ""
	local hidden = ""
	local bogus = ""
	for _, object in ipairs(links) do
		if object then
			local title = "Template:" .. object;
			local rtarget;
			errorvalue, rtarget = pcall(redr.getTarget, title)
			if errorvalue then
				title = rtarget or title
			end
			local code
			local title = mw.title.new(title)
			if title == nil then
				bogus = bogus .. "\n* " .. title
				code = nil
			else
				code = title:getContent()
			end
			if code then
				local unsubstified = mw.ustring.match(code, "#invoke:Unsubst|");
			    local nmg = stringCount(code, "<noinclude>[^/]*{{Tfm")
			    local ndel = stringCount(code, "<noinclude>[^/]*{{Template for discussion") 
			    local disabled = stringCount(code,"type=disabled")
			    local safesubst = mw.ustring.match(code,"{{{|safesubst:}}}") or mw.ustring.match(code, "<includeonly>safesubst:</includeonly>");
			    safesubst = safesubst or mw.ustring.match(code,"[Ss][Aa][Ff][Ee][Ss][Uu][bB][sS][Tt]:%s*<noinclude%s*/>");
			    local oldstylesubst = mw.ustring.match(code, "{{{subst|}}}") or mw.ustring.match(code, "<includeonly>[Ss]ubst:</includeonly>");
			    local tagCnt = stringCount(code,"{{Tfm") + stringCount(code,"{{Template for merging")
			    tagCnt = tagCnt + stringCount(code,"{{Template for discussion");
			    if tagCnt == 0 then
			    	if title.contentModel == "sanitized-css" then
			    		tagCnt = tagCnt + stringCount(code, "subst:[Tt]emplate for discussion|help=off") -- What twinkle does
						tagCnt = tagCnt + stringCount(code, "This template is being discussed in accordance with Wikipedia's") -- What the instructions say
			    	end
					if tagCnt == 0 then
				    	missing = missing .. "\n* {{tfd links|" .. object .. "}}";
				    end
				elseif (nmg + ndel + disabled >= tagCnt) and (unsubstified or not (safesubst or oldstylesubst)) then
					hidden = hidden .. "\n* {{tfd links|" .. object .. "}}";
				end
			end
		end
	end
	local output = ""
	if #bogus > 0 then
		output = output .. "=== Bogus listings at TfD === \n" .. bogus .. "\n"
	end
	if #missing > 0 then
		output = output .. "=== Pages with no TfD tag === \n" .. missing .. "\n"
	end
	if #hidden > 0 then
		output = output .. "=== Pages with noincluded TfD tags === " .. hidden
	end
	return frame:preprocess(output);
end
local function parseDay(day, links)
	local daytext = mw.title.new(day):getContent();
	daytext = mw.ustring.gsub(daytext,"Please do not modify it","{{tfd links|this is closed}}");
	daytext = mw.ustring.gsub(daytext,"{{[tT]fd links|([^}|]+)[^}]-|module=Module:[^}]-}}", "");
	daytext = mw.ustring.gsub(daytext,"{{[tT]fd links|([^}|]+)[^}]-|module=yes[^}]-}}", "");
	local daymatcher = mw.ustring.gmatch(daytext, "{{[tT]fd links|([^}|]+)[^}]-}}")
	local closed = false
	while true do
		local link = daymatcher();
		if not link then
			return links;
		end
		if link == "this is closed" then
			closed = not closed
		elseif link:sub(1, 7) == "S-line/" then
			-- "S-line/" nominations are almost always for unused templates that don't merit my attention.
		elseif not closed then
			links[#links+1] = link;
		end
	end
end
local p = {}
function p.main(frame)
	local links = {};
	local lang = mw.getContentLanguage()
	local logpage = "Wikipedia:Templates for discussion/Log/"
	for a = 0, 40 do
		parseDay(logpage .. lang.formatDate(lang, "Y F j", "-" .. tostring(a) .. "days"), links)
	end
	return parse(frame, links)
end
return p