Anonymous user
Module:Protection banner: Difference between revisions
simplify the structure a bit and write some more parameter functions
Enwikipedia>Mr. Stradivarius (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) |
Enwikipedia>Mr. Stradivarius (simplify the structure a bit and write some more parameter functions) |
||
Line 9:
local mProtectionLevel = require('Module:Effective protection level')
local yesno = require('Module:Yesno')
--------------------------------------------------------------------------------
Line 67 ⟶ 43:
end
-- Set
self._reason = args.reason
self._expiry = args.expiry or 'indef'
self._section = args.section
end
Line 88 ⟶ 63:
function ProtectionStatus:getExpiry()
return self._expiry
end
function ProtectionStatus:getSection()
return self._section
end
Line 121 ⟶ 100:
return nil
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
Line 129 ⟶ 196:
local Image = class('Image')
function Image:initialize(configObj, protectionStatusObj,
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self.
end
Line 143 ⟶ 210:
local configObj = self._configObj
local protectionStatusObj = self._protectionStatusObj
local
images = configObj:getConfigTable('images')
action = protectionStatusObj:getAction()
level = protectionStatusObj:getLevel()
reason = protectionStatusObj:getReason()
namespace =
end
Line 210 ⟶ 277:
:caption(self._caption)
:render()
end
Line 281 ⟶ 302:
local Padlock = BannerTemplate:subclass('Padlock')
--------------------------------------------------------------------------------
Line 531 ⟶ 510:
local ErrorCategory = Category:subclass('ErrorCategory')
--------------------------------------------------------------------------------
Line 574 ⟶ 526:
-- Get data objects
local
local theConfig = Config:new()
Line 581 ⟶ 532:
-- Render the banner
-- Render the categories
return table.concat(ret)
Line 614 ⟶ 541:
-- This is used to export the classes for testing purposes.
return {
ProtectionStatus = ProtectionStatus,
Config = Config,
Line 622 ⟶ 548:
Banner = Banner,
Padlock = Padlock,
Category = Category,
ProtectionCategory = ProtectionCategory,
ErrorCategory = ErrorCategory,
ExpiryCategory = ExpiryCategory,
}
end
|