Module:Protection banner: Difference between revisions
Content added Content deleted
Enwikipedia>Mr. Stradivarius (more efficient p.matchNamespace logic) |
Enwikipedia>Mr. Stradivarius (save progress in simplifying the attempt order algorithm) |
||
Line 59: | Line 59: | ||
} |
} |
||
local |
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} |
|||
--[[ |
|||
local behavior = behaviors[reason] |
|||
-- Define the key table and the order to test it in. |
|||
⚫ | |||
-- 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 |
|||
⚫ | |||
-- 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 |
|||
⚫ | |||
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 |
|||
} |
|||
⚫ | |||
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 |
for i = 1, 2^noActiveProperties do |
||
local key = {} |
local key = {} |
||
for |
for pos = 1, 5 do |
||
if |
if pos > noActiveProperties then |
||
key[ |
key[pos] = 'all' |
||
else |
else |
||
local quotient = i / 2 ^ (pos - 1) |
|||
⚫ | |||
quotient = floor(quotient) |
|||
if quotient % 2 == 1 then |
|||
key[pos] = protectionProperties[j] |
|||
else -- quotient % 2 == 0 |
|||
⚫ | |||
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 |