Jump to content

Module:WikiProject banner: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
make that an empty table instead
I have 0 expectation this will work
Line 5: Line 5:
require ('Module:No globals')
require ('Module:No globals')


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

local cfg = {
["subst_warning"] = {
["image"] = '[[File:Stop hand nuvola.svg|40px]]',
["type"] = 'content',
-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
["text"] = '<p>It seems that the <i>WikiProject %s</i> banner has ' ..
'been [[Wikipedia:Substitution|substituted]] on this page ' ..
'instead of being [[Wikipedia:Transclusion|transcluded]]. ' ..
'Please undo the edit and type <code>&#123;&#123;%s&#125;&#125;' ..
'</code> instead.</p>',
["categories"] = '<includeonly>[[Category:WikiProject banners with formatting errors|SUBST]]' ..
'[[Category:Pages with incorrectly substituted templates]]</includeonly>'
},
["namespace_warning"] = {
["image"] = '[[File:Stop hand nuvola.svg|40px]]',
["type"] = 'content',
-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
["text"] = '<p>The WikiProject banner below should be moved to this ' ..
'%s\'s <b>[[%s|talk page]]</b>.</p><p style="font-size:95%%">If ' ..
'this is a demonstration of the template, please set the ' ..
'parameter %s to prevent this page being miscategorised. %s</p>' ..
'<b>Note:</b> When merging or renaming banners, categories, ' ..
'projects or taskforces, you may need to update ' ..
'[[WP:AALERTS/LIST]] for [[WP:AALERTS|Article Alerts]] to work ' ..
'correctly (ask for help at [[WT:AALERTS]]).',
["on_template_page"] = 'If this message appears on the template ' ..
'page itself, please check that the value of the %s parameter ' ..
'is correctly set to <kbd>%s</kbd>.',
["categories"] = '<includeonly>[[Category:WikiProject banners with ' ..
'formatting errors|%s]]</includeonly>'
}
}


-- Make it cleaner to initialize 'trivial' variables.
-- Make it cleaner to initialize 'trivial' variables.
Line 30: Line 62:
local warning = require('Module:Message box').main( 'tmbox', {
local warning = require('Module:Message box').main( 'tmbox', {
image = '[[File:Stop hand nuvola.svg|40px]]',
image = cfg.subst_warning.image,
type = 'content',
type = cfg.subst_warning.type,
text = string.format(cfg.subst_warning.text, project, target_name)
-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
text = '<p>It seems that the <i>WikiProject ' ..
project ..
'</i> banner has been [[Wikipedia:Substitution|substituted]] on ' ..
'this page instead of being [[Wikipedia:Transclusion|transcluded]]. ' ..
'Please undo the edit and type <code>&#123;&#123;' ..
target_name ..
'&#125;&#125;</code> instead.</p>'
})
})


Line 48: Line 73:
-- Is the intent of <includeonly> only to add it to the banner invocation
-- Is the intent of <includeonly> only to add it to the banner invocation
-- and not elsewhere? TODO: Check docs.
-- and not elsewhere? TODO: Check docs.
categories = cfg.subst_warning.categories
categories = '<includeonly>[[Category:WikiProject banners with formatting errors|SUBST]]' ..
'[[Category:Pages with incorrectly substituted templates]]</includeonly>'
end
end
Line 76: Line 100:
local on_template_page
local on_template_page
if current_namespace == 'Template' then
if current_namespace == 'Template' then
on_template_page = string.format(
-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
cfg.namespace_warning.on_template_page,
on_template_page = 'If this message appears on the template page itself, ' ..
para_banner_name,
'please check that the value of the ' ..
current_title.rootText
para_banner_name ..
)
' parameter is correctly set to <kbd>' ..
current_title.rootText ..
'</kbd>.'
else
else
on_template_page = ''
on_template_page = ''
Line 88: Line 110:
local warning = require('Module:Message box').main( 'tmbox', {
local warning = require('Module:Message box').main( 'tmbox', {
image = '[[File:Stop hand nuvola.svg|40px]]',
image = cfg.namespace_warning.image,
type = 'content',
type = cfg.namespace_warning.type,
text = string.format(
text = '<p>The WikiProject banner below should be moved to this ' ..
cfg.namespace_warning.text,
require('Module:Pagetype')._main({}) ..
require('Module:Pagetype')._main({}), -- empty args table for pagetype
'\'s <b>[[' ..
current_title.talkPageTitle.fullText ..
current_title.talkPageTitle.fullText,
para_category,
'|talk page]]</b>.</p><p style="font-size:95%">If this is a ' ..
on_template_page
'demonstration of the template, please set the parameter ' ..
)
para_category ..
' to prevent this page being miscategorised. ' ..
on_template_page ..
'</p><b>Note:</b> When merging or renaming banners, categories, ' ..
'projects or taskforces, you may need to update [[WP:AALERTS/LIST]] ' ..
'for [[WP:AALERTS|Article Alerts]] to work correctly ' ..
'(ask for help at [[WT:AALERTS]]).'
})
})


Line 116: Line 132:
end
end
category = string.format(
category = '<includeonly>[[Category:WikiProject banners with formatting errors|' ..
cfg.namespace_warning.categories,
sortkey ..
sortkey
']]</includeonly>'
)
end
end
Line 158: Line 175:


function p.main(frame)
function p.main(frame)
local args = getArgs(frame)
local args = require ('Module:Arguments').getArgs(frame)
return p._main(args, frame)
return p._main(args, frame)
end
end

Revision as of 20:45, 9 November 2020

--[[
This implements {{WPBannerMeta}}

]]
require ('Module:No globals')

local p = {}

local cfg = {
	["subst_warning"] = {
		["image"] = '[[File:Stop hand nuvola.svg|40px]]',
		["type"] = 'content',
		-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
		["text"] = '<p>It seems that the <i>WikiProject %s</i> banner has ' ..
			'been [[Wikipedia:Substitution|substituted]] on this page ' ..
			'instead of being [[Wikipedia:Transclusion|transcluded]]. ' ..
			'Please undo the edit and type <code>&#123;&#123;%s&#125;&#125;' ..
			'</code> instead.</p>',
		["categories"] = '<includeonly>[[Category:WikiProject banners with formatting errors|SUBST]]' ..
			'[[Category:Pages with incorrectly substituted templates]]</includeonly>'
	},
	["namespace_warning"] = {
		["image"] = '[[File:Stop hand nuvola.svg|40px]]',
		["type"] = 'content',
		-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
		["text"] = '<p>The WikiProject banner below should be moved to this ' ..
			'%s\'s <b>[[%s|talk page]]</b>.</p><p style="font-size:95%%">If ' ..
			'this is a demonstration of the template, please set the ' ..
			'parameter %s to prevent this page being miscategorised. %s</p>' ..
			'<b>Note:</b> When merging or renaming banners, categories, ' ..
			'projects or taskforces, you may need to update ' ..
			'[[WP:AALERTS/LIST]] for [[WP:AALERTS|Article Alerts]] to work ' ..
			'correctly (ask for help at [[WT:AALERTS]]).',
		["on_template_page"] = 'If this message appears on the template ' ..
			'page itself, please check that the value of the %s parameter ' ..
			'is correctly set to <kbd>%s</kbd>.',
		["categories"] = '<includeonly>[[Category:WikiProject banners with ' ..
			'formatting errors|%s]]</includeonly>'
	}
}

-- Make it cleaner to initialize 'trivial' variables.
local function arg_or_default(args, from_arg, default)
	if args[from_arg] and args[from_arg] ~= '' then
		return args[from_arg]
	else
		return default
	end
end

local function warn_on_subst(subst, project, banner_name, frame, suppress_categories)
	if subst ~= 'SUBST' then
		return ''
	end
	
	local target_name
	if banner_name then
		target_name = mw.title.new( banner_name ).text
	else
		target_name = 'WikiProject ' .. project
	end
	
	local warning = require('Module:Message box').main( 'tmbox', {
		image = cfg.subst_warning.image,
		type = cfg.subst_warning.type,
		text = string.format(cfg.subst_warning.text, project, target_name)
	})

	local categories
	if suppress_categories then
		categories = ''
	else
		-- Is the intent of <includeonly> only to add it to the banner invocation
		-- and not elsewhere? TODO: Check docs.
		categories = cfg.subst_warning.categories
	end
	
	return warning .. categories
end

local function warn_on_namespace(args, frame, suppress_categories)
	local current_title = mw.title.getCurrentTitle()
	local current_namespace = current_title.nsText
	
	-- I don't really understand why this is the guard...
	-- TODO: More i18n to compare the namespace number...
	if current_namespace == 'User' then
		return ''
	end
	
	local para_category = frame:expandTemplate{
		title = 'para',
		args = {'category', 'no'}
	}
	local para_banner_name = frame:expandTemplate{
		title = 'para',
		args = {'BANNER_NAME'}
	}

	local on_template_page
	if current_namespace == 'Template' then
		on_template_page = string.format(
			cfg.namespace_warning.on_template_page,
			para_banner_name,
			current_title.rootText
		)
	else
		on_template_page = ''
	end
	
	local warning = require('Module:Message box').main( 'tmbox', {
		image = cfg.namespace_warning.image,
		type = cfg.namespace_warning.type,
		text = string.format(
			cfg.namespace_warning.text,
			require('Module:Pagetype')._main({}), -- empty args table for pagetype
			current_title.talkPageTitle.fullText,
			para_category,
			on_template_page
		)
	})

	local category
	if suppress_categories then
		category = ''
	else
		local sortkey
		if current_namespace == 'Template' then
			sortkey = "PARAMETER"
		else
			sortkey = "LOCATION"
		end
		
		category = string.format(
			cfg.namespace_warning.categories,
			sortkey
		)
	end
		
	return warning .. category
	
end

function p._main(args, frame)
	
	-- Initialize sufficient stuff to do warnings.
	local subst = arg_or_default(args, 'substcheck', '¬')
	local project = arg_or_default(args, 'PROJECT', nil)
	if not project then error('PROJECT must be defined') end
	local banner_name = arg_or_default(args, 'BANNER_NAME', nil)
	-- TODO: When done, remove suppress_categories (or convert).
	local category = arg_or_default(args, 'category', '¬')
	local suppress_categories = arg_or_default(args, 'suppress', nil)
	
	local subst_warning = warn_on_subst(subst, project, banner_name, frame,
		suppress_categories)
	local namespace_warning = warn_on_namespace(args, frame, suppress_categories)

	local project_link = arg_or_default(args, 'PROJECT_LINK', nil)
	
	local builder = mw.html.create('table')
	builder:addClass('tmbox'):addClass('tmbox-notice'):addClass('mw-collapsible')
		:addClass('innercollapse'):addClass('wpb')
		:css('height', '0')
		:attr('role', 'presentation')
		:tag('tr'):addClass('wpb-header')
			:tag('td')
			:cssText('text-align: right; padding: 0.3em 1em 0.3em 0.3em;'
				.. 'width: 50%; font-weight: bold;')
	
	return subst_warning ..
		namespace_warning ..
		tostring(builder)
end

function p.main(frame)
	local args = require ('Module:Arguments').getArgs(frame)
	return p._main(args, frame)
end

return p