Jump to content

Module:Protection banner: Difference between revisions

try to merge the edit conflict, one variable at a time, starting with action
((edit conflict) replace Protection getter functions with public attributes)
(try to merge the edit conflict, one variable at a time, starting with action)
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 mArguments, mMessageBox, lang, config
 
--------------------------------------------------------------------------------
-- Config class
--------------------------------------------------------------------------------
 
local Config = class('Config')
 
function Config:initialize(data)
data = data or mw.loadData('Module:Protection banner/config')
self._cfg = data.cfg
self._msg = data.msg
self._bannerConfigTables = {}
end
 
function Config:getBannerConfig(protectionObj)
if self._bannerConfigTables[protectionObj] then
return self._bannerConfigTables[protectionObj]
else
local ret = {}
local cfg = self._cfg
local action = protectionObj.action
local level = protectionObj.level
local reason = protectionObj.reason
local fields = {
'text',
'explanation',
'tooltip',
'alt',
'link',
'image'
}
local configTables = {}
if cfg.banners[action] then
configTables[#configTables + 1] = cfg.banners[action][reason]
end
if cfg.defaultBanners[action] then
configTables[#configTables + 1] = cfg.defaultBanners[action][level]
configTables[#configTables + 1] = cfg.defaultBanners[action].default
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(fields) do
for j, t in ipairs(configTables) do
if t[field] then
ret[field] = t[field]
break
end
end
end
self._bannerConfigTables[protectionObj] = ret
return ret
end
end
 
function Config:getConfigTable(key)
local blacklist = {
banners = true,
defaultBanners = true,
masterBanner = true
}
if not blacklist[key] then
return self._cfg[key]
else
return nil
end
end
 
function Config:getMessage(key)
return self._msg[key]
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 116: Line 47:
level = '*'
level = '*'
end
end
self.level = level or '*'
self._level = level or '*'
end
end


Line 137: Line 68:
-- Set expiry
-- Set expiry
if args.expiry then
if args.expiry then
local indefStrings = configObj:getConfigTable('indefStrings')
local indefStrings = configObj.cfg.indefStrings
if indefStrings[args.expiry] then
if indefStrings[args.expiry] then
self.expiry = 'indef'
self._expiry = 'indef'
elseif type(args.expiry) == 'number' then
elseif type(args.expiry) == 'number' then
self.expiry = args.expiry
self._expiry = args.expiry
else
else
self.expiry = validateDate(args.expiry, 'expiry date')
self._expiry = validateDate(args.expiry, 'expiry date')
end
end
end
end
Line 151: Line 82:
local reason = args.reason or args[1]
local reason = args.reason or args[1]
if reason then
if reason then
self.reason = reason:lower()
self._reason = reason:lower()
end
end
end
end


-- Set protection date
-- Set protection date
self.protectionDate = validateDate(args.date, 'protection date')
self._protectionDate = validateDate(args.date, 'protection date')
-- Set banner config
do
self.bannerConfig = {}
local cfg = configObj.cfg
local fields = {
'text',
'explanation',
'tooltip',
'alt',
'link',
'image'
}
local configTables = {}
if cfg.banners[self.action] then
configTables[#configTables + 1] = cfg.banners[self.action][self._reason]
end
if cfg.defaultBanners[self.action] then
configTables[#configTables + 1] = cfg.defaultBanners[self.action][self._level]
configTables[#configTables + 1] = cfg.defaultBanners[self.action].default
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(fields) do
for j, t in ipairs(configTables) do
if t[field] then
self.bannerConfig[field] = t[field]
break
end
end
end
end
end
end


Line 172: Line 134:
self._configObj = configObj
self._configObj = configObj
self._protectionObj = protectionObj
self._protectionObj = protectionObj
self._bannerConfig = configObj:getBannerConfig(protectionObj)
self._bannerConfig = protectionObj.bannerConfig
self._titleObj = titleObj
self._titleObj = titleObj
end
end
Line 201: Line 163:


function Blurb:_getExpandedMessage(msg)
function Blurb:_getExpandedMessage(msg)
local msg = self._configObj:getMessage(msg)
local msg = self._configObj.msg[msg]
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
end
end
Line 278: Line 240:


function Blurb:_makeDisputeBlurbParameter()
function Blurb:_makeDisputeBlurbParameter()
local expiry = self._protectionObj.expiry
local expiry = self._protectionObj:getExpiry()
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('dispute-blurb-expiry')
return self:_getExpandedMessage('dispute-blurb-expiry')
Line 306: Line 268:
local mEditRequest = require('Module:Submit an edit request')
local mEditRequest = require('Module:Submit an edit request')
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj:getLevel()
-- Get the display message key.
-- Get the display message key.
Line 332: Line 294:


function Blurb:_makeExpiryParameter()
function Blurb:_makeExpiryParameter()
local expiry = self._protectionObj.expiry
local expiry = self._protectionObj:getExpiry()
if expiry == 'indef' then
if expiry == 'indef' then
return nil
return nil
Line 345: Line 307:
function Blurb:_makeExplanationBlurbParameter()
function Blurb:_makeExplanationBlurbParameter()
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj:getLevel()
local namespace = self._titleObj.namespace
local namespace = self._titleObj.namespace
local isTalk = self._titleObj.isTalkPage
local isTalk = self._titleObj.isTalkPage
Line 376: Line 338:


function Blurb:_makeImageLinkParameter()
function Blurb:_makeImageLinkParameter()
local imageLinks = self._configObj:getConfigTable('imageLinks')
local imageLinks = self._configObj.cfg.imageLinks
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj:getLevel()
local msg
local msg
if imageLinks[action][level] then
if imageLinks[action][level] then
Line 391: Line 353:


function Blurb:_makeIntroBlurbParameter()
function Blurb:_makeIntroBlurbParameter()
local expiry = self._protectionObj.expiry
local expiry = self._protectionObj:getExpiry()
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('intro-blurb-expiry')
return self:_getExpandedMessage('intro-blurb-expiry')
Line 400: Line 362:


function Blurb:_makeOfficeBlurbParameter()
function Blurb:_makeOfficeBlurbParameter()
local protectionDate = self._protectionObj.protectionDate
local protectionDate = self._protectionObj:getProtectionDate()
if protectionDate then
if protectionDate then
return self:_getExpandedMessage('office-blurb-protectiondate')
return self:_getExpandedMessage('office-blurb-protectiondate')
Line 409: Line 371:


function Blurb:_makePagetypeParameter()
function Blurb:_makePagetypeParameter()
local pagetypes = self._configObj:getConfigTable('pagetypes')
local pagetypes = self._configObj.cfg.pagetypes
local namespace = self._titleObj.namespace
local namespace = self._titleObj.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')
Line 415: Line 377:


function Blurb:_makeProtectionBlurbParameter()
function Blurb:_makeProtectionBlurbParameter()
local protectionBlurbs = self._configObj:getConfigTable('protectionBlurbs')
local protectionBlurbs = self._configObj.cfg.protectionBlurbs
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj:getLevel()
local msg
local msg
if protectionBlurbs[action][level] then
if protectionBlurbs[action][level] then
Line 432: Line 394:


function Blurb:_makeProtectionDateParameter()
function Blurb:_makeProtectionDateParameter()
local protectionDate = self._protectionObj.protectionDate
local protectionDate = self._protectionObj:getProtectionDate()
if type(protectionDate) == 'number' then
if type(protectionDate) == 'number' then
return Blurb.formatDate(protectionDate)
return Blurb.formatDate(protectionDate)
Line 441: Line 403:


function Blurb:_makeProtectionLevelParameter()
function Blurb:_makeProtectionLevelParameter()
local protectionLevels = self._configObj:getConfigTable('protectionLevels')
local protectionLevels = self._configObj.cfg.protectionLevels
local action = self._protectionObj.action
local action = self._protectionObj.action
local level = self._protectionObj.level
local level = self._protectionObj:getLevel()
local msg
local msg
if protectionLevels[action][level] then
if protectionLevels[action][level] then
Line 478: Line 440:


function Blurb:_makeResetBlurbParameter()
function Blurb:_makeResetBlurbParameter()
local protectionDate = self._protectionObj.protectionDate
local protectionDate = self._protectionObj:getProtectionDate()
if protectionDate then
if protectionDate then
return self:_getExpandedMessage('reset-blurb-protectiondate')
return self:_getExpandedMessage('reset-blurb-protectiondate')
Line 499: Line 461:


function Blurb:_makeTooltipBlurbParameter()
function Blurb:_makeTooltipBlurbParameter()
local expiry = self._protectionObj.expiry
local expiry = self._protectionObj:getExpiry()
if type(expiry) == 'number' then
if type(expiry) == 'number' then
return self:_getExpandedMessage('tooltip-blurb-expiry')
return self:_getExpandedMessage('tooltip-blurb-expiry')
Line 572: Line 534:


local action = protectionObj.action
local action = protectionObj.action
local level = protectionObj.level
local level = protectionObj:getLevel()
local expiry = protectionObj.expiry
local expiry = protectionObj:getExpiry()
local namespace = titleObj.namespace
local namespace = titleObj.namespace
Line 584: Line 546:
-- Fully protected modules and templates get the special red "indef"
-- Fully protected modules and templates get the special red "indef"
-- padlock.
-- padlock.
self._imageFilename = self._configObj:getMessage('image-filename-indef')
self._imageFilename = self._configObj.msg['image-filename-indef']
return nil
return nil
end
end


-- Deal with regular protection types.
-- Deal with regular protection types.
local images = self._configObj:getConfigTable('images')
local images = self._configObj.cfg.images
if images[action] then
if images[action] then
if images[action][level] then
if images[action][level] then
Line 613: Line 575:
function BannerTemplate:renderImage()
function BannerTemplate:renderImage()
local filename = self._imageFilename
local filename = self._imageFilename
or self._configObj:getMessage('image-filename-default')
or self._configObj.msg['image-filename-default']
or 'Transparent.gif'
or 'Transparent.gif'
return newFileLink(filename)
return newFileLink(filename)
Line 747: Line 709:
-- Get the expiry.
-- Get the expiry.
local expiry = protectionObj.expiry
local expiry = protectionObj:getExpiry()
if type(expiry) == 'number' then
if type(expiry) == 'number' then
expiry = 'temp'
expiry = 'temp'
Line 758: Line 720:
do
do
local namespace = titleObj.namespace
local namespace = titleObj.namespace
local categoryNamespaces = configObj:getConfigTable('categoryNamespaceKeys')
local categoryNamespaces = configObj.cfg.categoryNamespaceKeys
nskey = categoryNamespaces[namespace]
nskey = categoryNamespaces[namespace]
if not nskey and namespace % 2 == 1 then
if not nskey and namespace % 2 == 1 then
Line 766: Line 728:


-- Get the other inputs.
-- Get the other inputs.
local reason = protectionObj.reason
local reason = protectionObj:getReason()
local action = protectionObj.action
local action = protectionObj.action
local level = protectionObj.level
local level = protectionObj:getLevel()
   
   
--[[
--[[
Line 793: Line 755:
local configOrder = {}
local configOrder = {}
do
do
local reasonsWithNamespacePriority = configObj:getConfigTable('reasonsWithNamespacePriority')
local reasonsWithNamespacePriority = configObj.cfg.reasonsWithNamespacePriority
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
local namespaceFirst = reason and reasonsWithNamespacePriority[reason] or false
for propertiesKey, t in pairs(properties) do
for propertiesKey, t in pairs(properties) do
Line 862: Line 824:
-- pos field in the property table.
-- pos field in the property table.
--]]
--]]
local cats = configObj:getConfigTable('protectionCategories')
local cats = configObj.cfg.protectionCategories
local cat
local cat
for i = 1, 2^noActive do
for i = 1, 2^noActive do
Line 899: Line 861:


function ExpiryCategory:render()
function ExpiryCategory:render()
local reasonsWithoutExpiryCheck = self._configObj:getConfigTable('reasonsWithoutExpiryCheck')
local configObj = self._configObj
local expiryCheckActions = self._configObj:getConfigTable('expiryCheckActions')
local protectionObj = self._protectionObj
local expiry = self._protectionObj.expiry
local action = self._protectionObj.action
local reasonsWithoutExpiryCheck = configObj.cfg.reasonsWithoutExpiryCheck
local reason = self._protectionObj.reason
local expiryCheckActions = configObj.cfg.expiryCheckActions
local expiry = protectionObj:getExpiry()
local action = protectionObj.action
local reason = protectionObj:getReason()
if not expiry
if not expiry
Line 910: Line 875:
and not reasonsWithoutExpiryCheck[reason]
and not reasonsWithoutExpiryCheck[reason]
then
then
self:setName(configObj:getMessage('tracking-category-expiry'))
self:setName(configObj.msg['tracking-category-expiry'])
end
end
return Category.render(self)
return Category.render(self)
Line 924: Line 889:
local configObj = self._configObj
local configObj = self._configObj
local protectionObj = self._protectionObj
local protectionObj = self._protectionObj
local expiry = protectionObj.expiry
local expiry = protectionObj:getExpiry()
local action = protectionObj.action
local action = protectionObj.action
local level = protectionObj.level
local level = protectionObj:getLevel()


if not protectionObj:isProtected()
if not protectionObj:isProtected()
or type(expiry) == 'number' and expiry < os.time()
or type(expiry) == 'number' and expiry < os.time()
then
then
self:setName(configObj:getMessage('tracking-category-incorrect'))
self:setName(configObj.msg['tracking-category-incorrect'])
end
end
return Category.render(self)
return Category.render(self)
Line 949: Line 915:
function TemplateCategory:render()
function TemplateCategory:render()
local configObj = self._configObj
local configObj = self._configObj
local action = self._protectionObj.action
local protectionObj = self._protectionObj
local level = self._protectionObj.level
local titleObj = self._titleObj
local namespace = self._titleObj.namespace
local action = protectionObj.action
local level = protectionObj:getLevel()
local namespace = titleObj.namespace
if level == 'templateeditor'
if level == 'templateeditor'
Line 959: Line 928:
)
)
then
then
self:setName(configObj:getMessage('tracking-category-template'))
self:setName(configObj.msg['tracking-category-template'])
end
end
return Category.render(self)
return Category.render(self)
Line 980: Line 949:


-- Get data objects
-- Get data objects
local configObj = Config:new()
if not config then
config = mw.loadData('Module:Protection banner/config')
end
local configObj = config
local protectionObj = Protection:new(args, configObj, titleObj)
local protectionObj = Protection:new(args, configObj, titleObj)


Line 1,003: Line 975:


-- Set the image fields
-- Set the image fields
local bannerConfig = configObj:getBannerConfig(protectionObj)
local bannerConfig = protectionObj.bannerConfig
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
bannerObj:setImageFilename(bannerConfig.image, protectionObj, titleObj)
if isPadlock then
if isPadlock then
Line 1,043: Line 1,015:
return {
return {
Protection = Protection,
Protection = Protection,
Config = Config,
Blurb = Blurb,
Blurb = Blurb,
BannerTemplate = BannerTemplate,
BannerTemplate = BannerTemplate,
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.