Module:Protection banner: Difference between revisions
get rid of the config class while still allowing replacing it for testing
(don't keep the whole export table around when we only need one thing from it) |
(get rid of the config class while still allowing replacing it for testing) |
||
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 | local mArguments, mMessageBox, lang, config | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 137: | Line 68: | ||
-- Set expiry | -- Set expiry | ||
if args.expiry then | if args.expiry then | ||
local indefStrings = configObj | local indefStrings = configObj.cfg.indefStrings | ||
if indefStrings[args.expiry] then | if indefStrings[args.expiry] then | ||
self._expiry = 'indef' | self._expiry = 'indef' | ||
Line 157: | Line 88: | ||
-- 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 | end | ||
Line 192: | Line 154: | ||
self._configObj = configObj | self._configObj = configObj | ||
self._protectionObj = protectionObj | self._protectionObj = protectionObj | ||
self._bannerConfig = | self._bannerConfig = protectionObj.bannerConfig | ||
self._titleObj = titleObj | self._titleObj = titleObj | ||
end | end | ||
Line 221: | Line 183: | ||
function Blurb:_getExpandedMessage(msg) | function Blurb:_getExpandedMessage(msg) | ||
local msg = self._configObj | local msg = self._configObj.msg[msg] | ||
return self:_substituteParameters(msg) | return self:_substituteParameters(msg) | ||
end | end | ||
Line 396: | Line 358: | ||
function Blurb:_makeImageLinkParameter() | function Blurb:_makeImageLinkParameter() | ||
local imageLinks = self._configObj | local imageLinks = self._configObj.cfg.imageLinks | ||
local action = self._protectionObj:getAction() | local action = self._protectionObj:getAction() | ||
local level = self._protectionObj:getLevel() | local level = self._protectionObj:getLevel() | ||
Line 429: | Line 391: | ||
function Blurb:_makePagetypeParameter() | function Blurb:_makePagetypeParameter() | ||
local pagetypes = self._configObj | local pagetypes = self._configObj.cfg.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 435: | Line 397: | ||
function Blurb:_makeProtectionBlurbParameter() | function Blurb:_makeProtectionBlurbParameter() | ||
local protectionBlurbs = self._configObj | local protectionBlurbs = self._configObj.cfg.protectionBlurbs | ||
local action = self._protectionObj:getAction() | local action = self._protectionObj:getAction() | ||
local level = self._protectionObj:getLevel() | local level = self._protectionObj:getLevel() | ||
Line 461: | Line 423: | ||
function Blurb:_makeProtectionLevelParameter() | function Blurb:_makeProtectionLevelParameter() | ||
local protectionLevels = self._configObj | local protectionLevels = self._configObj.cfg.protectionLevels | ||
local action = self._protectionObj:getAction() | local action = self._protectionObj:getAction() | ||
local level = self._protectionObj:getLevel() | local level = self._protectionObj:getLevel() | ||
Line 604: | Line 566: | ||
-- 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 | self._imageFilename = self._configObj.msg['image-filename-indef'] | ||
return nil | return nil | ||
end | end | ||
-- Deal with regular protection types. | -- Deal with regular protection types. | ||
local images = self._configObj | local images = self._configObj.cfg.images | ||
if images[action] then | if images[action] then | ||
if images[action][level] then | if images[action][level] then | ||
Line 633: | Line 595: | ||
function BannerTemplate:renderImage() | function BannerTemplate:renderImage() | ||
local filename = self._imageFilename | local filename = self._imageFilename | ||
or self._configObj | or self._configObj.msg['image-filename-default'] | ||
or 'Transparent.gif' | or 'Transparent.gif' | ||
return newFileLink(filename) | return newFileLink(filename) | ||
Line 778: | Line 740: | ||
do | do | ||
local namespace = titleObj.namespace | local namespace = titleObj.namespace | ||
local categoryNamespaces = configObj | local categoryNamespaces = configObj.cfg.categoryNamespaceKeys | ||
nskey = categoryNamespaces[namespace] | nskey = categoryNamespaces[namespace] | ||
if not nskey and namespace % 2 == 1 then | if not nskey and namespace % 2 == 1 then | ||
Line 813: | Line 775: | ||
local configOrder = {} | local configOrder = {} | ||
do | do | ||
local reasonsWithNamespacePriority = configObj | local reasonsWithNamespacePriority = configObj.cfg.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 882: | Line 844: | ||
-- pos field in the property table. | -- pos field in the property table. | ||
--]] | --]] | ||
local cats = configObj | local cats = configObj.cfg.protectionCategories | ||
local cat | local cat | ||
for i = 1, 2^noActive do | for i = 1, 2^noActive do | ||
Line 922: | Line 884: | ||
local protectionObj = self._protectionObj | local protectionObj = self._protectionObj | ||
local reasonsWithoutExpiryCheck = configObj | local reasonsWithoutExpiryCheck = configObj.cfg.reasonsWithoutExpiryCheck | ||
local expiryCheckActions = configObj | local expiryCheckActions = configObj.cfg.expiryCheckActions | ||
local expiry = protectionObj:getExpiry() | local expiry = protectionObj:getExpiry() | ||
local action = protectionObj:getAction() | local action = protectionObj:getAction() | ||
Line 933: | Line 895: | ||
and not reasonsWithoutExpiryCheck[reason] | and not reasonsWithoutExpiryCheck[reason] | ||
then | then | ||
self:setName(configObj | self:setName(configObj.msg['tracking-category-expiry']) | ||
end | end | ||
return Category.render(self) | return Category.render(self) | ||
Line 955: | Line 917: | ||
or type(expiry) == 'number' and expiry < os.time() | or type(expiry) == 'number' and expiry < os.time() | ||
then | then | ||
self:setName(configObj | self:setName(configObj.msg['tracking-category-incorrect']) | ||
end | end | ||
return Category.render(self) | return Category.render(self) | ||
Line 986: | Line 948: | ||
) | ) | ||
then | then | ||
self:setName(configObj | self:setName(configObj.msg['tracking-category-template']) | ||
end | end | ||
return Category.render(self) | return Category.render(self) | ||
Line 1,007: | Line 969: | ||
-- Get data objects | -- Get data objects | ||
local configObj = | if not config then | ||
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 1,030: | Line 995: | ||
-- Set the image fields | -- Set the image fields | ||
local bannerConfig = | local bannerConfig = protectionObj.bannerConfig | ||
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj) | bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj) | ||
if isPadlock then | if isPadlock then | ||
Line 1,070: | Line 1,035: | ||
return { | return { | ||
Protection = Protection, | Protection = Protection, | ||
Blurb = Blurb, | Blurb = Blurb, | ||
BannerTemplate = BannerTemplate, | BannerTemplate = BannerTemplate, |