Module:Protection banner: Difference between revisions

validate expiry and protection dates
(allow specifying banner config by protection level as well as by action)
(validate expiry and protection dates)
Line 9: Line 9:
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')


-- Lazily initialise modules we don't always need.
-- Lazily initialise modules and objects we don't always need.
local mArguments, mMessageBox
local mArguments, mMessageBox, lang
 
--------------------------------------------------------------------------------
-- ProtectionStatus class
--------------------------------------------------------------------------------
 
local ProtectionStatus = class('ProtectionStatus')
 
function ProtectionStatus:initialize(args, titleObj)
-- Set action
do
local actions = {
create = true,
edit = true,
move = true,
autoreview = true
}
if args.action and actions[args.action] then
self._action = args.action
else
self._action = 'edit'
end
end
 
-- Set level
do
local level = mProtectionLevel._main(self._action, titleObj)
if level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
-- since templateeditors can do both.
level = 'templateeditor'
end
self._level = level or '*'
end
 
-- Set other params
self._reason = args.reason
self._expiry = args.expiry or 'indef'
self._protectionDate = args.date
end
 
function ProtectionStatus:getAction()
return self._action
end
 
function ProtectionStatus:getLevel()
return self._level
end
 
function ProtectionStatus:getReason()
return self._reason
end
 
function ProtectionStatus:getExpiry()
return self._expiry
end
 
function ProtectionStatus:getProtectionDate()
return self._protectionDate
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 137: Line 78:
function Config:getMessage(key)
function Config:getMessage(key)
return self._msg[key]
return self._msg[key]
end
--------------------------------------------------------------------------------
-- ProtectionStatus class
--------------------------------------------------------------------------------
local ProtectionStatus = class('ProtectionStatus')
function ProtectionStatus:initialize(args, configObj, titleObj)
-- Set action
do
local actions = {
create = true,
edit = true,
move = true,
autoreview = true
}
if args.action and actions[args.action] then
self._action = args.action
else
self._action = 'edit'
end
end
-- Set level
do
local level = mProtectionLevel._main(self._action, titleObj)
if level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
-- since templateeditors can do both.
level = 'templateeditor'
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
-- Set expiry
if args.expiry then
local indefStrings = configObj:getConfigTable('indefStrings')
if indefStrings[args.expiry] then
self._expiry = 'indef'
elseif type(args.expiry) == 'number' then
self._expiry = args.expiry
else
self._expiry = validateDate(args.expiry, 'expiry date')
end
end
-- Set other params
self._reason = args.reason
self._protectionDate = validateDate(args.date, 'protection date')
end
function ProtectionStatus:getAction()
return self._action
end
function ProtectionStatus:getLevel()
return self._level
end
function ProtectionStatus:getReason()
return self._reason
end
function ProtectionStatus:getExpiry()
return self._expiry
end
function ProtectionStatus:getProtectionDate()
return self._protectionDate
end
end


Line 273: Line 300:
-- parameter $5
-- parameter $5
local protectionDate = self._protectionStatusObj:getProtectionDate()
local protectionDate = self._protectionStatusObj:getProtectionDate()
if protectionDate then
if type(protectionDate) == 'number' then
local lang = mw.language.getContentLanguage()
local date = os.date('%e %B %Y', protectionDate)
local success, date = pcall(
date = date:gsub('^ ', '') -- The %e option replaces leading zeroes with spaces, but we don't want spaces.
lang.formatDate,
return date
lang,
else
'j F Y',
return protectionDate
protectionDate
)
if success and date then
return date
end
end
end
end
end
Line 827: Line 849:


-- Get data objects
-- Get data objects
local theProtectionStatus = ProtectionStatus:new(args, title)
local theConfig = Config:new()
local theConfig = Config:new()
local theProtectionStatus = ProtectionStatus:new(args, theConfig, title)


local ret = {}
local ret = {}