Open main menu

Changes

Module:Protection banner

3,772 bytes added, 15:24, 9 June 2014
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
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
--------------------------------------------------------------------------------
local Image = class('Image')
function Image:setFilenameinitialize(filename, configObj, protectionStatusObj, namespacepageDataObj) self._configObj = configObj self._protectionStatusObj = protectionStatusObj self._pageDataObj = pageDataObjend function Image:setFilename(filename)
if filename then
self._filename = filename
else
local images, action, level, reason, namespace do local configObj = self._configObj local protectionStatusObj = self._protectionStatusObj local pageDataObj = self._pageDataObj images = configObj:getConfigTable('images') local action = protectionStatusObj:getAction() local level = protectionStatusObj:getLevel() local reason = protectionStatusObj:getReason() namespace = pageDataObj:getNamespace() end 
local image
if reason == 'office' or reason == 'reset' then
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)
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
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
local namespace = self._pageDataObj:getNamespace() return pagetypes[self._namespacenamespace] or pagetypes.default or 'page'
end
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
--------------------------------------------------------------------------------
end
end
 
--------------------------------------------------------------------------------
-- ExpiryCategory class
--------------------------------------------------------------------------------
 
local ExpiryCategory = Category:subclass('ExpiryCategory')
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ExpiryCategory CategoryFactory class
--------------------------------------------------------------------------------
local CategoryFactory = class('CategoryFactory') function CategoryFactory:initialize( configObj, protectionStatusObj, pageDataObj )  -- Set dependent objects self._configObj = configObj self._protectionStatusObj = protectionStatusObj self._pageDataObj = pageDataObjend function CategoryFactory:getCategoryObjects() local ret = {} local classes = {ProtectionCategory, ExpiryCategory , ErrorCategory} for i, aClass in ipairs(classes) do ret[#ret + 1] = CategoryaClass:subclassnew('ExpiryCategory') end return retend
--------------------------------------------------------------------------------
function ProtectionBanner.exportToLua(args, title)
title = title or mw.title.getCurrentTitle()
local pstatus = ProtectionStatus.new(args, title)
local cfg = Config:new()
-- Get the banner template objectdata objects local thePageData = PageData:new(title) local theProtectionStatus = ProtectionStatus.new(args, title) local theConfig = Config:new()  local ret = {}  -- Render the banner
do
local bannerClasstheBannerFactory = BannerFactory:new( args, theConfig, theProtectionStatus, thePageData ) local banner = theBannerFactory:newBannerTemplate() local image = theBannerFactory:newImage() local blurb = theBannerFactory:newBlurb() --TODO: actual rendering if yesnoret[#ret + 1] = banner:export(args.small) then end  -- Render the categories do local theCategoryFactory = CategoryFactory:new( theConfig, theProtectionStatus, bannerClass thePageData ) local categoryObjects = PadlocktheCategoryFactory:getCategoryObjects() elsefor i, obj in ipairs(categoryObjects) do bannerClass ret[#ret + 1] = Bannerobj: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,
Banner = Banner,
Padlock = Padlock,
BannerFactory = BannerFactory,
Category = Category,
ProtectionCategory = ProtectionCategory,
ErrorCategory = ErrorCategory,
ExpiryCategory = ExpiryCategory, CategoryFactory = CategoryFactory
}
end
return ProtectionBanner
Anonymous user