Module:Progression rainbow
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
This module implements Template:Progression rainbow.
Usage
{{#invoke:Progression rainbow|main}}
--[[
This implements {{progression rainbow}}
]]
require ('Module:No globals')
local getArgs = require ('Module:Arguments').getArgs
local p = {}
-- rounding to first decimal, from http://lua-users.org/wiki/SimpleRound
local function round(num)
return math.floor(num * 10 + 0.5) / 10
end
local function class_percent(param, divisor)
return tostring(round(100 * param / divisor)) .. '%'
end
local function percent_remaining(sum, divisor)
sum = sum - divisor
if sum ~= 0 then -- find this a bit specious
return tostring(round(-100 * sum / divisor)) .. '%'
else
return nil
end
end
local function category_count(category, project)
return mw.site.stats.pagesInCategory(
category .. ' ' .. project .. ' articles',
'pages'
)
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args, frame)
end
function p._main(args, frame)
-- Is there a way to avoid passing a reference to the parent frame?
-- extensionTag must be available in p._main, which on the frame object.
local classes = {
{count = 0, class = 'List', category = 'List-Class'},
{count = 0, class = 'Stub', category = 'Stub-Class'},
{count = 0, class = 'Start', category = 'Start-Class'},
{count = 0, class = 'C', category = 'C-Class'},
{count = 0, class = 'B', category = 'B-Class'},
{count = 0, class = 'GA', category = 'GA-Class'},
{count = 0, class = 'A', category = 'A-Class'},
{count = 0, class = 'FA', category = 'FA-Class'}
}
local project_classes = {
{count = 0, class = 'FL', category = 'FL-Class'},
{count = 0, class = 'Unassessed', category = 'Unassessed'}
}
local project
if args['project'] and args['project'] ~= '' then
project = args['project']
else
project = nil
end
local sum_classes = 0
if project then
for i, class in pairs(classes) do
class['count'] = category_count(class['category'], project)
if class['class'] == 'FA' then
class['count'] = class['count'] + category_count(
project_classes[1]['category'],
project
)
end
sum_classes = sum_classes + class['count']
end
else
for i, class in pairs(classes) do
if args[i] then
class['count'] = tonumber(args[i])
sum_classes = sum_classes + class['count']
end
end
end
local divisor
if project then
divisor = sum_classes + category_count(
project_classes[2]['category'],
project
)
else
divisor = args[9] or 100
end
local root = mw.html.create('table')
root:addClass('progression-rainbow')
:attr('role', 'presentation')
for i, class in pairs(classes) do
if class['count'] ~= 0 then
local percentage = class_percent(class['count'], divisor)
root:newline()
:tag('td')
:css('background', frame:expandTemplate{
title = 'class/colour', args = { class['class'] }}
)
:css('width', percentage)
:tag('span')
:wikitext(percentage .. " " .. class['category'])
:done()
:done()
end
end
root:newline()
local remaining = percent_remaining(sum_classes, divisor)
if remaining then
root:tag('td')
:addClass('remaining')
:css('width', remaining)
:tag('span')
:wikitext(remaining .. " remaining")
:done()
:done()
:newline()
end
-- Not sure if p._main should be stringified instead of returning an object.
return frame:extensionTag{
name = 'templatestyles',
args = { src = 'Progression rainbow/styles.css'}
} .. '\n' .. tostring(root)
end
return p