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, | 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. | self.action = args.action | ||
else | else | ||
self. | self.action = 'edit' | ||
end | end | ||
end | end | ||
Zeile 36: | Zeile 105: | ||
-- Set level | -- Set level | ||
do | do | ||
local level = effectiveProtectionLevel(self. | 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. | 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. | 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 | local indefStrings = configObj:getConfigTable('indefStrings') | ||
if indefStrings[args.expiry] then | if indefStrings[args.expiry] then | ||
self. | self.expiry = 'indef' | ||
elseif type(args.expiry) == 'number' then | elseif type(args.expiry) == 'number' then | ||
self. | self.expiry = args.expiry | ||
else | else | ||
self. | 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. | self.reason = reason:lower() | ||
end | end | ||
end | end | ||
-- Set protection date | -- Set protection date | ||
self. | self.protectionDate = validateDate(args.date, 'protection date') | ||
end | end | ||
function Protection:isProtected() | function Protection:isProtected() | ||
return self._level ~= '*' | return self._level ~= '*' | ||
end | end | ||
Zeile 154: | Zeile 172: | ||
self._configObj = configObj | self._configObj = configObj | ||
self._protectionObj = protectionObj | self._protectionObj = protectionObj | ||
self._bannerConfig = protectionObj | 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 | 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 | 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 | 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 | local action = self._protectionObj.action | ||
local level = self._protectionObj | 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 | 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 | local action = self._protectionObj.action | ||
local level = self._protectionObj | 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 | local imageLinks = self._configObj:getConfigTable('imageLinks') | ||
local action = self._protectionObj | local action = self._protectionObj.action | ||
local level = self._protectionObj | 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 | 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 | 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 | 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 | local protectionBlurbs = self._configObj:getConfigTable('protectionBlurbs') | ||
local action = self._protectionObj | local action = self._protectionObj.action | ||
local level = self._protectionObj | 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 | 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 | local protectionLevels = self._configObj:getConfigTable('protectionLevels') | ||
local action = self._protectionObj | local action = self._protectionObj.action | ||
local level = self._protectionObj | 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 | 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 | 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 | 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 | local action = protectionObj.action | ||
local level = protectionObj | local level = protectionObj.level | ||
local expiry = protectionObj | 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 | 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 | 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 | 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 | 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 | 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 | local reason = protectionObj.reason | ||
local action = protectionObj | local action = protectionObj.action | ||
local level = protectionObj | local level = protectionObj.level | ||
--[[ | --[[ | ||
Zeile 775: | Zeile 793: | ||
local configOrder = {} | local configOrder = {} | ||
do | do | ||
local reasonsWithNamespacePriority = configObj | 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 | 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 | local reasonsWithoutExpiryCheck = self._configObj:getConfigTable('reasonsWithoutExpiryCheck') | ||
local | local expiryCheckActions = self._configObj:getConfigTable('expiryCheckActions') | ||
local expiry = self._protectionObj.expiry | |||
local | local action = self._protectionObj.action | ||
local | local reason = self._protectionObj.reason | ||
local reason = | |||
if not expiry | if not expiry | ||
Zeile 895: | Zeile 910: | ||
and not reasonsWithoutExpiryCheck[reason] | and not reasonsWithoutExpiryCheck[reason] | ||
then | then | ||
self:setName(configObj | 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 | local action = protectionObj.action | ||
local action = protectionObj | local level = protectionObj.level | ||
local level = protectionObj | |||
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 | 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 | local action = self._protectionObj.action | ||
local | local level = self._protectionObj.level | ||
local namespace = self._titleObj.namespace | |||
local namespace = | |||
if level == 'templateeditor' | if level == 'templateeditor' | ||
Zeile 948: | Zeile 959: | ||
) | ) | ||
then | then | ||
self:setName(configObj | 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 | ||
local configObj = Config:new() | |||
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 | 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, |