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.


-- FormUtil
local forms = {}

-- if you have an ordered list of fields and an ordered list of values
function forms.makeBaseQueryURL(form, template, fields, values)
	local tbl = {}
	for k, v in ipairs(fields) do
		tbl[#tbl+1] = template .. '%5B' .. v .. '%5D=' .. values[k]
	end
	
	partialurl = string.format('%s/Special:RunQuery/%s?%s',
		mw.site.server,
		form,
		string.gsub(table.concat(tbl,'&'),' ','%%20')
		)
		
	url = partialurl .. '&pfRunQueryFormName=' .. form
		
	return { full = url, partial = partialurl }
end

-- 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 = {}
	for k, v in pairs(args) do
		if v ~= 'No' then
			tbl[#tbl+1] = template .. '%5B' .. k .. '%5D=' .. v
		end
	end
	
	partialurl = string.format('%s/Special:RunQuery/%s?%s',
		mw.site.server,
		form,
		string.gsub(table.concat(tbl,'&'),' ','%%20')
		)
		
	url = partialurl .. '&pfRunQueryFormName=' .. form
	if returnValue == 'full' then
		return url
	elseif returnValue == 'partial' then
		return partialurl
	else
		return { full = url, partial = partialurl }
	end
end

-- 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)
function forms.addToQueryURL(form, template, oldquery, newfields, newvalues)
	
	local tbl = {}
	for k, v in ipairs(newfields) do
		tbl[#tbl+1] = template .. '%5B' .. v .. '%5D=' .. newvalues[k]
	end
	
	partialurl = oldquery .. '&' .. string.gsub(table.concat(tbl,'&'),' ','%%20')
		
	url = partialurl .. '&pfRunQueryFormName=' .. form
		
	return { full = url, partial = partialurl }
	
	
end

return forms