Module:Protection banner: Difference between revisions

don't use Module:Middleclass
(add category sort keys)
(don't use Module:Middleclass)
Line 4:
-- Initialise necessary modules.
require('Module:No globals')
local class = require('Module:Middleclass').class
local newFileLink = require('Module:File link').new
local effectiveProtectionLevel = require('Module:Effective protection level')._main
Line 71 ⟶ 70:
--------------------------------------------------------------------------------
 
local Protection = class('Protection'){}
Protection.__index = Protection
 
Protection.supportedActions = {
Line 88:
}
 
function Protection:initialize.new(args, cfg, title)
self._cfglocal obj = cfg{}
selfobj._cfg = cfg
selfobj.title = title or mw.title.getCurrentTitle()
 
-- Set action
if not args.action then
selfobj.action = 'edit'
elseif selfProtection.supportedActions[args.action] then
selfobj.action = args.action
else
error(string.format(
Line 105 ⟶ 106:
 
-- Set level
selfobj.level = effectiveProtectionLevel(selfobj.action, selfobj.title)
if selfobj.level == 'accountcreator' then
-- Lump titleblacklisted pages in with template-protected pages,
-- since templateeditors can do both.
selfobj.level = 'templateeditor'
elseif not selfobj.level or (selfobj.action == 'move' and selfobj.level == 'autoconfirmed') then
-- Users need to be autoconfirmed to move pages anyway, so treat
-- semi-move-protected pages as unprotected.
selfobj.level = '*'
end
 
Line 119 ⟶ 120:
if args.expiry then
if cfg.indefStrings[args.expiry] then
selfobj.expiry = 'indef'
elseif type(args.expiry) == 'number' then
selfobj.expiry = args.expiry
else
selfobj.expiry = validateDate(args.expiry, 'expiry date')
end
end
Line 129 ⟶ 130:
-- Set reason
if args[1] then
selfobj.reason = mw.ustring.lower(args[1])
if selfobj.reason:find('|') then
error('reasons cannot contain the pipe character ("|")', 0)
end
Line 137 ⟶ 138:
-- Set protection date
if args.date then
selfobj.protectionDate = validateDate(args.date, 'protection date')
end
-- Set banner config
do
selfobj.bannerConfig = {}
local configTables = {}
if cfg.banners[selfobj.action] then
configTables[#configTables + 1] = cfg.banners[selfobj.action][selfobj.reason]
end
if cfg.defaultBanners[selfobj.action] then
configTables[#configTables + 1] = cfg.defaultBanners[selfobj.action][selfobj.level]
configTables[#configTables + 1] = cfg.defaultBanners[selfobj.action].default
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(selfobj.bannerConfigFields) do
for j, t in ipairs(configTables) do
if t[field] then
selfobj.bannerConfig[field] = t[field]
break
end
Line 161 ⟶ 162:
end
end
return setmetatable(obj, Protection)
end
 
Line 362 ⟶ 364:
--------------------------------------------------------------------------------
 
local Blurb = class('Blurb'){}
Blurb.__index = Blurb
 
Blurb.bannerTextFields = {
Line 372 ⟶ 375:
}
 
function Blurb:initialize.new(protectionObj, args, cfg)
return setmetatable({
self._cfg = cfg
_cfg = cfg,
self. _protectionObj = protectionObj,
self. _args = args
}, Blurb)
end
 
Line 679 ⟶ 684:
--------------------------------------------------------------------------------
 
local BannerTemplate = class('BannerTemplate'){}
BannerTemplate.__index = BannerTemplate
 
function BannerTemplate:initialize.new(protectionObj, cfg)
self._cfglocal obj = cfg{}
obj._cfg = cfg
 
-- Set the image filename.
local imageFilename = protectionObj.bannerConfig.image
if imageFilename then
selfobj._imageFilename = imageFilename
else
-- If an image filename isn't specified explicitly in the banner config,
Line 703 ⟶ 710:
-- Fully protected modules and templates get the special red "indef"
-- padlock.
selfobj._imageFilename = selfobj._cfg.msg['image-filename-indef']
else
-- Deal with regular protection types.
local images = selfobj._cfg.images
if images[action] then
if images[action][level] then
selfobj._imageFilename = images[action][level]
elseif images[action].default then
selfobj._imageFilename = images[action].default
end
end
end
end
return setmetatable(obj, BannerTemplate)
end
 
Line 742 ⟶ 750:
--------------------------------------------------------------------------------
 
local Banner = BannerTemplate:subclasssetmetatable('Banner'{}, BannerTemplate)
Banner.__index = Banner
 
function Banner:initialize.new(protectionObj, blurbObj, cfg)
BannerTemplatelocal obj = Banner.initializenew(self, protectionObj, cfg) -- This doesn't need the blurb.
selfobj:setImageWidth(40)
selfobj:setImageTooltip(blurbObj:makeBannerText('alt')) -- Large banners use the alt text for the tooltip.
selfobj._reasonText = blurbObj:makeBannerText('text')
selfobj._explanationText = blurbObj:makeBannerText('explanation')
selfobj._page = protectionObj.title.prefixedText -- Only makes a difference in testing.
return setmetatable(obj, Banner)
end
 
Line 775 ⟶ 785:
--------------------------------------------------------------------------------
 
local Padlock = BannerTemplate:subclasssetmetatable('Padlock'{}, BannerTemplate)
Padlock.__index = Padlock
 
function Padlock:initialize.new(protectionObj, blurbObj, cfg)
local obj = BannerTemplate.initializenew(self, protectionObj, cfg) -- This doesn't need the blurb.
selfobj:setImageWidth(20)
selfobj:setImageTooltip(blurbObj:makeBannerText('tooltip'))
selfobj._imageAlt = blurbObj:makeBannerText('alt')
selfobj._imageLink = blurbObj:makeBannerText('link')
selfobj._right = cfg.padlockPositions[protectionObj.action]
or cfg.padlockPositions.default
or '55px'
return setmetatable(obj, Padlock)
end
 
Line 821 ⟶ 833:
-- Initialise the protection object and check for errors
local protectionObjCreated, protectionObj = pcall(
Protection.new, Protection, -- equivalent to Protection:new()
args,
cfg,
Line 842 ⟶ 854:
-- Initialise the blurb object
local blurbObj = Blurb:.new(protectionObj, args, cfg)
 
local ret = {}
Line 850 ⟶ 862:
ret[#ret + 1] = tostring(
(yesno(args.small) and Padlock or Banner)
:.new(protectionObj, blurbObj, cfg)
)
end
Anonymous user