Jump to content

Module:WikiProject banner/templatepage

Kufuma Wikipedia

Documentation for this module may be created at Module:WikiProject banner/templatepage/doc

require('strict')
local p = {}
p.templatepage = function(args, raw_args)
---------------------------
-- Initialise variables ---
---------------------------
local sandbox --= '/sandbox'
local config = mw.loadData('Module:WikiProject banner/config' .. (sandbox or ''))
local cfg = config.template_page -- convenient shortcut for template_page configuration settings
local lang = mw.language.getContentLanguage()
local current_page = mw.title.getCurrentTitle()
local on_sandbox = lang:lc(current_page.subpageText)=='sandbox' -- on sandbox subpage
	or current_page.namespace==2 -- in User namespace
	or current_page.rootText=='WPBannerMeta' -- subpage of Template:WPBannerMeta
local frame = mw.getCurrentFrame()
local parameter_format = function(parameter, value)
	local code = mw.html.create('code')
		:addClass('tpl-para')
		:css('word-break', 'break-word')
		:wikitext('|' .. parameter .. '=' .. (value or ''))
		:done()
	return tostring(code)
end
local yesno = require('Module:Yesno')
local messageBox = require('Module:Message box').main
local project = args.PROJECT or ''
local banner_name = mw.title.new(args.BANNER_NAME or 'Template:WikiProject ' .. project)
local notices, tracking_cats = {}, {}
local add_tracking = function(tracking, show_on_sandbox)
	local category = tracking.category or cfg.default_tracking -- uses [[Category:WikiProject banners with formatting errors]] by default
	local key = tracking.sort_key or (project~='' and project or '*') -- sort key defaults to name of project
	if not on_sandbox or show_on_sandbox then
		table.insert(tracking_cats, '[[Category:' .. category .. '|' .. key .. ']]')
	end
end
---------------------------
-- Demonstrative banner ---
---------------------------
args.PROJECT = args.PROJECT or ''
args.class = raw_args.class and 'C'
args.importance = raw_args.importance and 'High'
args.priority = raw_args.priority and 'High'
args.auto = raw_args.auto and 'inherit'
args.attention = raw_args.attention and 'yes'
args.infobox = raw_args.infobox and 'yes'
local note_count = 0
local task_forces, notes = {}, {}
for arg_name, _ in pairs(raw_args) do
	local tf_match = mw.ustring.match(arg_name,'^tf (%d+)$')
	if tf_match then
		table.insert(task_forces, tf_match)
		args[arg_name] = 'yes'
		if raw_args['tf '..tf_match..' importance'] then
			args['tf '..tf_match..' importance'] = 'Top'
		end
	end
	local note_match = mw.ustring.match(arg_name,'^note (%d+)$')
	if note_match then
		table.insert(notes, note_match)
		local text_arg = args['NOTE_'..note_match..'_TEXT']
		if text_arg and text_arg~='' then
			args[arg_name] = 'yes'
			note_count = note_count + 1
		end
	end
end
table.sort(task_forces)
table.sort(notes)
local demo_banner = require('Module:WikiProject banner'  .. (sandbox or ''))._main(args, args, true, banner_name)
---------------------------
-- Custom class mask ------
---------------------------
local custom_mask = banner_name:subPageTitle('class')
if args.QUALITY_SCALE=='subpage' then
	if custom_mask.exists and #custom_mask:getContent()>1 then
		table.insert(notices, string.format(
			cfg.custom_quality_mask.in_use,
			custom_mask.prefixedText
		))
	else
		table.insert(notices, string.format(
			cfg.custom_quality_mask.missing,
			parameter_format('QUALITY_SCALE'),
			custom_mask.prefixedText
		))
	end
elseif custom_mask.exists and #custom_mask:getContent()>1 and args.QUALITY_CRITERIA=='custom' then
	table.insert(notices, string.format(
		cfg.custom_quality_mask.unused,
		custom_mask.prefixedText,
		parameter_format('QUALITY_SCALE','subpage')
	))
end
---------------------------
-- Quality criteria -------
---------------------------
if args.QUALITY_CRITERIA=='custom' then
	table.insert(notices, cfg.quality_criteria.custom)
	add_tracking(cfg.quality_criteria.custom_tracking)
elseif args.QUALITY_SCALE=='inline' or args.QUALITY_SCALE=='subpage' then
	table.insert(notices, string.format(
		cfg.quality_criteria.standard,
		parameter_format('QUALITY_SCALE')
	))
	add_tracking(cfg.quality_criteria.standard_tracking)
end
---------------------------
-- Custom importance mask -
---------------------------
local custom_mask = banner_name:subPageTitle('importance')
if args.IMPORTANCE_SCALE=='subpage' then
	if custom_mask.exists and #custom_mask:getContent()>1 then
		table.insert(notices, string.format(
			cfg.custom_importance_mask.in_use,
			custom_mask.prefixedText
		))
	else
		table.insert(notices, string.format(
			cfg.custom_importance_mask.missing,
			parameter_format('IMPORTANCE_SCALE'),
			custom_mask.prefixedText
		))
	end
elseif custom_mask.exists and #custom_mask:getContent()>1 then
	table.insert(notices, string.format(
		cfg.custom_importance_mask.unused,
		custom_mask.prefixedText,
		parameter_format('IMPORTANCE_SCALE','subpage')
	))
end
---------------------------
-- Collapsed section ------
---------------------------
local collapse_threshold = tonumber(args.COLLAPSED) or 2
local hook_note = args.HOOK_NOTE
local hook_collapsed
if args.HOOK_COLLAPSED then
	local success, result = pcall(mw.ext.ParserFunctions.expr, args.HOOK_COLLAPSED)
	hook_collapsed = success and tonumber(result) or nil
	if args.HOOK_COLLAPSED=='auto' then
		hook_collapsed = hook_note and 1 or 0 -- default assumption is that HOOK_NOTE produces max one note
	end
end
note_count = note_count
	+ (raw_args.auto and 1 or 0)
	+ (raw_args.attention and 1 or 0)
	+ (raw_args.infobox and 1 or 0)
	+ (hook_collapsed or 0)
local more_than = collapse_threshold>0 and string.format(cfg.collapsing_notes.more_than, collapse_threshold) or cfg.collapsing_notes.any
if note_count > collapse_threshold then
	table.insert(notices, string.format(
		cfg.collapsing_notes.text,
		note_count,
		more_than,
		parameter_format('COLLAPSED')
	))
end
if hook_note then
	if not hook_collapsed then
		table.insert(notices, string.format(
			cfg.collapsing_notes.note_counter,
			parameter_format('HOOK_NOTE'),
			parameter_format('HOOK_COLLAPSED')
		))
		add_tracking(cfg.collapsing_notes.tracking)
	end
end
---------------------------
-- Assessment link --------
---------------------------
if raw_args.class or raw_args.importance then
	local url = mw.uri.fullUrl(banner_name.prefixedText,{
		action = 'edit',
		summary = cfg.assessment_link.edit_summary
	})
	if args.ASSESSMENT_LINK then
		if not mw.title.new(args.ASSESSMENT_LINK).exists then
			table.insert(notices, string.format(
				cfg.assessment_link.missing,
				'[[' .. args.ASSESSMENT_LINK .. ']]',
				tostring(url),
				parameter_format('ASSESSMENT_LINK')
			))
		end
	else
		local project_link = args.PROJECT_LINK or 'Wikipedia:WikiProject ' .. project
		local default = mw.title.new(project_link .. '/Assessment')
		if default.exists then
			table.insert(notices, string.format(
				cfg.assessment_link.default,
				'[[' .. default.prefixedText .. ']]',
				tostring(url),
				parameter_format('ASSESSMENT_LINK')
			))
		end
	end
end
---------------------------
-- Project parameter ------
---------------------------
if args.PROJECT=='' then
	table.insert(notices, cfg.project.text)
	add_tracking(cfg.project.tracking)
end
---------------------------
-- Subst check ------------
---------------------------
if not raw_args.substcheck then
	table.insert(notices, string.format(
		cfg.substcheck.text,
		parameter_format('substcheck')
	))
	add_tracking(cfg.substcheck.tracking)
end
---------------------------
-- Portal link ------------
---------------------------
if args.PORTAL then
	local portal_image = require('Module:Portal')._image(args.PORTAL)
	local portal_link = mw.title.new('Portal:' .. args.PORTAL)
	local explain
	if portal_link and portal_link.exists then
		if portal_image=='Portal-puzzle.svg' then
			explain = string.format(cfg.portal.no_image, cfg.portal.instructions)
		else
			explain = string.format(
				cfg.portal.image_link,
				'[[:File:' .. portal_image .. ']]',
				cfg.portal.instructions
			)
		end
	else
		explain = string.format(cfg.portal.missing, parameter_format('PORTAL'))
		add_tracking(cfg.portal.tracking)
	end
	local text = string.format(
		cfg.portal.text,
		portal_link.prefixedText,
		explain
	)
	table.insert(notices, text)
end
---------------------------
-- Task forces ------------
---------------------------
for _, k in ipairs(task_forces) do
	local name_parameter = 'TF_'..k..'_NAME'
	if raw_args['tf '..k] and not args['TF_'..k..'_NAME'] then
		table.insert(notices, string.format(
			cfg.task_force.text,
			parameter_format('TF_'..k..'_NAME')
		))
		add_tracking(cfg.task_force.tracking)
	end
end
---------------------------
-- Produce notices --------
---------------------------
local notice_html, notice_list
if #notices>=1 then
	notice_html = mw.html.create('ul')
	for _, notice in ipairs(notices) do
		notice_html:tag('li'):wikitext(notice):done()
	end
	notice_list = ' ' .. cfg.notice_text .. tostring(notice_html)
end
local info =  messageBox('ombox', {
	type = 'notice',
	image = '[[File:Icon tools.svg|50px]]',
	text = string.format(cfg.info, frame:expandTemplate{title='tlx', args={'WPBannerMeta'}}) .. (notice_list or '')
})
---------------------------
-- Assessment categories --
---------------------------
local importance_name = args.IMPN or (raw_args.priority and 'priority' or config.importance.default_name)
local check_categories = function(project, assessment_cat, quality_scale, importance_scale, prefix)
	assessment_cat = assessment_cat or ((project or '') .. ' articles')
	local missing_cats = {}
	local check_cat = function(cat_name, preload)
		local category = mw.title.new('Category:' .. cat_name)
		if not category.exists then
			local url = mw.uri.fullUrl('Category:' .. cat_name, {
				action = 'edit',
				preload = preload,
				editintro = cfg.check_assessment.editintro,
				preview = 'no',
				summary = cfg.check_assessment.create_summary,
				['preloadparams[1]'] = project,
				['preloadparams[2]'] = mw.ustring.gsub(assessment_cat, ' articles', '')
			})
			local pages_in_category = mw.site.stats.pagesInCategory(cat_name, 'pages')
			local label = pages_in_category>0 and '<b>create</b>' or 'create'
			local create_link = '[[:' .. category.prefixedText .. ']] &ndash; ([' .. tostring(url) .. ' ' .. label .. '])'
			table.insert(missing_cats, create_link)
		end
	end
	if quality_scale and quality_scale~='inline' and quality_scale~='subpage' then
		check_cat(lang:ucfirst(assessment_cat) .. ' by quality', cfg.check_assessment.meta_preload)
		for _, class in ipairs(cfg.check_assessment.classes) do
			local cat_name = (class=='Unassessed' and 'Unassessed' or class..'-Class') .. ' ' .. assessment_cat
			check_cat(cat_name, cfg.check_assessment.quality_preload)
		end
	end
	if quality_scale=='extended' then
		for _, class in ipairs(cfg.check_assessment.extended_classes) do
			local cat_name = (class=='Unassessed' and 'Unassessed' or class..'-Class') .. ' ' .. assessment_cat
			check_cat(cat_name, cfg.check_assessment.quality_preload)
		end
	end
	if importance_scale and importance_scale~='inline' and importance_scale~='subpage' then
		check_cat(lang:ucfirst(assessment_cat) .. ' by ' .. importance_name, cfg.check_assessment.meta_preload)
		for _, importance in ipairs(cfg.check_assessment.importances) do
			local cat_name = importance .. '-' .. importance_name .. ' ' .. assessment_cat
			check_cat(cat_name, cfg.check_assessment.importance_preload)
		end
	end
	if #missing_cats>0 then
		local intro = string.format(
			cfg.check_assessment.text,
			project or '',
			parameter_format((prefix or '') .. cfg.check_assessment.parameter_suffix)
		)
		local list = mw.html.create('ul')
		for _, missing in ipairs(missing_cats) do
			list:tag('li'):wikitext(missing):done()
		end
		list:done()
		add_tracking(cfg.check_assessment.tracking)
		return messageBox('ombox', {
			type = 'content',
			image = '[[File:' .. cfg.check_assessment.icon .. '|50px]]',
			text = intro .. tostring(list)
		})
	end
	return ''
end
local warnings = {}
table.insert(warnings, check_categories(
	project,
	args.ASSESSMENT_CAT,
	raw_args.class and (args.QUALITY_SCALE or ''),
	(raw_args.importance or raw_args.priority) and (args.IMPORTANCE_SCALE or '')
))
for _, k in ipairs(task_forces) do
	local tf_prefix = 'TF_' .. k .. '_'
	table.insert(warnings, check_categories(
		args[tf_prefix..'NAME'],
		args[tf_prefix..'ASSESSMENT_CAT'],
		raw_args.class and yesno(args[tf_prefix..'QUALITY']) and (args.QUALITY_SCALE or ''),
		raw_args['tf '..k..' importance'] and (args.IMPORTANCE_SCALE or ''),
		tf_prefix
	))
end
---------------------------
-- Other categories -------
---------------------------
local missing_cats = {}
local check_cat = function(cat_name)
	if cat_name and cat_name~='none' then
		local category = mw.title.new('Category:' .. (cat_name or ''))
		if category and not category.exists then
			table.insert(missing_cats, '[[:' .. category.prefixedText .. ']]')
		end
	end
end
if raw_args.attention then
	local attention_cat = args.ATTENTION_CAT or string.format(config.attention.default_cat, project)
	check_cat(attention_cat)
end
if raw_args.infobox then
	local infobox_cat = args.INFOBOX_CAT or string.format(config.infobox.default_cat, project)
	check_cat(infobox_cat)
end
if raw_args.auto then
	local auto_cat = args.AUTO_ASSESS_CAT or string.format(config.auto.default_cat, project)
	check_cat(auto_cat)
end
check_cat(args.MAIN_CAT)
for _, k in ipairs(notes) do
	check_cat(args['NOTE_'..k..'_CAT'])
end
for _, k in ipairs(task_forces) do
	check_cat(args['TF_'..k..'_MAIN_CAT'])
end
if #missing_cats>0 then
	local list = mw.html.create('ul')
	for _, missing in ipairs(missing_cats) do
		list:tag('li'):wikitext(missing):done()
	end
	list:done()
	table.insert(warnings, messageBox('ombox', {
		type = 'content',
		image = '[[File:' .. cfg.check_other.icon .. '|50px]]',
		text = cfg.check_other.text .. tostring(list)
	}))
	add_tracking(cfg.check_other.tracking)
end
---------------------------
-- Template categories ----
---------------------------
if on_sandbox then
	add_tracking(cfg.template_categories.sandbox, true)
else
	if raw_args.class then
		add_tracking(cfg.template_categories.with_quality)
	else
		add_tracking(cfg.template_categories.without_quality)
	end
	if (args.PROJECT_NAME=='WikiProject '..project or not args.PROJECT_NAME) and current_page.rootPageTitle.prefixedText~='Template:WikiProject ' .. project then
		add_tracking(cfg.template_categories.non_standard)
	end
end
---------------------------
-- Unknown parameters -----
---------------------------
local append_table = function(source, destination)
	local out = destination or {}
	for _, v in ipairs(source) do
		table.insert(out, v)
	end
	return out
end
local config_parameters = append_table(cfg.parameters.config)
local parameters = append_table(cfg.parameters.passthrough)
for _, k in ipairs(task_forces) do
	table.insert(parameters, 'tf '..k)
	table.insert(parameters, 'tf '..k..' importance')
	for _, p in ipairs(cfg.parameters.taskforce.suffix) do
		table.insert(
			config_parameters,
			string.format(cfg.parameters.taskforce.prefix, k) .. p
		)
	end
end
for _, k in ipairs(notes) do
	table.insert(parameters, 'note '..k)
	for _, p in ipairs(cfg.parameters.note.suffix) do
		table.insert(
			config_parameters,
			string.format(cfg.parameters.note.prefix, k) .. p
		)
	end
end
--local template_code = banner_name:getContent()
--local parameter_name = {}
--for _, parameter in ipairs(parameters) do
--	parameter_name[parameter] = string.match(template_code, parameter..'%s*=%s*{%{%{([%a%d_%s-]*)')
--end
parameters = append_table(config_parameters, parameters)
parameters.preview = cfg.parameters.preview
parameters.unknown = ''
local parameter_check = require('Module:Check for unknown parameters')._check(parameters, raw_args)
---------------------------
-- Documentation ----------
---------------------------
local doc = ''
if args.DOC=='custom' or args.DOC=='auto+custom' then
	doc = require('Module:Documentation').main()
end

return parameter_check .. demo_banner .. info .. table.concat(warnings) .. table.concat(tracking_cats) .. doc
end

return p