Open main menu

Changes

Module:Protection banner

115 bytes added, 17:48, 22 July 2014
don't use Module:Middleclass
-- 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
--------------------------------------------------------------------------------
local Protection = class('{}Protection.__index = Protection')
Protection.supportedActions = {
}
function Protection:initialize.new(args, cfg, title) selflocal obj = {} obj._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(
-- 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
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
-- 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
-- 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
end
end
return setmetatable(obj, Protection)
end
--------------------------------------------------------------------------------
local Blurb = class('{}Blurb.__index = Blurb')
Blurb.bannerTextFields = {
}
function Blurb:initialize.new(protectionObj, args, cfg) self.return setmetatable({ _cfg = cfg, self. _protectionObj = protectionObj, self. _args = args }, Blurb)
end
--------------------------------------------------------------------------------
local BannerTemplate = class('{}BannerTemplate.__index = BannerTemplate')
function BannerTemplate:initialize.new(protectionObj, cfg) selflocal obj = {} 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,
-- 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
--------------------------------------------------------------------------------
local Banner = setmetatable({}, BannerTemplate:subclass(')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
--------------------------------------------------------------------------------
local Padlock = setmetatable({}, BannerTemplate:subclass(')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
-- Initialise the protection object and check for errors
local protectionObjCreated, protectionObj = pcall(
Protection.new, Protection, -- equivalent to Protection:new()
args,
cfg,
-- Initialise the blurb object
local blurbObj = Blurb:.new(protectionObj, args, cfg)
local ret = {}
ret[#ret + 1] = tostring(
(yesno(args.small) and Padlock or Banner)
:.new(protectionObj, blurbObj, cfg)
)
end
Anonymous user