Module:Protection banner: Difference between revisions
Content deleted Content added
simplify reason-setting code |
m 11 revisions imported: import clickable buttons 2 |
||
(70 intermediate revisions by 12 users not shown) | |||
Line 4:
-- Initialise necessary modules.
require('Module:No globals')
local
local effectiveProtectionLevel = require('Module:Effective protection level')._main
local effectiveProtectionExpiry = require('Module:Effective protection expiry')._main
local yesno = require('Module:Yesno')
-- Lazily initialise modules and objects we don't always need.
local getArgs, makeMessageBox, lang
-- Set constants.
local CONFIG_MODULE = 'Module:Protection banner/config'
--------------------------------------------------------------------------------
Line 16 ⟶ 19:
--------------------------------------------------------------------------------
local function
if cat then
return string.format(
'[[%s:%s|%s]]',
mw.site.namespaces[14].name,
cat,
sort
)
end
end
Line 40 ⟶ 32:
-- Validation function for the expiry and the protection date
local function validateDate(dateString, dateType)
if not lang then
lang = mw.language.getContentLanguage()
end
local success, result = pcall(lang.formatDate, lang, 'U', dateString)
if success then
Line 49 ⟶ 43:
end
error(string.format(
'invalid %s:
dateType,
tostring(dateString)
),
end
Line 61 ⟶ 55:
display
)
end
-- Given a directed graph formatted as node -> table of direct successors,
-- get a table of all nodes reachable from a given node (though always
-- including the given node).
local function getReachableNodes(graph, 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) -- This always gets the "first" key
if k == nil then
return retval
end
toWalk[k] = nil
retval[k] = true
for _,v in ipairs(graph[k]) do
if not retval[v] then
toWalk[v] = true
end
end
end
end
Line 67 ⟶ 82:
--------------------------------------------------------------------------------
local Protection =
Protection.__index = Protection
Protection.supportedActions = {
edit = true,
move = true,
autoreview = true,
upload = true
}
Line 84 ⟶ 101:
}
function Protection
obj._cfg = cfg
obj.title = title or mw.title.getCurrentTitle()
-- Set action
if not args.action then
elseif
else
error(string.format(
'invalid action:
tostring(args.action)
),
end
-- Set level
if
-- Users need to be autoconfirmed to move pages anyway, so treat
-- semi-move-protected pages as unprotected.
end
-- Set expiry
local effectiveExpiry = effectiveProtectionExpiry(obj.action, obj.title)
end
-- Set reason
if args[1] then
if obj.reason:find('|') then
error('reasons cannot contain the pipe character ("|")', 3)
end
end
-- Set protection date
if args.date then
obj.protectionDate = validateDate(args.date, 'protection date')
end
-- Set banner config
do
local configTables = {}
if cfg.banners[
configTables[#configTables + 1] = cfg.banners[
end
if cfg.defaultBanners[
configTables[#configTables + 1] = cfg.defaultBanners[
configTables[#configTables + 1] = cfg.defaultBanners[
end
configTables[#configTables + 1] = cfg.masterBanner
for i, field in ipairs(
for j, t in ipairs(configTables) do
if t[field] then
break
end
Line 152 ⟶ 168:
end
end
return setmetatable(obj, Protection)
end
function Protection:isProtected()
return self.level ~= '*'
end
function Protection:isTemporary()
return type(self.expiry) == 'number'
end
Line 176 ⟶ 197:
-- Get the namespace key fragment.
local namespaceFragment = cfg.categoryNamespaceKeys[title.namespace]
if not namespaceFragment and title.namespace % 2 == 1 then
namespaceFragment =
end
Line 195 ⟶ 213:
}
--[[
-- The old protection templates used an ad-hoc protection category system,
-- with some templates prioritising namespaces in their categories, and
--
-- reasons for which namespaces have priority over protection reason.
-- If we are dealing with one of those reasons, move the namespace table to
-- the end of the order table, i.e. give it highest priority. If not, the
-- reason should have highest priority, so move that to the end of the table
-- instead.
--]]
table.insert(order, table.remove(order, self.reason and cfg.reasonsWithNamespacePriority[self.reason] and 2 or 3))
--[[
Line 275 ⟶ 298:
end
end
key = table.concat(key, '
local attempt = cats[key]
if attempt then
return makeCategoryLink(attempt, title.text)
end
end
return ''
end
Line 309 ⟶ 325:
local msg = self._cfg.msg
local ret = { self:makeProtectionCategory() }
if self:isIncorrect() then
ret[#ret + 1] = makeCategoryLink(
msg['tracking-category-incorrect'],
self.title.text
)
end
if self:isTemplateProtectedNonTemplate() then
ret[#ret + 1] = makeCategoryLink(
msg['tracking-category-template'],
self.title.text
)
end
return table.concat(ret)
Line 325 ⟶ 344:
--------------------------------------------------------------------------------
local Blurb =
Blurb.__index = Blurb
Blurb.bannerTextFields = {
explanation = true,
tooltip = true,
alt = true,
link = true
}
function Blurb.new(protectionObj, args, cfg)
return setmetatable({
_cfg = cfg,
_protectionObj = protectionObj,
_args = args
}, Blurb)
end
--
function Blurb
-- Formats a Unix timestamp into dd Month, YYYY format.
lang = lang or mw.language.getContentLanguage()
Line 342 ⟶ 371:
lang.formatDate,
lang,
self._cfg.msg['expiry-date-format'] or 'j F Y',
'@' .. tostring(num)
)
Line 349 ⟶ 378:
end
end
function Blurb:_getExpandedMessage(msgKey)
Line 361 ⟶ 388:
parameterFuncs.CURRENTVERSION = self._makeCurrentVersionParameter
parameterFuncs.EDITREQUEST = self._makeEditRequestParameter
parameterFuncs.EXPIRY = self._makeExpiryParameter
Line 368 ⟶ 393:
parameterFuncs.IMAGELINK = self._makeImageLinkParameter
parameterFuncs.INTROBLURB = self._makeIntroBlurbParameter
parameterFuncs.
parameterFuncs.PAGETYPE = self._makePagetypeParameter
parameterFuncs.PROTECTIONBLURB = self._makeProtectionBlurbParameter
Line 374 ⟶ 399:
parameterFuncs.PROTECTIONLEVEL = self._makeProtectionLevelParameter
parameterFuncs.PROTECTIONLOG = self._makeProtectionLogParameter
parameterFuncs.TALKPAGE = self._makeTalkPageParameter
parameterFuncs.TOOLTIPBLURB = self._makeTooltipBlurbParameter
parameterFuncs.TOOLTIPFRAGMENT = self._makeTooltipFragmentParameter
parameterFuncs.VANDAL = self._makeVandalTemplateParameter
Line 414 ⟶ 439:
self:_getExpandedMessage('current-version-edit-display')
)
end
end
Line 445 ⟶ 446:
local action = self._protectionObj.action
local level = self._protectionObj.level
-- Get the edit request type.
Line 460 ⟶ 452:
if level == 'autoconfirmed' then
requestType = 'semi'
elseif level == 'extendedconfirmed' then
requestType = 'extended'
elseif level == 'templateeditor' then
requestType = 'template'
Line 466 ⟶ 460:
requestType = requestType or 'full'
-- Get the display value.
local display = self:_getExpandedMessage('edit-request-display')
return mEditRequest._link{type = requestType, display = display}
end
Line 472 ⟶ 469:
local expiry = self._protectionObj.expiry
if type(expiry) == 'number' then
return
else
return expiry
Line 479 ⟶ 476:
function Blurb:_makeExplanationBlurbParameter()
-- Cover special cases first.
if self._protectionObj.title.namespace == 8 then
-- MediaWiki namespace
return self:_getExpandedMessage('explanation-blurb-nounprotect')
end
-- Get explanation blurb table keys
local action = self._protectionObj.action
local level = self._protectionObj.level
local
-- Find the message in the explanation blurb table and substitute any
-- parameters.
local explanations = self._cfg.explanationBlurbs
local msg
if explanations[action][level] and explanations[action][level][talkKey] then
msg = explanations[action][level][talkKey]
elseif explanations[action][level] and explanations[action][level].default then
msg = explanations[action][level].default
elseif explanations[action].default and explanations[action].default[talkKey] then
msg = explanations[action].default[talkKey]
elseif explanations[action].default and explanations[action].default.default then
msg = explanations[action].default.default
else
error(string.format(
'could not find explanation blurb for action "%s", level "%s" and talk key "%s"',
action,
level,
talkKey
), 8)
end
return self:
end
Line 519 ⟶ 526:
function Blurb:_makeIntroBlurbParameter()
if
return self:_getExpandedMessage('intro-blurb-expiry')
else
Line 526 ⟶ 533:
end
function Blurb:
if self._protectionObj
return self:_getExpandedMessage('
else
return self:_getExpandedMessage('
end
end
Line 538 ⟶ 545:
return pagetypes[self._protectionObj.title.namespace]
or pagetypes.default
or error('no default pagetype defined', 8)
end
Line 553 ⟶ 560:
msg = protectionBlurbs.edit.default
else
error('no protection blurb defined for protectionBlurbs.edit.default', 8)
end
return self:_substituteParameters(msg)
Line 561 ⟶ 568:
local protectionDate = self._protectionObj.protectionDate
if type(protectionDate) == 'number' then
return
else
return protectionDate
Line 579 ⟶ 586:
msg = protectionLevels.edit.default
else
error('no protection level defined for protectionLevels.edit.default', 8)
end
return self:_substituteParameters(msg)
Line 600 ⟶ 607:
self:_getExpandedMessage('protection-log-display')
)
end
end
Line 616 ⟶ 615:
mw.site.namespaces[self._protectionObj.title.namespace].talk.name,
self._protectionObj.title.text,
self.
self:_getExpandedMessage('talk-page-link-display')
)
Line 622 ⟶ 621:
function Blurb:_makeTooltipBlurbParameter()
if
return self:_getExpandedMessage('tooltip-blurb-expiry')
else
return self:_getExpandedMessage('tooltip-blurb-noexpiry')
end
end
function Blurb:_makeTooltipFragmentParameter()
if self._protectionObj:isTemporary() then
return self:_getExpandedMessage('tooltip-fragment-expiry')
else
return self:_getExpandedMessage('tooltip-fragment-noexpiry')
end
end
Line 631 ⟶ 638:
function Blurb:_makeVandalTemplateParameter()
return require('Module:Vandal-m')._main{
self.
}
end
Line 637 ⟶ 644:
-- Public methods --
function Blurb:
-- Validate input.
if not key or not Blurb.bannerTextFields[key] then
error(string.format(
'"%s" is not a valid banner config field',
tostring(key)
), 2)
end
-- Generate the text.
local msg = self._protectionObj.bannerConfig[key]
if type(msg) == 'string' then
return self:_substituteParameters(msg)
elseif type(msg) == 'function' then
msg = msg(self._protectionObj, self._args)
if type(msg) ~= 'string' then
error(string.format(
'bad output from banner config function with key "%s"'
.. ' (expected string, got %s)',
tostring(key),
type(msg)
), 4)
end
return self:_substituteParameters(msg)
end
end
Line 668 ⟶ 675:
--------------------------------------------------------------------------------
local BannerTemplate =
BannerTemplate.__index = BannerTemplate
function BannerTemplate
obj._cfg = cfg
-- Set the image filename.
local imageFilename = protectionObj.bannerConfig.image
if imageFilename then
else
-- If an image filename isn't specified explicitly in the banner config,
Line 682 ⟶ 691:
local action = protectionObj.action
local level = protectionObj.level
local namespace = protectionObj.title.namespace
local reason = protectionObj.reason
-- Deal with special cases first.
if (
namespace == 10
or namespace == 828
or reason and obj._cfg.indefImageReasons[reason]
)
and action == 'edit'
and level == 'sysop'
and not
then
-- Fully protected modules and templates get the special red "indef"
-- padlock.
else
-- Deal with regular protection types.
local images =
if images[action] then
if images[action][level] then
elseif images[action].default then
end
end
end
end
return setmetatable(obj, BannerTemplate)
end
Line 720 ⟶ 726:
or self._cfg.msg['image-filename-default']
or 'Transparent.gif'
return
file = filename,
caption = self.imageCaption
}
end
Line 732 ⟶ 739:
--------------------------------------------------------------------------------
local Banner =
Banner.__index = Banner
function Banner
local obj = BannerTemplate.
obj.imageWidth = 40
return setmetatable(obj, Banner)
end
Line 746 ⟶ 755:
-- Renders the banner.
makeMessageBox = makeMessageBox or require('Module:Message box').main
local reasonText = self._reasonText or error('no reason text set', 2)
local explanationText = self._explanationText
local mbargs = {
Line 765 ⟶ 774:
--------------------------------------------------------------------------------
local Padlock =
Padlock.__index = Padlock
function Padlock
local obj = BannerTemplate.
obj.imageWidth = 20
or cfg.
or '
return setmetatable(obj, Padlock)
end
function Padlock:__tostring()
local
-- The nowiki tag helps prevent whitespace at the top of articles.
return frame:extensionTag{name = 'nowiki'} .. frame:extensionTag{
name = 'indicator',
}
end
Line 806 ⟶ 817:
function p._main(args, cfg, title)
args = args or {}
cfg = cfg or require(CONFIG_MODULE)
local protectionObj = Protection.new(args, cfg, title)
local ret = {}
-- If a page's edit protection is equally or more restrictive than its
-- protection from some other action, then don't bother displaying anything
-- for the other action (except categories).
if protectionObj.action == 'edit' or
args.demolevel or
not getReachableNodes(
cfg.hierarchy,
protectionObj.level
)[effectiveProtectionLevel('edit', protectionObj.title)]
then
-- Initialise the blurb object
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
-- Render the categories
if yesno(args.category) ~= false then
Line 843 ⟶ 854:
end
function p.main(frame, cfg)
cfg = cfg or require(CONFIG_MODULE)
-- Find default args, if any.
local parent = frame.getParent and frame:getParent()
local defaultArgs = parent and cfg.wrappers[parent:getTitle():gsub('/sandbox$', '')]
-- Find user args, and use the parent frame if we are being called from a
-- wrapper template.
getArgs = getArgs or require('Module:Arguments').getArgs
local userArgs = getArgs(frame, {
parentOnly = defaultArgs,
frameOnly = not defaultArgs
})
-- Build the args table. User-specified args overwrite default args.
local args = {}
for k, v in pairs(defaultArgs or {}) do
args[k] = v
end
for k, v in pairs(userArgs) do
args[k] = v
end
return p._main(args, cfg)
end
|