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