Module:Protection banner: Difference between revisions
key components are in the correct order now - property order still a long way off, though
(change this to something that should work if I can get the order figured out) |
(key components are in the correct order now - property order still a long way off, though) |
||
Line 84: | Line 84: | ||
local tconcat = table.concat | local tconcat = table.concat | ||
local tinsert = table.insert | local tinsert = table.insert | ||
local tremove = table.remove | |||
local floor = math.floor | local floor = math.floor | ||
local function toTableEnd(t, pos) | |||
-- Sends the item at position pos to the end of array t, | |||
-- and shifts all the other array items down accordingly. | |||
tinsert(t, tremove(t, pos)) | |||
end | |||
local p = {} | local p = {} | ||
Line 92: | Line 99: | ||
-- namespace parameter of p.getCategoryName. | -- namespace parameter of p.getCategoryName. | ||
if not ns or type(ns) ~= 'number' then | if not ns or type(ns) ~= 'number' then | ||
return | return nil | ||
end | end | ||
local nskey = nskeys[ns] | local nskey = nskeys[ns] | ||
if not nskey | if not nskey and ns % 2 == 1 then | ||
nskey = 'talk' | nskey = 'talk' | ||
end | end | ||
return nskey | return nskey | ||
Line 109: | Line 112: | ||
-- the protection type, the protection level, the namespace number, the | -- the protection type, the protection level, the namespace number, the | ||
-- reason for protection, and the expiry date. | -- reason for protection, and the expiry date. | ||
cats = cats or categories | cats = cats or categories | ||
local properties = {} | |||
local properties = { | |||
{pos = 5, val = expiry}, | |||
{pos = 3, val = namespace}, | |||
{pos = 2, val = protLevel}, | |||
if | {pos = 1, val = protType}, | ||
{pos = 4, val = reason} | |||
} | |||
local behavior = 'reasonFirst' | |||
if reason then | |||
behavior = behaviors[reason] | |||
if behavior == 'namespaceFirst' then | |||
toTableEnd(properties, 2) -- move namespace to the end | |||
elseif behavior ~= 'reasonFirst' then | |||
error(reason .. ' is not a valid reason') | |||
end | |||
end | end | ||
local | local active, inactive = {}, {} | ||
for i, t in ipairs(properties) do | |||
if t.val then | |||
active[#active + 1] = t | |||
else | |||
inactive[#inactive + 1] = t | |||
end | |||
end | end | ||
local noActive = #active | |||
local | local attemptOrder = active | ||
for i, | for i, t in ipairs(inactive) do | ||
attemptOrder[#attemptOrder + 1] = t | |||
end | end | ||
for i = 1, 2^noActive do | |||
local key = {} | |||
for i = 1, 2^ | for j, t in ipairs(attemptOrder) do | ||
local | local pos = t.pos | ||
for pos = | local val = t.val | ||
if | if j > noActive then | ||
key[pos] = 'all' | |||
else | else | ||
local quotient = i / 2 ^ ( | local quotient = i / 2 ^ (j - 1) | ||
quotient = floor(quotient) | quotient = floor(quotient) | ||
if quotient % 2 == 1 then | if quotient % 2 == 1 then | ||
key[pos] = val | |||
else | else | ||
key[pos] = 'all' | |||
end | end | ||
end | end | ||
end | end | ||
key = tconcat(key, '-') | |||
mw.log( | mw.log(key) -- for debugging | ||
local attempt = cats[ | local attempt = cats[key] | ||
if attempt then | if attempt then | ||
return attempt | return attempt |