Jump to content

Module:Articles by class

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 19:04, 19 December 2024 (add_cat). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')
local p = {}
local classes = {'FA', 'A', 'GA', 'B', 'C', 'Start', 'Stub', 'FL', 'AL', 'BL', 'CL', 'List', 'SIA', 'Future', 'Category', 'Disambig', 'Draft', 'FM', 'File', 'Needed', 'Portal', 'Project', 'Redirect', 'Template', 'User', 'NA'}

p.main = function(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local title = args.page and mw.title.new(page) or mw.title.getCurrentTitle()
	local class, topic = title.text:match('^(%a+)-Class (%a+) %a+$')
	if args.topic then -- override topic
		topic = args.topic
	end
	local exist = {}
	local cats = {}
	local add_category = function(cat)
		table.append(cats, '[[Category:' .. cat)
	end
	local cat_in_use = function(cat)
		local title = mw.title.new('Category:' .. cat)
		return title.exists or mw.site.stats.pagesInCategory(cat, 'pages')>0
	end
	for _, class in ipairs(classes) do
		if cat_in_use(class .. '-Class' .. ' ' .. topic .. ' pages') then
			exist[class] = 'pages'
		elseif cat_in_use(class .. '-Class' .. ' ' .. topic .. ' articles') then
			exist[class] = 'articles'
		else
			exist[class] = false
		end
	end
	local class_template = function(class, page)
		return frame:expandTemplate{
			title = 'class',
			args = {
				[1] = class,
				topic = topic,
				category = topic .. ' ' .. page,
				bold = 'no'
			}
		}
	end
	local header_row = mw.html.create('tr')
	for _, class in ipairs(classes) do
		if exist[class] then
			header_row:node(class_template(class, exist[class]))
		end
	end
	header_row:node(class_template('Unassessed', 'articles'))
	local pages_in_cat = function(cat)
		local pages = mw.site.stats.pagesInCategory(cat, 'pages')
		return mw.language.getContentLanguage():formatNum(pages)
	end
	local second_row = mw.html.create('tr')
	for _, class in ipairs(classes) do
		if exist[class] then
			local col = mw.html.create('td')
				:attr('align', 'right')
				:wikitext(pages_in_cat(class .. '-Class' .. ' ' .. topic .. ' ' .. exist[class]))
			second_row:node(col)
		end
	end
	local unassessed_col = mw.html.create('td')
		:attr('align', 'right')
		:wikitext(pages_in_cat('Unassessed ' .. topic .. ' articles'))
	second_row:node(unassessed_col)
	local tab = mw.html.create('table')
		:addClass('toccolours'):addClass('nomobile')
		:css('table-layout', 'fixed')
		:css('margin', '1em auto')
		:node(header_row)
		:node(second_row)
	return tostring(tab) .. table.concat(cats)
end

return p