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!
Enwikipedia>Mr. Stradivarius
(allow specifying a data table to config objects for testing purposes)
Enwikipedia>Mr. Stradivarius
(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:
end
 
-- Set other paramsreason
self._reason =if args.reason then
self._reason = args.reason:lower()
end
 
-- Set protection date
self._protectionDate = validateDate(args.date, 'protection date')
end
Line 393 ⟶ 397:
-- parameter $10
local action = self._protectionStatusObj:getAction()
local pagename = self._titleObj.prefixedText
if action == 'autoreview' then
-- We need the pending changes log.
Line 464 ⟶ 469:
-- Don't display these links if we are on a talk page.
if not self._titleObj.isTalkPage then
local msg = self._config_configObj:getMessage('semi-subject-page-links')
return self.:_substituteParameters(msg)
end
end
Line 535 ⟶ 540:
function Blurb:makeAltText()
local msg = self._bannerConfig.alt
return self:_substituteParameters(msg)
end
 
function Blurb:makeLinkText()
local msg = self._bannerConfig.link
return self:_substituteParameters(msg)
end
Line 560 ⟶ 570:
return nil
end
 
-- Deal with special cases first.
if (namespace == 10 or namespace == 828) -- Maybe we don't need the namespace check?
Line 592 ⟶ 602:
end
 
function BannerTemplate:setImageAltsetImageTooltip(alttooltip)
self._imageAlt_imageCaption = alttooltip
end
 
function BannerTemplate:setImageLink(link)
self._imageLink = link
end
 
function BannerTemplate:setImageCaption(caption)
self._imageCaption = caption
end
 
function BannerTemplate:renderImage()
local filename = self._filename_imageFilename
or self._configObj:getMessage('image-filename-default')
or 'Transparent.gif'
Line 627 ⟶ 629:
local Banner = BannerTemplate:subclass('Banner')
 
function Banner:initialize(configObj)
BannerTemplate.initialize(self, configObj)
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
 
Line 637 ⟶ 668:
local Padlock = BannerTemplate:subclass('Padlock')
 
function Padlock:initialize(configObj)
BannerTemplate.initialize(self, configObj)
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
 
Line 855 ⟶ 909:
local ProtectionBanner = {}
 
function ProtectionBanner.exportToWiki(frame, titletitleObj)
mArguments = mArguments or require('Module:Arguments')
local args = mArguments.getArgs(frame)
return ProtectionBanner.exportToLua(args, titletitleObj)
end
 
function ProtectionBanner.exportToLua(args, titletitleObj)
titletitleObj = titletitleObj or mw.title.getCurrentTitle()
 
-- Get data objects
local theConfigconfigObj = Config:new()
local theProtectionStatusprotectionObj = ProtectionStatus:new(args, theConfigconfigObj, titletitleObj)
 
-- 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 = {}
 
-- Render the banner
do
local theBanner
-- Get the banner object
if yesno(args.small) then
local isPadlock = yesno(args.small)
theBanner = Padlock.new(theConfig, theProtectionStatus, title)
local bannerObj
else
if isPadlock then
theBanner = Banner.new(theConfig, theProtectionStatus, title)
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
theBanner = Padlock.new(theConfig, theProtectionStatus, title)
theBanner:setDeletionDiscussionPage(args.xfd)
theBanner:setUsername(args.user)
theBanner:setSection(args.section)
ret[#ret + 1] = theBanner:render()
-- Render the categories
 
return table.concat(ret)
end