Module:Cargo

local Cargo = {}

local checkType = require( 'libraryUtil' ).checkType local cargoQuery = mw.ext.cargo.query

-- Clear empty string values from a given table and all subtables. -- -- @param tbl local function clearEmptyValues( tbl ) local newTbl = {} for k,v in pairs( tbl ) do   if type( v ) == 'table' then newTbl[k] = clearEmptyValues( v ) elseif v ~= '' then newTbl[k] = v   end end

return newTbl end

-- Run a Cargo query. -- -- @param tables -- @param fields -- @param args function Cargo.query( tables, fields, args ) checkType( 'query', 1, tables, 'string' ) checkType( 'query', 2, fields, 'string' ) checkType( 'query', 3, args, 'table', true )

local results = cargoQuery( tables, fields, args ) -- Return nil if no results were found. if #results == 0 then return end results = clearEmptyValues( results )

return results end

-- Store values to a Cargo table. -- -- @param cargoTable -- @param values function Cargo.store( cargoTable, values ) checkType( 'store', 1, cargoTable, 'string' ) checkType( 'store', 2, values, 'table' )

local args = {} args._table = cargoTable for k,v in pairs( values ) do   k = tostring( k ) v = tostring( v ) args[k] = v end

mw.getCurrentFrame:callParserFunction( '#cargo_store', args ) end

return Cargo