Module:Protection banner: Difference between revisions

allow bannerConfig table fields to be functions
Enwikipedia>Mr. Stradivarius
(use a comment instead of a variable assignment to make it clear that protectionObj is the error message)
Enwikipedia>Mr. Stradivarius
(allow bannerConfig table fields to be functions)
Line 316:
 
local Blurb = class('Blurb')
 
Blurb.bannerTextFields = {
text = true,
explanation = true,
tooltip = true,
alt = true,
link = true
}
 
function Blurb:initialize(protectionObj, args, cfg)
self._cfg = cfg
self._protectionObj = protectionObj
self._username_data = args.user{
self._section username = args.sectionuser,
section = args.section
}
end
 
Line 418 ⟶ 428:
-- "disputes", with or without a section link
local disputes = self:_getExpandedMessage('dispute-section-link-display')
if self._section_data.section then
return string.format(
'[[%s:%s#%s|%s]]',
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
self._protectionObj.title.text,
self._section_data.section,
disputes
)
Line 606 ⟶ 616:
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
self._protectionObj.title.text,
self._section_data.section or 'top',
self:_getExpandedMessage('talk-page-link-display')
)
Line 621 ⟶ 631:
function Blurb:_makeVandalTemplateParameter()
return require('Module:Vandal-m')._main{
self._username_data.username or self._protectionObj.title.baseText
}
end
Line 627 ⟶ 637:
-- Public methods --
 
function Blurb:makeReasonTextmakeBannerText(key)
-- Validate input.
local msg = self._protectionObj.bannerConfig.text
if not key or not Blurb.bannerTextFields[key] then
if msg then
error(string.format(
'"%s" is not a valid banner field',
tostring(key)
), 2)
end
 
-- Generate the text.
local msg = self._protectionObj.bannerConfig.text[key]
if type(msg) == 'string' then
return self:_substituteParameters(msg)
elseif type(msg) == 'function' then
local msg = msg(self._protectionObj, self.bannerConfig.alt_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)
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 ⟶ 737:
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
self:setImageWidth(40)
self:setImageTooltip(blurbObj:makeAltTextmakeBannerText('alt')) -- Large banners use the alt text for the tooltip.
self._reasonText = blurbObj:makeReasonTextmakeBannerText('text')
self._explanationText = blurbObj:makeExplanationTextmakeBannerText('explanation')
self._page = protectionObj.title.prefixedText -- Only makes a difference in testing.
end
Line 760 ⟶ 770:
BannerTemplate.initialize(self, protectionObj, cfg) -- This doesn't need the blurb.
self:setImageWidth(20)
self:setImageTooltip(blurbObj:makeTooltipTextmakeBannerText('tooltip'))
self._imageAlt = blurbObj:makeAltTextmakeBannerText('alt')
self._imageLink = blurbObj:makeLinkTextmakeBannerText('link')
self._right = cfg.padlockPositions[protectionObj.action]
or cfg.padlockPositions.default
Line 798 ⟶ 808:
args = args or {}
if not cfg then
cfg = mw.loadDatarequire('Module:Protection banner/config')
end