Module:Protection banner: Difference between revisions

key components are in the correct order now - property order still a long way off, though
en>Mr. Stradivarius
(change this to something that should work if I can get the order figured out)
en>Mr. Stradivarius
(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 'all'
return nil
end
end
local nskey = nskeys[ns]
local nskey = nskeys[ns]
if not nskey then
if not nskey and ns % 2 == 1 then
if ns % 2 == 1 then
nskey = 'talk'
nskey = 'talk'
else
nskey = 'all'
end
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.
-- Preprocess parameters
cats = cats or categories
cats = cats or categories
local properties = {}
 
properties.protType = protType or 'all'
local properties = {
properties.protLevel = protLevel or 'all'
{pos = 5, val = expiry},
properties.namespace = p.matchNamespace(namespace)
{pos = 3, val = namespace},
properties.reason = reason or 'all'
{pos = 2, val = protLevel},
if not expiry then
{pos = 1, val = protType},
properties.expiry = 'all'
{pos = 4, val = reason}
elseif expiry ~= 'indef' then
}
properties.expiry = 'temp'
 
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 order = {'expiry', 'namespace', 'protLevel', 'protType', 'reason'}  
local active, inactive = {}, {}
local behavior = behaviors[reason]
for i, t in ipairs(properties) do
if behavior == 'namespaceFirst' then
if t.val then
tinsert(order, table.remove(order, 2)) -- move namespace to the end
active[#active + 1] = t
elseif behavior ~= 'reasonFirst' and reason ~= 'all' then
else
error(reason .. ' is not a valid reason')
inactive[#inactive + 1] = t
end
end
end
local noActive = #active


local activePropertyKeys = {}
local attemptOrder = active
for i, propertyKey in ipairs(order) do
for i, t in ipairs(inactive) do
if properties[propertyKey] ~= 'all' then
attemptOrder[#attemptOrder + 1] = t
activePropertyKeys[#activePropertyKeys + 1] = propertyKey
end
end
end
local noActiveProperties = #activePropertyKeys
 
for i = 1, 2^noActive do
-- Try successively generic matches until we run out of key combinations
local key = {}
for i = 1, 2^noActiveProperties do
for j, t in ipairs(attemptOrder) do
local categoryKey = {}
local pos = t.pos
for pos = 1, 5 do
local val = t.val
if pos > noActiveProperties then
if j > noActive then
categoryKey[pos] = 'all'
key[pos] = 'all'
else
else
local quotient = i / 2 ^ (pos - 1)
local quotient = i / 2 ^ (j - 1)
quotient = floor(quotient)
quotient = floor(quotient)
if quotient % 2 == 1 then
if quotient % 2 == 1 then
categoryKey[pos] = properties[activePropertyKeys[pos]]
key[pos] = val
else -- quotient % 2 == 0
else
categoryKey[pos] = 'all'
key[pos] = 'all'
end
end
end
end
end
end
categoryKey = tconcat(categoryKey, '-')
key = tconcat(key, '-')
mw.log(categoryKey) -- for debugging
mw.log(key) -- for debugging
local attempt = cats[categoryKey]
local attempt = cats[key]
if attempt then
if attempt then
return attempt
return attempt
Anonymous user