پودمان:Protection banner: تفاوت میان نسخهها
بدون خلاصۀ ویرایش
جز (۱ نسخه واردشده) |
Pourrezaei (بحث | مشارکتها) بدون خلاصۀ ویرایش |
||
خط ۱: | خط ۱: | ||
-- 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('Module:No globals') | require('Module:No globals') | ||
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 | ||
خط ۲۴۱: | خط ۱۹۷: | ||
-- 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 | ||
خط ۲۷۱: | خط ۲۲۴: | ||
-- 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 | ||
خط ۳۸۹: | خط ۳۲۵: | ||
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') | ||
خط ۷۱۷: | خط ۶۴۸: | ||
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 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) | ||
خط ۹۲۶: | خط ۸۵۹: | ||
-- 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 |