Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Sandbox/Aidan9382

From Wikipedia, the free encyclopedia

Sandboxes:


Any linter errors here are likely unintentional, and may be fixed

if mw.title.getCurrentTitle().prefixedText == "Module:Sandbox/Aidan9382" then
	require('Module:Module wikitext')._addText('{{User:Aidan9382/sandbox/header|nolint=y}}')
end
require("strict")
local p = {}

function p.main()
	return "<div style=text-align:right;font-size:80%>This does nothing as of right now. "..os.time().."</div>"
end

--== Shorthand way to call Aidan9382/Benchmarker ==--
function p.Debug(arg1, arg2) -- General-purpose debugger/testing tool; combines multiple submodules
	-- Runs CodeAnalysis and Benchmarker
	-- Benchmarker expects either p and the module name or the return function and the module name
	-- This is to be required in the module, not `#invoke:`ed
	require('Module:Module wikitext')._addText('{{#invoke:Sandbox/Aidan9382/CodeAnalysis|main}}')
	local Hook = require("Module:Sandbox/Aidan9382/Benchmarker")
	local t = type(arg1) == "table" and arg1 or type(arg1) == "nil" and {} or nil
	if t then
		return Hook(t, arg2 or "root module")
	elseif type(arg1) == "function" then
		return Hook(arg1, arg2 or "root module")
	elseif arg1 ~= false then
		mw.log("Aidan9382 | Unable to load Benchmarker (unexpected arg setup)")
	end
end

--== Permanent utility ==--
function p.testframe(options) --For the Debug console - easy way into template entry points
	--To be invoked with the wanted arguments and page titles, or none for the default.
	--Setting .args after testframe() has been called is allowed but discouraged over simply
	--calling the function with provided arguments so as to match the real functionality
	local options = options or {}
	local base = mw.getCurrentFrame()

	local childArgs = options.args or {}
	local parentArgs = options.parentArgs or childArgs
	local childTitle = options.title or base:getTitle()
	local parentTitle = options.parentTitle

	local parentFrame = base:newChild({title=parentTitle, args=parentArgs})
	local childFrame = parentFrame:newChild({title=childTitle, args=childArgs})

	--Hide the extra parents, which would be unexpected outside of a debug setting
	parentFrame.getParent = function() return nil end
	--This 2nd hook is required, or else childFrame:getParent() somehow returns
	--the unmodified parent frame, which doesn't have the getParent hook. :/
	childFrame.getParent = function() return parentFrame end

	return childFrame
end

function p.SpeedTest(frame)
	local text = mw.text.unstripNoWiki(frame.args[1])
	local outs = {}
	local start = os.clock()
	for i = 1, tonumber(frame.args[2]) or 10 do
		local r = tostring(math.random())
		outs[#outs+1] = string.sub(frame:preprocess(text..r),1,-(1+#r))
	end
	local duration = os.clock() - start
	return "Time taken: " .. duration .. "\n\n" .. table.concat(outs, frame.args[3] or "\n")
end

function p.SpeedTestNoRandom(frame)
	local text = mw.text.unstripNoWiki(frame.args[1])
	local outs = {}
	local start = os.clock()
	for i = 1, tonumber(frame.args[2]) or 10 do
		outs[#outs+1] = frame:preprocess(text)
	end
	local duration = os.clock() - start
	return "Time taken: " .. duration .. "\n\n" .. table.concat(outs, frame.args[3] or "\n")
end

function p.WasteExecutionTime(frame)
	local t = tonumber(frame.args[1]) or 0
	local start = os.clock()
	while os.clock()-start < t do end
	return "Wasted " .. (os.clock()-start) .. " seconds"
end

p["+expensive"] = function()
	mw.incrementExpensiveFunctionCount()
	return ""
end

--== Test functions ==--
function p.getargs(frame)
	local text = "Frame arguments:"
	for k,v in pairs(frame.args) do
		k,v = tostring(k), tostring(v)
		text = text .. "\n\n'<code>" .. mw.text.nowiki(mw.text.unstrip(k)) .. "</code>' = '<code>" .. mw.text.nowiki(mw.text.unstrip(v)) .. "</code>'"
	end
	return text
end

function p.testGP(frame)
	local s1 = "Frame args:"
	local s2 = "getParameters args:"
	local p1 = frame.args
	local p2 = require("Module:GetParameters").getParameters(p1, {"a", "b", "c"})
	for k,v in pairs(p1) do
		s1 = s1 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
	end
	for k,v in pairs(p2) do
		s2 = s2 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
	end
	return s1 .. "\n\n" .. s2 .. "\n\n\n"
end

function p.magicWordTests(frame)
	local args = frame.args
	if args.process == "y" then
		frame:preprocess("{{DEFAULTSORT:XYZ}}")
	elseif args.returnprocess == "y" then
		return frame:preprocess("{{DEFAULTSORT:XYZ}}")
	else
		return "{{DEFAULTSORT:XYZ}}"
	end
end

function p.substMagic(frame)
	local arg = tonumber(frame.args[1]) or 0
	return "{{safesubst:#invoke:Sandbox/Aidan9382|substMagic|" .. arg+1 .. "|~~~~}}"
end

function p.AnalysisTest()
	repeat
		local x = 5
	until x == 5
end

function p.requireCaching()
	mw.getCurrentFrame():preprocess("{{#invoke:Sandbox/Aidan9382/require|a}}")
	return mw.getCurrentFrame():preprocess("{{#invoke:Sandbox/Aidan9382/require|a}}")
end

function p.loadCaching()
	local s = os.clock()
	mw.loadData("Module:Citation/CS1/Suggestions")
	local e = os.clock()
	return tostring(e-s)
end

function p.PEIS(frame)
	if frame.args[1] == "1" then
		frame:preprocess("{{cite web|url=https://example.com|title=test}}")
		return "simple text"
	elseif frame.args[1] == "2" then
		return frame:preprocess("{{cite web|url=https://example.com|title=test}}")
	elseif frame.args[1] == "3" then
		require("Module:Citation/CS1")._citation(frame, {url="https://example.com", title="test"}, {CitationClass="web"})
		return "also simple text"
	elseif frame.args[1] == "4" then
		return require("Module:Citation/CS1")._citation(frame, {url="https://example.com", title="test"}, {CitationClass="web"})
	else
		return "?? bad invocation"
	end
end

function p.expensive(frame)
	local mode = tonumber(frame.args[1])
	if mode == 1 then
		return mw.title.new(frame.args[2]).exists and "exists" or "doesn't exist"
	elseif mode == 2 then
		return mw.title.new(frame.args[2]):getContent() ~= nil and "exists" or "doesn't exist"
	else
		return "invalid call"
	end
end

function p.testWP(frame)
	local page = frame.args[1]
	local issandbox = frame.args[2]
	issandbox = issandbox and issandbox ~= "" and "/sandbox" or ""
	local PrepareText = require("Module:Wikitext Parsing" .. issandbox).PrepareText
	local content = mw.title.new(page):getContent()
	local Prepared = PrepareText(content)
	return #content - #Prepared -- just to make sure it worked
end

--== Return setup ==--
function p:namecall()
	return self
end

return setmetatable(p,{
	__index=function(t,k)
		mw.log("Attempted to access unknown function '"..tostring(k).."' from Module:Sandbox/Aidan9382\n"..debug.traceback())
		return function()
			return "<span class=\"error\">Attempted to access unknown function '"..tostring(k).."'</span>"
			--local frame = mw.getCurrentFrame()
			--local traceback = frame:extensionTag("syntaxhighlight", debug.traceback(), {lang="text"})
			--return "<span class=\"error\">Attempted to access unknown function '"..tostring(k).."'</span>\n"..traceback
		end
	end
})