Jump to content

Module:WikiProject assessment progression/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sasuke Sarutobi (talk | contribs) at 15:47, 6 December 2020 (Extract category count to new function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('Module:No globals')

local classes = {
	'List','Stub','Start','C','B',
	'GA','A','FA','FL','FM',
	'Book','Category','Disambig','File',
	'Portal','Project','Redirect','Template',
}

local getArgs = require('Module:Arguments').getArgs
local errorMessage = require('Module:Error')
local p = {}

local function categoryCount(category, project)
	return mw.site.stats.pagesInCategory(
		string.format('%s %s articles', category, project),
		'pages'
	)
end

-- 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 percentComplete(sum, total)
	return tostring(round(100 * sum / total))
end

local function countCategoryArticles(classes)
	local classCount = 0
	for _, class in ipairs(classes) do
		local category = class..'-Class'
		classCount = classCount + categoryCount(category, project)
	end
	return classCount
end

local function calculateProjectTotal(classCount)
	return classCount + categoryCount('Unassessed', project)
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local project = args['project']
	local classCount = countCategoryArticles(classes)
	local total = calculateProjectTotal(classCount)
	if total == 0 then
		return errorMessage('Error: project not found for project name "'..project..'"')
	else
		return percentComplete(classCount, total)
	end
end

return p