Module:Protection banner: Difference between revisions

make the Protection class be in charge of the title, and some other tweaks
(config isn't an object anymore)
(make the Protection class be in charge of the title, and some other tweaks)
Line 10: Line 10:


-- Lazily initialise modules and objects we don't always need.
-- Lazily initialise modules and objects we don't always need.
local mArguments, mMessageBox, lang
local getArgs, makeMessageBox, lang


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 67: Line 67:
}
}


function Protection:initialize(args, cfg, titleObj)
function Protection:initialize(args, cfg)
self._cfg = cfg
self._cfg = cfg
self._titleObj = titleObj
if args.title then
self.title = mw.title.new(args.title)
if not self.title then
error('Invalid title "' .. args.title .. '"', 2)
end
else
self.title = mw.title.getCurrentTitle()
end


-- Set action
-- Set action
Line 84: Line 91:
-- Set level
-- Set level
do
do
self.level = effectiveProtectionLevel(self.action, titleObj)
self.level = effectiveProtectionLevel(self.action, self.title)
if self.level == 'accountcreator' then
if self.level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
-- Lump titleblacklisted pages in with template-protected pages,
Line 147: Line 154:
function Protection:makeProtectionCategory()
function Protection:makeProtectionCategory()
local cfg = self._cfg
local cfg = self._cfg
local titleObj = self._titleObj
local title = self.title
-- Exit if the page is not protected.
-- Exit if the page is not protected.
Line 165: Line 172:
local nskey
local nskey
do
do
local namespace = titleObj.namespace
local namespace = title.namespace
local categoryNamespaces = cfg.categoryNamespaceKeys
local categoryNamespaces = cfg.categoryNamespaceKeys
nskey = categoryNamespaces[namespace]
nskey = categoryNamespaces[namespace]
Line 326: Line 333:
function Protection:makeTemplateCategory()
function Protection:makeTemplateCategory()
local cfg = self._cfg
local cfg = self._cfg
local titleObj = self._titleObj
local title = self.title
local cat
local cat
Line 332: Line 339:
and (
and (
(self.action ~= 'edit' and self.action ~= 'move')
(self.action ~= 'edit' and self.action ~= 'move')
or (titleObj.namespace ~= 10 and titleObj.namespace ~= 828)
or (title.namespace ~= 10 and title.namespace ~= 828)
)
)
then
then
Line 346: Line 353:
local Blurb = class('Blurb')
local Blurb = class('Blurb')


function Blurb:initialize(cfg, protectionObj, titleObj)
function Blurb:initialize(cfg, protectionObj)
self._cfg = cfg
self._cfg = cfg
self._protectionObj = protectionObj
self._protectionObj = protectionObj
self._bannerConfig = protectionObj.bannerConfig
self._bannerConfig = protectionObj.bannerConfig
self._titleObj = titleObj
self._title = protectionObj.title
end
end


Line 428: Line 435:
-- protection.
-- protection.
local action = self._protectionObj.action
local action = self._protectionObj.action
local pagename = self._titleObj.prefixedText
local pagename = self._title.prefixedText
if action == 'move' then
if action == 'move' then
-- We need the move log link.
-- We need the move log link.
Line 470: Line 477:
return string.format(
return string.format(
'[[%s:%s#%s|%s]]',
'[[%s:%s#%s|%s]]',
mw.site.namespaces[self._titleObj.namespace].talk.name,
mw.site.namespaces[self._title.namespace].talk.name,
self._titleObj.text,
self._title.text,
section,
section,
disputes
disputes
Line 523: Line 530:
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj.level
local namespace = self._titleObj.namespace
local namespace = self._title.namespace
local isTalk = self._titleObj.isTalkPage
local isTalk = self._title.isTalkPage


-- @TODO: add semi-protection and pending changes blurbs
-- @TODO: add semi-protection and pending changes blurbs
Line 587: Line 594:
function Blurb:_makePagetypeParameter()
function Blurb:_makePagetypeParameter()
local pagetypes = self._cfg.pagetypes
local pagetypes = self._cfg.pagetypes
local namespace = self._titleObj.namespace
local namespace = self._title.namespace
return pagetypes[namespace] or pagetypes.default or error('no default pagetype defined')
return pagetypes[namespace] or pagetypes.default or error('no default pagetype defined')
end
end
Line 636: Line 643:
function Blurb:_makeProtectionLogParameter()
function Blurb:_makeProtectionLogParameter()
local action = self._protectionObj.action
local action = self._protectionObj.action
local pagename = self._titleObj.prefixedText
local pagename = self._title.prefixedText
if action == 'autoreview' then
if action == 'autoreview' then
-- We need the pending changes log.
-- We need the pending changes log.
Line 668: Line 675:
return string.format(
return string.format(
'[[%s:%s#%s|%s]]',
'[[%s:%s#%s|%s]]',
mw.site.namespaces[self._titleObj.namespace].talk.name,
mw.site.namespaces[self._title.namespace].talk.name,
self._titleObj.text,
self._title.text,
section or 'top',
section or 'top',
display
display
Line 687: Line 694:
local mVandalM = require('Module:Vandal-m')
local mVandalM = require('Module:Vandal-m')
local username = self._username
local username = self._username
username = username or self._titleObj.baseText
username = username or self._title.baseText
return mVandalM._main{username}
return mVandalM._main{username}
end
end
Line 742: Line 749:
end
end


function BannerTemplate:setImageFilename(filename, protectionObj, titleObj)
function BannerTemplate:setImageFilename(filename, protectionObj)
if filename then
if filename then
self._imageFilename = filename
self._imageFilename = filename
Line 751: Line 758:
local level = protectionObj.level
local level = protectionObj.level
local expiry = protectionObj.expiry
local expiry = protectionObj.expiry
local namespace = titleObj.namespace
local namespace = protectionObj.title.namespace
-- Deal with special cases first.
-- Deal with special cases first.
Line 827: Line 834:
function Banner:__tostring()
function Banner:__tostring()
-- Renders the banner.
-- Renders the banner.
mMessageBox = mMessageBox or require('Module:Message box')
makeMessageBox = makeMessageBox or require('Module:Message box').main
local reasonText = self._reasonText or error('no reason text set')
local reasonText = self._reasonText or error('no reason text set')
local explanationText = self._explanationText
local explanationText = self._explanationText
Line 840: Line 847:
)
)
}
}
return mMessageBox.main('mbox', mbargs)
return makeMessageBox('mbox', mbargs)
end
end


Line 877: Line 884:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ProtectionBanner class
-- Exports
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local ProtectionBanner = {}
local p = {}


function ProtectionBanner.exportToWiki(frame, cfg, titleObj)
function p._exportClasses()
mArguments = mArguments or require('Module:Arguments')
-- This is used for testing purposes.
local args = mArguments.getArgs(frame)
return {
return ProtectionBanner.exportToLua(args, cfg, titleObj)
Protection = Protection,
Blurb = Blurb,
BannerTemplate = BannerTemplate,
Banner = Banner,
Padlock = Padlock,
}
end
end


function ProtectionBanner.exportToLua(args, cfg, titleObj)
function p._main(args, cfg)
cfg = cfg or mw.loadData('Module:Protection banner/config')
if not cfg then
titleObj = titleObj or mw.title.getCurrentTitle()
cfg = mw.loadData('Module:Protection banner/config')
end


-- Initialise protection and blurb objects
-- Initialise protection and blurb objects
local protectionObj = Protection:new(args, cfg, titleObj)
local protectionObj = Protection:new(args, cfg)
local blurbObj = Blurb:new(cfg, protectionObj, titleObj)
local blurbObj = Blurb:new(cfg, protectionObj)
blurbObj:setDeletionDiscussionPage(args.xfd)
blurbObj:setDeletionDiscussionPage(args.xfd)
blurbObj:setUsername(args.user)
blurbObj:setUsername(args.user)
Line 914: Line 927:
-- Set the image fields
-- Set the image fields
local bannerConfig = protectionObj.bannerConfig
local bannerConfig = protectionObj.bannerConfig
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
bannerObj:setImageFilename(bannerConfig.image, protectionObj)
if isPadlock then
if isPadlock then
bannerObj:setImageTooltip(blurbObj:makeTooltipText())
bannerObj:setImageTooltip(blurbObj:makeTooltipText())
Line 928: Line 941:
bannerObj:setReasonText(blurbObj:makeReasonText())
bannerObj:setReasonText(blurbObj:makeReasonText())
bannerObj:setExplanationText(blurbObj:makeExplanationText())
bannerObj:setExplanationText(blurbObj:makeExplanationText())
bannerObj:setPage(titleObj.prefixedText)
bannerObj:setPage(protectionObj.title.prefixedText)
end
end


Line 945: Line 958:
end
end


function ProtectionBanner._exportClasses()
function p.main(frame)
-- This is used to export the classes for testing purposes.
if not getArgs then
return {
getArgs = require('Module:Arguments').getArgs
Protection = Protection,
end
Blurb = Blurb,
return p._main(getArgs(frame))
BannerTemplate = BannerTemplate,
Banner = Banner,
Padlock = Padlock,
}
end
end


return ProtectionBanner
return p
Anonymous user