Changes
Jump to navigation
Jump to search
--------------------------------------------------------------------------------
-- 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
-- Set expiry
end
--------------------------------------------------------------------------------
-- Blurb class
--------------------------------------------------------------------------------
local Blurb = class('Blurb')
function Blurb:initialize(configObj, protectionStatusObj, pageDataObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj)
self._pageDataObj = pageDataObj
end
function Blurb:_makeIntroParameter()
end
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
local namespace = self._pageDataObj:getNamespace()
return pagetypes[namespace] or pagetypes.default or 'page'
end
function Blurb:_substituteParameters(msg)
if not self._params then
local params, parameterFuncs = {}, {}
setmetatable(params, {
__index = function (t, k)
local param
if parameterFuncs[k] then
param = parameterFuncs[k]()
end
params[k] = param
return param
end
})
parameterFuncs[1] = function ()
return self:_makeIntroParameter()
end
-- TODO: Write parameter functions
self._params = params
end
return mw.message.newRawMessage(msg):params(self._params):plain()
--------------------------------------------------------------------------------
-- 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
--------------------------------------------------------------------------------
-- CategoryFactory class
--------------------------------------------------------------------------------
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
local thePageData = PageData:new(title) local theProtectionStatus = ProtectionStatus.:new(args, title)
do 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
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
PageData = PageData,
BannerFactory = BannerFactory,
CategoryFactory = CategoryFactory
simplify the structure a bit and write some more parameter functions
local mProtectionLevel = require('Module:Effective protection level')
local yesno = require('Module:Yesno')
--------------------------------------------------------------------------------
end
-- Set reasonother params
self._reason = args.reason
self._expiry = args.expiry or 'indef'
self._section = args.section
end
function ProtectionStatus:getExpiry()
return self._expiry
end
function ProtectionStatus:getSection()
return self._section
end
return nil
end
end
function Config:getMessage(key)
return self._cfg.msg[key]
end
--------------------------------------------------------------------------------
-- Blurb class
--------------------------------------------------------------------------------
local Blurb = class('Blurb')
function Blurb:initialize(configObj, protectionStatusObj, titleObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._bannerConfig = configObj:getBannerConfig(protectionStatusObj)
self._titleObj = titleObj
end
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypeNamespaces')
local namespace = self._titleObj:getNamespace()
return pagetypes[namespace] or pagetypes.default or 'page'
end
function Blurb:_substituteParameters(msg)
if not self._params then
local params, parameterFuncs = {}, {}
setmetatable(params, {
__index = function (t, k)
local param
if parameterFuncs[k] then
param = parameterFuncs[k]()
end
param = param or ''
params[k] = param
return param
end
})
parameterFuncs[1] = function ()
-- Intro blurb
local key
local action = self._protectionStatusObj:getAction()
local level = self._protectionStatusObj:getLevel()
if action == 'edit' and level == 'autoconfirmed' then
key = 'reason-text-semi'
elseif action == 'move' then
key = 'reason-text-move'
elseif action == 'create' then
key = 'reason-text-create'
else
key = 'reason-text-default'
end
local msg = self._configObj:getMessage(key)
return self:_substituteParameters(msg)
end
parameterFuncs[2] = function ()
-- "until" or "or until" depending on the expiry
local expiry = self._protectionStatusObj:getExpiry()
if expiry then
return 'or until'
else
return 'until'
end
end
parameterFuncs[3] = function ()
-- "disputes", with or without a section link
local section = self._protectionStatusObj:getSection()
local disputes = self.configObj:getMessage('dispute-section-link-display')
if section then
return string.format(
'[[%s:%s#%s|%s]]',
mw.site.namespaces[self._titleObj.namespace].talk.name,
self._titleObj.text,
section,
disputes
)
else
return disputes
end
end
self._params = params
end
return mw.message.newRawMessage(msg):params(self._params):plain()
end
local Image = class('Image')
function Image:initialize(configObj, protectionStatusObj, pageDataObjtitleObj)
self._configObj = configObj
self._protectionStatusObj = protectionStatusObj
self._pageDataObj _titleObj = pageDataObjtitleObj
end
local configObj = self._configObj
local protectionStatusObj = self._protectionStatusObj
local pageDataObj titleObj = self._pageDataObj_titleObj
images = configObj:getConfigTable('images')
action = protectionStatusObj:getAction()
level = protectionStatusObj:getLevel()
reason = protectionStatusObj:getReason()
namespace = pageDataObjtitleObj:getNamespace()
end
:caption(self._caption)
:render()
end
local Padlock = BannerTemplate:subclass('Padlock')
--------------------------------------------------------------------------------
local ErrorCategory = Category:subclass('ErrorCategory')
--------------------------------------------------------------------------------
-- Get data objects
local theConfig = Config:new()
-- Render the banner
-- Render the categories
return table.concat(ret)
-- This is used to export the classes for testing purposes.
return {
ProtectionStatus = ProtectionStatus,
Config = Config,
Banner = Banner,
Padlock = Padlock,
Category = Category,
ProtectionCategory = ProtectionCategory,
ErrorCategory = ErrorCategory,
ExpiryCategory = ExpiryCategory,
}
end
return ProtectionBanner