Jump to content

Module:Articles by class/sandbox

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by MSGJ (talk | contribs) at 16:17, 7 May 2025 (fix for NA-importance). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
require('strict')
local p = {}
local frame = mw.getCurrentFrame()
local args = require('Module:Arguments').getArgs(frame)
local title = args.page and mw.title.new(args.page) or mw.title.getCurrentTitle()
local cat_name = '%s-%s %s %s'
local cats = {}
local lang = mw.language.getContentLanguage()

local class_suffix = function(class)
	local suffix = class:lower()=='unassessed' and '' or '-Class'
	return class .. suffix
end

local col_num = function(n, link)
	local text
	local number = lang:formatNum(n)
	if link then
		text = '[[' .. link .. '|' .. number .. ']]'
	else
		text = number
	end
	return mw.html.create('td')
		:attr('align', 'center')
		:wikitext(text)
end

local add_category = function(cat, _sort)
	local link = '[[Category:' .. cat .. (_sort and ('|' .. _sort) or '') .. ']]'
	table.insert(cats, link)
end

local cat_in_use = function(cat)
	local cat_title = mw.title.new('Category:' .. cat)
	return cat_title and cat_title.exists or mw.site.stats.pagesInCategory(cat, 'pages')>0
end

local template = function(template, class, topic, page)
	return frame:expandTemplate{
		title = template,
		args = {
			[1] = class,
			topic = topic,
			category = topic .. ' ' .. page,
			bold = 'no'
		}
	}
end

local create_table = function(cfg)
	local yesno = require('Module:Yesno')
	local class, topic, typ = title.text:match(cfg.pattern)
	if not class and cfg.pattern2 then -- try alternate pattern
		class, topic, typ = title.text:match(cfg.pattern2)
	end
	if not topic then
		topic = args.topic or title.text:match('^(.+) articles by ' .. cfg.qualimp .. '$')
	end
	local out, exist, cats = '', {}, {}
	if args.class and class and args.topic and topic and (args.class:lower()~=class:lower() or args.topic~=topic) then
		add_category('WikiProject assessment categories needing attention')
	end
	topic = topic or args.topic or ''
	class = class or args.class or ''
	typ = typ or 'articles'
	if title.namespace==14 and not title.text:match('^' .. topic .. ' articles by ' .. cfg.qualimp .. '$') then
		out = frame:expandTemplate{title='Possibly empty category'}
	end
	for _, class in ipairs(cfg.classes) do
		if cat_in_use(cat_name:format(class, cfg.suffix, topic, 'pages')) then
			exist[class] = 'pages'
		elseif cat_in_use(cat_name:format(class, cfg.suffix, topic, 'articles')) then
			exist[class] = 'articles'
		else
			exist[class] = false
		end
	end
	local header_row = mw.html.create('tr')
	for _, class in ipairs(cfg.classes) do
		if exist[class] then
			header_row:node(template(cfg.template_name, class, topic, exist[class]))
		end
	end
	if args.custom1 then
		header_row:node(template(cfg.template_name, args.custom1, topic, 'articles'))
	end
	if args.custom2 then
		header_row:node(template(cfg.template_name, args.custom2, topic, 'articles'))
	end
	if cfg.custom then
		header_row:node(template(cfg.template_name, cfg.custom.name, topic, 'articles'))
	end
	local total_cell = mw.html.create('td'):attr('align', 'center'):wikitext('Total')
	header_row:node(total_cell)
	local total = 0
	local pages_in_cat = function(cat)
		local pages = mw.site.stats.pagesInCategory(cat, 'pages')
		total = total + pages
		return col_num(pages)
	end
	local second_row = mw.html.create('tr')
	for _, class in ipairs(cfg.classes) do
		if exist[class] then
			second_row:node(pages_in_cat(cat_name:format(class, cfg.suffix, topic, exist[class])))
		end
	end
	if args.custom1 then
		second_row:node(pages_in_cat(cat_name:format(args.custom1, cfg.suffix, topic, 'articles')))
	end
	if args.custom2 then
		second_row:node(pages_in_cat(cat_name:format(args.custom2, cfg.suffix, topic, 'articles')))
	end
	if cfg.custom then
		second_row:node(pages_in_cat(cfg.custom.category:format(topic)))
	end
	second_row:node(col_num(total))
	local caption
	if args.project then -- display caption
		local abc = 'articles by ' .. cfg.qualimp
		local text = '[[Wikipedia:WikiProject ' .. args.project .. '|WikiProject ' .. args.project .. ']] '
			.. (
				mw.title.new('Category:' .. topic .. ' ' .. abc).exists
				and '[[:Category:' .. topic .. ' ' .. abc .. '|' .. abc .. ']]'
				or abc
			) .. string.rep('  ', 3)
			.. '<small>' .. frame:expandTemplate{title = 'Purge', args = {'Refresh'}} .. '</small>'
		caption = mw.html.create('caption')
			:attr('align', 'bottom')
			:wikitext(text)
	end
	local tab = mw.html.create('table')
		:addClass(args.format or 'wikitable') -- add custom CSS class if specified
		:addClass('toccolours'):addClass('nomobile')
		:css('table-layout', 'fixed')
		:css('margin', '1em auto')
		:node(header_row)
		:node(second_row)
		:node(caption)
	if class and class~='' and class~='nocat' then
		add_category(args.parent or (topic .. ' articles by ' .. cfg.qualimp), class)
		local suffix = class:lower()=='unassessed' and '' or ('-' .. cfg.suffix)
		add_category(class .. suffix .. ' ' .. typ, args.sort or topic)
	end
	return out .. tostring(tab) .. table.concat(cats)
end

p.quality_and_importance = function()
	local cfg = {
		patterns = {
			'^(%a+)-Class (.+) (%a+) of (%a+)-importance$'
		},
		importance_classes = {'Top', 'High', 'Mid', 'Low', 'Bottom', 'NA', 'Unknown'},
		quality_classes = {'FA', 'A', 'GA', 'B', 'C', 'Start', 'Stub', 'FL', 'List', 'Draft', 'Redirect'}
	}
	local totals = {}
	local grand_total = 0
	local class, topic, typ, importance = title.text:match(cfg.patterns[1])
	if class and class~='' and importance and importance ~='' then
		add_category(topic .. ' articles by quality and importance', class..importance)
		add_category(class_suffix(class) .. ' ' .. topic .. ' ' .. typ, '*' .. importance)
		add_category(importance .. '-importance' .. ' ' .. topic .. ' ' .. (importance=='NA' and 'pages' or 'articles'), '*' .. class)
	end
	local corner_cell = mw.html.create('th')
		:attr('colspan', '2')
		:attr('rowspan', '2')
		:wikitext(lang:ucfirst(topic) .. '<br>articles')
	local importance_cell = mw.html.create('th')
		:attr('colspan' , '8')
		:wikitext('[[Wikipedia:Version 1.0 Editorial Team/Release Version Criteria#Priority of topic|Importance]]')
	local top_row = mw.html.create('tr')
		:node(corner_cell)
		:node(importance_cell)
	local importance_row = mw.html.create('tr')
	for _, importance in ipairs(cfg.importance_classes) do
		importance_row:node(template('Importance', importance, topic, importance=='NA' and 'pages' or 'articles'))
	end
	local importance_cell = mw.html.create('th'):wikitext('Total')
	importance_row:node(importance_cell)
	local quality_cells = function(row, class)
		local total = 0
		row:node(template('Class', class, topic, 'articles'))
		for _, importance in ipairs(cfg.importance_classes) do
			local _typ = importance=='NA' and 'pages' or 'articles'
			local cat_name = class_suffix(class) .. ' ' .. topic .. ' ' .. _typ .. ' of ' .. importance .. '-importance'
			local pages = mw.site.stats.pagesInCategory(cat_name, 'pages')
			total = total + pages
			if totals[importance] then
				totals[importance] = totals[importance] + pages
			else
				totals[importance] = pages
			end
			row:node(col_num(pages, ':Category:' .. cat_name))
		end
		row:node(col_num(total, ':Category:' .. class_suffix(class) .. ' ' .. topic .. ' ' .. typ))
		grand_total = grand_total + total
	end
	local tab = mw.html.create('table')
		:addClass(args.format or 'wikitable')
		:addClass('toccolours'):addClass('nomobile')
		:css('table-layout', 'fixed')
		:css('margin', '1em auto')
		:node(top_row)
		:node(importance_row)
	local quality_column = mw.html.create('th')
		:attr('rowspan', '12')
		:wikitext('[[Wikipedia:Content assessment#Grades|Quality]]')
	for n, _class in ipairs(cfg.quality_classes) do
		local row = mw.html.create('tr')
		if n==1 then
			row:node(quality_column)
		end
		quality_cells(row, cfg.quality_classes[n])
		tab:node(row)
	end
	local total_row = mw.html.create('tr')
		:node(importance_cell)
	for _, importance in ipairs(cfg.importance_classes) do
		local cat_name = importance .. '-importance ' .. topic .. ' ' .. (importance=='NA' and 'pages' or 'articles')
		total_row:node(col_num(totals[importance] or 0, ':Category:' .. cat_name))
	end
	total_row:node(col_num(grand_total))
	tab:node(total_row)
	return tostring(tab) .. table.concat(cats)
end

p.quality = function()
	return create_table{
		pattern = '^(%a+)-Class (.+) (%a+)$',
		pattern2 = '^(Unassessed) (.+) (%a+)$', -- match unassessed category
		qualimp = 'quality',
		classes = {'FA', 'A', 'GA', 'B', 'C', 'Start', 'Stub', 'FL', 'AL', 'BL', 'CL', 'List', 'SIA', 'Future', 'Category', 'Disambig', 'Draft', 'FM', 'File', 'Portal', 'Project', 'Redirect', 'Template', 'User', 'NA'},
		template_name = 'Class',
		suffix = 'Class',
		custom = {name = 'Unassessed', category = 'Unassessed %s articles'}
	}
end

p.importance = function()
	return create_table{
		pattern = '^(%a+)-importance (.+) (%a+)$',
		qualimp = 'importance',
		classes = {'Top', 'High', 'Mid', 'Low', 'Bottom', 'NA', 'Unknown'},
		template_name = 'Importance',
		suffix = 'importance'
	}
end

return p