protected function execute($arguments = array(), $options = array()) { $timer = new QubitTimer(); // overall timing $context = sfContext::createInstance($this->configuration); if (empty($arguments['folder']) || !file_exists($arguments['folder'])) { throw new sfException('You must specify a valid import folder or file'); } // Set indexing preference if ($options['noindex']) { QubitSearch::getInstance()->disabled = true; } else { QubitSearch::getInstance()->getEngine()->enableBatchMode(); } if (is_dir($arguments['folder'])) { // Recurse into the import folder $files = $this->dir_tree(rtrim($arguments['folder'], '/')); } else { $files = array($arguments['folder']); } // TODO: Add some colour $this->log("Importing " . count($files) . " files from " . $arguments['folder'] . " (indexing is " . ($options['noindex'] ? "DISABLED" : "ENABLED") . ") ...\n"); $count = 0; $total = count($files); foreach ($files as $file) { $start = microtime(true); // Choose import type based on file extension, eg. csv, xml if ('csv' == pathinfo($file, PATHINFO_EXTENSION)) { $importer = new QubitCsvImport(); $importer->import($file, $options); } elseif ('xml' == pathinfo($file, PATHINFO_EXTENSION)) { $importer = new QubitXmlImport(); $importer->import($file, array('strictXmlParsing' => false)); } else { // Move on to the next file continue; } // Try to free up memory unset($importer); $count++; $split = microtime(true) - $start; // Store details if output is specified if ($options['output']) { $rows[] = array($count, $split, memory_get_usage()); } if ($options['v']) { $this->log(basename($file) . " imported (" . round($split, 2) . " s) (" . $count . "/" . $total . ")"); } } // Create/open output file if specified if ($options['output']) { $fh = fopen($options['output'], 'w+'); foreach ($rows as $row) { fputcsv($fh, $row); } fputcsv($fh, array('', $timer->elapsed(), memory_get_peak_usage())); fclose($fh); } // Optimize index if enabled if (!$options['noindex']) { QubitSearch::getInstance()->getEngine()->optimize(); } $this->log("\nSuccessfully imported " . $count . " XML/CSV files in " . $timer->elapsed() . " s. " . memory_get_peak_usage() . " bytes used."); }
public function execute($request) { $this->timer = new QubitTimer(); $file = $request->getFiles('file'); // if we got here without a file upload, go to file selection if (!isset($file)) { $this->redirect(array('module' => 'object', 'action' => 'importSelect')); } // set indexing preference if ($request->getParameter('noindex')) { QubitSearch::getInstance()->disabled = true; } else { QubitSearch::getInstance()->getEngine()->enableBatchMode(); } try { // choose import type based on file extension, eg. csv, xml if ('csv' == pathinfo($file['name'], PATHINFO_EXTENSION)) { $importer = new QubitCsvImport(); $importer->import($file['tmp_name'], array('schema' => $request->getParameter('schema'))); } elseif ('xml' == pathinfo($file['name'], PATHINFO_EXTENSION)) { $importer = new QubitXmlImport(); $importer->import($file['tmp_name'], array('strictXmlParsing' => false)); } elseif ('zip' == pathinfo($file['name'], PATHINFO_EXTENSION) && class_exists('ZipArchive')) { $zipFolder = $file['tmp_name'] . '-zip'; if (!file_exists($zipFolder)) { mkdir($zipFolder, 0755); } // extract the zip archive into the temporary folder // TODO: need some error handling here $zip = new ZipArchive(); $zip->open($file['tmp_name']); $zip->extractTo($zipFolder); $zip->close(); $files = $this->dir_tree($zipFolder); // this code is from lib/importBulkTask.class.php foreach ($files as $import_file) { // try to free up memory unset($importer); // choose import type based on file extension, eg. csv, xml if ('csv' == pathinfo($import_file, PATHINFO_EXTENSION)) { $importer = new QubitCsvImport(); $importer->import($import_file, array('schema')); } elseif ('xml' == pathinfo($import_file, PATHINFO_EXTENSION)) { $importer = new QubitXmlImport(); $importer->import($import_file, array('strictXmlParsing' => false)); } else { // move on to the next file continue; } } } else { $this->context->user->setFlash('error', $this->context->i18n->__('Unable to import selected file')); $this->redirect(array('module' => 'object', 'action' => 'importSelect')); } } catch (sfException $e) { $this->context->user->setFlash('error', $e->getMessage()); $this->redirect(array('module' => 'object', 'action' => 'importSelect')); } // optimize index if enabled if (!$request->getParameter('noindex')) { QubitSearch::getInstance()->getEngine()->optimize(); } $this->errors = $importer->getErrors(); $this->rootObject = $importer->getRootObject(); $this->objectType = strtr(get_class($this->rootObject), array('Qubit' => '')); }