Module:Protection banner: Difference between revisions
simplify the structure a bit and write some more parameter functions
(use factory classes to get the banner and category classes, and add a PageData class so we don't expose all of mw.title to every class) |
(simplify the structure a bit and write some more parameter functions) |
||
Line 9: | Line 9: | ||
local mProtectionLevel = require('Module:Effective protection level') | local mProtectionLevel = require('Module:Effective protection level') | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 67: | Line 43: | ||
end | end | ||
-- Set | -- Set other params | ||
self._reason = args.reason | self._reason = args.reason | ||
self._expiry = args.expiry or 'indef' | self._expiry = args.expiry or 'indef' | ||
self._section = args.section | |||
end | end | ||
Line 88: | Line 63: | ||
function ProtectionStatus:getExpiry() | function ProtectionStatus:getExpiry() | ||
return self._expiry | return self._expiry | ||
end | |||
function ProtectionStatus:getSection() | |||
return self._section | |||
end | end | ||
Line 121: | Line 100: | ||
return nil | return nil | ||
end | end | ||
end | |||
function Config:getMessage(key) | |||
return self._cfg.msg[key] | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- Blurb class | |||
-------------------------------------------------------------------------------- | |||
local Blurb = class('Blurb') | |||
function Blurb:initialize(configObj, protectionStatusObj, titleObj) | |||
self._configObj = configObj | |||
self._protectionStatusObj = protectionStatusObj | |||
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj) | |||
self._titleObj = titleObj | |||
end | |||
function Blurb:_makePagetypeParameter() | |||
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces') | |||
local namespace = self._titleObj:getNamespace() | |||
return pagetypes[namespace] or pagetypes.default or 'page' | |||
end | |||
function Blurb:_substituteParameters(msg) | |||
if not self._params then | |||
local params, parameterFuncs = {}, {} | |||
setmetatable(params, { | |||
__index = function (t, k) | |||
local param | |||
if parameterFuncs[k] then | |||
param = parameterFuncs[k]() | |||
end | |||
param = param or '' | |||
params[k] = param | |||
return param | |||
end | |||
}) | |||
parameterFuncs[1] = function () | |||
-- Intro blurb | |||
local key | |||
local action = self._protectionStatusObj:getAction() | |||
local level = self._protectionStatusObj:getLevel() | |||
if action == 'edit' and level == 'autoconfirmed' then | |||
key = 'reason-text-semi' | |||
elseif action == 'move' then | |||
key = 'reason-text-move' | |||
elseif action == 'create' then | |||
key = 'reason-text-create' | |||
else | |||
key = 'reason-text-default' | |||
end | |||
local msg = self._configObj:getMessage(key) | |||
return self:_substituteParameters(msg) | |||
end | |||
parameterFuncs[2] = function () | |||
-- "until" or "or until" depending on the expiry | |||
local expiry = self._protectionStatusObj:getExpiry() | |||
if expiry then | |||
return 'or until' | |||
else | |||
return 'until' | |||
end | |||
end | |||
parameterFuncs[3] = function () | |||
-- "disputes", with or without a section link | |||
local section = self._protectionStatusObj:getSection() | |||
local disputes = self.configObj:getMessage('dispute-section-link-display') | |||
if section then | |||
return string.format( | |||
'[[%s:%s#%s|%s]]', | |||
mw.site.namespaces[self._titleObj.namespace].talk.name, | |||
self._titleObj.text, | |||
section, | |||
disputes | |||
) | |||
else | |||
return disputes | |||
end | |||
end | |||
self._params = params | |||
end | |||
return mw.message.newRawMessage(msg):params(self._params):plain() | |||
end | end | ||
Line 129: | Line 196: | ||
local Image = class('Image') | local Image = class('Image') | ||
function Image:initialize(configObj, protectionStatusObj, | function Image:initialize(configObj, protectionStatusObj, titleObj) | ||
self._configObj = configObj | self._configObj = configObj | ||
self._protectionStatusObj = protectionStatusObj | self._protectionStatusObj = protectionStatusObj | ||
self. | self._titleObj = titleObj | ||
end | end | ||
Line 143: | Line 210: | ||
local configObj = self._configObj | local configObj = self._configObj | ||
local protectionStatusObj = self._protectionStatusObj | local protectionStatusObj = self._protectionStatusObj | ||
local | local titleObj = self._titleObj | ||
images = configObj:getConfigTable('images') | images = configObj:getConfigTable('images') | ||
action = protectionStatusObj:getAction() | action = protectionStatusObj:getAction() | ||
level = protectionStatusObj:getLevel() | level = protectionStatusObj:getLevel() | ||
reason = protectionStatusObj:getReason() | reason = protectionStatusObj:getReason() | ||
namespace = | namespace = titleObj:getNamespace() | ||
end | end | ||
Line 210: | Line 277: | ||
:caption(self._caption) | :caption(self._caption) | ||
:render() | :render() | ||
end | end | ||
Line 281: | Line 302: | ||
local Padlock = BannerTemplate:subclass('Padlock') | local Padlock = BannerTemplate:subclass('Padlock') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 531: | Line 510: | ||
local ErrorCategory = Category:subclass('ErrorCategory') | local ErrorCategory = Category:subclass('ErrorCategory') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Line 574: | Line 526: | ||
-- Get data objects | -- Get data objects | ||
local theProtectionStatus = ProtectionStatus:new(args, title) | |||
local theProtectionStatus = ProtectionStatus | |||
local theConfig = Config:new() | local theConfig = Config:new() | ||
Line 581: | Line 532: | ||
-- Render the banner | -- Render the banner | ||
-- Render the categories | -- Render the categories | ||
return table.concat(ret) | return table.concat(ret) | ||
Line 614: | Line 541: | ||
-- This is used to export the classes for testing purposes. | -- This is used to export the classes for testing purposes. | ||
return { | return { | ||
ProtectionStatus = ProtectionStatus, | ProtectionStatus = ProtectionStatus, | ||
Config = Config, | Config = Config, | ||
Line 622: | Line 548: | ||
Banner = Banner, | Banner = Banner, | ||
Padlock = Padlock, | Padlock = Padlock, | ||
Category = Category, | Category = Category, | ||
ProtectionCategory = ProtectionCategory, | ProtectionCategory = ProtectionCategory, | ||
ErrorCategory = ErrorCategory, | ErrorCategory = ErrorCategory, | ||
ExpiryCategory = ExpiryCategory, | ExpiryCategory = ExpiryCategory, | ||
} | } | ||
end | end | ||
return ProtectionBanner | return ProtectionBanner |