Jump to content

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: Line 83:
-- Define often-used functions as local variables.
-- Define often-used functions as local variables.
local tconcat = table.concat
local tconcat = table.concat
local tinsert = table.insert
local tremove = table.remove
local ceil = math.ceil
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 = {}
local p = {}
Line 109: Line 101:


function p.getCategoryName(cats, protType, protLevel, namespace, reason, expiry)
function p.getCategoryName(cats, protType, protLevel, namespace, reason, expiry)
--[[
-- Gets a category name from the category table, given a combination of
-- Gets a category name from the category table, given a combination of
-- the protection type, the protection level, the namespace number, the
-- the protection type, the protection level, the namespace number, the
-- reason for protection, and the expiry date.
-- reason for protection, and the expiry date.
--]]
cats = cats or categories
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 = {
local properties = {
{pos = 5, val = expiry},
{pos = 5, val = expiry},
Line 122: Line 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'
local behavior = 'reasonFirst'
if reason then
if reason then
behavior = behaviors[reason]
behavior = behaviors[reason]
if behavior == 'namespaceFirst' then
if behavior == 'namespaceFirst' then
toTableEnd(properties, 2) -- move namespace to the end
table.insert(properties, table.remove(properties, 2))
elseif behavior ~= 'reasonFirst' then
elseif behavior ~= 'reasonFirst' then
error(reason .. ' is not a valid reason')
error(reason .. ' is not a valid reason')
Line 132: Line 138:
end
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 = {}, {}
local active, inactive = {}, {}
for i, t in ipairs(properties) do
for i, t in ipairs(properties) do
Line 141: Line 155:
end
end
local noActive = #active
local noActive = #active
local attemptOrder = active
local attemptOrder = active
for i, t in ipairs(inactive) do
for i, t in ipairs(inactive) do
Line 147: Line 160:
end
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
for i = 1, 2^noActive do
local key = {}
local key = {}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.