Module:Protection banner: Difference between revisions

allow bannerConfig table fields to be functions
(use a comment instead of a variable assignment to make it clear that protectionObj is the error message)
(allow bannerConfig table fields to be functions)
Line 316: Line 316:


local Blurb = class('Blurb')
local Blurb = class('Blurb')
Blurb.bannerTextFields = {
text = true,
explanation = true,
tooltip = true,
alt = true,
link = true
}


function Blurb:initialize(protectionObj, args, cfg)
function Blurb:initialize(protectionObj, args, cfg)
self._cfg = cfg
self._cfg = cfg
self._protectionObj = protectionObj
self._protectionObj = protectionObj
self._username = args.user
self._data = {
self._section = args.section
username = args.user,
section = args.section
}
end
end


Line 418: Line 428:
-- "disputes", with or without a section link
-- "disputes", with or without a section link
local disputes = self:_getExpandedMessage('dispute-section-link-display')
local disputes = self:_getExpandedMessage('dispute-section-link-display')
if self._section then
if self._data.section then
return string.format(
return string.format(
'[[%s:%s#%s|%s]]',
'[[%s:%s#%s|%s]]',
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
self._protectionObj.title.text,
self._protectionObj.title.text,
self._section,
self._data.section,
disputes
disputes
)
)
Line 606: Line 616:
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
self._protectionObj.title.text,
self._protectionObj.title.text,
self._section or 'top',
self._data.section or 'top',
self:_getExpandedMessage('talk-page-link-display')
self:_getExpandedMessage('talk-page-link-display')
)
)
Line 621: Line 631:
function Blurb:_makeVandalTemplateParameter()
function Blurb:_makeVandalTemplateParameter()
return require('Module:Vandal-m')._main{
return require('Module:Vandal-m')._main{
self._username or self._protectionObj.title.baseText
self._data.username or self._protectionObj.title.baseText
}
}
end
end
Line 627: Line 637:
-- Public methods --
-- Public methods --


function Blurb:makeReasonText()
function Blurb:makeBannerText(key)
local msg = self._protectionObj.bannerConfig.text
-- Validate input.
if msg then
if not key or not Blurb.bannerTextFields[key] then
error(string.format(
'"%s" is not a valid banner field',
tostring(key)
), 2)
end
 
-- Generate the text.
local msg = self._protectionObj.bannerConfig[key]
if type(msg) == 'string' then
return self:_substituteParameters(msg)
elseif type(msg) == 'function' then
msg = msg(self._protectionObj, self._data)
if type(msg) ~= 'string' then
error(string.format(
'bad output from banner config function with key "%s"'
.. ' (expected string, got %s)',
tostring(key),
type(msg)
))
end
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
end
end
end
end
 
function Blurb:makeExplanationText()
local msg = self._protectionObj.bannerConfig.explanation
return self:_substituteParameters(msg)
end
 
function Blurb:makeTooltipText()
local msg = self._protectionObj.bannerConfig.tooltip
return self:_substituteParameters(msg)
end
 
function Blurb:makeAltText()
local msg = self._protectionObj.bannerConfig.alt
return self:_substituteParameters(msg)
end
 
function Blurb:makeLinkText()
local msg = self._protectionObj.bannerConfig.link
return self:_substituteParameters(msg)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 727: Line 737:
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
self:setImageWidth(40)
self:setImageWidth(40)
self:setImageTooltip(blurbObj:makeAltText()) -- Large banners use the alt text for the tooltip.
self:setImageTooltip(blurbObj:makeBannerText('alt')) -- Large banners use the alt text for the tooltip.
self._reasonText = blurbObj:makeReasonText()
self._reasonText = blurbObj:makeBannerText('text')
self._explanationText = blurbObj:makeExplanationText()
self._explanationText = blurbObj:makeBannerText('explanation')
self._page = protectionObj.title.prefixedText -- Only makes a difference in testing.
self._page = protectionObj.title.prefixedText -- Only makes a difference in testing.
end
end
Line 760: Line 770:
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
self:setImageWidth(20)
self:setImageWidth(20)
self:setImageTooltip(blurbObj:makeTooltipText())
self:setImageTooltip(blurbObj:makeBannerText('tooltip'))
self._imageAlt = blurbObj:makeAltText()
self._imageAlt = blurbObj:makeBannerText('alt')
self._imageLink = blurbObj:makeLinkText()
self._imageLink = blurbObj:makeBannerText('link')
self._right = cfg.padlockPositions[protectionObj.action]
self._right = cfg.padlockPositions[protectionObj.action]
or cfg.padlockPositions.default
or cfg.padlockPositions.default
Line 798: Line 808:
args = args or {}
args = args or {}
if not cfg then
if not cfg then
cfg = mw.loadData('Module:Protection banner/config')
cfg = require('Module:Protection banner/config')
end
end