Module:Protection banner: Difference between revisions
Content deleted Content added
undo, as a lot of code would need to be changed for template demos to look respectable on unprotected pages |
m 1 revision imported from wikipedia:Module:Protection_banner |
||
(40 intermediate revisions by 12 users not shown) | |||
Line 3:
-- Initialise necessary modules.
require('
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 makeCategoryLink(cat, sort)
if cat then
return string.format(
'[[%s:%s|%s]]',
mw.site.namespaces[14].name,
cat,
sort
)
end
end
Line 30 ⟶ 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 39 ⟶ 43:
end
error(string.format(
'invalid %s:
dateType,
tostring(dateString)
),
end
Line 53 ⟶ 57:
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 63 ⟶ 82:
--------------------------------------------------------------------------------
local Protection =
Protection.__index = Protection
Protection.supportedActions = {
edit = true,
move = true,
autoreview = true,
upload = true
}
Line 80 ⟶ 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
error('reasons cannot contain the pipe character ("|")',
end
end
Line 129 ⟶ 144:
-- Set protection date
if args.date then
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 153 ⟶ 168:
end
end
return setmetatable(obj, Protection)
end
function Protection:isUserScript()
-- Whether the page is a user JavaScript or CSS page.
local title = self.title
return title.namespace == 2 and (
title.contentModel == 'javascript' or title.contentModel == 'css'
)
end
Line 158 ⟶ 182:
return self.level ~= '*'
end
function Protection:shouldShowLock()
-- Whether we should output a banner/padlock
return self:isProtected() and not self:isUserScript()
end
-- Whether this page needs a protection category.
Protection.shouldHaveProtectionCategory = Protection.shouldShowLock
function Protection:isTemporary()
Line 164 ⟶ 196:
function Protection:makeProtectionCategory()
if not self:shouldHaveProtectionCategory() then
return ''
end
local cfg = self._cfg
local title = self.title
-- Get the expiry key fragment.
Line 181 ⟶ 212:
-- Get the namespace key fragment.
local namespaceFragment = cfg.categoryNamespaceKeys[title.namespace]
if not namespaceFragment and title.namespace % 2 == 1 then
namespaceFragment =
end
-- Define the order that key fragments are tested in. This is done with an
-- array of tables containing the value to be tested, along with its
Line 211 ⟶ 239:
-- instead.
--]]
--[[
Line 293 ⟶ 316:
local attempt = cats[key]
if attempt then
return makeCategoryLink(attempt, title.text)
end
end
return ''
end
function Protection:isIncorrect()
local expiry = self.expiry
return not self:
or type(expiry) == 'number' and expiry < os.time()
end
Line 324 ⟶ 339:
function Protection:makeCategoryLinks()
local msg = self._cfg.msg
local ret = {
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 341 ⟶ 359:
--------------------------------------------------------------------------------
local Blurb =
Blurb.__index = Blurb
Blurb.bannerTextFields = {
Line 351 ⟶ 370:
}
function Blurb
return setmetatable({
_cfg = cfg,
_protectionObj = protectionObj,
_args = args
}, Blurb)
end
Line 387 ⟶ 408:
parameterFuncs.IMAGELINK = self._makeImageLinkParameter
parameterFuncs.INTROBLURB = self._makeIntroBlurbParameter
parameterFuncs.INTROFRAGMENT = self._makeIntroFragmentParameter
parameterFuncs.PAGETYPE = self._makePagetypeParameter
parameterFuncs.PROTECTIONBLURB = self._makeProtectionBlurbParameter
Line 394 ⟶ 416:
parameterFuncs.TALKPAGE = self._makeTalkPageParameter
parameterFuncs.TOOLTIPBLURB = self._makeTooltipBlurbParameter
parameterFuncs.TOOLTIPFRAGMENT = self._makeTooltipFragmentParameter
parameterFuncs.VANDAL = self._makeVandalTemplateParameter
Line 444 ⟶ 467:
if level == 'autoconfirmed' then
requestType = 'semi'
elseif level == 'extendedconfirmed' then
requestType = 'extended'
elseif level == 'templateeditor' then
requestType = 'template'
Line 495 ⟶ 520:
level,
talkKey
), 8)
end
return self:_substituteParameters(msg)
Line 520 ⟶ 545:
else
return self:_getExpandedMessage('intro-blurb-noexpiry')
end
end
function Blurb:_makeIntroFragmentParameter()
if self._protectionObj:isTemporary() then
return self:_getExpandedMessage('intro-fragment-expiry')
else
return self:_getExpandedMessage('intro-fragment-noexpiry')
end
end
Line 527 ⟶ 560:
return pagetypes[self._protectionObj.title.namespace]
or pagetypes.default
or error('no default pagetype defined', 8)
end
Line 542 ⟶ 575:
msg = protectionBlurbs.edit.default
else
error('no protection blurb defined for protectionBlurbs.edit.default', 8)
end
return self:_substituteParameters(msg)
Line 568 ⟶ 601:
msg = protectionLevels.edit.default
else
error('no protection level defined for protectionLevels.edit.default', 8)
end
return self:_substituteParameters(msg)
Line 607 ⟶ 640:
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
function Blurb:_makeVandalTemplateParameter()
return mw.getCurrentFrame():expandTemplate{
title="vandal-m",
args={self._args.user or self._protectionObj.title.baseText}
}
end
Line 639 ⟶ 681:
tostring(key),
type(msg)
), 4)
end
return self:_substituteParameters(msg)
Line 649 ⟶ 691:
--------------------------------------------------------------------------------
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 664 ⟶ 708:
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'
Line 673 ⟶ 722:
-- 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 700 ⟶ 742:
or self._cfg.msg['image-filename-default']
or 'Transparent.gif'
return
file = filename,
caption = self.imageCaption
}
end
Line 712 ⟶ 755:
--------------------------------------------------------------------------------
local Banner =
Banner.__index = Banner
function Banner
local obj = BannerTemplate.
obj.imageWidth = 40
return setmetatable(obj, Banner)
end
Line 726 ⟶ 771:
-- 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 745 ⟶ 790:
--------------------------------------------------------------------------------
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 787 ⟶ 834:
function p._main(args, cfg, title)
args = args or {}
cfg = cfg or require(
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).
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:shouldShowLock() 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 830 ⟶ 871:
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
|