Module:Submit an edit request: Difference between revisions
Appearance
fix "Lua error in Module:Submit_an_edit_request at line 101: Tried to write global sectionname" |
copy of Module:Submit an edit request/sandbox, minus '/sandbox', as requested at the talk page (Special:Permalink/1033974993#Protected_edit_request_on_16_July_2021) by Pppery |
||
| Line 81: | Line 81: | ||
if not useTalkPage then | if not useTalkPage then | ||
if level == 'manual' then | if level == 'manual' then | ||
return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page') | return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page'))) | ||
else | else | ||
pageToEdit = message('protected-talk-page-request-page') | pageToEdit = message('protected-talk-page-request-page') | ||
| Line 103: | Line 100: | ||
end | end | ||
local content = mw.title.new(pageToEdit):getContent() | local content = mw.title.new(pageToEdit):getContent() | ||
if content and content:find("== *" .. escape(sectionname) .. " *==") then | if content and content:find("===? *" .. escape(sectionname) .. " *=?==") then | ||
local dedup = 2 | local dedup = 2 | ||
while true do | while true do | ||
local newname = message("preload-title-dedup-suffix", sectionname, dedup) | local newname = message("preload-title-dedup-suffix", sectionname, dedup) | ||
if not content:find("== *" .. escape(newname) .. " *==") then | if not content:find("===? *" .. escape(newname) .. " *=?==") then | ||
sectionname = newname | sectionname = newname | ||
break | break | ||
| Line 114: | Line 111: | ||
end | end | ||
end | end | ||
local | local args = { | ||
action = 'edit', | action = 'edit', | ||
editintro = getLevelInfo(level, 'editintro'), | editintro = getLevelInfo(level, 'editintro'), | ||
preload = message('preload-template'), | preload = message('preload-template'), | ||
section = 'new' | section = 'new' | ||
} | } | ||
url = tostring( | if not useTalkPage then | ||
args.nosummary = 'true' | |||
-- Section header is handled by the preload | |||
else | |||
args.preloadtitle = sectionname | |||
end | |||
local url = tostring(mw.uri.fullUrl(pageToEdit, args)) | |||
-- Add the preload parameters. @TODO: merge this into the mw.uri.fullUrl | -- Add the preload parameters. @TODO: merge this into the mw.uri.fullUrl | ||
| Line 130: | Line 132: | ||
url = url .. encodeParam('preloadparams[]', getLevelInfo(level, 'requestTemplate')) | url = url .. encodeParam('preloadparams[]', getLevelInfo(level, 'requestTemplate')) | ||
url = url .. encodeParam('preloadparams[]', titleObj.prefixedText) | url = url .. encodeParam('preloadparams[]', titleObj.prefixedText) | ||
if not useTalkPage then | |||
url = url .. encodeParam('preloadparams[]', '=== ' .. titleObj.prefixedText .. ' ===\n') | |||
else | |||
-- Empty 3rd parameter | |||
url = url .. encodeParam('preloadparams[]', '') | |||
end | |||
return url | return url | ||
end | end | ||
Revision as of 11:45, 17 July 2021
Documentation for this module may be created at Module:Submit an edit request/doc
-- This module implements {{Submit an edit request}}.
local CONFIG_MODULE = 'Module:Submit an edit request/config'
-- Load necessary modules
local mRedirect = require('Module:Redirect')
local cfg = mw.loadData(CONFIG_MODULE)
local effectiveProtectionLevel = require('Module:Effective protection level')._main
local escape = require("Module:String")._escapePattern
local lang = mw.language.getContentLanguage()
local p = {}
local validLevels = {
semi = 'semi',
extended = 'extended',
template = 'template',
full = 'full',
interface = 'interface',
manual = 'manual'
}
local function message(key, ...)
local params = {...}
local msg = cfg[key]
if #params < 1 then
return msg
else
return mw.message.newRawMessage(msg):params(params):plain()
end
end
local function isTranscludedOnMainPage(titleObj)
local mainPage = message('main-page')
for i, source in ipairs(titleObj.cascadingProtection.sources) do
if source == mainPage then
return true
end
end
return false
end
local function validateLevel(level)
return level and validLevels[level] or 'full'
end
local function getLevelInfo(level, field)
return cfg.protectionLevels[level][field]
end
local function resolveRedirect(page)
return mRedirect.luaMain(page)
end
local function isProtected(page)
local action = mw.title.new(page).exists and 'edit' or 'create'
return effectiveProtectionLevel(action, page) ~= '*'
end
function p.makeRequestUrl(level, titleObj)
titleObj = titleObj or mw.title.getCurrentTitle()
if isTranscludedOnMainPage(titleObj) then
local basePage = titleObj.basePageTitle.fullText
if cfg['main-page-content'][basePage] then
return tostring(mw.uri.fullUrl(message('main-page-request-page')))
end
end
local talkPageName = titleObj.talkPageTitle
local useTalkPage = true
if talkPageName == nil then
useTalkPage = false
else
talkPageName = resolveRedirect(talkPageName.prefixedText)
if isProtected(talkPageName) then
useTalkPage = false
end
end
level = validateLevel(level)
local pageToEdit
local sectionname
if not useTalkPage then
if level == 'manual' then
return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
else
pageToEdit = message('protected-talk-page-request-page')
sectionname = titleObj.prefixedText
end
elseif level == 'manual' then
return tostring(mw.uri.fullUrl(talkPageName, {
action = 'edit',
section = 'new'
}))
else
pageToEdit = talkPageName
sectionname = message(
'preload-title-text',
getLevelInfo(level, 'levelText'),
lang:formatDate(message('preload-title-date-format'))
)
end
local content = mw.title.new(pageToEdit):getContent()
if content and content:find("===? *" .. escape(sectionname) .. " *=?==") then
local dedup = 2
while true do
local newname = message("preload-title-dedup-suffix", sectionname, dedup)
if not content:find("===? *" .. escape(newname) .. " *=?==") then
sectionname = newname
break
end
dedup = dedup + 1
end
end
local args = {
action = 'edit',
editintro = getLevelInfo(level, 'editintro'),
preload = message('preload-template'),
section = 'new'
}
if not useTalkPage then
args.nosummary = 'true'
-- Section header is handled by the preload
else
args.preloadtitle = sectionname
end
local url = tostring(mw.uri.fullUrl(pageToEdit, args))
-- Add the preload parameters. @TODO: merge this into the mw.uri.fullUrl
-- query table once [[phab:T93059]] is fixed.
local function encodeParam(key, val)
return string.format('&%s=%s', mw.uri.encode(key), mw.uri.encode(val))
end
url = url .. encodeParam('preloadparams[]', getLevelInfo(level, 'requestTemplate'))
url = url .. encodeParam('preloadparams[]', titleObj.prefixedText)
if not useTalkPage then
url = url .. encodeParam('preloadparams[]', '=== ' .. titleObj.prefixedText .. ' ===\n')
else
-- Empty 3rd parameter
url = url .. encodeParam('preloadparams[]', '')
end
return url
end
function p._link(args)
return string.format(
'<span class="plainlinks">[%s %s]</span>',
p.makeRequestUrl(args.type),
args.display or message('default-display-value')
)
end
function p._button(args)
return require('Module:Clickable button 2').luaMain{
[1] = args.display or message('default-display-value'),
url = p.makeRequestUrl(args.type),
class = 'mw-ui-progressive'
}
end
local function makeInvokeFunc(func, wrapper)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = {wrapper}
})
return func(args)
end
end
p.link = makeInvokeFunc(p._link, message('link-wrapper-template'))
p.button = makeInvokeFunc(p._button, message('button-wrapper-template'))
return p