Module:Protection banner: Difference between revisions

(edit conflict) replace Protection getter functions with public attributes
Enwikipedia>Jackmcbarn
(get rid of the config class while still allowing replacing it for testing)
Enwikipedia>Mr. Stradivarius
((edit conflict) replace Protection getter functions with public attributes)
Line 10:
 
-- Lazily initialise modules and objects we don't always need.
local mArguments, mMessageBox, lang, config
 
--------------------------------------------------------------------------------
-- 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 ⟶ 97:
}
if args.action and actions[args.action] then
self._actionaction = args.action
else
self._actionaction = 'edit'
end
end
Line 36 ⟶ 105:
-- Set level
do
local level = effectiveProtectionLevel(self._actionaction, titleObj)
if level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
Line 42 ⟶ 111:
level = 'templateeditor'
end
if self._actionaction == 'move' and level == 'autoconfirmed' then
-- Users need to be autoconfirmed to move pages anyway, so treat
-- semi-move-protected pages as unprotected.
level = '*'
end
self._levellevel = level or '*'
end
 
Line 68 ⟶ 137:
-- Set expiry
if args.expiry then
local indefStrings = configObj.cfg.:getConfigTable('indefStrings')
if indefStrings[args.expiry] then
self._expiryexpiry = 'indef'
elseif type(args.expiry) == 'number' then
self._expiryexpiry = args.expiry
else
self._expiryexpiry = validateDate(args.expiry, 'expiry date')
end
end
Line 82 ⟶ 151:
local reason = args.reason or args[1]
if reason then
self._reasonreason = reason:lower()
end
end
 
-- Set protection date
self._protectionDateprotectionDate = 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
 
function Protection:isProtected()
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
 
Line 154 ⟶ 172:
self._configObj = configObj
self._protectionObj = protectionObj
self._bannerConfig = configObj:getBannerConfig(protectionObj.bannerConfig)
self._titleObj = titleObj
end
Line 183 ⟶ 201:
 
function Blurb:_getExpandedMessage(msg)
local msg = self._configObj.:getMessage(msg[msg])
return self:_substituteParameters(msg)
end
Line 232 ⟶ 250:
-- A link to the page history or the move log, depending on the kind of
-- protection.
local action = self._protectionObj:getAction().action
local pagename = self._titleObj.prefixedText
if action == 'move' then
Line 260 ⟶ 278:
 
function Blurb:_makeDisputeBlurbParameter()
local expiry = self._protectionObj:getExpiry().expiry
if type(expiry) == 'number' then
return self:_getExpandedMessage('dispute-blurb-expiry')
Line 287 ⟶ 305:
function Blurb:_makeEditRequestParameter()
local mEditRequest = require('Module:Submit an edit request')
local action = self._protectionObj:getAction().action
local level = self._protectionObj:getLevel().level
-- Get the display message key.
Line 314 ⟶ 332:
 
function Blurb:_makeExpiryParameter()
local expiry = self._protectionObj:getExpiry().expiry
if expiry == 'indef' then
return nil
Line 326 ⟶ 344:
 
function Blurb:_makeExplanationBlurbParameter()
local action = self._protectionObj:getAction().action
local level = self._protectionObj:getLevel().level
local namespace = self._titleObj.namespace
local isTalk = self._titleObj.isTalkPage
Line 358 ⟶ 376:
 
function Blurb:_makeImageLinkParameter()
local imageLinks = self._configObj.cfg.:getConfigTable('imageLinks')
local action = self._protectionObj:getAction().action
local level = self._protectionObj:getLevel().level
local msg
if imageLinks[action][level] then
Line 373 ⟶ 391:
 
function Blurb:_makeIntroBlurbParameter()
local expiry = self._protectionObj:getExpiry().expiry
if type(expiry) == 'number' then
return self:_getExpandedMessage('intro-blurb-expiry')
Line 382 ⟶ 400:
 
function Blurb:_makeOfficeBlurbParameter()
local protectionDate = self._protectionObj:getProtectionDate().protectionDate
if protectionDate then
return self:_getExpandedMessage('office-blurb-protectiondate')
Line 391 ⟶ 409:
 
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj.cfg.:getConfigTable('pagetypes')
local namespace = self._titleObj.namespace
return pagetypes[namespace] or pagetypes.default or error('no default pagetype defined')
Line 397 ⟶ 415:
 
function Blurb:_makeProtectionBlurbParameter()
local protectionBlurbs = self._configObj.cfg.:getConfigTable('protectionBlurbs')
local action = self._protectionObj:getAction().action
local level = self._protectionObj:getLevel().level
local msg
if protectionBlurbs[action][level] then
Line 414 ⟶ 432:
 
function Blurb:_makeProtectionDateParameter()
local protectionDate = self._protectionObj:getProtectionDate().protectionDate
if type(protectionDate) == 'number' then
return Blurb.formatDate(protectionDate)
Line 423 ⟶ 441:
 
function Blurb:_makeProtectionLevelParameter()
local protectionLevels = self._configObj.cfg.:getConfigTable('protectionLevels')
local action = self._protectionObj:getAction().action
local level = self._protectionObj:getLevel().level
local msg
if protectionLevels[action][level] then
Line 440 ⟶ 458:
 
function Blurb:_makeProtectionLogParameter()
local action = self._protectionObj:getAction().action
local pagename = self._titleObj.prefixedText
if action == 'autoreview' then
Line 460 ⟶ 478:
 
function Blurb:_makeResetBlurbParameter()
local protectionDate = self._protectionObj:getProtectionDate().protectionDate
if protectionDate then
return self:_getExpandedMessage('reset-blurb-protectiondate')
Line 481 ⟶ 499:
 
function Blurb:_makeTooltipBlurbParameter()
local expiry = self._protectionObj:getExpiry().expiry
if type(expiry) == 'number' then
return self:_getExpandedMessage('tooltip-blurb-expiry')
Line 553 ⟶ 571:
end
 
local action = protectionObj:getAction().action
local level = protectionObj:getLevel().level
local expiry = protectionObj:getExpiry().expiry
local namespace = titleObj.namespace
Line 566 ⟶ 584:
-- Fully protected modules and templates get the special red "indef"
-- padlock.
self._imageFilename = self._configObj.msg[:getMessage('image-filename-indef'])
return nil
end
 
-- Deal with regular protection types.
local images = self._configObj.cfg.:getConfigTable('images')
if images[action] then
if images[action][level] then
Line 595 ⟶ 613:
function BannerTemplate:renderImage()
local filename = self._imageFilename
or self._configObj.msg[:getMessage('image-filename-default'])
or 'Transparent.gif'
return newFileLink(filename)
Line 729 ⟶ 747:
-- Get the expiry.
local expiry = protectionObj:getExpiry().expiry
if type(expiry) == 'number' then
expiry = 'temp'
Line 740 ⟶ 758:
do
local namespace = titleObj.namespace
local categoryNamespaces = configObj.cfg.:getConfigTable('categoryNamespaceKeys')
nskey = categoryNamespaces[namespace]
if not nskey and namespace % 2 == 1 then
Line 748 ⟶ 766:
 
-- Get the other inputs.
local reason = protectionObj:getReason().reason
local action = protectionObj:getAction().action
local level = protectionObj:getLevel().level
--[[
Line 775 ⟶ 793:
local configOrder = {}
do
local reasonsWithNamespacePriority = configObj.cfg.:getConfigTable('reasonsWithNamespacePriority')
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
for propertiesKey, t in pairs(properties) do
Line 844 ⟶ 862:
-- pos field in the property table.
--]]
local cats = configObj.cfg.:getConfigTable('protectionCategories')
local cat
for i = 1, 2^noActive do
Line 881 ⟶ 899:
 
function ExpiryCategory:render()
local configObjreasonsWithoutExpiryCheck = self._configObj:getConfigTable('reasonsWithoutExpiryCheck')
local expiryCheckActions = self._configObj:getConfigTable('expiryCheckActions')
local protectionObj = self._protectionObj
local expiry = self._protectionObj.expiry
local action = self._protectionObj.action
local reasonsWithoutExpiryCheck = configObj.cfg.reasonsWithoutExpiryCheck
local reason = self._protectionObj.reason
local expiryCheckActions = configObj.cfg.expiryCheckActions
local expiry = protectionObj:getExpiry()
local action = protectionObj:getAction()
local reason = protectionObj:getReason()
if not expiry
Line 895 ⟶ 910:
and not reasonsWithoutExpiryCheck[reason]
then
self:setName(configObj.msg[:getMessage('tracking-category-expiry']))
end
return Category.render(self)
Line 909 ⟶ 924:
local configObj = self._configObj
local protectionObj = self._protectionObj
local expiry = protectionObj.expiry
local expiryaction = protectionObj:getExpiry().action
local actionlevel = protectionObj:getAction().level
local level = protectionObj:getLevel()
 
if not protectionObj:isProtected()
or type(expiry) == 'number' and expiry < os.time()
then
self:setName(configObj.msg[:getMessage('tracking-category-incorrect']))
end
return Category.render(self)
Line 935 ⟶ 949:
function TemplateCategory:render()
local configObj = self._configObj
local protectionObjaction = self._protectionObj.action
local titleObjlevel = self._titleObj_protectionObj.level
local namespace = self._titleObj.namespace
local action = protectionObj:getAction()
local level = protectionObj:getLevel()
local namespace = titleObj.namespace
if level == 'templateeditor'
Line 948 ⟶ 959:
)
then
self:setName(configObj.msg[:getMessage('tracking-category-template']))
end
return Category.render(self)
Line 969 ⟶ 980:
 
-- Get data objects
local configObj = Config:new()
if not config then
config = mw.loadData('Module:Protection banner/config')
end
local configObj = config
local protectionObj = Protection:new(args, configObj, titleObj)
 
Line 995 ⟶ 1,003:
 
-- Set the image fields
local bannerConfig = configObj:getBannerConfig(protectionObj.bannerConfig)
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
if isPadlock then
Line 1,035 ⟶ 1,043:
return {
Protection = Protection,
Config = Config,
Blurb = Blurb,
BannerTemplate = BannerTemplate,