Module:Protection banner: Difference between revisions
some cleanup
(use a __tostring metamethod to render the banner objects) |
(some cleanup) |
||
Line 11: | Line 11: | ||
-- Lazily initialise modules and objects we don't always need. | -- Lazily initialise modules and objects we don't always need. | ||
local mArguments, mMessageBox, lang | local mArguments, mMessageBox, lang | ||
-------------------------------------------------------------------------------- | |||
-- Helper functions | |||
-------------------------------------------------------------------------------- | |||
local function makeCategoryLink(cat) | |||
if cat then | |||
return string.format( | |||
'[[%s:%s]]', | |||
mw.site.namespaces[14].name, | |||
cat | |||
) | |||
else | |||
return '' | |||
end | |||
end | |||
-- Validation function for the expiry and the protection date | |||
local function validateDate(dateString, dateType) | |||
lang = lang or mw.language.getContentLanguage() | |||
local success, result = pcall(lang.formatDate, lang, 'U', dateString) | |||
if success then | |||
result = tonumber(result) | |||
if result then | |||
return result | |||
end | |||
end | |||
error(string.format( | |||
'invalid %s ("%s")', | |||
dateType, | |||
tostring(dateString) | |||
)) | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 17: | Line 50: | ||
local Protection = class('Protection') | local Protection = class('Protection') | ||
Protection.supportedActions = { | |||
create = true, | |||
edit = true, | |||
move = true, | |||
autoreview = true | |||
} | |||
Protection.bannerConfigFields = { | |||
'text', | |||
'explanation', | |||
'tooltip', | |||
'alt', | |||
'link', | |||
'image' | |||
} | |||
function Protection:initialize(args, configObj, titleObj) | function Protection:initialize(args, configObj, titleObj) | ||
Line 24: | Line 73: | ||
-- Set action | -- Set action | ||
do | do | ||
if not args.action then | |||
self.action = 'edit' | |||
elseif self.supportedActions[args.action] then | |||
self.action = args.action | self.action = args.action | ||
else | else | ||
error('Unsupported action ' .. args.action, 2) | |||
end | end | ||
end | end | ||
Line 39: | Line 84: | ||
-- Set level | -- Set level | ||
do | do | ||
self.level = effectiveProtectionLevel(self.action, titleObj) | |||
if level == 'accountcreator' then | if self.level == 'accountcreator' then | ||
-- Lump titleblacklisted pages in with template-protected pages, | -- Lump titleblacklisted pages in with template-protected pages, | ||
-- since templateeditors can do both. | -- since templateeditors can do both. | ||
level = 'templateeditor' | self.level = 'templateeditor' | ||
elseif not self.level or (self.action == 'move' and self.level == 'autoconfirmed') then | |||
-- Users need to be autoconfirmed to move pages anyway, so treat | -- Users need to be autoconfirmed to move pages anyway, so treat | ||
-- semi-move-protected pages as unprotected. | -- semi-move-protected pages as unprotected. | ||
self.level = '*' | |||
end | end | ||
end | end | ||
Line 71: | Line 98: | ||
-- Set expiry | -- Set expiry | ||
if args.expiry then | if args.expiry then | ||
if configObj.cfg.indefStrings[args.expiry] then | |||
self.expiry = 'indef' | self.expiry = 'indef' | ||
elseif type(args.expiry) == 'number' then | elseif type(args.expiry) == 'number' then | ||
Line 96: | Line 122: | ||
self.bannerConfig = {} | self.bannerConfig = {} | ||
local cfg = configObj.cfg | local cfg = configObj.cfg | ||
local configTables = {} | local configTables = {} | ||
if cfg.banners[self.action] then | if cfg.banners[self.action] then | ||
Line 113: | Line 131: | ||
end | end | ||
configTables[#configTables + 1] = cfg.masterBanner | configTables[#configTables + 1] = cfg.masterBanner | ||
for i, field in ipairs( | for i, field in ipairs(self.bannerConfigFields) do | ||
for j, t in ipairs(configTables) do | for j, t in ipairs(configTables) do | ||
if t[field] then | if t[field] then | ||
Line 126: | Line 144: | ||
function Protection:isProtected() | function Protection:isProtected() | ||
return self.level ~= '*' | return self.level ~= '*' | ||
end | end | ||
Line 291: | Line 296: | ||
end | end | ||
return | return makeCategoryLink(cat) | ||
end | end | ||
Line 306: | Line 311: | ||
cat = self._configObj.msg['tracking-category-expiry'] | cat = self._configObj.msg['tracking-category-expiry'] | ||
end | end | ||
return | return makeCategoryLink(cat) | ||
end | end | ||
Line 317: | Line 322: | ||
cat = configObj.msg['tracking-category-incorrect'] | cat = configObj.msg['tracking-category-incorrect'] | ||
end | end | ||
return | return makeCategoryLink(cat) | ||
end | end | ||
Line 333: | Line 338: | ||
cat = configObj.msg['tracking-category-template'] | cat = configObj.msg['tracking-category-template'] | ||
end | end | ||
return | return makeCategoryLink(cat) | ||
end | end | ||