Module:Protection banner: Difference between revisions
Add render methods to the Padlock and Banner classes, and call them from the exportToLua function. Now we have something to look at, yay!
(allow specifying a data table to config objects for testing purposes) |
(Add render methods to the Padlock and Banner classes, and call them from the exportToLua function. Now we have something to look at, yay!) |
||
Line 141: | Line 141: | ||
end | end | ||
-- Set | -- Set reason | ||
self._reason = args.reason | if args.reason then | ||
self._reason = args.reason:lower() | |||
end | |||
-- Set protection date | |||
self._protectionDate = validateDate(args.date, 'protection date') | self._protectionDate = validateDate(args.date, 'protection date') | ||
end | end | ||
Line 393: | Line 397: | ||
-- parameter $10 | -- parameter $10 | ||
local action = self._protectionStatusObj:getAction() | local action = self._protectionStatusObj:getAction() | ||
local pagename = self._titleObj.prefixedText | |||
if action == 'autoreview' then | if action == 'autoreview' then | ||
-- We need the pending changes log. | -- We need the pending changes log. | ||
Line 464: | Line 469: | ||
-- Don't display these links if we are on a talk page. | -- Don't display these links if we are on a talk page. | ||
if not self._titleObj.isTalkPage then | if not self._titleObj.isTalkPage then | ||
local msg = self. | local msg = self._configObj:getMessage('semi-subject-page-links') | ||
return self | return self:_substituteParameters(msg) | ||
end | end | ||
end | end | ||
Line 535: | Line 540: | ||
function Blurb:makeAltText() | function Blurb:makeAltText() | ||
local msg = self._bannerConfig.alt | local msg = self._bannerConfig.alt | ||
return self:_substituteParameters(msg) | |||
end | |||
function Blurb:makeLinkText() | |||
local msg = self._bannerConfig.link | |||
return self:_substituteParameters(msg) | return self:_substituteParameters(msg) | ||
end | end | ||
Line 560: | Line 570: | ||
return nil | return nil | ||
end | end | ||
-- Deal with special cases first. | -- Deal with special cases first. | ||
if (namespace == 10 or namespace == 828) -- Maybe we don't need the namespace check? | if (namespace == 10 or namespace == 828) -- Maybe we don't need the namespace check? | ||
Line 592: | Line 602: | ||
end | end | ||
function BannerTemplate: | function BannerTemplate:setImageTooltip(tooltip) | ||
self._imageCaption = tooltip | |||
self._imageCaption = | |||
end | end | ||
function BannerTemplate:renderImage() | function BannerTemplate:renderImage() | ||
local filename = self. | local filename = self._imageFilename | ||
or self._configObj:getMessage('image-filename-default') | or self._configObj:getMessage('image-filename-default') | ||
or 'Transparent.gif' | or 'Transparent.gif' | ||
Line 627: | Line 629: | ||
local Banner = BannerTemplate:subclass('Banner') | local Banner = BannerTemplate:subclass('Banner') | ||
function Banner:initialize() | function Banner:initialize(configObj) | ||
BannerTemplate.initialize(self, configObj) | |||
self:setImageWidth(40) | self:setImageWidth(40) | ||
end | |||
function Banner:setReasonText(s) | |||
self._reasonText = s | |||
end | |||
function Banner:setExplanationText(s) | |||
self._explanationText = s | |||
end | |||
function Banner:render(page) | |||
-- Renders the banner. | |||
-- The page parameter specifies the page to generate the banner for, for | |||
-- testing purposes. | |||
mMessageBox = mMessageBox or require('Module:Message box') | |||
local reasonText = self._reasonText or error('no reason text set') | |||
local explanationText = self._explanationText | |||
local mbargs = { | |||
page = page, | |||
type = 'protection', | |||
image = self:renderImage(), | |||
text = string.format( | |||
"'''%s'''%s", | |||
reasonText, | |||
explanationText and '<br />' .. explanationText or '' | |||
) | |||
} | |||
return mMessageBox.main('mbox', mbargs) | |||
end | end | ||
Line 637: | Line 668: | ||
local Padlock = BannerTemplate:subclass('Padlock') | local Padlock = BannerTemplate:subclass('Padlock') | ||
function Padlock:initialize() | function Padlock:initialize(configObj) | ||
BannerTemplate.initialize(self, configObj) | |||
self:setImageWidth(20) | self:setImageWidth(20) | ||
end | |||
function Padlock:setImageAlt(alt) | |||
self._imageAlt = alt | |||
end | |||
function Padlock:setImageLink(link) | |||
self._imageLink = link | |||
end | |||
function Padlock:setRight(px) | |||
self._right = px | |||
end | |||
function Padlock:render() | |||
local root = mw.html.create('div') | |||
root | |||
:addClass('metadata topicon nopopups') | |||
:attr('id', 'protected-icon') | |||
:css{display = 'none', right = self._right or '55px'} | |||
:wikitext(self:renderImage()) | |||
return tostring(root) | |||
end | end | ||
Line 855: | Line 909: | ||
local ProtectionBanner = {} | local ProtectionBanner = {} | ||
function ProtectionBanner.exportToWiki(frame, | function ProtectionBanner.exportToWiki(frame, titleObj) | ||
mArguments = mArguments or require('Module:Arguments') | mArguments = mArguments or require('Module:Arguments') | ||
local args = mArguments.getArgs(frame) | local args = mArguments.getArgs(frame) | ||
return ProtectionBanner.exportToLua(args, | return ProtectionBanner.exportToLua(args, titleObj) | ||
end | end | ||
function ProtectionBanner.exportToLua(args, | function ProtectionBanner.exportToLua(args, titleObj) | ||
titleObj = titleObj or mw.title.getCurrentTitle() | |||
-- Get data objects | -- Get data objects | ||
local | local configObj = Config:new() | ||
local | local protectionObj = ProtectionStatus:new(args, configObj, titleObj) | ||
-- Initialise the blurb object | |||
local blurbObj = Blurb:new(configObj, protectionObj, titleObj) | |||
blurbObj:setDeletionDiscussionPage(args.xfd) | |||
blurbObj:setUsername(args.user) | |||
blurbObj:setSection(args.section) | |||
local ret = {} | local ret = {} | ||
-- Render the banner | -- Render the banner | ||
local | do | ||
-- Get the banner object | |||
local isPadlock = yesno(args.small) | |||
local bannerObj | |||
if isPadlock then | |||
bannerObj = Padlock:new(configObj) | |||
else | |||
bannerObj = Banner:new(configObj) | |||
end | |||
-- Set the image fields | |||
local bannerConfig = configObj:getBannerConfig(protectionObj) | |||
local imageFilename = bannerConfig.image | |||
bannerObj:setImageFilename( | |||
imageFilename, | |||
protectionObj:getAction(), | |||
protectionObj:getLevel(), | |||
titleObj.namespace, | |||
protectionObj:getExpiry() | |||
) | |||
if isPadlock then | |||
bannerObj:setImageTooltip(blurbObj:makeTooltipText()) | |||
bannerObj:setImageAlt(blurbObj:makeAltText()) | |||
bannerObj:setImageLink(blurbObj:makeLinkText()) | |||
else | |||
-- Large banners use the alt text for the tooltip. | |||
bannerObj:setImageTooltip(blurbObj:makeAltText()) | |||
end | |||
-- Set the text fields | |||
if not isPadlock then | |||
bannerObj:setReasonText(blurbObj:makeReasonText()) | |||
bannerObj:setExplanationText(blurbObj:makeExplanationText()) | |||
end | |||
ret[#ret + 1] = bannerObj:render() | |||
end | end | ||
-- Render the categories | -- Render the categories | ||
return table.concat(ret) | return table.concat(ret) | ||
end | end |