Module:Protection banner: Difference between revisions
Content added Content deleted
Enwikipedia>Mr. Stradivarius (save progress in simplifying the attempt order algorithm) |
Enwikipedia>Mr. Stradivarius (change this to something that should work if I can get the order figured out) |
||
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 tinsert = table.insert |
|||
local floor = math.floor |
local floor = math.floor |
||
Line 111: | Line 112: | ||
-- Preprocess parameters |
-- Preprocess parameters |
||
cats = cats or categories |
cats = cats or categories |
||
local properties = {} |
|||
protType = protType or 'all' |
|||
properties.protType = protType or 'all' |
|||
properties.protLevel = protLevel or 'all' |
|||
namespace = p.matchNamespace(namespace) |
properties.namespace = p.matchNamespace(namespace) |
||
reason = reason or 'all' |
properties.reason = reason or 'all' |
||
if not expiry then |
if not expiry then |
||
expiry = 'all' |
properties.expiry = 'all' |
||
elseif expiry ~= 'indef' then |
elseif expiry ~= 'indef' then |
||
expiry = 'temp' |
properties.expiry = 'temp' |
||
end |
end |
||
local |
local order = {'expiry', 'namespace', 'protLevel', 'protType', 'reason'} |
||
local behavior = behaviors[reason] |
local behavior = behaviors[reason] |
||
if behavior == 'namespaceFirst' then |
if behavior == 'namespaceFirst' then |
||
tinsert( |
tinsert(order, table.remove(order, 2)) -- move namespace to the end |
||
elseif behavior ~= 'reasonFirst' and reason ~= 'all' then |
elseif behavior ~= 'reasonFirst' and reason ~= 'all' then |
||
error(reason .. ' is not a valid reason') |
error(reason .. ' is not a valid reason') |
||
end |
end |
||
local |
local activePropertyKeys = {} |
||
for i, |
for i, propertyKey in ipairs(order) do |
||
if |
if properties[propertyKey] ~= 'all' then |
||
activePropertyKeys[#activePropertyKeys + 1] = propertyKey |
|||
end |
end |
||
end |
end |
||
local noActiveProperties = # |
local noActiveProperties = #activePropertyKeys |
||
-- Try successively generic matches until we run out of key combinations |
-- Try successively generic matches until we run out of key combinations |
||
for i = 1, 2^noActiveProperties do |
for i = 1, 2^noActiveProperties do |
||
local |
local categoryKey = {} |
||
for pos = 1, 5 do |
for pos = 1, 5 do |
||
if pos > noActiveProperties then |
if pos > noActiveProperties then |
||
categoryKey[pos] = 'all' |
|||
else |
else |
||
local quotient = i / 2 ^ (pos - 1) |
local quotient = i / 2 ^ (pos - 1) |
||
quotient = floor(quotient) |
quotient = floor(quotient) |
||
if quotient % 2 == 1 then |
if quotient % 2 == 1 then |
||
categoryKey[pos] = properties[activePropertyKeys[pos]] |
|||
else -- quotient % 2 == 0 |
else -- quotient % 2 == 0 |
||
categoryKey[pos] = 'all' |
|||
end |
end |
||
end |
end |
||
end |
end |
||
categoryKey = tconcat(categoryKey, '-') |
|||
mw.log( |
mw.log(categoryKey) -- for debugging |
||
local attempt = cats[ |
local attempt = cats[categoryKey] |
||
if attempt then |
if attempt then |
||
return attempt |
return attempt |