Modul:Protection banner: Unterschied zwischen den Versionen

(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)
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, 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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Zeile 28: Zeile 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
Zeile 36: Zeile 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,
Zeile 42: Zeile 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


Zeile 68: Zeile 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
Zeile 82: Zeile 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


Zeile 154: Zeile 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
Zeile 183: Zeile 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
Zeile 232: Zeile 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
Zeile 260: Zeile 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')
Zeile 287: Zeile 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.
Zeile 314: Zeile 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
Zeile 326: Zeile 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
Zeile 358: Zeile 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
Zeile 373: Zeile 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')
Zeile 382: Zeile 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')
Zeile 391: Zeile 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')
Zeile 397: Zeile 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
Zeile 414: Zeile 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)
Zeile 423: Zeile 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
Zeile 440: Zeile 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
Zeile 460: Zeile 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')
Zeile 481: Zeile 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')
Zeile 553: Zeile 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
Zeile 566: Zeile 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
Zeile 595: Zeile 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)
Zeile 729: Zeile 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'
Zeile 740: Zeile 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
Zeile 748: Zeile 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
   
   
--[[
--[[
Zeile 775: Zeile 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
Zeile 844: Zeile 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
Zeile 881: Zeile 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
Zeile 895: Zeile 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)
Zeile 909: Zeile 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)
Zeile 935: Zeile 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'
Zeile 948: Zeile 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)
Zeile 969: Zeile 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)


Zeile 995: Zeile 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
Zeile 1.035: Zeile 1.043:
return {
return {
Protection = Protection,
Protection = Protection,
Config = Config,
Blurb = Blurb,
Blurb = Blurb,
BannerTemplate = BannerTemplate,
BannerTemplate = BannerTemplate,
Anonymer Benutzer