NBA 2K Esports Wiki
[checked revision][checked revision]
m (Syncing content across wikis, if something seems broken as a result let me know)
Esen (talk | contribs)
No edit summary
 
Line 1: Line 1:
  +
local util_table = require('Module:TableUtil')
-- FormUtil
 
local forms = {}
+
local util_vars = require('Module:VarsUtil')
  +
local i18n = require('Module:i18nUtil')
   
  +
local p = {}
-- if you have an ordered list of fields and an ordered list of values
 
  +
local h = {}
function forms.makeBaseQueryURL(form, template, fields, values)
 
  +
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 = {}
 
local tbl = {}
for k, v in ipairs(fields) do
+
for k, v in pairs(args) do
  +
if v and v ~= 'No' then
tbl[#tbl+1] = template .. '%5B' .. v .. '%5D=' .. values[k]
 
  +
tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
  +
end
 
end
 
end
  +
local form_args = h.escape(table.concat(tbl,'&'))
 
partialurl = string.format('%s/Special:RunQuery/%s?%s',
+
return ('%s/Special:RunQuery/%s?%s&pfRunQueryFormName=%s'):format(
 
mw.site.server,
 
mw.site.server,
form,
+
formInfo.form,
  +
form_args,
string.gsub(table.concat(tbl,'&'),' ','%%20')
 
  +
formInfo.form
)
 
 
)
 
url = partialurl .. '&pfRunQueryFormName=' .. form
 
 
return { full = url, partial = partialurl }
 
 
end
 
end
   
  +
function p.partialURL(formInfo, args)
-- if you don't care about order and just want to make a query from all args
 
function forms.makeBaseQueryURLFromArgs(form, template, args, returnValue)
 
 
local tbl = {}
 
local tbl = {}
 
for k, v in pairs(args) do
 
for k, v in pairs(args) do
if v ~= 'No' then
+
if v and v ~= 'No' then
tbl[#tbl+1] = template .. '%5B' .. k .. '%5D=' .. v
+
tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
 
end
 
end
 
end
 
end
  +
local form_args = h.escape(table.concat(tbl,'&'))
 
partialurl = string.format('%s/Special:RunQuery/%s?%s',
+
return ('%s/Special:RunQuery/%s?%s'):format(
 
mw.site.server,
 
mw.site.server,
form,
+
formInfo.form,
  +
form_args
string.gsub(table.concat(tbl,'&'),' ','%%20')
 
)
+
)
  +
end
 
  +
url = partialurl .. '&pfRunQueryFormName=' .. form
 
  +
function p.addToURL(formInfo, args)
if returnValue == 'full' then
 
  +
local tbl = {}
return url
 
  +
for k, v in pairs(args) do
elseif returnValue == 'partial' then
 
  +
if v and v ~= 'No' then
return partialurl
 
  +
tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v))
else
 
  +
end
return { full = url, partial = partialurl }
 
 
end
 
end
  +
local newQuery = h.escape(table.concat(tbl, '&'))
  +
return ('%s&%s&pfRunQueryFormName=%s'):format(
  +
formInfo.partial,
  +
newQuery,
  +
formInfo.form
 
)
 
end
 
end
   
  +
function p.templateArgs(formInfo, args)
-- if you have a partial query and an ordered list of fields and values to add to it at the end (this requires 2 separate lists of what to add)
 
  +
local formInfo = util_table.guaranteeTable(formInfo)
function forms.addToQueryURL(form, template, oldquery, newfields, newvalues)
 
 
 
local tbl = {}
 
local tbl = {}
for k, v in ipairs(newfields) do
+
for k, v in pairs(args) do
tbl[#tbl+1] = template .. '%5B' .. v .. '%5D=' .. newvalues[k]
+
tbl[#tbl+1] = '|' .. k .. '=' .. v
 
end
 
end
  +
return mw.text.nowiki(('{{%s%s}}'):format(formInfo.template, util_table.concat(tbl, '')))
 
  +
end
partialurl = oldquery .. '&' .. string.gsub(table.concat(tbl,'&'),' ','%%20')
 
  +
 
  +
function p.permalink(output, args, formInfo)
url = partialurl .. '&pfRunQueryFormName=' .. form
 
  +
output:wikitext(i18n.print('Permalink'), p.fullURL(formInfo, args))
 
  +
output:wikitext('<br>')
return { full = url, partial = partialurl }
 
  +
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
 
end
   
return forms
+
return p

Latest revision as of 21:50, 4 September 2019

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