Modul:Submit an edit request: Unterschied zwischen den Versionen

Permanently protected module
ZIM HilfeWiki - das Wiki
 
imported>Oetterer
 
(Eine dazwischenliegende Version von einem anderen Benutzer wird nicht angezeigt)
Zeile 1: Zeile 1:
 
-- This module implements {{Submit an edit request}}.
 
-- This module implements {{Submit an edit request}}.
 +
 +
local CONFIG_MODULE = 'Module:Submit an edit request/config'
  
 
-- Load necessary modules
 
-- Load necessary modules
 
local mRedirect = require('Module:Redirect')
 
local mRedirect = require('Module:Redirect')
local cfg = mw.loadData('Module:Submit an edit request/config')
+
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 p = {}
 +
 +
local validLevels = {
 +
semi = 'semi',
 +
extended = 'extended',
 +
template = 'template',
 +
full = 'full',
 +
interface = 'interface',
 +
manual = 'manual'
 +
}
  
 
local function message(key, ...)
 
local function message(key, ...)
Zeile 15: Zeile 29:
 
return mw.message.newRawMessage(msg):params(params):plain()
 
return mw.message.newRawMessage(msg):params(params):plain()
 
end
 
end
 +
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
 
end
  
 
function p.makeRequestUrl(level, titleObj)
 
function p.makeRequestUrl(level, titleObj)
 
titleObj = titleObj or mw.title.getCurrentTitle()
 
titleObj = titleObj or mw.title.getCurrentTitle()
+
local basePage = titleObj.basePageTitle.fullText
do
+
if cfg['main-page-content'][basePage] then
local levels = {
+
return tostring(mw.uri.fullUrl(message('main-page-request-page')))
semi = true,
+
end
template = true,
+
 
full = true
+
local talkPageName = titleObj.talkPageTitle
}
+
if talkPageName == nil then
level = level and levels[level] and level or 'full'
+
return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
 
end
 
end
+
talkPageName = resolveRedirect(talkPageName.prefixedText)
local editintro, requestTemplate, levelText
+
if isProtected(talkPageName) then
do
+
return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
local messages = {
 
semi = {
 
editintro = 'semi-editintro',
 
requestTemplate = 'semi-request-template',
 
levelText = 'semi-protectionlevel'
 
},
 
template = {
 
editintro = 'template-editintro',
 
requestTemplate = 'template-request-template',
 
levelText = 'template-protectionlevel'
 
},
 
full = {
 
editintro = 'full-editintro',
 
requestTemplate = 'full-request-template',
 
levelText = 'full-protectionlevel'
 
}
 
}
 
local levelMessages = messages[level]
 
editintro = message(levelMessages.editintro)
 
requestTemplate = message(levelMessages.requestTemplate)
 
levelText = message(levelMessages.levelText)
 
 
end
 
end
+
level = validateLevel(level)
local preloadtitle, talkpagename
+
if level == 'manual' then
do
+
return tostring(mw.uri.fullUrl(talkPageName, {
-- Get the date text.
+
action = 'edit',
local dateFormat = message('preload-title-date-format')
+
section = 'new'
local lang = mw.language.getContentLanguage()
+
}))
local date = lang:formatDate(dateFormat)
 
 
-- Get the talk page name, and resolve it if it is a redirect.
 
local namespace = titleObj.namespace
 
talkpagename = mw.site.namespaces[namespace].talk.name
 
.. ':'
 
..  titleObj.text
 
talkpagename = mRedirect.luaMain(talkpagename)
 
preloadtitle = message('preload-title-text', levelText, date)
 
 
end
 
end
+
local sectionname = message(
local preloadTemplate = message('preload-template')
+
'preload-title-text',
+
getLevelInfo(level, 'levelText'),
local function encode(key, value)
+
lang:formatDate(message('preload-title-date-format'))
key = mw.uri.encode(key)
+
)
value = mw.uri.encode(value)
+
local content = mw.title.new(talkPageName):getContent()
return key .. '=' .. value
+
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 url = mw.uri.fullUrl(talkPageName, {
 +
action = 'edit',
 +
editintro = getLevelInfo(level, 'editintro'),
 +
preload = message('preload-template'),
 +
preloadtitle = sectionname,
 +
section = 'new'
 +
})
 +
url = tostring(url)
 +
 
 +
-- 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
 
end
 +
url = url .. encodeParam('preloadparams[]', getLevelInfo(level, 'requestTemplate'))
 +
url = url .. encodeParam('preloadparams[]', titleObj.prefixedText)
  
local query = {}
 
query[#query + 1] = encode('preload', preloadTemplate)
 
query[#query + 1] = encode('editintro', editintro)
 
query[#query + 1] = encode('preloadparams[]', requestTemplate)
 
query[#query + 1] = encode('preloadtitle', preloadtitle)
 
query[#query + 1] = 'section=new'
 
query[#query + 1] = encode('preloadparams[]', titleObj.prefixedText)
 
 
local url = mw.uri.fullUrl(talkpagename, {action = 'edit'})
 
url = tostring(url) .. '&' .. table.concat(query, '&')
 
 
return url
 
return url
 
end
 
end
Zeile 95: Zeile 111:
 
'<span class="plainlinks">[%s %s]</span>',
 
'<span class="plainlinks">[%s %s]</span>',
 
p.makeRequestUrl(args.type),
 
p.makeRequestUrl(args.type),
args.display or message('default-display-value' .. (((args.language and cfg['default-display-value-' .. args.language]) and '-' .. args.language) or ''))
+
args.display or message('default-display-value')
 
)
 
)
 
end
 
end
Zeile 101: Zeile 117:
 
function p._button(args)
 
function p._button(args)
 
return require('Module:Clickable button 2').luaMain{
 
return require('Module:Clickable button 2').luaMain{
[1] = args.display or message('default-display-value' .. (((args.language and cfg['default-display-value-' .. args.language]) and '-' .. args.language) or '')),
+
[1] = args.display or message('default-display-value'),
 
url = p.makeRequestUrl(args.type),
 
url = p.makeRequestUrl(args.type),
 
class = 'mw-ui-progressive'
 
class = 'mw-ui-progressive'
Zeile 110: Zeile 126:
 
return function (frame)
 
return function (frame)
 
local args = require('Module:Arguments').getArgs(frame, {
 
local args = require('Module:Arguments').getArgs(frame, {
wrappers = wrapper
+
wrappers = {wrapper}
 
})
 
})
 
return func(args)
 
return func(args)

Aktuelle Version vom 8. Oktober 2022, 08:41 Uhr

Documentation icon Module documentation

This module implements the {{submit an edit request}} and {{submit an edit request/link}} templates.

Usage from wikitext[Quelltext bearbeiten]

To use this template from wikitext, you should normally use the {{submit an edit request}} and {{submit an edit request/link}} templates. However, the module can also be used directly from #invoke. For the edit request button, use {{#invoke:Submit an edit request|button|args}}, and for the edit request link only, use {{#invoke:Submit an edit request|link|args}}. Please see the respective template pages for a list of available parameters.

Usage from Lua modules[Quelltext bearbeiten]

To use this module from other Lua modules, first load the module.

local mEditRequest = require('Module:Submit an edit request')

You can then use the _button function to generate an edit request button, and the _link function to generate an edit request link.

mEditRequest._button(args)
mEditRequest._link(args)

The args variable should be a table containing the arguments to pass to the module. To see the different arguments that can be specified and how they affect the module output, please refer to the documentation of {{Submit an edit request}} and {{Submit an edit request/link}}.

Configuration[Quelltext bearbeiten]

This module can be translated and configured for other wikis by editing Module:Submit an edit request/config.


-- 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 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()
	local basePage = titleObj.basePageTitle.fullText
	if cfg['main-page-content'][basePage] then
		return tostring(mw.uri.fullUrl(message('main-page-request-page')))
	end

	local talkPageName = titleObj.talkPageTitle
	if talkPageName == nil then
		return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
	end
	talkPageName = resolveRedirect(talkPageName.prefixedText)
	if isProtected(talkPageName) then
		return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
	end
	level = validateLevel(level)
	if level == 'manual' then
		return tostring(mw.uri.fullUrl(talkPageName, {
			action = 'edit',
			section = 'new'
		}))
	end
	local sectionname = message(
			'preload-title-text',
			getLevelInfo(level, 'levelText'),
			lang:formatDate(message('preload-title-date-format'))
	)
	local content = mw.title.new(talkPageName):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 url = mw.uri.fullUrl(talkPageName, {
		action = 'edit',
		editintro = getLevelInfo(level, 'editintro'),
		preload = message('preload-template'),
		preloadtitle = sectionname,
		section = 'new'
	})
	url = tostring(url)

	-- 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)

	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
Cookies helfen uns bei der Bereitstellung des ZIM HilfeWikis. Bei der Nutzung vom ZIM HilfeWiki werden die in der Datenschutzerklärung beschriebenen Cookies gespeichert.