Jump to content

Module:Noinclude tfd

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Pppery (talk | contribs) at 14:16, 15 September 2018 (Undid revision 859667557 by Pppery (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 output = ""
	for _, object in ipairs(links) do
		if object then
			local title = "Template:" .. object;
			local rtarget;
			if #output > 0 then
				errorvalue, rtarget = pcall(redr.getTarget, title);
				if not errorvalue then
					-- the most likely error here is too many expensive parser function calls
					return frame:preprocess(output .. "\n* (too many expensive parser functions to continue)");
				end
			else
				-- no output, so we might as well let any error propogate
				rtarget = redr.getTarget(title)
			end
			title = rtarget or title;
			local code = mw.title.new(title):getContent();
			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 not tagCnt or ((nmg + ndel + disabled >= tagCnt) and (unsubstified or not (safesubst or oldstylesubst))) then
				    output = output .. "\n* {{tfd links|" .. object .. "}}";
				end
			end
		end
	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:[^}]-}}", "");
	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 not closed then
			links[#links+1] = link;
		end
	end
end
local p = {}
function p.main(frame)
	local links = {};
	local tfdtext = mw.title.new("Wikipedia:Templates for discussion"):getContent();
	local matcher = string.gmatch(tfdtext, "{{(Wikipedia:Templates for discussion/Log/201%d [^%d]* %d%d?)}}");
	local today = frame:preprocess("{{CURRENTYEAR}} {{CURRENTMONTHNAME}} {{CURRENTDAY}}")
	today = "Wikipedia:Templates for discussion/Log/" .. today;
	links = parseDay(today, links);
	while true do
		local day = matcher();
		if not day then
			return parse(frame, links);
		end
		links = parseDay(day, links);
	end
end
return p