Module:Protection banner: Difference between revisions

save progress in simplifying the attempt order algorithm
(more efficient p.matchNamespace logic)
(save progress in simplifying the attempt order algorithm)
Line 59:
}
 
local reasonBehaviorsbehaviors = {
vandalism = 'namespaceFirst',
dispute = 'reasonFirst',
Line 83:
-- Define often-used functions as local variables.
local tconcat = table.concat
local floor = math.floor
 
local p = {}
Line 119 ⟶ 120:
expiry = 'temp'
end
 
local properties = {expiry, namespace, protLevel, protType, reason}
--[[
local behavior = behaviors[reason]
-- Define the key table and the order to test it in.
if reasonBehaviorbehavior == 'namespaceFirst' then
-- The idiosyncracies of the old template system have been distilled down to
tinsert(properties, table.remove(properties, 2)) -- move namespace to the end
-- two basic behaviors - templates that prioritise matching the reason over
elseif reasonBehaviorbehavior =~= 'reasonFirst' orand reason =~= 'all' then
-- matching the namespace, and templates that prioritise matching the
-- namespace over matching the reason. In each case the function tries to
-- match specific keys, and if there is no match, it proceeds to match
-- increasingly generic keys, until eventually it will match the key
-- "all-all-all-all-all".
--]]
local protectionProperties = {protType, protLevel, namespace, reason, expiry}
local reasonBehavior = reasonBehaviors[reason]
local attemptOrder
if reasonBehavior == 'namespaceFirst' then
attemptOrder = {
-- type, level, ns, reason, expiry
-- true corresponds to the original protectionProperties key,
-- and false corresponds to the key "all".
{true, true, true, true, true}, -- 1
{true, true, true, true, false}, -- 2
{true, true, true, false, true}, -- 3
{true, true, true, false, false}, -- 4
{true, false, true, true, true}, -- 5
{true, false, true, true, false}, -- 6
{true, false, true, false, true}, -- 7
{true, false, true, false, false}, -- 8
{false, false, true, false, false}, -- 9
{true, true, false, true, true}, -- 10
{true, true, false, true, false}, -- 11
{true, true, false, false, true}, -- 12
{true, false, false, true, true}, -- 13
{true, false, false, true, false}, -- 14
{true, false, false, false, true}, -- 15
{true, false, false, false, false}, -- 16
{false, false, false, false, false} -- 17
}
elseif reasonBehavior == 'reasonFirst' or reason == 'all' then
attemptOrder = {
-- type, level, ns, reason, expiry
{true, true, true, true, true}, -- 1
{true, true, true, true, false}, -- 2
{true, true, false, true, true}, -- 3
{true, true, false, true, false}, -- 4
{true, false, true, true, true}, -- 5
{true, false, true, true, false}, -- 6
{true, false, false, true, true}, -- 7
{true, false, false, true, false}, -- 8
{false, false, false, true, false}, -- 9
{true, true, true, false, true}, -- 10
{true, true, true, false, false}, -- 11
{true, true, false, false, true}, -- 12
{true, false, true, false, true}, -- 13
{true, false, true, false, false}, -- 14
{true, false, false, false, true}, -- 15
{true, false, false, false, false}, -- 16
{false, false, false, false, false} -- 17
}
else
error(reason .. ' is not a valid reason')
end
 
local activeProperties = {}
for i, property in ipairs(properties) do
if property ~= 'all' then
activeProperties[#activeProperties + 1] = property
end
end
local noActiveProperties = #activeProperties
-- Try successively generic matches until we run out of key combinations
for i, t= in1, ipairs(attemptOrder)2^noActiveProperties do
local key = {}
for j,pos useProperty= in1, ipairs(t)5 do
if usePropertypos > noActiveProperties then
key[jpos] = protectionProperties[j]'all'
else
local quotient = i / 2 ^ (pos - 1)
key[j] = 'all'
quotient = floor(quotient)
if quotient % 2 == 1 then
key[pos] = protectionProperties[j]
else -- quotient % 2 == 0
key[jpos] = 'all'
end
end
end
Line 199 ⟶ 161:
end
error('No category match found; please define the category for key "all-all-all-all-all"')
end
 
return p