Module:Protection banner: Difference between revisions

validate expiry and protection dates
Enwikipedia>Mr. Stradivarius
(allow specifying banner config by protection level as well as by action)
Enwikipedia>Mr. Stradivarius
(validate expiry and protection dates)
Line 9:
local yesno = require('Module:Yesno')
 
-- Lazily initialise modules and objects we don't always need.
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 ⟶ 78:
function Config:getMessage(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
 
Line 273 ⟶ 300:
-- parameter $5
local protectionDate = self._protectionStatusObj:getProtectionDate()
if type(protectionDate) == 'number' then
local date = os.date('%e %B %Y', protectionDate)
local lang = mw.language.getContentLanguage()
date = date:gsub('^ ', '') -- The %e option replaces leading zeroes with spaces, but we don't want spaces.
local success, date = pcall(
return date
lang.formatDate,
else
lang,
return protectionDate
'j F Y',
protectionDate
)
if success and date then
return date
end
end
end
Line 827 ⟶ 849:
 
-- Get data objects
local theProtectionStatus = ProtectionStatus:new(args, title)
local theConfig = Config:new()
local theProtectionStatus = ProtectionStatus:new(args, theConfig, title)
 
local ret = {}