Module:Protection banner: Difference between revisions

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