p = {};
function p.partition( frame )
local page = frame.args[1];
local finish = frame.args[2] or "true"
page = page:match( "^%s*(.-)%s*$" );
local tt = mw.title.new( page );
if tt == nil then
return "No such page";
end
local content = tt:getContent();
local result = '';
local blocks = {};
local name;
local current;
-- Remove table confusion
content = content:gsub( "[^{]({|)", "" );
content = content:gsub( "(|})[^}]", "" );
for block in content:gmatch( "%b{}" ) do
name = block:match( "^{{%s*([^|}]-)%s*[|}]" );
if name ~= nil then
name = name:lower();
current = blocks[ name ];
if current == nil then
current = {};
end
table.insert( current, block );
blocks[ name ] = current;
end
end
local blocks2 = {};
for k, v in pairs( blocks ) do
table.insert( blocks2, {k, v} );
end
blocks = blocks2;
table.sort( blocks, comp );
local count;
result = "This is a template rendering profile for [[" .. page .. "]].\n\n";
result = result .. "This tool measures the time taken for various templates on the page to be expanded and converted into wikitext " ..
" (similar to the function of [[Special:ExpandTemplates]]). " ..
"This only captures a portion of the total time required to render a page, but will often identify templates that render slowly. " ..
"In particular, this tool does not measure the time required by the parser to convert wikitext into HTML, which may be " ..
"a significant fraction of the total rendering time in many cases.\n\n"
result = result .. "{| class='wikitable sortable' \n|-\n!Template !! Iterations !! Time (s) \n|-\n";
for _, v in ipairs( blocks ) do
k = v[1];
v = v[2];
count = #v;
v = table.concat(v);
v = v:gsub( "=", "{{=}}" );
v = v:gsub( "|", "{{!}}" );
result = result .. "| " .. k .. " || " ..
tostring( count ) .. " || " ..
"{{#invoke: RenderProfile | time | " .. v .. "}}" .. "\n|-\n";
end
result = result .. "|}";
finish = finish:match( "^%s*(.-)%s*$");
if finish:lower() == 'true' then
return frame:preprocess( result );
else
return result;
end
end
function p.time( frame )
local start = os.clock();
local start2 = os.time();
local test = frame.args[1];
test = frame:preprocess( test );
local stop = os.clock();
local stop2 = os.time();
return string.format("%5.3f", stop - start);
end
function comp( a, b )
return #(a[2]) > #(b[2]);
end
return p