Jump to content

Module:Articles by importance

Permanently protected module
From Wikipedia, the free encyclopedia
require('strict')
local p = {}
local importances = {'Top', 'High', 'Mid', 'Low', 'Bottom', 'NA', 'Unknown'}

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 imp, topic, typ = title.text:match('^(%a+)-importance (.+) (%a+)$')
	local out, exist, cats = '', {}, {}
	local add_category = function(cat, sort)
		local link = '[[Category:' .. cat .. (sort and ('|' .. sort) or '') .. ']]'
		table.insert(cats, link)
	end
	if args.importance and imp and args.topic and topic and (args.importance:lower()~=imp:lower() or args.topic~=topic) then
		add_category('WikiProject assessment categories needing attention')
	end
	topic = args.topic or topic or ''
	imp = args.imp or imp or ''
	typ = typ or 'articles'
	if title.namespace==14 then
		out = frame:expandTemplate{title='Possibly empty category'}
	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
	for _, importance in ipairs(importances) do
		if cat_in_use(importance .. '-importance' .. ' ' .. topic .. ' pages') then
			exist[importance] = 'pages'
		elseif cat_in_use(importance .. '-importance' .. ' ' .. topic .. ' articles') then
			exist[importance] = 'articles'
		else
			exist[importance] = false
		end
	end
	local imp_template = function(importance, page)
		return frame:expandTemplate{
			title = 'importance',
			args = {
				[1] = importance,
				topic = topic,
				category = topic .. ' ' .. page,
				bold = 'no'
			}
		}
	end
	local header_row = mw.html.create('tr')
	for _, importance in ipairs(importances) do
		if exist[importance] then
			header_row:node(imp_template(importance, exist[importance]))
		end
	end
	if args.custom1 then
		header_row:node(imp_template(args.custom1, 'articles'))
	end
	if args.custom2 then
		header_row:node(imp_template(args.custom2, 'articles'))
	end
	local pages_in_cat = function(cat)
		local pages = mw.site.stats.pagesInCategory(cat, 'pages')
		local col = mw.html.create('td')
			:attr('align', 'right')
			:wikitext(mw.language.getContentLanguage():formatNum(pages))
		return col
	end
	local second_row = mw.html.create('tr')
	for _, importance in ipairs(importances) do
		if exist[importance] then
			second_row:node(pages_in_cat(importance .. '-importance' .. ' ' .. topic .. ' ' .. exist[importance]))
		end
	end
	if args.custom1 then
		second_row:node(pages_in_cat(args.custom1 .. '-importance' .. ' ' .. topic .. ' articles'))
	end
	if args.custom2 then
		second_row:node(pages_in_cat(args.custom2 .. '-importance' .. ' ' .. topic .. ' articles'))
	end
	local tab = mw.html.create('table')
		:addClass('toccolours'):addClass('nomobile')
		:css('table-layout', 'fixed')
		:css('margin', '1em auto')
		:node(header_row)
		:node(second_row)
	if imp and imp~='' and imp~='nocat' then
		add_category(args.parent or (topic .. ' articles by importance'), imp)
		add_category(imp .. '-importance ' .. typ, args.sort or topic)
	end
	return out .. tostring(tab) .. table.concat(cats)
end

return p