Module:Protection banner: Difference between revisions

tidy this up and comment it so it will be easier for others to understand
(fix logic for reasonBehavior == 'all' and fix the office protection category key)
(tidy this up and comment it so it will be easier for others to understand)
Line 3:
 
local categories = {
-- The key strings follow this format:
-- type, level, ns, reason, expiry
['edit-autoconfirmed-user-all-all'] = 'Wikipedia semi-protected user and user talk pages',
['edit-autoconfirmed-project-all-all'] = 'Semi-protected project pages',
Line 57 ⟶ 59:
}
 
local reasonBehaviors = {
vandalism = 'namespaceFirst',
dispute = 'reasonFirst',
blp = 'reasonFirst',
sock = 'reasonFirst',
office = 'reasonFirst',
 
--[[
-- Not currently used
local error_categories = {
incorrect = 'Wikipedia pages with incorrect protection templates',
Line 63 ⟶ 75:
template = 'Wikipedia template-protected pages other than templates and modules'
}
--]]
 
local template_types = {
vandalism = {
namespaces = false, -- defaults to all namespaces
editLevels = false, -- all edit protection levels
moveLevels = false, -- all move protection levels
createLevels = false, -- all create protection levels
header = false,
reason = 'due to [[Wikipedia:Vandalism|vandalism]]',
icon_reason = 'due to vandalism',
}
 
--------------------------------------------------------------------------------
Line 80 ⟶ 81:
--------------------------------------------------------------------------------
 
-- Define often-used functions as local variables.
local getArgs = require('Module:Arguments').getArgs
local yesnotconcat = require('Module:Yesno')table.concat
 
local p = {}
 
function p.mainmatchNamespace(framens)
-- Matches a namespace number to a string that can be passed to the
-- Get data
-- namespacep parameter of p.getCategoryName.
local args = getArgs(frame)
local title = p.getTitle(args.page)
local protType, protLevel = p.getProtectionStatus(title)
local namespace = p.getNamespace(title)
local expiry = p.getExpiry(args.expiry)
local reason = p.getReason(args.reason)
 
-- Sort categories and build a category link with the best match
categories = p.sortCategories(categories, protType, protLevel, namespace, expiry, reason)
local category = categories[1].name
category = string.format('[[Category:%s]]', category)
 
-- Find whether we are small or not and output a padlock or a banner as appropriate
local isSmall = yesno(args.small, true)
if isSmall then
return p.exportPadlock()
else
return p.exportBanner()
end
end
 
function p.parseNamespace(ns)
-- The ns variable is the namespace number.
if not ns or type(ns) ~= 'number' then
return 'all'
Line 123 ⟶ 102:
 
function p.getCategoryName(cats, protType, protLevel, namespace, reason, expiry)
-- Gets a category name from the category table, given a combination of
-- the protection type, the protection level, the namespace number, the
-- reason for protection, and the expiry date.
-- Preprocess parameters
cats = cats or categories
protType = protType or 'all'
protLevel = protLevel or 'all'
namespace = p.parseNamespacematchNamespace(namespace)
reason = reason or 'all'
if not expiry then
Line 135 ⟶ 118:
end
--[[
-- Define the key table and the order to test it in.
-- The idiosyncracies of the old template system have been distilled down to
-- This part is a little like playing regex golf - we have to make up a new
-- ruletwo forbasic everybehaviors idiosyncracy- templates that wasprioritise inmatching the old templatereason system.over
-- matching the namespace, and templates that prioritise matching the
local keyTable = {protType, protLevel, namespace, reason, expiry}
-- namespace over matching the reason. In each case the function tries to
-- match specific keys, and if there is no match, it proceeds to match
local reasonBehaviors = {
-- increasingly generic keys, until eventually it will match the key
vandalism = 'namespaceFirst',
-- "all-all-all-all-all".
dispute = 'reasonFirst',
--]]
blp = 'reasonFirst',
local keyTableprotectionProperties = {protType, protLevel, namespace, reason, expiry}
sock = 'reasonFirst',
office = 'reasonFirst',
}
 
local reasonBehavior = reasonBehaviors[reason]
local attemptOrder
Line 153 ⟶ 134:
attemptOrder = {
-- type, level, ns, reason, expiry
-- true corresponds to the original protectionProperties key,
-- and false corresponds to the key "all".
{true, true, true, true, true}, -- 1
{true, true, true, true, false}, -- 2
Line 199 ⟶ 182:
for i, t in ipairs(attemptOrder) do
local key = {}
for j, useKeyTableuseProperty in ipairs(t) do
if useKeyTableuseProperty then
key[j] = keyTableprotectionProperties[j]
else
key[j] = 'all'
end
end
key = table.concattconcat(key, '-')
mw.log(key) -- for debugging
local attempt = cats[key]