As part of the Unified Community Platform project, your wiki will be migrated to the new platform in the next few weeks. Read more here.

Module:FormUtil

From PUBG Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.

Suggested usage: Retrieve both url and partialurl and send partialurl back to this module if you need to restrict the query further in the same module.


local util_table = require('Module:TableUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')

local p = {}
local h = {}
function h.escape(str)
	local lookup = {
		[' '] = '%%20',
		['"'] = '%%22',
		['>'] = '%%3E',
		['<'] = '%%3C',
		['+'] = '%%2B',
	}
	for k, v in pairs(lookup) do
		str = str:gsub(k, v)
	end
	return str
end

function p.fullURL(formInfo, args)
	local tbl = {}
	for k, v in pairs(args) do
		if v and v ~= 'No' then
			tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
		end
	end
	local form_args = h.escape(table.concat(tbl,'&'))
	return ('%s/Special:RunQuery/%s?%s&pfRunQueryFormName=%s'):format(
		mw.site.server,
		formInfo.form,
		form_args,
		formInfo.form
	)
end

function p.partialURL(formInfo, args)
	local tbl = {}
	for k, v in pairs(args) do
		if v and v ~= 'No' then
			tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
		end
	end
	local form_args = h.escape(table.concat(tbl,'&'))
	return ('%s/Special:RunQuery/%s?%s'):format(
		mw.site.server,
		formInfo.form,
		form_args
	)
end

function p.addToURL(formInfo, args)
	local tbl = {}
	for k, v in pairs(args) do
		if v and v ~= 'No' then
			tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
		end
	end
	local newQuery = h.escape(table.concat(tbl, '&'))
	return ('%s&%s&pfRunQueryFormName=%s'):format(
		formInfo.partial,
		newQuery,
		formInfo.form
	)
end

function p.templateArgs(formInfo, args)
	local formInfo = util_table.guaranteeTable(formInfo)
	local tbl = {}
	for k, v in pairs(args) do
		tbl[#tbl+1] = '|' .. k .. '=' .. v
	end
	return mw.text.nowiki(('{{%s%s}}'):format(formInfo.template, util_table.concat(tbl, '')))
end

function p.permalink(output, args, formInfo)
	output:wikitext(i18n.print('Permalink'), p.fullURL(formInfo, args))
	output:wikitext('<br>')
	output:wikitext(i18n.print('TemplateArgs'))
	output:tag('div')
		:css('display', 'inline-block')
		:wikitext(mw.text.tag{
		name = 'code',
		content = p.templateArgs(formInfo, args)
	})
end

function p.printLog(output)
	local log = util_vars.getVar('log')
	if not log then return end
	output:tag('br')
	output:wikitext(log)
end

return p