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 other params
-- 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._config:getMessage('semi-subject-page-links')
local msg = self._configObj:getMessage('semi-subject-page-links')
return self._substituteParameters(msg)
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:setImageAlt(alt)
function BannerTemplate:setImageTooltip(tooltip)
self._imageAlt = alt
self._imageCaption = tooltip
end
 
function BannerTemplate:setImageLink(link)
self._imageLink = link
end
 
function BannerTemplate:setImageCaption(caption)
self._imageCaption = caption
end
end


function BannerTemplate:renderImage()
function BannerTemplate:renderImage()
local filename = self._filename
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, title)
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, title)
return ProtectionBanner.exportToLua(args, titleObj)
end
end


function ProtectionBanner.exportToLua(args, title)
function ProtectionBanner.exportToLua(args, titleObj)
title = title or mw.title.getCurrentTitle()
titleObj = titleObj or mw.title.getCurrentTitle()


-- Get data objects
-- Get data objects
local theConfig = Config:new()
local configObj = Config:new()
local theProtectionStatus = ProtectionStatus:new(args, theConfig, title)
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 theBanner
do
if yesno(args.small) then
-- Get the banner object
theBanner = Padlock.new(theConfig, theProtectionStatus, title)
local isPadlock = yesno(args.small)
else
local bannerObj
theBanner = Banner.new(theConfig, theProtectionStatus, title)
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
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
-- Render the categories
 
return table.concat(ret)
return table.concat(ret)
end
end