Module:Protection banner: Difference between revisions

simplify Protection:makeProtectionCategory and update the comments
(rm unnecessary do/end blocks)
(simplify Protection:makeProtectionCategory and update the comments)
Line 150: Line 150:
end
end
-- Get the expiry.
-- Get the expiry key fragment.
local expiry = self.expiry
local expiryFragment
if type(expiry) == 'number' then
if self.expiry == 'indef' then
expiry = 'temp'
expiryFragment = self.expiry
elseif expiry ~= 'indef' then
elseif type(self.expiry) == 'number' then
expiry = nil
expiryFragment = 'temp'
end
end


-- Get the namespace category key.
-- Get the namespace key fragment.
local nskey
local namespaceFragment
do
do
local namespace = title.namespace
namespaceFragment = cfg.categoryNamespaceKeys[title.namespace]
local categoryNamespaces = cfg.categoryNamespaceKeys
if not namespaceFragment and title.namespace % 2 == 1 then
nskey = categoryNamespaces[namespace]
namespaceFragment = 'talk'
if not nskey and namespace % 2 == 1 then
nskey = 'talk'
end
end
end
end
-- Get the other inputs.
local reason = self.reason
local action = self.action
local level = self.level
   
   
--[[
-- Define the order that key fragments are tested in. This is done with an
-- Define the properties table. Each property is a table containing the
-- array of tables containing the value to be tested, along with its
-- canonical order that the property is tested in, the position the
-- position in the cfg.protectionCategories table.
-- property has in the category key strings, and the property value itself.
local order = {
--]]
{val = expiryFragment,   keypos = 1},
local properties = {
{val = namespaceFragment, keypos = 2},
expiry    = {order = 1, val = expiry},
{val = self.reason,       keypos = 3},
namespace = {order = 2, val = nskey},
{val = self.level,       keypos = 4},
reason    = {order = 3, val = reason},
{val = self.action,       keypos = 5}
level    = {order = 4, val = level},
action    = {order = 5, val = action}
}
}
 
--[[
-- To generate the correct category for some reason values, we need to
-- Apply the category order configuration, if any. The configuration value
-- prioritise the position of the namespace key fragment over that of the
-- will be a property string, e.g. 'reason', 'namespace', etc. The property
-- reason key fragment. For these reasn values, swap the namespace subtable
-- corresponding to that string is tested last (i.e. it is the most
-- and the reason subtable around.
-- important, because it keeps its specified value the longest) and the
if self.reason and cfg.reasonsWithNamespacePriority[self.reason] then
-- other properties are tested in the canonical order. If no configuration
table.insert(order, 3, table.remove(order, 2))
-- value is specified then the canonical order is used.
--]]
local configOrder = {}
do
local reasonsWithNamespacePriority = cfg.reasonsWithNamespacePriority
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
for propertiesKey, t in pairs(properties) do
configOrder[t.order] = t
end
if namespaceFirst then
-- Swap namespace and reason around.
local namespaceTable = table.remove(configOrder, 2)
table.insert(configOrder, 3, namespaceTable)
end
end
end
   
   
--[[
--[[
-- Define the attempt order. Properties with no value defined are moved
-- Define the attempt order. Inactive subtables (subtables with nil "value"
-- to the end, where they will later be given the value "all". This is
-- fields) are moved to the end, where they will later be given the key
-- to cut down on the number of table lookups in the cats table, which
-- "all". This is to cut down on the number of table lookups in
-- grows exponentially with the number of properties with valid values.
-- cfg.protectionCategories, which grows exponentially with the number of
-- We keep track of the number of active properties with the noActive
-- non-nil keys. We keep track of the number of active subtables with the
-- parameter.
-- noActive parameter.
--]]
--]]
local noActive, attemptOrder
local noActive, attemptOrder
do
do
local active, inactive = {}, {}
local active, inactive = {}, {}
for i, t in ipairs(configOrder) do
for i, t in ipairs(order) do
if t.val then
if t.val then
active[#active + 1] = t
active[#active + 1] = t
Line 235: Line 212:
   
   
--[[
--[[
-- Check increasingly generic key combinations until we find a match.
-- Check increasingly generic key combinations until we find a match. If a
-- If a specific category exists for the combination of properties
-- specific category exists for the combination of key fragments we are
-- we are given, that match will be found first. If not, we keep
-- given, that match will be found first. If not, we keep trying different
-- trying different key combinations until we match using the key
-- key fragment combinations until we match using the key
-- "all-all-all-all-all".
-- "all-all-all-all-all".
--
--
-- To generate the keys, we index the property subtables using a
-- To generate the keys, we index the key subtables using a binary matrix
-- binary matrix with indexes i and j. j is only calculated up to
-- with indexes i and j. j is only calculated up to the number of active
-- the number of active properties. For example, if there were three
-- subtables. For example, if there were three active subtables, the matrix
-- active properties, the matrix would look like this, with 0
-- would look like this, with 0 corresponding to the key fragment "all", and
-- corresponding to the string "all", and 1 corresponding to the
-- 1 corresponding to other key fragments.
-- val field in the property table:
--  
--  
--  j 1  2  3
--  j 1  2  3
Line 259: Line 235:
-- 8  0  0  0
-- 8  0  0  0
--  
--  
-- Values of j higher than the number of active properties are set
-- Values of j higher than the number of active subtables are set
-- to the string "all".
-- to the string "all".
--
--
-- A key for the category table is constructed for each value of i.
-- A key for cfg.protectionCategories is constructed for each value of i.
-- The correct position of the value in the key is determined by the
-- The position of the value in the key is determined by the keypos field in
-- pos field in the property table.
-- each subtable.
--]]
--]]
local cats = cfg.protectionCategories
local cats = cfg.protectionCategories
Line 271: Line 247:
for j, t in ipairs(attemptOrder) do
for j, t in ipairs(attemptOrder) do
if j > noActive then
if j > noActive then
key[t.order] = 'all'
key[t.keypos] = 'all'
else
else
local quotient = i / 2 ^ (j - 1)
local quotient = i / 2 ^ (j - 1)
quotient = math.ceil(quotient)
quotient = math.ceil(quotient)
if quotient % 2 == 1 then
if quotient % 2 == 1 then
key[t.order] = t.val
key[t.keypos] = t.val
else
else
key[t.order] = 'all'
key[t.keypos] = 'all'
end
end
end
end