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
 
--------------------------------------------------------------------------------
-- 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:getAction()
local level = protectionObj:getLevel()
local reason = protectionObj:getReason()
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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Zeile 137: Zeile 68:
-- Set expiry
-- Set expiry
if args.expiry then
if args.expiry then
local indefStrings = configObj:getConfigTable('indefStrings')
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 = configObj:getBannerConfig(protectionObj)
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:getMessage(msg)
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:getConfigTable('imageLinks')
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:getConfigTable('pagetypes')
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:getConfigTable('protectionBlurbs')
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:getConfigTable('protectionLevels')
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:getMessage('image-filename-indef')
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:getConfigTable('images')
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:getMessage('image-filename-default')
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:getConfigTable('categoryNamespaceKeys')
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:getConfigTable('reasonsWithNamespacePriority')
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:getConfigTable('protectionCategories')
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:getConfigTable('reasonsWithoutExpiryCheck')
local reasonsWithoutExpiryCheck = configObj.cfg.reasonsWithoutExpiryCheck
local expiryCheckActions = configObj:getConfigTable('expiryCheckActions')
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:getMessage('tracking-category-expiry'))
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:getMessage('tracking-category-incorrect'))
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:getMessage('tracking-category-template'))
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 = Config:new()
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 = configObj:getBannerConfig(protectionObj)
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,
Config = Config,
Blurb = Blurb,
Blurb = Blurb,
BannerTemplate = BannerTemplate,
BannerTemplate = BannerTemplate,
Anonymer Benutzer