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
Enwikipedia>Mr. Stradivarius
(some more work on the Blurb class)
Enwikipedia>Mr. Stradivarius
(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:
local mProtectionLevel = require('Module:Effective protection level')
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 ⟶ 129:
local Image = class('Image')
 
function Image:setFilenameinitialize(filename, configObj, protectionStatusObj, namespacepageDataObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._pageDataObj = pageDataObj
end
 
function Image:setFilename(filename)
if filename then
self._filename = filename
else
local images, =action, configObj:getConfigTable('images')level, reason, namespace
do
local action = protectionStatusObj:getAction()
local levelconfigObj = protectionStatusObj:getLevel()self._configObj
local protectionStatusObj = self._protectionStatusObj
local reason = protectionStatusObj:getReason()
local pageDataObj = self._pageDataObj
images = configObj:getConfigTable('images')
action = protectionStatusObj:getAction()
level = protectionStatusObj:getLevel()
reason = protectionStatusObj:getReason()
namespace = pageDataObj:getNamespace()
end
 
local image
if reason == 'office' or reason == 'reset' then
Line 147 ⟶ 185:
self._filename = image
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
 
function Image:export()
return mFileLink.new(self._filename or 'Transparent.gif')
:width(self._size_width or 20)
:alt(self._alt)
:link(self._link)
Line 164 ⟶ 218:
local Blurb = class('Blurb')
 
function Blurb:initialize(configObj, protectionStatusObj, namespacepageDataObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj)
self._namespace_pageDataObj = namespacepageDataObj
end
 
Line 176 ⟶ 230:
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
local namespace = self._pageDataObj:getNamespace()
return pagetypes[self._namespace] or pagetypes.default or 'page'
return pagetypes[namespace] or pagetypes.default or 'page'
end
 
Line 226 ⟶ 281:
 
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 ⟶ 519:
end
end
 
--------------------------------------------------------------------------------
-- ExpiryCategory class
--------------------------------------------------------------------------------
 
local ExpiryCategory = Category:subclass('ExpiryCategory')
 
--------------------------------------------------------------------------------
Line 430 ⟶ 533:
 
--------------------------------------------------------------------------------
-- ExpiryCategoryCategoryFactory class
--------------------------------------------------------------------------------
 
local ExpiryCategoryCategoryFactory = Category:subclassclass('ExpiryCategoryCategoryFactory')
 
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 ⟶ 572:
function ProtectionBanner.exportToLua(args, title)
title = title or mw.title.getCurrentTitle()
local pstatus = ProtectionStatus.new(args, title)
local cfg = Config:new()
 
-- Get thedata banner template objectobjects
local thePageData = PageData:new(title)
local banner
local theProtectionStatus = ProtectionStatus.new(args, title)
local theConfig = Config:new()
 
local ret = {}
 
-- Render the banner
do
local theBannerFactory = BannerFactory:new(
local bannerClass
if yesno( args.small) then,
theConfig,
bannerClass = Padlock
theProtectionStatus,
else
thePageData
bannerClass = Banner
)
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
banner = bannerClass:new()
end
 
return table.concat(ret)
end
 
function ProtectionBanner._exportClasses()
-- This is used to export the classes for testing purposes.
return {
PageData = PageData,
ProtectionStatus = ProtectionStatus,
Config = Config,
Line 473 ⟶ 622:
Banner = Banner,
Padlock = Padlock,
BannerFactory = BannerFactory,
Category = Category,
ProtectionCategory = ProtectionCategory,
ErrorCategory = ErrorCategory,
ExpiryCategory = ExpiryCategory,
CategoryFactory = CategoryFactory
}
end