Module:Protection banner: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(start converting the getCategoryName function) |
(finish converting p.getCategoryName) |
||
Line 224: | Line 224: | ||
local ProtectionCategory = Category:subclass('ProtectionCategory') |
local ProtectionCategory = Category:subclass('ProtectionCategory') |
||
function ProtectionCategory:setName( |
function ProtectionCategory:setName( |
||
name, |
|||
configObj, |
|||
protectionStatusObj, |
|||
namespace |
|||
⚫ | |||
--[[ |
--[[ |
||
-- Sets the protection category. If a category name is not provided, this |
-- Sets the protection category. If a category name is not provided, this |
||
Line 231: | Line 236: | ||
-- reason for protection, and the expiry date. |
-- reason for protection, and the expiry date. |
||
--]] |
--]] |
||
⚫ | |||
-- If a name was provided, use that. |
|||
⚫ | |||
Category.setName(self, name) |
|||
⚫ | |||
-- Get the namespace category key from the namespace number. |
-- Get the namespace category key from the namespace number. |
||
Line 253: | Line 262: | ||
--]] |
--]] |
||
local properties = { |
local properties = { |
||
expiry = {order = 1, keypos = 5, val = |
expiry = {order = 1, keypos = 5, val = protectionStatusObj:getExpiry()}, |
||
namespace = {order = 2, keypos = 3, val = nskey}, |
namespace = {order = 2, keypos = 3, val = nskey}, |
||
reason = {order = 3, keypos = 4, val = |
reason = {order = 3, keypos = 4, val = protectionStatusObj:getReason()}, |
||
level = {order = 4, keypos = 2, val = |
level = {order = 4, keypos = 2, val = protectionStatusObj:getLevel()}, |
||
action = {order = 5, keypos = 1, val = |
action = {order = 5, keypos = 1, val = protectionStatusObj:getAction()} |
||
} |
} |
||
--[[ |
--[[ |
||
-- |
-- Apply the category order configuration, if any. The configuration value |
||
-- will be a property string, e.g. 'reason', 'namespace', etc. The property |
|||
-- The configuration is stored in the categoryOrder field of each reason |
|||
-- |
-- corresponding to that string is tested last (i.e. it is the most |
||
-- values specified in the table. If the value is a string, then the |
|||
-- property corresponding to that string is tested last (i.e. it is the most |
|||
-- important, because it keeps its specified value the longest) and the |
-- important, because it keeps its specified value the longest) and the |
||
-- other properties are tested in the canonical order. If |
-- other properties are tested in the canonical order. If no configuration |
||
-- |
-- value is specified then the canonical order is used. |
||
--]] |
--]] |
||
local reasonTable = reason and cfg.reasons[reason] |
|||
⚫ | |||
local categoryOrderType = type(categoryOrder) |
|||
local configOrder = {} |
local configOrder = {} |
||
do |
|||
if categoryOrderType == 'table' then |
|||
local bannerConfig = configObj:getBannerConfig(protectionStatusObj) |
|||
local dupes = {} |
|||
⚫ | |||
for i = 1, 5 do |
|||
local propertiesKey = categoryOrder[i] |
|||
if not propertiesKey then |
|||
local msg = 'no entry found for key ' |
|||
.. i |
|||
.. ' in the cfg.reasons.' |
|||
.. reason |
|||
.. '.categoryOrder table' |
|||
⚫ | |||
⚫ | |||
local property = properties[propertiesKey] |
|||
⚫ | |||
local msg = 'invalid value "' |
|||
.. propertiesKey |
|||
.. '" detected in the cfg.reasons.' |
|||
.. reason |
|||
.. '.categoryOrder table' |
|||
error(msg) |
|||
⚫ | |||
if dupes[propertiesKey] then |
|||
local msg = 'duplicate values "' |
|||
.. propertiesKey |
|||
.. '" detected in the cfg.reasons.' |
|||
.. reason |
|||
.. '.categoryOrder table' |
|||
error(msg) |
|||
⚫ | |||
dupes[propertiesKey] = true |
|||
end |
|||
configOrder[i] = property |
|||
end |
|||
⚫ | |||
for propertiesKey, t in pairs(properties) do |
for propertiesKey, t in pairs(properties) do |
||
configOrder[t.order] = t |
configOrder[t.order] = t |
||
end |
end |
||
if |
if categoryOrder then |
||
local property = properties[categoryOrder] |
local property = properties[categoryOrder] |
||
if not property then |
if not property then |
||
Line 321: | Line 294: | ||
error(msg) |
error(msg) |
||
end |
end |
||
table.insert(configOrder, table.remove(configOrder, property.order)) |
|||
end |
end |
||
end |
end |
||
Line 333: | Line 306: | ||
-- parameter. |
-- parameter. |
||
--]] |
--]] |
||
local |
local noActive, attemptOrder |
||
do |
|||
⚫ | |||
local active, inactive = {}, {} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
active[#active + 1] = t |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 379: | Line 355: | ||
-- pos field in the property table. |
-- pos field in the property table. |
||
--]] |
--]] |
||
⚫ | |||
local cat |
|||
for i = 1, 2^noActive do |
for i = 1, 2^noActive do |
||
local key = {} |
local key = {} |
||
Line 397: | Line 375: | ||
local attempt = cats[key] |
local attempt = cats[key] |
||
if attempt then |
if attempt then |
||
cat = attempt |
|||
break |
|||
end |
end |
||
end |
end |
||
if cat then |
|||
error( |
|||
Category.setName(self, cat) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
) |
|||
⚫ | |||
end |
end |
||