Differential D18962 Diff 45486 src/applications/search/controller/PhabricatorApplicationSearchController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/controller/PhabricatorApplicationSearchController.php
| Show First 20 Lines • Show All 438 Lines • ▼ Show 20 Lines | private function processExportRequest() { | ||||
| // Try to default to the format the user used last time. If you just | // Try to default to the format the user used last time. If you just | ||||
| // exported to Excel, you probably want to export to Excel again. | // exported to Excel, you probably want to export to Excel again. | ||||
| $format_key = $this->readExportFormatPreference(); | $format_key = $this->readExportFormatPreference(); | ||||
| if (!isset($formats[$format_key])) { | if (!isset($formats[$format_key])) { | ||||
| $format_key = head_key($format_options); | $format_key = head_key($format_options); | ||||
| } | } | ||||
| // Check if this is a large result set or not. If we're exporting a | |||||
| // large amount of data, we'll build the actual export file in the daemons. | |||||
| $threshold = 1000; | |||||
| $query = $engine->buildQueryFromSavedQuery($saved_query); | |||||
| $pager = $engine->newPagerForSavedQuery($saved_query); | |||||
| $pager->setPageSize($threshold + 1); | |||||
| $objects = $engine->executeQuery($query, $pager); | |||||
| $object_count = count($objects); | |||||
| $is_large_export = ($object_count > $threshold); | |||||
| $errors = array(); | $errors = array(); | ||||
| $e_format = null; | $e_format = null; | ||||
| if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
| $format_key = $request->getStr('format'); | $format_key = $request->getStr('format'); | ||||
| if (isset($unavailable_formats[$format_key])) { | if (isset($unavailable_formats[$format_key])) { | ||||
| $unavailable = $unavailable_formats[$format_key]; | $unavailable = $unavailable_formats[$format_key]; | ||||
| Show All 15 Lines | if ($request->isFormPost()) { | ||||
| if (!$format) { | if (!$format) { | ||||
| $e_format = pht('Invalid'); | $e_format = pht('Invalid'); | ||||
| $errors[] = pht('Choose a valid export format.'); | $errors[] = pht('Choose a valid export format.'); | ||||
| } | } | ||||
| if (!$errors) { | if (!$errors) { | ||||
| $this->writeExportFormatPreference($format_key); | $this->writeExportFormatPreference($format_key); | ||||
| $query = $engine->buildQueryFromSavedQuery($saved_query); | $export_engine = id(new PhabricatorExportEngine()) | ||||
| // NOTE: We aren't reading the pager from the request. Exports always | |||||
| // affect the entire result set. | |||||
| $pager = $engine->newPagerForSavedQuery($saved_query); | |||||
| $pager->setPageSize(0x7FFFFFFF); | |||||
| $objects = $engine->executeQuery($query, $pager); | |||||
| $extension = $format->getFileExtension(); | |||||
| $mime_type = $format->getMIMEContentType(); | |||||
| $filename = $filename.'.'.$extension; | |||||
| $format = id(clone $format) | |||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->setTitle($sheet_title); | ->setSearchEngine($engine) | ||||
| ->setSavedQuery($saved_query) | |||||
| $export_data = $engine->newExport($objects); | ->setTitle($sheet_title) | ||||
| $objects = array_values($objects); | ->setFilename($filename) | ||||
| ->setExportFormat($format); | |||||
| $field_list = $engine->newExportFieldList(); | |||||
| $field_list = mpull($field_list, null, 'getKey'); | |||||
| $format->addHeaders($field_list); | |||||
| for ($ii = 0; $ii < count($objects); $ii++) { | |||||
| $format->addObject($objects[$ii], $field_list, $export_data[$ii]); | |||||
| } | |||||
| $export_result = $format->newFileData(); | |||||
| // We have all the data in one big string and aren't actually | |||||
| // streaming it, but pretending that we are allows us to actviate | |||||
| // the chunk engine and store large files. | |||||
| $iterator = new ArrayIterator(array($export_result)); | |||||
| $source = id(new PhabricatorIteratorFileUploadSource()) | if ($is_large_export) { | ||||
| ->setName($filename) | $job = $export_engine->newBulkJob($request); | ||||
| ->setViewPolicy(PhabricatorPolicies::POLICY_NOONE) | |||||
| ->setMIMEType($mime_type) | |||||
| ->setRelativeTTL(phutil_units('60 minutes in seconds')) | |||||
| ->setAuthorPHID($viewer->getPHID()) | |||||
| ->setIterator($iterator); | |||||
| $file = $source->uploadFile(); | return id(new AphrontRedirectResponse()) | ||||
| ->setURI($job->getMonitorURI()); | |||||
| } else { | |||||
| $file = $export_engine->exportFile(); | |||||
| return $this->newDialog() | return $this->newDialog() | ||||
| ->setTitle(pht('Download Results')) | ->setTitle(pht('Download Results')) | ||||
| ->appendParagraph( | ->appendParagraph( | ||||
| pht('Click the download button to download the exported data.')) | pht('Click the download button to download the exported data.')) | ||||
| ->addCancelButton($cancel_uri, pht('Done')) | ->addCancelButton($cancel_uri, pht('Done')) | ||||
| ->setSubmitURI($file->getDownloadURI()) | ->setSubmitURI($file->getDownloadURI()) | ||||
| ->setDisableWorkflowOnSubmit(true) | ->setDisableWorkflowOnSubmit(true) | ||||
| ->addSubmitButton(pht('Download Data')); | ->addSubmitButton(pht('Download Data')); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| $export_form = id(new AphrontFormView()) | $export_form = id(new AphrontFormView()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->appendControl( | ->appendControl( | ||||
| id(new AphrontFormSelectControl()) | id(new AphrontFormSelectControl()) | ||||
| ->setName('format') | ->setName('format') | ||||
| ->setLabel(pht('Format')) | ->setLabel(pht('Format')) | ||||
| ->setError($e_format) | ->setError($e_format) | ||||
| ▲ Show 20 Lines • Show All 445 Lines • Show Last 20 Lines | |||||