Module:CargoQuery

From Granblue Fantasy Wiki
Jump to navigation Jump to search

Based on https://help.gamepedia.com/Module:CargoQuery.

This module lets you get around the |no html= bug that Cargo has by avoiding |format=template.

Use Lua names of all query parameters, so |, |groupBy=, etc.

For simplicity of code, the named args parameter is required to be Yes, and you do not need to specify it.

Unlike |format=template, this wrapper will NOT rename parameters with underscores in them to use spaces instead.

Parameters & Invocation

{{#invoke:CargoQuery|template
|tables= 	corresponds to table / tables
|join= 		corresponds to join on
|fields= 	corresponds to fields
|where= 	corresponds to where
|groupBy= 	corresponds to group by
|having= 	corresponds to having
|orderBy= 	corresponds to order by
|limit= 	corresponds to limit
|template=
|intro=
|outro=
|delimiter=
|default=
}}

Dependencies


local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.template(frame)
	local args = getArgs(frame)
	return p._template(args)
end

function p._template(args)
	local frame = mw.getCurrentFrame()
	local known_fields = {
		['tables'] = true,
		['join'] = true,
		['fields'] = true,
		['where'] = true,
		['groupBy'] = true,
		['having'] = true,
		['orderBy'] = true,
		['limit'] = true
	}
	local query = {}
	for k, v in pairs(args) do
		if known_fields[k] == true then
			query[k] = v
		end
	end

	local data = mw.ext.cargo.query(query.tables, query.fields, query)
	if not next(data) then
		return frame:preprocess(args.default or '')
	end
	
	local result = {}
	for _, row in ipairs(data) do
		table.insert(result, frame:expandTemplate{ title = args.template, args = row })
	end
	local intro = frame:preprocess(args.intro or '')
	local outro = frame:preprocess(args.outro or '')
	return intro .. table.concat(result, args.delimiter or '') .. outro
end

function p.testTemplate()
	args = {}
	args.tables = 'trophies'
	args.fields = 'id,mission_id,title,jptitle,step,steps,description,reward,notes,quality,event_name,step_stars'
  args.where = 'event_id=6023'
  args.template = 'TrophyList/EventRow'
	return p._template(args)
end

return p