Modul:Protection banner: Unterschied zwischen den Versionen
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) |
||
| Zeile 10: | Zeile 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 | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Zeile 137: | Zeile 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' | ||
| Zeile 157: | Zeile 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 | ||
| Zeile 192: | Zeile 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 | ||
| Zeile 221: | Zeile 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 | ||
| Zeile 396: | Zeile 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() | ||
| Zeile 429: | Zeile 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') | ||
| Zeile 435: | Zeile 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() | ||
| Zeile 461: | Zeile 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() | ||
| Zeile 604: | Zeile 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 | ||
| Zeile 633: | Zeile 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) | ||
| Zeile 778: | Zeile 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 | ||
| Zeile 813: | Zeile 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 | ||
| Zeile 882: | Zeile 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 | ||
| Zeile 922: | Zeile 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() | ||
| Zeile 933: | Zeile 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) | ||
| Zeile 955: | Zeile 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) | ||
| Zeile 986: | Zeile 948: | ||
) | ) | ||
then | then | ||
self:setName(configObj | self:setName(configObj.msg['tracking-category-template']) | ||
end | end | ||
return Category.render(self) | return Category.render(self) | ||
| Zeile 1.007: | Zeile 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) | ||
| Zeile 1.030: | Zeile 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 | ||
| Zeile 1.070: | Zeile 1.035: | ||
return { | return { | ||
Protection = Protection, | Protection = Protection, | ||
Blurb = Blurb, | Blurb = Blurb, | ||
BannerTemplate = BannerTemplate, | BannerTemplate = BannerTemplate, | ||