Jump to content

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: Line 59:
}
}


local reasonBehaviors = {
local behaviors = {
vandalism = 'namespaceFirst',
vandalism = 'namespaceFirst',
dispute = 'reasonFirst',
dispute = 'reasonFirst',
Line 83: Line 83:
-- Define often-used functions as local variables.
-- Define often-used functions as local variables.
local tconcat = table.concat
local tconcat = table.concat
local floor = math.floor


local p = {}
local p = {}
Line 119: Line 120:
expiry = 'temp'
expiry = 'temp'
end
end
 
--[[
local properties = {expiry, namespace, protLevel, protType, reason}
-- Define the key table and the order to test it in.
local behavior = behaviors[reason]
-- The idiosyncracies of the old template system have been distilled down to
if behavior == 'namespaceFirst' then
-- two basic behaviors - templates that prioritise matching the reason over
tinsert(properties, table.remove(properties, 2)) -- move namespace to the end
-- matching the namespace, and templates that prioritise matching the
elseif behavior ~= 'reasonFirst' and reason ~= 'all' then
-- 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')
error(reason .. ' is not a valid reason')
end
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
-- Try successively generic matches until we run out of key combinations
for i, t in ipairs(attemptOrder) do
for i = 1, 2^noActiveProperties do
local key = {}
local key = {}
for j, useProperty in ipairs(t) do
for pos = 1, 5 do
if useProperty then
if pos > noActiveProperties then
key[j] = protectionProperties[j]
key[pos] = 'all'
else
else
key[j] = 'all'
local quotient = i / 2 ^ (pos - 1)
quotient = floor(quotient)
if quotient % 2 == 1 then
key[pos] = protectionProperties[j]
else -- quotient % 2 == 0
key[pos] = 'all'
end
end
end
end
end
Line 199: Line 161:
end
end
error('No category match found; please define the category for key "all-all-all-all-all"')
error('No category match found; please define the category for key "all-all-all-all-all"')
end
end


return p
return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.