Module:Protection banner: Difference between revisions

change this to something that should work if I can get the order figured out
en>Mr. Stradivarius
(save progress in simplifying the attempt order algorithm)
en>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
protType = protType or 'all'
local properties = {}
protLevel = protLevel or 'all'
properties.protType = protType or 'all'
namespace = p.matchNamespace(namespace)
properties.protLevel = protLevel or 'all'
reason = reason or 'all'
properties.namespace = p.matchNamespace(namespace)
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 properties = {expiry, namespace, protLevel, protType, reason}
local order = {'expiry', 'namespace', 'protLevel', 'protType', 'reason'}  
local behavior = behaviors[reason]
local behavior = behaviors[reason]
if behavior == 'namespaceFirst' then
if behavior == 'namespaceFirst' then
tinsert(properties, table.remove(properties, 2)) -- move namespace to the end
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 activeProperties = {}
local activePropertyKeys = {}
for i, property in ipairs(properties) do
for i, propertyKey in ipairs(order) do
if property ~= 'all' then
if properties[propertyKey] ~= 'all' then
activeProperties[#activeProperties + 1] = property
activePropertyKeys[#activePropertyKeys + 1] = propertyKey
end
end
end
end
local noActiveProperties = #activeProperties
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 key = {}
local categoryKey = {}
for pos = 1, 5 do
for pos = 1, 5 do
if pos > noActiveProperties then
if pos > noActiveProperties then
key[pos] = 'all'
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
key[pos] = protectionProperties[j]
categoryKey[pos] = properties[activePropertyKeys[pos]]
else -- quotient % 2 == 0
else -- quotient % 2 == 0
key[pos] = 'all'
categoryKey[pos] = 'all'
end
end
end
end
end
end
key = tconcat(key, '-')
categoryKey = tconcat(categoryKey, '-')
mw.log(key) -- for debugging
mw.log(categoryKey) -- for debugging
local attempt = cats[key]
local attempt = cats[categoryKey]
if attempt then
if attempt then
return attempt
return attempt
Anonymous user