memory alpha
Module documentation ()

This module produces a message box used to request edits to protected pages. Edit requests can be made for pages protected at any level and it is possible to request edits to multiple pages at once. This module should never be used directly and should instead be used through {{edit request}}.

Syntax

{{#invoke:edit request|_main}}

Module source

require('strict')
local p = {}
local frame = mw.getCurrentFrame()
local args = frame:getParent().args
local yesno = require('Module:Yesno')
local makeMessageBox = require('Module:Message box').main

local function msg(msgName, params)
	if params then
		return mw.message.new(msgName, params):plain()
	end
	return mw.message.new(msgName):plain()
end

local function printError(errorMsg, errorArgs)
	local errorCategory = '[[Category:Memory Alpha edit requests with errors]]'
	return '<span class="error bold">Error: ' .. msg('custom-edit-request-error-' .. errorMsg, errorArgs) .. '</span>' .. errorCategory
end

local function createTitlesTable()
	local titles = {}
	if args[1] then
		for k, v in pairs(args) do
			if type(k) == 'number' then
				table.insert(titles, v)
			end
		end
	else
		table.insert(titles, mw.title.getCurrentTitle().subjectPageTitle.prefixedText)
	end
	return titles
end

local function findLevel(titles)
	local protectionLevels = {
		['emailconfirmed'] = 0,
		['autoconfirmed'] = 1,
		['content-moderator'] = 2,
		['sysop'] = 3,
		['staff'] = 4,
	}
	local effectiveProtectionLevel = require('Module:Effective protection level')._main
	local topLevel = 0
	for k, v in pairs(titles) do
		local action
		if mw.title.new(v).exists then
			action = 'edit'
		else
			action = 'create'
		end
		local currentProtectionLevel = protectionLevels[effectiveProtectionLevel(action, v)]
		if topLevel < currentProtectionLevel then
			topLevel = currentProtectionLevel
		end
	end
	return topLevel
end

local function createLink(target, urlParams, display)
	return '[' .. tostring(mw.uri.fullUrl(target, urlParams)) .. ' ' .. display .. ']'
end

local function makeLinkList(title, hList)
	local hListItems = {}
	hListItems.class = 'inline edit-request-hlist smaller'
	-- Page links.
	table.insert(hListItems, createLink(title, {action = 'edit'}, 'edit'))
	table.insert(hListItems, createLink(title, {action = 'history'}, 'history'))
	table.insert(hListItems, createLink(title, {diff = 'cur', oldid = 'prev'}, 'last'))
	table.insert(hListItems, createLink('Special:WhatLinksHere/' .. title, {}, 'links'))
	-- Protection log link.
	if mw.title.new(title).namespace ~= 8 then -- MediaWiki pages don't have protection log entries.
		table.insert(hListItems, createLink('Special:Log/protect', {page = title}, 'protection log'))
	end
	return hList(hListItems)
end

local function createIntro(titles, hList)
	local getPagetype = require('Module:Pagetype').main
	local title = titles[1]
	local pagetype = getPagetype{page = title, defaultns = 'all'}
	local link = createLink(title, {redirect = 'no'}, title)
	local requested = '<span class="bold">It is requested that '
	local intro
	if #titles > 1 then
		intro = requested .. 'edits be made to the following pages:</span>'
	elseif not mw.title.new(title).exists then
		intro = requested .. 'the ' .. pagetype .. ' ' .. link .. ' be created.</span> ' .. makeLinkList(title, hList)
	else
		intro = requested .. 'an edit be made to the ' .. pagetype .. ' ' .. link .. '.</span> ' .. makeLinkList(title, hList)
	end
	return intro
end

local function createLinkLists(titles, hList)
	local ret = {}
	table.insert(ret, '<ul>')
	for i, v in ipairs(titles) do
		table.insert(ret, mw.ustring.format('<li>%s %s</li>', createLink(v, {redirect = 'no'}, v), makeLinkList(v, hList)))
	end
	table.insert(ret, '</ul>')
	return table.concat(ret)
end

local function createBody(titles, highestProtectionLevel)
	local hasNonInterfacePage = false
	for k, v in pairs(titles) do
		if mw.title.new(v).namespace ~= 8 then
			hasNonInterfacePage = true
		end
	end
	local isPlural = false
	if #titles > 1 then
		isPlural = true
	end
	local editText = 'edit'
	if isPlural then
		editText = editText .. 's'
	end
	local descriptionText = msg('custom-edit-request-description', editText)
	local theEdit = 'The edit'
	if isPlural then
		theEdit = 'These edits'
	end
	local groups = {
		[0] = 'registered user',
		[1] = '[[Memory Alpha:Autoconfirmed user|autoconfirmed user]]',
		[2] = '[[Memory Alpha:Content moderator|content moderator]] or [[Memory Alpha:Administrators|administrator]]',
		[3] = '[[Memory Alpha:Administrators|administrator]]',
		[4] = '[[w:Community Central:Staff|Fandom Staff member]]',
	}
	local userText = groups[highestProtectionLevel]
	local para = frame:expandTemplate{title = 'para', args = {'answered', 'no'}}
	local smallText = msg('custom-edit-request-instructions', {theEdit, userText, para})
	if hasNonInterfacePage then
		smallText = smallText .. ' ' .. msg('custom-edit-request-protect-unprotect')
	end
	if highestProtectionLevel > 1 then
		smallText = smallText .. '</p><p class="smaller">' .. msg('custom-edit-request-fully-protected')
	end
	return mw.ustring.format('<p>%s</p><p class="smaller">%s</p>', descriptionText, smallText)
end

local function setImage(highestProtectionLevel)
	local padlocks = {
		[0] = 'Protection shackle open.svg',		-- upload pending
		[1] = 'Protection shackle semi.svg',
		[2] = 'Protection shackle full.svg',
		[3] = 'Protection shackle interface.svg',
		[4] = 'Protection shackle staff.svg',		-- upload pending
	}
	local padlock = padlocks[highestProtectionLevel]
	local stringToFormat = '[[File:%s|%dpx|alt=|link=]]'
	return mw.ustring.format(stringToFormat, padlock, 60)
end

local function createPagesText(titles)
	local pagesText
	if #titles == 0 then
		pagesText = ''
	elseif #titles == 1 and mw.title.getCurrentTitle().subjectPageTitle.fullText == titles[1] then
		pagesText = ''
	else
		for i, v in pairs(titles) do
			if i == 1 then
				pagesText = ' to ' .. createLink(v, {redirect = 'no'}, v)
			elseif i == #titles and #titles < 3 then
				pagesText = pagesText .. ' and ' .. createLink(v, {redirect = 'no'}, v)
			elseif i == #titles then
				pagesText = pagesText .. ', and ' .. createLink(v, {redirect = 'no'}, v)
			else
				pagesText = pagesText .. ', ' .. createLink(v, {redirect = 'no'}, v)
			end
		end
	end
	return pagesText
end

local function openRequest()
	local title = mw.title.getCurrentTitle()
	local skipCheck = yesno(args.demo, true)
	if not title.isTalkPage and not skipCheck then
		return printError('not-talk-page')
	end
	local titles = createTitlesTable()
	local highestProtectionLevel = findLevel(titles)
	if highestProtectionLevel == 0 then
		return printError('unprotected')
	end
	local hList = require('Module:List').horizontal
	local templatestyles = frame:extensionTag{
		name = 'templatestyles',
		args = {src = 'Module:Edit request/styles.css'}
	}
	local intro = '<div>' .. createIntro(titles, hList) .. '</div>'
	if #titles > 1 then
		intro = intro .. createLinkLists(titles, hList)
	end
	local body = createBody(titles, highestProtectionLevel)
	local category = ''
	if not skipCheck then
		category = '[[Category:Memory Alpha edit requests]]'
	end
	local omboxArgs = {
		text = templatestyles .. intro .. body .. category,
		class = 'editrequest',
		image = setImage(highestProtectionLevel),
	}
	return makeMessageBox('ombox', omboxArgs)
end

local function closedRequest()
	local title = mw.title.getCurrentTitle()
	local skipCheck = yesno(args.demo, true)
	if not title.isTalkPage and not skipCheck then
		return printError('not-talk-page')
	end
	local titles = createTitlesTable()
	local pagesText = createPagesText(titles)
	local para = frame:expandTemplate{title = 'para', args = {'answered'}}
	local omboxArgs = {
		smalltext = msg('custom-edit-request-answered', {pagesText, para}),
		small = true,
		class = 'editrequest',
	}
	return makeMessageBox('ombox', omboxArgs)
end

function p._main()
	if not yesno(args.answered, true) then
		return openRequest()
	else
		return closedRequest()
	end
end

return p