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