Jump to content

Module:Protection banner: Difference between revisions

use factory classes to get the banner and category classes, and add a PageData class so we don't expose all of mw.title to every class
(some more work on the Blurb class)
(use factory classes to get the banner and category classes, and add a PageData class so we don't expose all of mw.title to every class)
Line 9: Line 9:
local mProtectionLevel = require('Module:Effective protection level')
local mProtectionLevel = require('Module:Effective protection level')
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
--------------------------------------------------------------------------------
-- PageData class
--------------------------------------------------------------------------------
--[[
-- This class represents a MediaWiki page, just as the mw.title object does.
-- The difference is that this class is much simpler, using only the fields
-- necessary for this module. This is to keep the module extensible while
-- keeping the code as simple as possible, e.g. this way we do not expose
-- mw.title's protectionLevels property to classes that only need to know a
-- page's namespace. The "data" in PageData is so that this class can be more
-- easily differentiated with mw.title.
--]]
local PageData = class('PageData')
function PageData:initialize(titleObj)
self._namespace = titleObj.namespace
end
function PageData:getNamespace()
return self._namespace
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 105: Line 129:
local Image = class('Image')
local Image = class('Image')


function Image:setFilename(filename, configObj, protectionStatusObj, namespace)
function Image:initialize(configObj, protectionStatusObj, pageDataObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._pageDataObj = pageDataObj
end
 
function Image:setFilename(filename)
if filename then
if filename then
self._filename = filename
self._filename = filename
else
else
local images = configObj:getConfigTable('images')
local images, action, level, reason, namespace
local action = protectionStatusObj:getAction()
do
local level = protectionStatusObj:getLevel()
local configObj = self._configObj
local reason = protectionStatusObj:getReason()
local protectionStatusObj = self._protectionStatusObj
local pageDataObj = self._pageDataObj
images = configObj:getConfigTable('images')
action = protectionStatusObj:getAction()
level = protectionStatusObj:getLevel()
reason = protectionStatusObj:getReason()
namespace = pageDataObj:getNamespace()
end
 
local image
local image
if reason == 'office' or reason == 'reset' then
if reason == 'office' or reason == 'reset' then
Line 147: Line 185:
self._filename = image
self._filename = image
end
end
end
function Image:setWidth(width)
self._width = width
end
function Image:setAlt(alt)
self._alt = alt
end
function Image:setLink(link)
self._link = link
end
function Image:setCaption(caption)
self._caption = caption
end
end


function Image:export()
function Image:export()
return mFileLink.new(self._filename or 'Transparent.gif')
return mFileLink.new(self._filename or 'Transparent.gif')
:width(self._size or 20)
:width(self._width or 20)
:alt(self._alt)
:alt(self._alt)
:link(self._link)
:link(self._link)
Line 164: Line 218:
local Blurb = class('Blurb')
local Blurb = class('Blurb')


function Blurb:initialize(configObj, protectionStatusObj, namespace)
function Blurb:initialize(configObj, protectionStatusObj, pageDataObj)
self._configObj = configObj
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._protectionStatusObj = protectionStatusObj
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj)
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj)
self._namespace = namespace
self._pageDataObj = pageDataObj
end
end


Line 176: Line 230:
function Blurb:_makePagetypeParameter()
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
return pagetypes[self._namespace] or pagetypes.default or 'page'
local namespace = self._pageDataObj:getNamespace()
return pagetypes[namespace] or pagetypes.default or 'page'
end
end


Line 226: Line 281:


local Padlock = BannerTemplate:subclass('Padlock')
local Padlock = BannerTemplate:subclass('Padlock')
--------------------------------------------------------------------------------
-- BannerFactory class
--------------------------------------------------------------------------------
local BannerFactory = class('BannerFactory')
function BannerFactory:initialize(
args,
configObj,
protectionStatusObj,
pageDataObj
)
-- Set dependent objects
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._pageDataObj = pageDataObj
-- Set object paradigm to use
if yesno(args.small) then
self._paradigm = 'padlock'
else
self._paradigm = 'banner'
end
end
function BannerFactory:newBannerTemplate()
end
function BannerFactory:newBlurb()
end
function BannerFactory:newImage()
local image = Image:new()
if self._paradigm == 'padlock' then
image:setWidth(20)
else
image:setWidth(40)
end
return image
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 422: Line 519:
end
end
end
end
--------------------------------------------------------------------------------
-- ExpiryCategory class
--------------------------------------------------------------------------------
local ExpiryCategory = Category:subclass('ExpiryCategory')


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 430: Line 533:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ExpiryCategory class
-- CategoryFactory class
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local ExpiryCategory = Category:subclass('ExpiryCategory')
local CategoryFactory = class('CategoryFactory')
 
function CategoryFactory:initialize(
configObj,
protectionStatusObj,
pageDataObj
)
 
-- Set dependent objects
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._pageDataObj = pageDataObj
end
 
function CategoryFactory:getCategoryObjects()
local ret = {}
local classes = {ProtectionCategory, ExpiryCategory, ErrorCategory}
for i, aClass in ipairs(classes) do
ret[#ret + 1] = aClass:new()
end
return ret
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 448: Line 572:
function ProtectionBanner.exportToLua(args, title)
function ProtectionBanner.exportToLua(args, title)
title = title or mw.title.getCurrentTitle()
title = title or mw.title.getCurrentTitle()
local pstatus = ProtectionStatus.new(args, title)
local cfg = Config:new()


-- Get the banner template object
-- Get data objects
local banner
local thePageData = PageData:new(title)
local theProtectionStatus = ProtectionStatus.new(args, title)
local theConfig = Config:new()
 
local ret = {}
 
-- Render the banner
do
do
local bannerClass
local theBannerFactory = BannerFactory:new(
if yesno(args.small) then
args,
bannerClass = Padlock
theConfig,
else
theProtectionStatus,
bannerClass = Banner
thePageData
)
local banner = theBannerFactory:newBannerTemplate()
local image = theBannerFactory:newImage()
local blurb = theBannerFactory:newBlurb()
--TODO: actual rendering
ret[#ret + 1] = banner:export()
end
 
-- Render the categories
do
local theCategoryFactory = CategoryFactory:new(
theConfig,
theProtectionStatus,
thePageData
)
local categoryObjects = theCategoryFactory:getCategoryObjects()
for i, obj in ipairs(categoryObjects) do
ret[#ret + 1] = obj:export()
end
end
banner = bannerClass:new()
end
end
return table.concat(ret)
end
end


function ProtectionBanner._exportClasses()
function ProtectionBanner._exportClasses()
-- This is used to export the classes for testing purposes.
return {
return {
PageData = PageData,
ProtectionStatus = ProtectionStatus,
ProtectionStatus = ProtectionStatus,
Config = Config,
Config = Config,
Line 473: Line 622:
Banner = Banner,
Banner = Banner,
Padlock = Padlock,
Padlock = Padlock,
BannerFactory = BannerFactory,
Category = Category,
Category = Category,
ProtectionCategory = ProtectionCategory,
ProtectionCategory = ProtectionCategory,
ErrorCategory = ErrorCategory,
ErrorCategory = ErrorCategory,
ExpiryCategory = ExpiryCategory
ExpiryCategory = ExpiryCategory,
CategoryFactory = CategoryFactory
}
}
end
end


return ProtectionBanner
return ProtectionBanner
Cookies help us deliver our services. By using our services, you agree to our use of cookies.