模块:Submit an affp report

-- This module implements {{Submit an affp report}}.

local CONFIG_MODULE = 'Module:Submit an affp report/config'

-- Load necessary modules
local cfg = mw.loadData(CONFIG_MODULE)

local p = {}

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

function p.makeRequestUrl(pageName)
	if pageName ~= nil then
		-- 在VE/CX中才有的錯誤
		-- Example: Special:Badtitle/ApiErrorFormatter::getDummyTitle, Special:Badtitle/dummy title for API calls set in api.php
		if string.find(pageName, 'Special:Badtitle/') == 1 then
			pageName = ''
		elseif pageName == '$1' then
			pageName = ''
		end
	end

	local url = mw.uri.fullUrl(message('request-page'), {
		action = 'edit',
		preload = message('preload-template'),
		preloadtitle = '',
		section = 'new',
		nosummary = '1',
	})
	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[]', pageName or '')

	return url
end

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

function p._warning(args)
	return string.format(
		'<span class="plainlinks">[%s %s]</span>',
		p.makeRequestUrl(args.page or mw.title.getCurrentTitle().prefixedText),
		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.page),
		class = 'mw-ui-destructive'
	}
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.warning = makeInvokeFunc(p._warning, message('warning-wrapper-template'))
p.button = makeInvokeFunc(p._button, message('button-wrapper-template'))

return p