پودمان:Protection banner: تفاوت میان نسخهها
use require('strict') instead of require('Module:No globals')
جز (۱ نسخه واردشده) |
fa>Wikinoor.ir (use require('strict') instead of require('Module:No globals')) |
||
خط ۱: | خط ۱: | ||
-- This module implements {{pp-meta}} and its daughter templates such as | -- This module implements {{pp-meta}} and its daughter templates such as | ||
-- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}. | -- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}. | ||
-- Initialise necessary modules. | -- Initialise necessary modules. | ||
require(' | require('strict') | ||
local | local makeFileLink = require('Module:File link')._main | ||
local effectiveProtectionLevel = require('Module:Effective protection level')._main | local effectiveProtectionLevel = require('Module:Effective protection level')._main | ||
local effectiveProtectionExpiry = require('Module:Effective protection expiry')._main | |||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
خط ۱۵: | خط ۱۴: | ||
-- Set constants. | -- Set constants. | ||
local CONFIG_MODULE = 'Module:Protection banner/config' | local CONFIG_MODULE = 'Module:Protection banner/config' | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Helper functions | -- Helper functions | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function makeCategoryLink(cat, sort) | local function makeCategoryLink(cat, sort) | ||
if cat then | |||
if cat | |||
return string.format( | return string.format( | ||
'[[%s:%s|%s]]', | '[[%s:%s|%s]]', | ||
mw.site.namespaces[14].name, | |||
cat, | cat, | ||
sort | sort | ||
) | ) | ||
end | end | ||
end | end | ||
خط ۶۹: | خط ۳۲: | ||
-- Validation function for the expiry and the protection date | -- Validation function for the expiry and the protection date | ||
local function validateDate(dateString, dateType) | local function validateDate(dateString, dateType) | ||
if not lang then | |||
lang = mw.language.getContentLanguage() | |||
local success, result = pcall(lang.formatDate, lang, 'U | end | ||
local success, result = pcall(lang.formatDate, lang, 'U', dateString) | |||
if success then | if success then | ||
result = tonumber(result) | result = tonumber(result) | ||
خط ۷۹: | خط ۴۳: | ||
end | end | ||
error(string.format( | error(string.format( | ||
'%s | 'invalid %s: %s', | ||
dateType, | dateType, | ||
tostring(dateString) | tostring(dateString) | ||
خط ۹۳: | خط ۵۷: | ||
end | 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 function | |||
local toWalk, retval = {[start] = true}, {} | local toWalk, retval = {[start] = true}, {} | ||
while true do | while true do | ||
-- Can't use pairs() since we're adding and removing things as we're iterating | -- Can't use pairs() since we're adding and removing things as we're iterating | ||
local k = next(toWalk) | local k = next(toWalk) -- This always gets the "first" key | ||
if k == nil then | if k == nil then | ||
return retval | |||
end | |||
toWalk[k] = nil | toWalk[k] = nil | ||
retval[k] = true | retval[k] = true | ||
for _,v in ipairs( | for _,v in ipairs(graph[k]) do | ||
if not retval[v] then | if not retval[v] then | ||
toWalk[v] = true | toWalk[v] = true | ||
خط ۱۱۳: | خط ۷۶: | ||
end | end | ||
end | end | ||
end | end | ||
خط ۱۲۶: | خط ۸۸: | ||
edit = true, | edit = true, | ||
move = true, | move = true, | ||
autoreview = true | autoreview = true, | ||
upload = true | |||
} | } | ||
خط ۱۵۰: | خط ۱۱۳: | ||
else | else | ||
error(string.format( | error(string.format( | ||
' | 'invalid action: %s', | ||
tostring(args.action) | tostring(args.action) | ||
), 3) | ), 3) | ||
خط ۱۵۷: | خط ۱۲۰: | ||
-- Set level | -- Set level | ||
obj.level = args.demolevel or effectiveProtectionLevel(obj.action, obj.title) | obj.level = args.demolevel or effectiveProtectionLevel(obj.action, obj.title) | ||
if | if 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. | ||
خط ۱۶۸: | خط ۱۲۷: | ||
-- Set expiry | -- Set expiry | ||
local effectiveExpiry = effectiveProtectionExpiry(obj.action, obj.title) | |||
if effectiveExpiry == 'infinity' then | |||
obj.expiry = 'indef' | |||
elseif effectiveExpiry ~= 'unknown' then | |||
obj.expiry = validateDate(effectiveExpiry, 'expiry date') | |||
end | end | ||
خط ۱۸۲: | خط ۱۳۸: | ||
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(' | error('reasons cannot contain the pipe character ("|")', 3) | ||
end | end | ||
end | end | ||
خط ۲۱۳: | خط ۱۶۹: | ||
end | end | ||
return setmetatable(obj, Protection) | 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 | end | ||
خط ۲۱۸: | خط ۱۸۲: | ||
return self.level ~= '*' | return self.level ~= '*' | ||
end | 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() | function Protection:isTemporary() | ||
خط ۲۲۴: | خط ۱۹۶: | ||
function Protection:makeProtectionCategory() | function Protection:makeProtectionCategory() | ||
if not self:shouldHaveProtectionCategory() then | |||
return '' | |||
end | |||
local cfg = self._cfg | local cfg = self._cfg | ||
local title = self.title | local title = self.title | ||
-- Get the expiry key fragment. | -- Get the expiry key fragment. | ||
خط ۲۴۱: | خط ۲۱۲: | ||
-- Get the namespace key fragment. | -- Get the namespace key fragment. | ||
local | local namespaceFragment = cfg.categoryNamespaceKeys[title.namespace] | ||
if not namespaceFragment and title.namespace % 2 == 1 then | |||
namespaceFragment = 'talk' | |||
end | end | ||
-- Define the order that key fragments are tested in. This is done with an | -- 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 | -- array of tables containing the value to be tested, along with its | ||
خط ۲۷۱: | خط ۲۳۹: | ||
-- instead. | -- instead. | ||
--]] | --]] | ||
table.insert(order, table.remove(order, self.reason and cfg.reasonsWithNamespacePriority[self.reason] and 2 or 3)) | |||
--[[ | --[[ | ||
خط ۳۵۷: | خط ۳۲۰: | ||
end | end | ||
return '' | return '' | ||
end | end | ||
function Protection:isIncorrect() | function Protection:isIncorrect() | ||
local expiry = self.expiry | local expiry = self.expiry | ||
return not self: | return not self:shouldHaveProtectionCategory() | ||
or type(expiry) == 'number' and expiry < os.time() | or type(expiry) == 'number' and expiry < os.time() | ||
end | end | ||
خط ۳۸۸: | خط ۳۳۹: | ||
function Protection:makeCategoryLinks() | function Protection:makeCategoryLinks() | ||
local msg = self._cfg.msg | local msg = self._cfg.msg | ||
local ret = { self:makeProtectionCategory() } | local ret = {self:makeProtectionCategory()} | ||
if self:isIncorrect() then | if self:isIncorrect() then | ||
ret[#ret + 1] = makeCategoryLink( | ret[#ret + 1] = makeCategoryLink( | ||
خط ۴۳۷: | خط ۳۸۲: | ||
function Blurb:_formatDate(num) | function Blurb:_formatDate(num) | ||
-- Formats a Unix timestamp into dd Month, YYYY format. | -- Formats a Unix timestamp into dd Month, YYYY format. | ||
lang = lang or mw.language.getContentLanguage() | |||
lang = lang or mw. | |||
local success, date = pcall( | local success, date = pcall( | ||
lang.formatDate, | lang.formatDate, | ||
lang, | lang, | ||
self._cfg.msg['expiry-date-format'] or ' | self._cfg.msg['expiry-date-format'] or 'j F Y', | ||
'@' .. | '@' .. tostring(num) | ||
) | ) | ||
if success then | if success then | ||
خط ۴۹۹: | خط ۴۴۳: | ||
-- We need the move log link. | -- We need the move log link. | ||
return makeFullUrl( | return makeFullUrl( | ||
' | 'Special:Log', | ||
{type = 'move', page = pagename}, | {type = 'move', page = pagename}, | ||
self:_getExpandedMessage('current-version-move-display') | self:_getExpandedMessage('current-version-move-display') | ||
خط ۵۲۳: | خط ۴۶۷: | ||
if level == 'autoconfirmed' then | if level == 'autoconfirmed' then | ||
requestType = 'semi' | requestType = 'semi' | ||
elseif level == 'extendedconfirmed' then | |||
requestType = 'extended' | |||
elseif level == 'templateeditor' then | elseif level == 'templateeditor' then | ||
requestType = 'template' | requestType = 'template' | ||
خط ۵۷۰: | خط ۵۱۶: | ||
else | else | ||
error(string.format( | error(string.format( | ||
' | 'could not find explanation blurb for action "%s", level "%s" and talk key "%s"', | ||
action, | action, | ||
level, | level, | ||
خط ۵۹۶: | خط ۵۴۲: | ||
function Blurb:_makeIntroBlurbParameter() | function Blurb:_makeIntroBlurbParameter() | ||
if self._protectionObj:isTemporary() then | if self._protectionObj:isTemporary() then | ||
return | return self:_getExpandedMessage('intro-blurb-expiry') | ||
else | else | ||
return self:_getExpandedMessage('intro-blurb-noexpiry') | return self:_getExpandedMessage('intro-blurb-noexpiry') | ||
خط ۶۰۴: | خط ۵۵۰: | ||
function Blurb:_makeIntroFragmentParameter() | function Blurb:_makeIntroFragmentParameter() | ||
if self._protectionObj:isTemporary() then | if self._protectionObj:isTemporary() then | ||
return | return self:_getExpandedMessage('intro-fragment-expiry') | ||
else | else | ||
return self:_getExpandedMessage('intro-fragment-noexpiry') | return self:_getExpandedMessage('intro-fragment-noexpiry') | ||
خط ۶۱۴: | خط ۵۶۰: | ||
return pagetypes[self._protectionObj.title.namespace] | return pagetypes[self._protectionObj.title.namespace] | ||
or pagetypes.default | or pagetypes.default | ||
or error(' | or error('no default pagetype defined', 8) | ||
end | end | ||
خط ۶۲۹: | خط ۵۷۵: | ||
msg = protectionBlurbs.edit.default | msg = protectionBlurbs.edit.default | ||
else | else | ||
error(' | error('no protection blurb defined for protectionBlurbs.edit.default', 8) | ||
end | end | ||
return self:_substituteParameters(msg) | return self:_substituteParameters(msg) | ||
خط ۶۵۵: | خط ۶۰۱: | ||
msg = protectionLevels.edit.default | msg = protectionLevels.edit.default | ||
else | else | ||
error(' | error('no protection level defined for protectionLevels.edit.default', 8) | ||
end | end | ||
return self:_substituteParameters(msg) | return self:_substituteParameters(msg) | ||
خط ۶۶۵: | خط ۶۱۱: | ||
-- We need the pending changes log. | -- We need the pending changes log. | ||
return makeFullUrl( | return makeFullUrl( | ||
' | 'Special:Log', | ||
{type = 'stable', page = pagename}, | {type = 'stable', page = pagename}, | ||
self:_getExpandedMessage('pc-log-display') | self:_getExpandedMessage('pc-log-display') | ||
خط ۶۷۲: | خط ۶۱۸: | ||
-- We need the protection log. | -- We need the protection log. | ||
return makeFullUrl( | return makeFullUrl( | ||
' | 'Special:Log', | ||
{type = 'protect', page = pagename}, | {type = 'protect', page = pagename}, | ||
self:_getExpandedMessage('protection-log-display') | self:_getExpandedMessage('protection-log-display') | ||
خط ۶۹۱: | خط ۶۳۷: | ||
function Blurb:_makeTooltipBlurbParameter() | function Blurb:_makeTooltipBlurbParameter() | ||
if self._protectionObj:isTemporary() then | if self._protectionObj:isTemporary() then | ||
return | return self:_getExpandedMessage('tooltip-blurb-expiry') | ||
else | else | ||
return self:_getExpandedMessage('tooltip-blurb-noexpiry') | return self:_getExpandedMessage('tooltip-blurb-noexpiry') | ||
خط ۶۹۹: | خط ۶۴۵: | ||
function Blurb:_makeTooltipFragmentParameter() | function Blurb:_makeTooltipFragmentParameter() | ||
if self._protectionObj:isTemporary() then | if self._protectionObj:isTemporary() then | ||
return | return self:_getExpandedMessage('tooltip-fragment-expiry') | ||
else | else | ||
return self:_getExpandedMessage('tooltip-fragment-noexpiry') | return self:_getExpandedMessage('tooltip-fragment-noexpiry') | ||
خط ۷۰۶: | خط ۶۵۲: | ||
function Blurb:_makeVandalTemplateParameter() | function Blurb:_makeVandalTemplateParameter() | ||
return | return mw.getCurrentFrame():expandTemplate{ | ||
self._args.user or self._protectionObj.title.baseText | title="vandal-m", | ||
args={self._args.user or self._protectionObj.title.baseText} | |||
} | } | ||
end | end | ||
خط ۷۱۷: | خط ۶۶۴: | ||
if not key or not Blurb.bannerTextFields[key] then | if not key or not Blurb.bannerTextFields[key] then | ||
error(string.format( | error(string.format( | ||
'"%s" | '"%s" is not a valid banner config field', | ||
tostring(key) | tostring(key) | ||
), 2) | ), 2) | ||
خط ۷۳۰: | خط ۶۷۷: | ||
if type(msg) ~= 'string' then | if type(msg) ~= 'string' then | ||
error(string.format( | error(string.format( | ||
' | 'bad output from banner config function with key "%s"' | ||
.. ' ( | .. ' (expected string, got %s)', | ||
tostring(key), | tostring(key), | ||
type(msg) | type(msg) | ||
خط ۷۸۹: | خط ۷۳۶: | ||
end | end | ||
return setmetatable(obj, BannerTemplate) | return setmetatable(obj, BannerTemplate) | ||
end | end | ||
خط ۸۰۳: | خط ۷۴۲: | ||
or self._cfg.msg['image-filename-default'] | or self._cfg.msg['image-filename-default'] | ||
or 'Transparent.gif' | or 'Transparent.gif' | ||
return | return makeFileLink{ | ||
file = filename, | |||
size = (self.imageWidth or 20) .. 'px', | |||
alt = self._imageAlt, | |||
link = self._imageLink, | |||
caption = self.imageCaption | |||
} | |||
end | end | ||
خط ۸۲۰: | خط ۷۶۰: | ||
function Banner.new(protectionObj, blurbObj, cfg) | function Banner.new(protectionObj, blurbObj, cfg) | ||
local obj = BannerTemplate.new(protectionObj, cfg) -- This doesn't need the blurb. | local obj = BannerTemplate.new(protectionObj, cfg) -- This doesn't need the blurb. | ||
obj | obj.imageWidth = 40 | ||
obj | obj.imageCaption = blurbObj:makeBannerText('alt') -- Large banners use the alt text for the tooltip. | ||
obj._reasonText = blurbObj:makeBannerText('text') | obj._reasonText = blurbObj:makeBannerText('text') | ||
obj._explanationText = blurbObj:makeBannerText('explanation') | obj._explanationText = blurbObj:makeBannerText('explanation') | ||
خط ۸۳۱: | خط ۷۷۱: | ||
-- 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(' | local reasonText = self._reasonText or error('no reason text set', 2) | ||
local explanationText = self._explanationText | local explanationText = self._explanationText | ||
local mbargs = { | local mbargs = { | ||
خط ۸۵۵: | خط ۷۹۵: | ||
function Padlock.new(protectionObj, blurbObj, cfg) | function Padlock.new(protectionObj, blurbObj, cfg) | ||
local obj = BannerTemplate.new(protectionObj, cfg) -- This doesn't need the blurb. | local obj = BannerTemplate.new(protectionObj, cfg) -- This doesn't need the blurb. | ||
obj | obj.imageWidth = 20 | ||
obj | obj.imageCaption = blurbObj:makeBannerText('tooltip') | ||
obj._imageAlt = blurbObj:makeBannerText('alt') | obj._imageAlt = blurbObj:makeBannerText('alt') | ||
obj._imageLink = blurbObj:makeBannerText('link') | obj._imageLink = blurbObj:makeBannerText('link') | ||
obj. | obj._indicatorName = cfg.padlockIndicatorNames[protectionObj.action] | ||
or cfg. | or cfg.padlockIndicatorNames.default | ||
or ' | or 'pp-default' | ||
return setmetatable(obj, Padlock) | return setmetatable(obj, Padlock) | ||
end | end | ||
function Padlock:__tostring() | function Padlock:__tostring() | ||
local | local frame = mw.getCurrentFrame() | ||
-- The nowiki tag helps prevent whitespace at the top of articles. | |||
return frame:extensionTag{name = 'nowiki'} .. frame:extensionTag{ | |||
name = 'indicator', | |||
args = {name = self._indicatorName}, | |||
content = self:renderImage() | |||
} | |||
end | end | ||
خط ۸۷۸: | خط ۸۱۸: | ||
-- Exports | -- Exports | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local p = {} | |||
function p._exportClasses() | function p._exportClasses() | ||
خط ۸۹۸: | خط ۸۴۰: | ||
local ret = {} | local ret = {} | ||
-- If a page's edit protection is equally or more restrictive than its protection from some other action, | -- If a page's edit protection is equally or more restrictive than its | ||
-- protection from some other action, then don't bother displaying anything | |||
if protectionObj.action == 'edit' or args.demolevel or not | -- for the other action (except categories). | ||
if not yesno(args.catonly) and (protectionObj.action == 'edit' or | |||
args.demolevel or | |||
not getReachableNodes( | |||
cfg.hierarchy, | |||
protectionObj.level | |||
)[effectiveProtectionLevel('edit', protectionObj.title)]) | |||
then | |||
-- Initialise the blurb object | -- Initialise the blurb object | ||
local blurbObj = Blurb.new(protectionObj, args, cfg) | local blurbObj = Blurb.new(protectionObj, args, cfg) | ||
-- Render the banner | -- Render the banner | ||
if protectionObj: | if protectionObj:shouldShowLock() then | ||
ret[#ret + 1] = tostring( | ret[#ret + 1] = tostring( | ||
(yesno(args.small) and Padlock or Banner) | (yesno(args.small) and Padlock or Banner) | ||
خط ۹۲۶: | خط ۸۷۵: | ||
-- Find default args, if any. | -- Find default args, if any. | ||
local parent = frame.getParent and frame:getParent() | local parent = frame.getParent and frame:getParent() | ||
local defaultArgs = parent and cfg.wrappers[ | 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 | -- Find user args, and use the parent frame if we are being called from a |