Module:Protection banner: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(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
local actions = {
create = true,
self.action = '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
self.action = args.action
else
else
self.action = 'edit'
error('Unsupported action ' .. args.action, 2)
end
end
end
end
Line 39: Line 84:
-- Set level
-- Set level
do
do
local level = effectiveProtectionLevel(self.action, titleObj)
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
end
if self.action == 'move' and 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.
level = '*'
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
end
end
Line 71: Line 98:
-- Set expiry
-- Set expiry
if args.expiry then
if args.expiry then
local indefStrings = configObj.cfg.indefStrings
if configObj.cfg.indefStrings[args.expiry] then
if 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 fields = {
'text',
'explanation',
'tooltip',
'alt',
'link',
'image'
}
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(fields) do
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

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
end


Line 291: Line 296:
end
end


return self._makeCategoryLink(cat)
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 self._makeCategoryLink(cat)
return makeCategoryLink(cat)
end
end


Line 317: Line 322:
cat = configObj.msg['tracking-category-incorrect']
cat = configObj.msg['tracking-category-incorrect']
end
end
return self._makeCategoryLink(cat)
return makeCategoryLink(cat)
end
end


Line 333: Line 338:
cat = configObj.msg['tracking-category-template']
cat = configObj.msg['tracking-category-template']
end
end
return self._makeCategoryLink(cat)
return makeCategoryLink(cat)
end
end