Module:Protection banner: Difference between revisions

add some lengthy comments explaining what's going on
(use p.matchNamespace)
(add some lengthy comments explaining what's going on)
Line 83:
-- Define often-used functions as local variables.
local tconcat = table.concat
local tinsert = table.insert
local tremove = table.remove
local ceil = math.ceil
 
local function toTableEnd(t, pos)
-- Sends the item at position pos to the end of array t,
-- and shifts all the other array items down accordingly.
tinsert(t, tremove(t, pos))
end
 
local p = {}
Line 109 ⟶ 101:
 
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.
--]]
cats = cats or categories
 
--[[
-- Define the initial order to test properties in. The subtable position
-- is the order the properties will be tested in, and the pos value in
-- each subtable is the position of the value in the category key.
--]]
local properties = {
{pos = 5, val = expiry},
Line 122 ⟶ 121:
}
 
--[[
-- Validate reason, and if it is specified as a "namespaceFirst" reason,
-- move the namespace subtable to the end of the properties table.
-- This is necessary to accommodate reasons like "vandalism", as the old
-- {{pp-vandalism}} template used namespace categories rather than
-- vandalism categories if they were available.
--]]
local behavior = 'reasonFirst'
if reason then
behavior = behaviors[reason]
if behavior == 'namespaceFirst' then
toTableEndtable.insert(properties, table.remove(properties, 2) -- move namespace to the end)
elseif behavior ~= 'reasonFirst' then
error(reason .. ' is not a valid reason')
Line 132 ⟶ 138:
end
 
--[[
-- Define the attempt order. Properties with no value defined are moved
-- to the end, where they will later be given the value "all". This is
-- to cut down on the number of table lookups in the cats table, which
-- grows exponentially with the number of properties with valid values.
-- We keep track of the number of active properties with the noActive
-- parameter.
--]]
local active, inactive = {}, {}
for i, t in ipairs(properties) do
Line 141 ⟶ 155:
end
local noActive = #active
 
local attemptOrder = active
for i, t in ipairs(inactive) do
Line 147 ⟶ 160:
end
 
--[[
-- Check increasingly generic key combinations until we find a match.
-- If a specific category exists for the combination of properties
-- we are given, that match will be found first. If not, we keep
-- trying different key combinations until we match using the key
-- "all-all-all-all-all".
--
-- To generate the keys, we index the property subtables using a
-- binary matrix with indexes i and j. j is only calculated up to
-- the number of active properties. For example, if there were three
-- active properties, the matrix would look like this, with 0
-- corresponding to the string "all", and 1 corresponding to the
-- val field in the property table:
--
-- j 1 2 3
-- i
-- 1 1 1 1
-- 2 0 1 1
-- 3 1 0 1
-- 4 0 0 1
-- 5 1 1 0
-- 6 0 1 0
-- 7 1 0 0
-- 8 0 0 0
--
-- Values of j higher than the number of active properties are set
-- to the string "all".
--
-- A key for the category table is constructed for each value of i.
-- The correct position of the value in the key is determined by the
-- pos field in the property table.
--]]
for i = 1, 2^noActive do
local key = {}