Module:Submit an edit request

From Aikatsu! Photo on Stage!!
Revision as of 18:28, 25 January 2016 by Rikki21 (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

Usage from wikitext[edit source | hide]

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[edit source | hide]

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.


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[edit source | hide]

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 lang = mw.language.getContentLanguage()

local p = {}

local validLevels = {
	semi = 'semi',
	template = 'template',
	full = 'full'

local function message(key, ...)
	local params = {...}
	local msg = cfg[key]
	if #params < 1 then
		return msg
		return mw.message.newRawMessage(msg):params(params):plain()

local function isTranscludedOnMainPage(titleObj)
	local mainPage = message('main-page')
	for i, source in ipairs(titleObj.cascadingProtection.sources) do
		if source == mainPage then
			return true
	return false

local function validateLevel(level)
	return level and validLevels[level] or 'full'

local function getLevelInfo(level, field)
	return cfg.protectionLevels[level][field]

local function resolveRedirect(page)
	return mRedirect.luaMain(page)

local function isProtected(page)
	local action = and 'edit' or 'create'
	return effectiveProtectionLevel(action, page) ~= '*'

function p.makeRequestUrl(level, titleObj)
	titleObj = titleObj or mw.title.getCurrentTitle()
	if isTranscludedOnMainPage(titleObj) then
		return tostring(mw.uri.fullUrl(message('main-page-request-page')))
	local talkPageName = resolveRedirect(titleObj.talkPageTitle.prefixedText)
	if isProtected(talkPageName) then
		return tostring(mw.uri.fullUrl(message('protected-talk-page-request-page')))
	level = validateLevel(level)
	local url = mw.uri.fullUrl(talkPageName, {
		action = 'edit',
		editintro = getLevelInfo(level, 'editintro'),
		preload = message('preload-template'),
		preloadtitle = message(
			getLevelInfo(level, 'levelText'),
		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))
	url = url .. encodeParam('preloadparams[]', getLevelInfo(level, 'requestTemplate'))
	url = url .. encodeParam('preloadparams[]', titleObj.prefixedText)

	return url

function p._link(args)
	return string.format(
		'<span class="plainlinks">[%s %s]</span>',
		args.display or message('default-display-value')

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'

local function makeInvokeFunc(func, wrapper)
	return function (frame)
		local args = require('Module:Arguments').getArgs(frame, {
			wrappers = {wrapper}
		return func(args)
end = makeInvokeFunc(p._link, message('link-wrapper-template'))
p.button = makeInvokeFunc(p._button, message('button-wrapper-template'))

return p