Modul:Protection banner: Unterschied zwischen den Versionen
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) |
||
Zeile 9: | Zeile 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 | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 105: | Zeile 129: | ||
local Image = class('Image') | local Image = class('Image') | ||
function Image: | 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 | ||
do | |||
local configObj = self._configObj | |||
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 | ||
Zeile 147: | Zeile 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. | :width(self._width or 20) | ||
:alt(self._alt) | :alt(self._alt) | ||
:link(self._link) | :link(self._link) | ||
Zeile 164: | Zeile 218: | ||
local Blurb = class('Blurb') | local Blurb = class('Blurb') | ||
function Blurb:initialize(configObj, protectionStatusObj, | 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. | self._pageDataObj = pageDataObj | ||
end | end | ||
Zeile 176: | Zeile 230: | ||
function Blurb:_makePagetypeParameter() | function Blurb:_makePagetypeParameter() | ||
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces') | local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces') | ||
return pagetypes[ | local namespace = self._pageDataObj:getNamespace() | ||
return pagetypes[namespace] or pagetypes.default or 'page' | |||
end | end | ||
Zeile 226: | Zeile 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 | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 422: | Zeile 519: | ||
end | end | ||
end | end | ||
-------------------------------------------------------------------------------- | |||
-- ExpiryCategory class | |||
-------------------------------------------------------------------------------- | |||
local ExpiryCategory = Category:subclass('ExpiryCategory') | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 430: | Zeile 533: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- CategoryFactory class | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local 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 | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Zeile 448: | Zeile 572: | ||
function ProtectionBanner.exportToLua(args, title) | function ProtectionBanner.exportToLua(args, title) | ||
title = title or mw.title.getCurrentTitle() | title = title or mw.title.getCurrentTitle() | ||
-- Get | -- 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 | local theBannerFactory = BannerFactory:new( | ||
args, | |||
theConfig, | |||
theProtectionStatus, | |||
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 | ||
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, | ||
Zeile 473: | Zeile 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 |