Module:Protection banner: Difference between revisions

Reverted to revision 31218 by Tito Dutta (talk): Test. (TW)
(Change)
(Reverted to revision 31218 by Tito Dutta (talk): Test. (TW))
 
(7 intermediate revisions by the same user not shown)
Line 52: Line 52:
dateType,
dateType,
tostring(dateString)
tostring(dateString)
), 0)
), 4)
end
end


Line 67: Line 67:
-- other items down accordingly.
-- other items down accordingly.
return table.insert(t, table.remove(t, pos))
return table.insert(t, table.remove(t, pos))
end
local function walkHierarchy(hierarchy, start)
local toWalk, retval = {[start] = true}, {}
while true do
-- Can't use pairs() since we're adding and removing things as we're iterating
local k = next(toWalk)
if k == nil then break end
toWalk[k] = nil
retval[k] = true
for _,v in ipairs(hierarchy[k]) do
if not retval[v] then
toWalk[v] = true
end
end
end
return retval
end
end


Line 105: Line 122:
'invalid action ("%s")',
'invalid action ("%s")',
tostring(args.action)
tostring(args.action)
), 0)
), 3)
end
end


Line 135: Line 152:
obj.reason = mw.ustring.lower(args[1])
obj.reason = mw.ustring.lower(args[1])
if obj.reason:find('|') then
if obj.reason:find('|') then
error('reasons cannot contain the pipe character ("|")', 0)
error('reasons cannot contain the pipe character ("|")', 3)
end
end
end
end
Line 526: Line 543:
level,
level,
talkKey
talkKey
))
), 8)
end
end
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
Line 566: Line 583:
return pagetypes[self._protectionObj.title.namespace]
return pagetypes[self._protectionObj.title.namespace]
or pagetypes.default
or pagetypes.default
or error('no default pagetype defined')
or error('no default pagetype defined', 8)
end
end


Line 581: Line 598:
msg = protectionBlurbs.edit.default
msg = protectionBlurbs.edit.default
else
else
error('no protection blurb defined for protectionBlurbs.edit.default')
error('no protection blurb defined for protectionBlurbs.edit.default', 8)
end
end
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
Line 607: Line 624:
msg = protectionLevels.edit.default
msg = protectionLevels.edit.default
else
else
error('no protection level defined for protectionLevels.edit.default')
error('no protection level defined for protectionLevels.edit.default', 8)
end
end
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
Line 686: Line 703:
tostring(key),
tostring(key),
type(msg)
type(msg)
))
), 4)
end
end
return self:_substituteParameters(msg)
return self:_substituteParameters(msg)
Line 783: Line 800:
-- Renders the banner.
-- Renders the banner.
makeMessageBox = makeMessageBox or require('Module:Message box').main
makeMessageBox = makeMessageBox or require('Module:Message box').main
local reasonText = self._reasonText or error('no reason text set')
local reasonText = self._reasonText or error('no reason text set', 2)
local explanationText = self._explanationText
local explanationText = self._explanationText
local mbargs = {
local mbargs = {
Line 846: Line 863:
function p._main(args, cfg, title)
function p._main(args, cfg, title)
args = args or {}
args = args or {}
cfg = cfg or require('Module:Protection banner/config')
cfg = cfg or require(CONFIG_MODULE)


-- Initialise the protection object and check for errors
local protectionObj = Protection.new(args, cfg, title)
local protectionObjCreated, protectionObj = pcall(
Protection.new,
args,
cfg,
title
)
if not protectionObjCreated then
local errorBlurb = cfg.msg['error-message-blurb'] or 'Error: $1.'
local errorText = mw.message.newRawMessage(errorBlurb)
:params(protectionObj) -- protectionObj is the error message
:plain()
return string.format(
'<strong class="error">%s</strong>%s',
errorText,
makeCategoryLink(
cfg.msg['tracking-category-incorrect'],
title and title.text or mw.title.getCurrentTitle().text
)
)
end
-- Initialise the blurb object
local blurbObj = Blurb.new(protectionObj, args, cfg)


local ret = {}
local ret = {}


-- Render the banner
-- If a page's edit protection is equally or more restrictive than its protection from some other action,
if protectionObj:isProtected() then
-- then don't bother displaying anything for the other action (except categories).
ret[#ret + 1] = tostring(
if protectionObj.action == 'edit' or not walkHierarchy(cfg.hierarchy, protectionObj.level)[effectiveProtectionLevel('edit', protectionObj.title)] then
(yesno(args.small) and Padlock or Banner)
-- Initialise the blurb object
.new(protectionObj, blurbObj, cfg)
local blurbObj = Blurb.new(protectionObj, args, cfg)
)
-- Render the banner
if protectionObj:isProtected() then
ret[#ret + 1] = tostring(
(yesno(args.small) and Padlock or Banner)
.new(protectionObj, blurbObj, cfg)
)
end
end
end
 
-- Render the categories
-- Render the categories
if yesno(args.category) ~= false then
if yesno(args.category) ~= false then
Line 895: Line 896:


-- Find default args, if any.
-- Find default args, if any.
local parentTitle = frame:getParent():getTitle()
local parent = frame.getParent and frame:getParent()
parentTitle = parentTitle:gsub('/sandbox$', '')
local defaultArgs = parent and cfg.wrappers[parent:getTitle():gsub('/sandbox$', '')]
local defaultArgs = cfg.wrappers[parentTitle] or {}


-- Find user args, and use the parent frame if we are being called from a
-- Find user args, and use the parent frame if we are being called from a
Line 909: Line 909:
-- Build the args table. User-specified args overwrite default args.
-- Build the args table. User-specified args overwrite default args.
local args = {}
local args = {}
for k, v in pairs(defaultArgs) do
for k, v in pairs(defaultArgs or {}) do
args[k] = v
args[k] = v
end
end
14,061

edits