Modul:Protection banner: Unterschied zwischen den Versionen
don't use Module:Middleclass
(add category sort keys) |
(don't use Module:Middleclass) |
||
Zeile 4: | Zeile 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 | ||
Zeile 71: | Zeile 70: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local Protection = | local Protection = {} | ||
Protection.__index = Protection | |||
Protection.supportedActions = { | Protection.supportedActions = { | ||
Zeile 88: | Zeile 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( | ||
Zeile 105: | Zeile 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 | ||
Zeile 119: | Zeile 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 | ||
Zeile 129: | Zeile 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 | ||
Zeile 137: | Zeile 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 | ||
Zeile 161: | Zeile 162: | ||
end | end | ||
end | end | ||
return setmetatable(obj, Protection) | |||
end | end | ||
Zeile 362: | Zeile 364: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local Blurb = | local Blurb = {} | ||
Blurb.__index = Blurb | |||
Blurb.bannerTextFields = { | Blurb.bannerTextFields = { | ||
Zeile 372: | Zeile 375: | ||
} | } | ||
function Blurb | function Blurb.new(protectionObj, args, cfg) | ||
return setmetatable({ | |||
_cfg = cfg, | |||
_protectionObj = protectionObj, | |||
_args = args | |||
}, Blurb) | |||
end | end | ||
Zeile 679: | Zeile 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, | ||
Zeile 703: | Zeile 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 | ||
Zeile 742: | Zeile 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 | ||
Zeile 775: | Zeile 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 | ||
Zeile 821: | Zeile 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, | ||
Zeile 842: | Zeile 854: | ||
-- Initialise the blurb object | -- Initialise the blurb object | ||
local blurbObj = Blurb | local blurbObj = Blurb.new(protectionObj, args, cfg) | ||
local ret = {} | local ret = {} | ||
Zeile 850: | Zeile 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 |