Module:Sandbox/Aidan9382: Difference between revisions

Content deleted Content added
fix
that's cool
 
(47 intermediate revisions by the same user not shown)
Line 1:
if mw.title.getCurrentTitle().prefixedText == "Module:Sandbox/Aidan9382" then
require('Module:Module wikitext')._addText('{{User:Aidan9382/sandbox/header|nolint=y}}')
require('Module:Module wikitext')._addText('{{User:Aidan9382/sandbox/header|nolint=y}}')
end
require("strict")
local p = {}
 
function p.main(data)
return "<div style=text-align:right;font-size:80%>This does nothing as of right now. "..os.time().."</div>"
end
 
function p.Debug(arg1, arg2) -- General-purpose debugger/testing tool; combines multiple submodules
function p.iloveregexold(data)
-- Runs CodeAnalysis and Benchmarker
local r = "%[?%[?.-:([^{|]+)%]?%]?"
-- Benchmarker expects either p and the module name or the return function and the module name
local args = {mw.ustring.match("[[File:Hey.jpg|thumb|left|250px]]",r)}
-- This is to be required in the module, not `#invoke:`ed
return table.concat(args," -- ")
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
 
function p.iloveregex(data)
function p.getargs(frame)
--This assumes [[ is present, and that therefore ]] is present
local rtext = "%[%[%a-Frame arguments:([^{|]+)(.+)%]%]"
for k,v in pairs(frame.args) do
local args = {mw.ustring.match("[[File:Hey.jpg|thumb|left|250px|All for [[Module:Excerpt/portals]]|alt=text]]",r)}
k,v = tostring(k), tostring(v)
return table.concat(args," -- ")
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.parentingLogictestGP(frame)
local s1 = "Frame args:"
--Note: Not even sure this thing goes beyond 1 parent, but have overcomplicated code anyways
local parentChains2 = {}"getParameters args:"
local newframep1 = frame.args
local p2 = require("Module:GetParameters").getParameters(p1, {"a", "b", "c"})
while true do
for k,v in pairs(p1) do
local parent = newframe:getParent()
s1 = s1 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
if not parent then
break
end
newframe = parent
parentChain[#parentChain+1] = parent
end
for k,v in pairs(p2) do
local parentText = ""
s2 = s2 .. "\n'" .. tostring(k) .. "' = '" .. v .. "'"
for i,p in next,parentChain do
parentText = parentText .. " parent"..i.."="..p:getTitle()
end
return s1 .. "\n\n" .. s2 .. "\n\n\n"
return "frame="..frame:getTitle()..parentText.." mw.gct="..mw.title.getCurrentTitle().text
end
 
function p.testframe(options) --For the Debug console - easy way into template entry points
p["module-use"] = function(frame)
--To be invoked with the wanted arguments and page titles, or none for the default.
local parentLogicStuff = p.parentingLogic(frame)
--Setting .args after testframe() has been called is allowed but discouraged over simply
local testlogic = "\n\n"
--calling the function with provided arguments so as to match the real functionality
local i = 1
local options = options or {}
while true do
local cbase = framemw.args["_test"..i]getCurrentFrame()
 
if c then
local childArgs = options.args or {}
testlogic = testlogic .. "_test"..i.."="..c
local parentArgs = options.parentArgs or childArgs
else
local childTitle = options.title or base:getTitle()
break
local parentTitle = options.parentTitle
end
 
i = i + 1
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.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
return parentLogicStuff..testlogic
end
 
function p.getargssubstMagic(frame)
local textarg = "Frametonumber(frame.args[1]) arguments:"or 0
return "{{safesubst:#invoke:Sandbox/Aidan9382|substMagic|" .. arg+1 .. "|~~~~}}"
for k,v in pairs(frame.args) do
end
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>'"
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 text
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.AnalysisTest()
repeat
local x = 5
until x == 5
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()
function p.testframe() --For the Debug console
mw.incrementExpensiveFunctionCount()
local f = mw.getCurrentFrame()
f.getParent = function() return f end""
return f
end
 
Line 75 ⟶ 135:
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>"