Module:Protection banner: Difference between revisions

move image code to the BannerTemplate class, and simplify it
(move section, username and deletion discussion methods out of the ProtectionStatus class, as they don't really have anything to do with protection status)
(move image code to the BannerTemplate class, and simplify it)
Line 422: Line 422:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Image class
-- BannerTemplate class
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local Image = class('Image')
local BannerTemplate = class('BannerTemplate')


function Image:initialize(configObj, protectionStatusObj, titleObj)
function BannerTemplate:initialize(configObj)
self._configObj = configObj
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._titleObj = titleObj
end
end


function Image:setFilename(filename)
function BannerTemplate:setImageFilename(filename, action, level, namespace, expiry)
if filename then
if filename then
self._filename = filename
self._imageFilename = filename
else
return nil
local images, action, level, reason, namespace
end
do
local configObj = self._configObj
if not action or not level then
local protectionStatusObj = self._protectionStatusObj
-- If the filename is not specified, we need the action and the level
local titleObj = self._titleObj
-- to find the image name. The namespace and the expiry are optional,
images = configObj:getConfigTable('images')
-- however.
action = protectionStatusObj:getAction()
return nil
level = protectionStatusObj:getLevel()
end
reason = protectionStatusObj:getReason()
namespace = titleObj:getNamespace()
-- Deal with special cases first.
if (namespace == 10 or namespace == 828) -- Maybe we don't need the namespace check?
and action == 'edit'
and level == 'sysop'
and not expiry
then
-- Fully protected modules and templates get the special red "indef"
-- padlock.
self._imageFilename = self._configObj:getMessage('image-filename-indef')
return nil
end
-- Deal with regular protection types.
local images = self._configObj:getConfigTable('images')
if images[action] then
if images[action][level] then
self._imageFilename = images[action][level]
return nil
elseif images[action].default then
self._imageFilename = images[action].default
return nil
end
end
end


local image
return nil
if reason == 'office' or reason == 'reset' then
image = images.office
elseif namespace == 10 or namespace == 828 then
-- We are in the template or module namespaces.
if level == 'templateeditor' then
image = images.template
elseif level == 'sysop' then
image = images.indef
end
elseif action == 'create' then
image = images.create
elseif action == 'move'
and (
level == 'templateeditor'
or level == 'sysop'
)
then
image = images.move
elseif action == 'edit' then
if level == 'sysop' then
image = images.full
elseif level == 'autoconfirmed' then
image = images.semi
end
elseif action == 'autoreview' then
if level == 'autoconfirmed' then
image = images.pc1
elseif level == 'reviewer' then
image = images.pc2
end
end
self._filename = image
end
end
end


function Image:setWidth(width)
function BannerTemplate:setImageWidth(width)
self._width = width
self._imageWidth = width
end
end


function Image:setAlt(alt)
function BannerTemplate:setImageAlt(alt)
self._alt = alt
self._imageAlt = alt
end
end


function Image:setLink(link)
function BannerTemplate:setImageLink(link)
self._link = link
self._imageLink = link
end
end


function Image:setCaption(caption)
function BannerTemplate:setImageCaption(caption)
self._caption = caption
self._imageCaption = caption
end
end


function Image:export()
function BannerTemplate:renderImage()
return mFileLink.new(self._filename or 'Transparent.gif')
local filename = self._filename
:width(self._width or 20)
or self._configObj:getMessage('image-filename-default')
:alt(self._alt)
or 'Transparent.gif'
:link(self._link)
return mFileLink.new(filename)
:caption(self._caption)
:width(self._imageWidth or 20)
:alt(self._imageAlt)
:link(self._imageLink)
:caption(self._imageCaption)
:render()
:render()
end
--------------------------------------------------------------------------------
-- BannerTemplate class
--------------------------------------------------------------------------------
local BannerTemplate = class('BannerTemplate')
function BannerTemplate:initialize()
end
end


function BannerTemplate:render()
function BannerTemplate:render()
-- Dummy method, to be implemented by the subclasses.
return ''
end
end


Line 527: Line 509:


local Banner = BannerTemplate:subclass('Banner')
local Banner = BannerTemplate:subclass('Banner')
function Banner:initialize()
self:setImageWidth(40)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 533: Line 519:


local Padlock = BannerTemplate:subclass('Padlock')
local Padlock = BannerTemplate:subclass('Padlock')
function Padlock:initialize()
self:setImageWidth(20)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 773: Line 763:
theBanner:setUsername(args.user)
theBanner:setUsername(args.user)
theBanner:setSection(args.section)
theBanner:setSection(args.section)
ret[#ret + 1] = theBanner:export()
ret[#ret + 1] = theBanner:render()
-- Render the categories
-- Render the categories