Jump to content

Module:Progression rainbow

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 22:42, 27 October 2020 (return pagesincat as number). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
This implements {{progression rainbow/manual}}

Next up, {{progression rainbow}} fully?

]]
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(class, project, frame)
	return tonumber(frame:callParserFunction(
		'PAGESINCATEGORY',
		class .. project .. 'articles',
		'R'
	))
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args, frame)
		-- pretty sure that this call to extensionTag should be in _main
		-- since a caller from a module should get the associated styles also
end


function p._main(args, frame)
-- is there a way that I can avoid passing a reference to the parent frame?
-- I need to be able to access extensionTag and callParserFunction in p._main
-- these are defined on frame
	
	local project
	if args['project'] and args['project'] ~= '' then
		project = args['project']
	else
		project = nil
	end
	
	local classes = {
		{0, 'List', 'List-Class'},
		{0, 'Stub', 'Stub-Class'},
		{0, 'Start', 'Start-Class'},
		{0, 'C', 'C-Class'},
		{0, 'B', 'B-Class'},
		{0, 'GA', 'GA-Class'},
		{0, 'A', 'A-Class'},
		{0, 'FA', 'FA-Class'}
	}
	
	local project_classes = {
		{0, 'FL', 'FL-Class'},
		{0, 'Unassessed', 'Unassessed'}
	}
	
	-- set the value from arguments in classes, first column
	local sum_classes = 0
	for i, class in pairs(classes) do
		if project then
			class[1] = category_count(class[3], project, frame)
			if class[2] == 'FA' then
				class[1] = class[1] + category_count(
					project_classes[1][3],
					project,
					frame
				)
			end
		elseif args[i] then
			class[1] = tonumber(args[i])
		end
		sum_classes = sum_classes + class[1]
	end

	local divisor
	if project then
		divisor = sum_classes + category_count(
			project_classes[2][3],
			project,
			frame
		)
	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[1] ~= 0 then
			local percentage = class_percent(class[1], divisor)
			root:newline()
				:tag('td')
				:css('background', frame:expandTemplate{
					title = 'class/colour', args = { class[2] }}
				)
				:css('width', percentage)
				:tag('span')
				:wikitext(percentage .. " " .. class[3])
				: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 I should be stringifying p._main return instead of returning
	-- a table
	return frame:extensionTag{
		name = 'templatestyles',
		args = { src = 'Progression rainbow/styles.css'}
	} .. '\n' .. tostring(root)
end

return p