Modul:Protection banner: Unterschied zwischen den Versionen
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) |
||
Zeile 9: | Zeile 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') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 67: | Zeile 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 | ||
Zeile 88: | Zeile 63: | ||
function ProtectionStatus:getExpiry() | function ProtectionStatus:getExpiry() | ||
return self._expiry | return self._expiry | ||
end | |||
function ProtectionStatus:getSection() | |||
return self._section | |||
end | end | ||
Zeile 121: | Zeile 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 | ||
Zeile 129: | Zeile 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 | ||
Zeile 143: | Zeile 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 | ||
Zeile 210: | Zeile 277: | ||
:caption(self._caption) | :caption(self._caption) | ||
:render() | :render() | ||
end | end | ||
Zeile 281: | Zeile 302: | ||
local Padlock = BannerTemplate:subclass('Padlock') | local Padlock = BannerTemplate:subclass('Padlock') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 531: | Zeile 510: | ||
local ErrorCategory = Category:subclass('ErrorCategory') | local ErrorCategory = Category:subclass('ErrorCategory') | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 574: | Zeile 526: | ||
-- Get data objects | -- Get data objects | ||
local theProtectionStatus = ProtectionStatus:new(args, title) | |||
local theProtectionStatus = ProtectionStatus | |||
local theConfig = Config:new() | local theConfig = Config:new() | ||
Zeile 581: | Zeile 532: | ||
-- Render the banner | -- Render the banner | ||
-- Render the categories | -- Render the categories | ||
return table.concat(ret) | return table.concat(ret) | ||
Zeile 614: | Zeile 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, | ||
Zeile 622: | Zeile 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 |