Module:Protection banner: Difference between revisions
Content added Content deleted
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) |
Enwikipedia>Mr. Stradivarius (simplify the structure a bit and write some more parameter functions) |
||
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 67: | Line 43: | ||
end |
end |
||
-- Set |
-- Set other params |
||
self._reason = args.reason |
self._reason = args.reason |
||
-- Set expiry |
|||
self._expiry = args.expiry or 'indef' |
self._expiry = args.expiry or 'indef' |
||
self._section = args.section |
|||
end |
end |
||
Line 88: | Line 63: | ||
function ProtectionStatus:getExpiry() |
function ProtectionStatus:getExpiry() |
||
return self._expiry |
return self._expiry |
||
end |
|||
function ProtectionStatus:getSection() |
|||
return self._section |
|||
end |
end |
||
Line 121: | Line 100: | ||
return nil |
return nil |
||
end |
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 |
end |
||
Line 129: | Line 196: | ||
local Image = class('Image') |
local Image = class('Image') |
||
function Image:initialize(configObj, protectionStatusObj, |
function Image:initialize(configObj, protectionStatusObj, titleObj) |
||
self._configObj = configObj |
self._configObj = configObj |
||
self._protectionStatusObj = protectionStatusObj |
self._protectionStatusObj = protectionStatusObj |
||
self. |
self._titleObj = titleObj |
||
end |
end |
||
Line 143: | Line 210: | ||
local configObj = self._configObj |
local configObj = self._configObj |
||
local protectionStatusObj = self._protectionStatusObj |
local protectionStatusObj = self._protectionStatusObj |
||
local |
local titleObj = self._titleObj |
||
images = configObj:getConfigTable('images') |
images = configObj:getConfigTable('images') |
||
action = protectionStatusObj:getAction() |
action = protectionStatusObj:getAction() |
||
level = protectionStatusObj:getLevel() |
level = protectionStatusObj:getLevel() |
||
reason = protectionStatusObj:getReason() |
reason = protectionStatusObj:getReason() |
||
namespace = |
namespace = titleObj:getNamespace() |
||
end |
end |
||
Line 210: | Line 277: | ||
:caption(self._caption) |
:caption(self._caption) |
||
:render() |
:render() |
||
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() |
|||
end |
end |
||
Line 281: | Line 302: | ||
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 531: | Line 510: | ||
local ErrorCategory = Category:subclass('ErrorCategory') |
local ErrorCategory = Category:subclass('ErrorCategory') |
||
-------------------------------------------------------------------------------- |
|||
-- 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 |
|||
-------------------------------------------------------------------------------- |
-------------------------------------------------------------------------------- |
||
Line 574: | Line 526: | ||
-- Get data objects |
-- Get data objects |
||
local |
local theProtectionStatus = ProtectionStatus:new(args, title) |
||
local theProtectionStatus = ProtectionStatus.new(args, title) |
|||
local theConfig = Config:new() |
local theConfig = Config:new() |
||
Line 581: | Line 532: | ||
-- Render the banner |
-- Render the banner |
||
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 |
-- 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 |
|||
return table.concat(ret) |
return table.concat(ret) |
||
Line 614: | Line 541: | ||
-- This is used to export the classes for testing purposes. |
-- This is used to export the classes for testing purposes. |
||
return { |
return { |
||
PageData = PageData, |
|||
ProtectionStatus = ProtectionStatus, |
ProtectionStatus = ProtectionStatus, |
||
Config = Config, |
Config = Config, |
||
Line 622: | Line 548: | ||
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 |