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 self.expiry == 'indef' then | |||
expiryFragment = self.expiry | |||
elseif type(self.expiry) == 'number' then | |||
expiryFragment = 'temp' | |||
end | end | ||
-- Get the namespace | -- Get the namespace key fragment. | ||
local | local namespaceFragment | ||
do | do | ||
namespaceFragment = cfg.categoryNamespaceKeys[title.namespace] | |||
if not namespaceFragment and title.namespace % 2 == 1 then | |||
namespaceFragment = 'talk' | |||
if not | |||
end | end | ||
end | end | ||
-- Define the order that key fragments are tested in. This is done with an | |||
-- Define the | -- array of tables containing the value to be tested, along with its | ||
-- | -- position in the cfg.protectionCategories table. | ||
-- | local order = { | ||
{val = expiryFragment, keypos = 1}, | |||
local | {val = namespaceFragment, keypos = 2}, | ||
{val = self.reason, keypos = 3}, | |||
{val = self.level, keypos = 4}, | |||
{val = self.action, keypos = 5} | |||
} | } | ||
-- | -- To generate the correct category for some reason values, we need to | ||
-- prioritise the position of the namespace key fragment over that of the | |||
-- | -- reason key fragment. For these reasn values, swap the namespace subtable | ||
-- and the reason subtable around. | |||
-- | if self.reason and cfg.reasonsWithNamespacePriority[self.reason] then | ||
-- | table.insert(order, 3, table.remove(order, 2)) | ||
end | end | ||
--[[ | --[[ | ||
-- Define the attempt order. | -- Define the attempt order. Inactive subtables (subtables with nil "value" | ||
-- to the end, where they will later be given the | -- fields) are moved to the end, where they will later be given the key | ||
-- "all". This is to cut down on the number of table lookups in | |||
-- grows exponentially with the number of | -- cfg.protectionCategories, which grows exponentially with the number of | ||
-- We keep track of the number of active | -- 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( | 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 | ||
-- | -- specific category exists for the combination of key fragments we are | ||
-- | -- given, that match will be found first. If not, we keep trying different | ||
-- | -- 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 | -- To generate the keys, we index the key subtables using a binary matrix | ||
-- | -- with indexes i and j. j is only calculated up to the number of active | ||
-- | -- subtables. For example, if there were three active subtables, the matrix | ||
-- | -- would look like this, with 0 corresponding to the key fragment "all", and | ||
-- 1 corresponding to other key fragments. | |||
-- | |||
-- | -- | ||
-- 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 | -- Values of j higher than the number of active subtables are set | ||
-- to the string "all". | -- to the string "all". | ||
-- | -- | ||
-- A key for | -- A key for cfg.protectionCategories is constructed for each value of i. | ||
-- The | -- The position of the value in the key is determined by the keypos field in | ||
-- | -- 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. | 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. | key[t.keypos] = t.val | ||
else | else | ||
key[t. | key[t.keypos] = 'all' | ||
end | end | ||
end | end |