Jump to content

Module:Protection banner: Difference between revisions

(edit conflict) replace Protection getter functions with public attributes
(get rid of the config class while still allowing replacing it for testing)
((edit conflict) replace Protection getter functions with public attributes)
Line 10: Line 10:


-- Lazily initialise modules and objects we don't always need.
-- Lazily initialise modules and objects we don't always need.
local mArguments, mMessageBox, lang, config
local mArguments, mMessageBox, lang
 
--------------------------------------------------------------------------------
-- Config class
--------------------------------------------------------------------------------
 
local Config = class('Config')
 
function Config:initialize(data)
data = data or mw.loadData('Module:Protection banner/config')
self._cfg = data.cfg
self._msg = data.msg
self._bannerConfigTables = {}
end
 
function Config:getBannerConfig(protectionObj)
if self._bannerConfigTables[protectionObj] then
return self._bannerConfigTables[protectionObj]
else
local ret = {}
local cfg = self._cfg
local action = protectionObj.action
local level = protectionObj.level
local reason = protectionObj.reason
local fields = {
'text',
'explanation',
'tooltip',
'alt',
'link',
'image'
}
local configTables = {}
if cfg.banners[action] then
configTables[#configTables + 1] = cfg.banners[action][reason]
end
if cfg.defaultBanners[action] then
configTables[#configTables + 1] = cfg.defaultBanners[action][level]
configTables[#configTables + 1] = cfg.defaultBanners[action].default
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(fields) do
for j, t in ipairs(configTables) do
if t[field] then
ret[field] = t[field]
break
end
end
end
self._bannerConfigTables[protectionObj] = ret
return ret
end
end
 
function Config:getConfigTable(key)
local blacklist = {
banners = true,
defaultBanners = true,
masterBanner = true
}
if not blacklist[key] then
return self._cfg[key]
else
return nil
end
end
 
function Config:getMessage(key)
return self._msg[key]
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 28: Line 97:
}
}
if args.action and actions[args.action] then
if args.action and actions[args.action] then
self._action = args.action
self.action = args.action
else
else
self._action = 'edit'
self.action = 'edit'
end
end
end
end
Line 36: Line 105:
-- Set level
-- Set level
do
do
local level = effectiveProtectionLevel(self._action, titleObj)
local level = effectiveProtectionLevel(self.action, titleObj)
if level == 'accountcreator' then
if level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
-- Lump titleblacklisted pages in with template-protected pages,
Line 42: Line 111:
level = 'templateeditor'
level = 'templateeditor'
end
end
if self._action == 'move' and level == 'autoconfirmed' then
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 = '*'
level = '*'
end
end
self._level = level or '*'
self.level = level or '*'
end
end


Line 68: Line 137:
-- Set expiry
-- Set expiry
if args.expiry then
if args.expiry then
local indefStrings = configObj.cfg.indefStrings
local indefStrings = configObj:getConfigTable('indefStrings')
if 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
self._expiry = args.expiry
self.expiry = args.expiry
else
else
self._expiry = validateDate(args.expiry, 'expiry date')
self.expiry = validateDate(args.expiry, 'expiry date')
end
end
end
end
Line 82: Line 151:
local reason = args.reason or args[1]
local reason = args.reason or args[1]
if reason then
if reason then
self._reason = reason:lower()
self.reason = reason:lower()
end
end
end
end


-- Set protection date
-- Set protection date
self._protectionDate = validateDate(args.date, 'protection date')
self.protectionDate = validateDate(args.date, 'protection date')
-- Set banner config
do
self.bannerConfig = {}
local cfg = configObj.cfg
local fields = {
'text',
'explanation',
'tooltip',
'alt',
'link',
'image'
}
local configTables = {}
if cfg.banners[self._action] then
configTables[#configTables + 1] = cfg.banners[self._action][self._reason]
end
if cfg.defaultBanners[self._action] then
configTables[#configTables + 1] = cfg.defaultBanners[self._action][self._level]
configTables[#configTables + 1] = cfg.defaultBanners[self._action].default
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(fields) do
for j, t in ipairs(configTables) do
if t[field] then
self.bannerConfig[field] = t[field]
break
end
end
end
end
end
 
function Protection:getAction()
return self._action
end
 
function Protection:getLevel()
return self._level
end
end


function Protection:isProtected()
function Protection:isProtected()
return self._level ~= '*'
return self._level ~= '*'
end
function Protection:getReason()
return self._reason
end
function Protection:getExpiry()
return self._expiry
end
function Protection:getProtectionDate()
return self._protectionDate
end
end


Line 154: Line 172:
self._configObj = configObj
self._configObj = configObj
self._protectionObj = protectionObj
self._protectionObj = protectionObj
self._bannerConfig = protectionObj.bannerConfig
self._bannerConfig = configObj:getBannerConfig(protectionObj)
self._titleObj = titleObj
self._titleObj = titleObj
end
end
Line 183: Line 201:


function Blurb:_getExpandedMessage(msg)
function Blurb:_getExpandedMessage(msg)
local msg = self._configObj.msg[msg]
local msg = self._configObj:getMessage(msg)
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
end
end
Line 232: Line 250:
-- A link to the page history or the move log, depending on the kind of
-- A link to the page history or the move log, depending on the kind of
-- protection.
-- protection.
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local pagename = self._titleObj.prefixedText
local pagename = self._titleObj.prefixedText
if action == 'move' then
if action == 'move' then
Line 260: Line 278:


function Blurb:_makeDisputeBlurbParameter()
function Blurb:_makeDisputeBlurbParameter()
local expiry = self._protectionObj:getExpiry()
local expiry = self._protectionObj.expiry
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('dispute-blurb-expiry')
return self:_getExpandedMessage('dispute-blurb-expiry')
Line 287: Line 305:
function Blurb:_makeEditRequestParameter()
function Blurb:_makeEditRequestParameter()
local mEditRequest = require('Module:Submit an edit request')
local mEditRequest = require('Module:Submit an edit request')
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local level = self._protectionObj:getLevel()
local level = self._protectionObj.level
-- Get the display message key.
-- Get the display message key.
Line 314: Line 332:


function Blurb:_makeExpiryParameter()
function Blurb:_makeExpiryParameter()
local expiry = self._protectionObj:getExpiry()
local expiry = self._protectionObj.expiry
if expiry == 'indef' then
if expiry == 'indef' then
return nil
return nil
Line 326: Line 344:


function Blurb:_makeExplanationBlurbParameter()
function Blurb:_makeExplanationBlurbParameter()
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local level = self._protectionObj:getLevel()
local level = self._protectionObj.level
local namespace = self._titleObj.namespace
local namespace = self._titleObj.namespace
local isTalk = self._titleObj.isTalkPage
local isTalk = self._titleObj.isTalkPage
Line 358: Line 376:


function Blurb:_makeImageLinkParameter()
function Blurb:_makeImageLinkParameter()
local imageLinks = self._configObj.cfg.imageLinks
local imageLinks = self._configObj:getConfigTable('imageLinks')
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local level = self._protectionObj:getLevel()
local level = self._protectionObj.level
local msg
local msg
if imageLinks[action][level] then
if imageLinks[action][level] then
Line 373: Line 391:


function Blurb:_makeIntroBlurbParameter()
function Blurb:_makeIntroBlurbParameter()
local expiry = self._protectionObj:getExpiry()
local expiry = self._protectionObj.expiry
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('intro-blurb-expiry')
return self:_getExpandedMessage('intro-blurb-expiry')
Line 382: Line 400:


function Blurb:_makeOfficeBlurbParameter()
function Blurb:_makeOfficeBlurbParameter()
local protectionDate = self._protectionObj:getProtectionDate()
local protectionDate = self._protectionObj.protectionDate
if protectionDate then
if protectionDate then
return self:_getExpandedMessage('office-blurb-protectiondate')
return self:_getExpandedMessage('office-blurb-protectiondate')
Line 391: Line 409:


function Blurb:_makePagetypeParameter()
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj.cfg.pagetypes
local pagetypes = self._configObj:getConfigTable('pagetypes')
local namespace = self._titleObj.namespace
local namespace = self._titleObj.namespace
return pagetypes[namespace] or pagetypes.default or error('no default pagetype defined')
return pagetypes[namespace] or pagetypes.default or error('no default pagetype defined')
Line 397: Line 415:


function Blurb:_makeProtectionBlurbParameter()
function Blurb:_makeProtectionBlurbParameter()
local protectionBlurbs = self._configObj.cfg.protectionBlurbs
local protectionBlurbs = self._configObj:getConfigTable('protectionBlurbs')
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local level = self._protectionObj:getLevel()
local level = self._protectionObj.level
local msg
local msg
if protectionBlurbs[action][level] then
if protectionBlurbs[action][level] then
Line 414: Line 432:


function Blurb:_makeProtectionDateParameter()
function Blurb:_makeProtectionDateParameter()
local protectionDate = self._protectionObj:getProtectionDate()
local protectionDate = self._protectionObj.protectionDate
if type(protectionDate) == 'number' then
if type(protectionDate) == 'number' then
return Blurb.formatDate(protectionDate)
return Blurb.formatDate(protectionDate)
Line 423: Line 441:


function Blurb:_makeProtectionLevelParameter()
function Blurb:_makeProtectionLevelParameter()
local protectionLevels = self._configObj.cfg.protectionLevels
local protectionLevels = self._configObj:getConfigTable('protectionLevels')
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local level = self._protectionObj:getLevel()
local level = self._protectionObj.level
local msg
local msg
if protectionLevels[action][level] then
if protectionLevels[action][level] then
Line 440: Line 458:


function Blurb:_makeProtectionLogParameter()
function Blurb:_makeProtectionLogParameter()
local action = self._protectionObj:getAction()
local action = self._protectionObj.action
local pagename = self._titleObj.prefixedText
local pagename = self._titleObj.prefixedText
if action == 'autoreview' then
if action == 'autoreview' then
Line 460: Line 478:


function Blurb:_makeResetBlurbParameter()
function Blurb:_makeResetBlurbParameter()
local protectionDate = self._protectionObj:getProtectionDate()
local protectionDate = self._protectionObj.protectionDate
if protectionDate then
if protectionDate then
return self:_getExpandedMessage('reset-blurb-protectiondate')
return self:_getExpandedMessage('reset-blurb-protectiondate')
Line 481: Line 499:


function Blurb:_makeTooltipBlurbParameter()
function Blurb:_makeTooltipBlurbParameter()
local expiry = self._protectionObj:getExpiry()
local expiry = self._protectionObj.expiry
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('tooltip-blurb-expiry')
return self:_getExpandedMessage('tooltip-blurb-expiry')
Line 553: Line 571:
end
end


local action = protectionObj:getAction()
local action = protectionObj.action
local level = protectionObj:getLevel()
local level = protectionObj.level
local expiry = protectionObj:getExpiry()
local expiry = protectionObj.expiry
local namespace = titleObj.namespace
local namespace = titleObj.namespace
Line 566: Line 584:
-- Fully protected modules and templates get the special red "indef"
-- Fully protected modules and templates get the special red "indef"
-- padlock.
-- padlock.
self._imageFilename = self._configObj.msg['image-filename-indef']
self._imageFilename = self._configObj:getMessage('image-filename-indef')
return nil
return nil
end
end


-- Deal with regular protection types.
-- Deal with regular protection types.
local images = self._configObj.cfg.images
local images = self._configObj:getConfigTable('images')
if images[action] then
if images[action] then
if images[action][level] then
if images[action][level] then
Line 595: Line 613:
function BannerTemplate:renderImage()
function BannerTemplate:renderImage()
local filename = self._imageFilename
local filename = self._imageFilename
or self._configObj.msg['image-filename-default']
or self._configObj:getMessage('image-filename-default')
or 'Transparent.gif'
or 'Transparent.gif'
return newFileLink(filename)
return newFileLink(filename)
Line 729: Line 747:
-- Get the expiry.
-- Get the expiry.
local expiry = protectionObj:getExpiry()
local expiry = protectionObj.expiry
if type(expiry) == 'number' then
if type(expiry) == 'number' then
expiry = 'temp'
expiry = 'temp'
Line 740: Line 758:
do
do
local namespace = titleObj.namespace
local namespace = titleObj.namespace
local categoryNamespaces = configObj.cfg.categoryNamespaceKeys
local categoryNamespaces = configObj:getConfigTable('categoryNamespaceKeys')
nskey = categoryNamespaces[namespace]
nskey = categoryNamespaces[namespace]
if not nskey and namespace % 2 == 1 then
if not nskey and namespace % 2 == 1 then
Line 748: Line 766:


-- Get the other inputs.
-- Get the other inputs.
local reason = protectionObj:getReason()
local reason = protectionObj.reason
local action = protectionObj:getAction()
local action = protectionObj.action
local level = protectionObj:getLevel()
local level = protectionObj.level
   
   
--[[
--[[
Line 775: Line 793:
local configOrder = {}
local configOrder = {}
do
do
local reasonsWithNamespacePriority = configObj.cfg.reasonsWithNamespacePriority
local reasonsWithNamespacePriority = configObj:getConfigTable('reasonsWithNamespacePriority')
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
for propertiesKey, t in pairs(properties) do
for propertiesKey, t in pairs(properties) do
Line 844: Line 862:
-- pos field in the property table.
-- pos field in the property table.
--]]
--]]
local cats = configObj.cfg.protectionCategories
local cats = configObj:getConfigTable('protectionCategories')
local cat
local cat
for i = 1, 2^noActive do
for i = 1, 2^noActive do
Line 881: Line 899:


function ExpiryCategory:render()
function ExpiryCategory:render()
local configObj = self._configObj
local reasonsWithoutExpiryCheck = self._configObj:getConfigTable('reasonsWithoutExpiryCheck')
local protectionObj = self._protectionObj
local expiryCheckActions = self._configObj:getConfigTable('expiryCheckActions')
local expiry = self._protectionObj.expiry
local reasonsWithoutExpiryCheck = configObj.cfg.reasonsWithoutExpiryCheck
local action = self._protectionObj.action
local expiryCheckActions = configObj.cfg.expiryCheckActions
local reason = self._protectionObj.reason
local expiry = protectionObj:getExpiry()
local action = protectionObj:getAction()
local reason = protectionObj:getReason()
if not expiry
if not expiry
Line 895: Line 910:
and not reasonsWithoutExpiryCheck[reason]
and not reasonsWithoutExpiryCheck[reason]
then
then
self:setName(configObj.msg['tracking-category-expiry'])
self:setName(configObj:getMessage('tracking-category-expiry'))
end
end
return Category.render(self)
return Category.render(self)
Line 909: Line 924:
local configObj = self._configObj
local configObj = self._configObj
local protectionObj = self._protectionObj
local protectionObj = self._protectionObj
local expiry = protectionObj.expiry
local expiry = protectionObj:getExpiry()
local action = protectionObj.action
local action = protectionObj:getAction()
local level = protectionObj.level
local level = protectionObj:getLevel()


if not protectionObj:isProtected()
if not protectionObj:isProtected()
or type(expiry) == 'number' and expiry < os.time()
or type(expiry) == 'number' and expiry < os.time()
then
then
self:setName(configObj.msg['tracking-category-incorrect'])
self:setName(configObj:getMessage('tracking-category-incorrect'))
end
end
return Category.render(self)
return Category.render(self)
Line 935: Line 949:
function TemplateCategory:render()
function TemplateCategory:render()
local configObj = self._configObj
local configObj = self._configObj
local protectionObj = self._protectionObj
local action = self._protectionObj.action
local titleObj = self._titleObj
local level = self._protectionObj.level
local namespace = self._titleObj.namespace
local action = protectionObj:getAction()
local level = protectionObj:getLevel()
local namespace = titleObj.namespace
if level == 'templateeditor'
if level == 'templateeditor'
Line 948: Line 959:
)
)
then
then
self:setName(configObj.msg['tracking-category-template'])
self:setName(configObj:getMessage('tracking-category-template'))
end
end
return Category.render(self)
return Category.render(self)
Line 969: Line 980:


-- Get data objects
-- Get data objects
if not config then
local configObj = Config:new()
config = mw.loadData('Module:Protection banner/config')
end
local configObj = config
local protectionObj = Protection:new(args, configObj, titleObj)
local protectionObj = Protection:new(args, configObj, titleObj)


Line 995: Line 1,003:


-- Set the image fields
-- Set the image fields
local bannerConfig = protectionObj.bannerConfig
local bannerConfig = configObj:getBannerConfig(protectionObj)
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
if isPadlock then
if isPadlock then
Line 1,035: Line 1,043:
return {
return {
Protection = Protection,
Protection = Protection,
Config = Config,
Blurb = Blurb,
Blurb = Blurb,
BannerTemplate = BannerTemplate,
BannerTemplate = BannerTemplate,
Cookies help us deliver our services. By using our services, you agree to our use of cookies.