Open main menu

Changes

Module:Protection banner

630 bytes removed, 03:53, 29 June 2014
simplify Protection:makeProtectionCategory and update the comments
end
-- Get the expirykey fragment. local expiryFragment if self.expiry == 'indef' then expiryFragment = self.expiry if elseif type(self.expiry) == 'number' then expiry expiryFragment = 'temp' elseif expiry ~= 'indef' then expiry = nil
end
-- Get the namespace category keyfragment. local nskeynamespaceFragment
do
local namespace = title.namespace local categoryNamespaces namespaceFragment = cfg.categoryNamespaceKeys nskey = categoryNamespaces[title.namespace] if not nskey namespaceFragment and title.namespace % 2 == 1 then nskey namespaceFragment = 'talk'
end
end
 
-- Get the other inputs.
local reason = self.reason
local action = self.action
local level = self.level
--[[ -- Define the properties tableorder that key fragments are tested in. Each property This is a table containing thedone with an -- canonical order that array of tables containing the property is value to be tested in, the position thealong with its -- property has position in the category key strings, and the property value itselfcfg.protectionCategories table. --]] local properties order = { expiry = {order val = 1expiryFragment, val keypos = expiry1}, namespace = {order val = 2namespaceFragment, val keypos = nskey2}, reason = {order val = 3self.reason, val keypos = reason3}, level = {order val = 4self.level, val keypos = level4}, action = {order val = 5self.action, val keypos = action5}
}
--[[ -- Apply To generate the correct category order configurationfor some reason values, if any. The configuration valuewe need to -- will be a property string, e.g. 'reason', 'prioritise the position of the namespace', etc. The property -- corresponding to key fragment over that string is tested last (i.e. it is of the most -- importantreason key fragment. For these reasn values, because it keeps its specified value the longest) and swap thenamespace subtable -- other properties are tested in and the canonical orderreason subtable around. If no configuration -- value is specified then the canonical order is usedif self. --]] local configOrder = {} do local reasonsWithNamespacePriority = reason and cfg.reasonsWithNamespacePriority local namespaceFirst = reason and reasonsWithNamespacePriority[self.reason] or falsethen for propertiesKey, t in pairstable.insert(properties) do configOrder[t.order] = t end if namespaceFirst then -- Swap namespace and reason around. local namespaceTable = , 3, table.remove(configOrderorder, 2) table.insert(configOrder, 3, namespaceTable) end
end
--[[
-- Define the attempt order. Properties Inactive subtables (subtables with no nil "value defined are moved" -- fields) are moved to the end, where they will later be given the value key -- "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 properties subtables 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
--[[
-- 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 keeptrying different -- trying different key fragment combinations until we match using the key
-- "all-all-all-all-all".
--
-- To generate the keys, we index the property key subtables using abinary matrix -- binary matrix with indexes i and j. j is only calculated up tothe number of active -- the number of active propertiessubtables. For example, if there were threeactive subtables, the matrix -- active properties, the matrix would look like this, with 0 -- corresponding to the string key fragment "all", and 1 corresponding to the -- val field in the property table:1 corresponding to other key fragments.
--
-- j 1 2 3
-- 8 0 0 0
--
-- Values of j higher than the number of active properties subtables are set
-- to the string "all".
--
-- A key for the category table cfg.protectionCategories is constructed for each value of i. -- The correct position of the value in the key is determined by thekeypos field in -- pos field in the property tableeach subtable.
--]]
local cats = cfg.protectionCategories
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
Anonymous user