/** * Checks the parameters and returns the appropriate exporter (if any) * @param string $sExpression The OQL query to export or null * @param string $sQueryId The entry in the query phrasebook if $sExpression is null * @param string $sFormat The code of export format: csv, pdf, html, xlsx * @throws MissingQueryArgument * @return Ambigous <iBulkExport, NULL> */ function CheckParameters($sExpression, $sQueryId, $sFormat) { $oExporter = null; if ($sExpression === null && $sQueryId === null) { ReportErrorAndUsage("Missing parameter. The parameter 'expression' or 'query' must be specified."); } // Either $sExpression or $sQueryId must be specified if ($sExpression === null) { $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId)); $oQueries = new DBObjectSet($oSearch); if ($oQueries->Count() > 0) { $oQuery = $oQueries->Fetch(); $sExpression = $oQuery->Get('oql'); $sFields = $oQuery->Get('fields'); if (strlen($sFields) == 0) { $sFields = trim($oQuery->Get('fields')); } } else { ReportErrorAndExit("Invalid query phrasebook identifier: '{$sQueryId}'"); } } if ($sFormat === null) { ReportErrorAndUsage("Missing parameter 'format'."); } // Check if the supplied query is valid (and all the parameters are supplied try { $oSearch = DBObjectSearch::FromOQL($sExpression); $aArgs = array(); foreach ($oSearch->GetQueryParams() as $sParam => $foo) { $value = utils::ReadParam('arg_' . $sParam, null, true, 'raw_data'); if (!is_null($value)) { $aArgs[$sParam] = $value; } else { throw new MissingQueryArgument("Missing parameter '--arg_{$sParam}'"); } } $oSearch->SetInternalParams($aArgs); $sFormat = utils::ReadParam('format', 'html', true, 'raw_data'); $oExporter = BulkExport::FindExporter($sFormat, $oSearch); if ($oExporter == null) { $aSupportedFormats = BulkExport::FindSupportedFormats(); ReportErrorAndExit("Invalid output format: '{$sFormat}'. The supported formats are: " . implode(', ', array_keys($aSupportedFormats))); } } catch (MissingQueryArgument $e) { ReportErrorAndUsage("Invalid OQL query: '{$sExpression}'.\n" . $e->getMessage()); } catch (OQLException $e) { ReportErrorAndExit("Invalid OQL query: '{$sExpression}'.\n" . $e->getMessage()); } catch (Exception $e) { ReportErrorAndExit($e->getMessage()); } $oExporter->SetFormat($sFormat); $oExporter->SetChunkSize(EXPORTER_DEFAULT_CHUNK_SIZE); $oExporter->SetObjectList($oSearch); $oExporter->ReadParameters(); return $oExporter; }
$sFormat = utils::ReadParam('format', ''); $sExpression = utils::ReadParam('expression', null, false, 'raw_data'); $iQueryId = utils::ReadParam('query', null); if ($sExpression === null) { $oQuerySearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $iQueryId)); $oQueries = new DBObjectSet($oQuerySearch); if ($oQueries->Count() > 0) { $oQuery = $oQueries->Fetch(); $sExpression = $oQuery->Get('oql'); } else { $aResult = array('code' => 'error', 'percentage' => 100, 'message' => "Invalid query phrasebook identifier: '{$iQueryId}'"); } } if ($sExpression !== null) { $oSearch = DBObjectSearch::FromOQL($sExpression); $oExporter = BulkExport::FindExporter($sFormat, $oSearch); $oExporter->SetObjectList($oSearch); $oExporter->SetFormat($sFormat); $oExporter->SetChunkSize(EXPORTER_DEFAULT_CHUNK_SIZE); $oExporter->ReadParameters(); } // First pass, generate the headers $data .= $oExporter->GetHeader(); } else { $oExporter = BulkExport::FindExporterFromToken($token); } if ($oExporter) { $data .= $oExporter->GetNextChunk($aResult); if ($aResult['code'] != 'done') { $oExporter->AppendToTmpFile($data); $aResult['token'] = $oExporter->SaveState();