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 p = {}

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

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