Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Cargo: Difference between revisions

From EmissaryFGC
dustloop>Alistair3149
No edit summary
dustloop>Alistair3149
No edit summary
Line 91: Line 91:
local args = {}
local args = {}


mw.dumpObject( { tables, fields, args } )
return mw.dumpObject( { tables, fields, args } )
end
end




return cargo
return cargo

Revision as of 20:05, 11 May 2024

Documentation for this module may be created at Module:Cargo/doc

local cargo = {}
local extCargo = mw.ext.cargo
local checkType = require( 'libraryUtil' ).checkType
local mArguments
local cache = {}


--- Flatten key value pairs table into string
---
--- @param t table Table to be flatten
--- @return string
local function flattenTableToString( t )
	local stringTable = {}
	for k, v in pairs( t ) do
		table.insert( stringTable, string.format( '%s:%s', k, v ) )
	end
	return table.concat( stringTable, ',' )
end


--- Cache Cargo query result
---
--- @param results table Results from Cargo query
--- @param tables string Tables param from Cargo query
--- @param fields string Fields param from Cargo query
--- @param argsString string Flatten args param from Cargo query
local function cacheQueryResults( results, tables, fields, argsString )
	-- Init index
	if not cache[ tables ] then
		cache[ tables ] = {}
	end
	if not cache[ tables ][ fields ] then
		cache[ tables ][ fields ] = {}
	end
	if not cache[ tables ][ fields ][ argsString ] then
		cache[ tables ][ fields ][ argsString ] = {}
	end
	mw.log( string.format( '[Cargo] Cached Cargo query result at: cache[\'%s\'][\'%s\'][\'%s\']', tables, fields, argsString ) )
	cache[ tables ][ fields ][ argsString ] = results
end


--- Return the result of a Cargo query (#cargo_query)
--- TODO: Move to a shared module since this will be reused
---
--- For info on the parameters:
--- @see https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support
---
--- @param tables string The set of Cargo tables to be queried
--- @param fields string The field or set of fields to be displayed
--- @param args table Optional parameters for the query
--- @return table
function cargo.getQueryResults( tables, fields, args )
	-- args are optional
	args = args or {}

	checkType( 'Module:Cargo/cargo.getQueryResults', 1, tables, 'string' )
	checkType( 'Module:Cargo/cargo.getQueryResults', 2, fields, 'string' )
	checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' )

	local argsString = flattenTableToString( args )

	if cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then
		return cache[ tables ][ fields ][ argsString ]
	end

	local success, results = pcall( extCargo.query, tables, fields, args )
	mw.logObject( { tables, fields, args }, '[Cargo] Run Cargo query with the following params' )

	if not success then
		error( 'Failed to run cargo query at getQueryResults()' )
	end

	mw.logObject( results, '[Cargo] Found Cargo query result' )
	cacheQueryResults( results, tables, fields, argsString )
	return results
end


function cargo.getQueryResultsFromWikitext( frame )
	mArguments = require( 'Module:Arguments' )
	local templateArgs = mArguments.getArgs( frame )

	local tables = templateArgs[ 'tables' ]
	local fields = templateArgs[ 'fields' ]

	if not tables or not fields then
		error( 'Missing tables or fields parameter' )
	end

	local args = {}

	return mw.dumpObject( { tables, fields, args } )
end


return cargo
MediaWiki Appliance - Powered by TurnKey Linux