Author: mcantelon Date: Mon Feb 6 13:18:33 2012 New Revision: 10811 Log: Added CLI tool to define ad-hoc import logic using external files.
Added: trunk/lib/task/import/csvCustomImportTask.class.php Added: trunk/lib/task/import/csvCustomImportTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/import/csvCustomImportTask.class.php Mon Feb 6 13:18:33 2012 (r10811) @@ -0,0 +1,76 @@ +<?php + +/* + * This file is part of Qubit Toolkit. + * + * Qubit Toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Qubit Toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qubit Toolkit. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Import csv data + * + * @package symfony + * @subpackage task + * @author Mike Cantelon <[email protected]> + * @version SVN: $Id: csvImportTask.class.php 10797 2012-02-02 19:59:06Z mcantelon $ + */ +class csvImportTask extends csvImportBaseTask +{ + protected $namespace = 'csv'; + protected $name = 'custom-import'; + protected $briefDescription = 'Import csv data using custom criteria'; + + protected $detailedDescription = <<<EOF +Import CSV data using import logic defined in a file +EOF; + + /** + * @see csvImportBaseTask + */ + protected function configure() + { + parent::configure(); + + $this->addOptions(array( + new sfCommandOption('import-definition', null, sfCommandOption::PARAMETER_REQUIRED, 'PHP file defining and returing an import object.') + )); + } + + /** + * @see sfTask + */ + public function execute($arguments = array(), $options = array()) + { + $this->validateOptions($options); + + $skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0; + + $sourceName = ($options['source-name']) + ? $options['source-name'] + : basename($arguments['filename']); + + if (false === $fh = fopen($arguments['filename'], 'rb')) + { + throw new sfException('You must specify a valid filename'); + } + + $databaseManager = new sfDatabaseManager($this->configuration); + $conn = $databaseManager->getDatabase('propel')->getConnection(); + + // get import definition + $import = require($options['import-definition']); + + $import->csv($fh, $skipRows); + } +} -- You received this message because you are subscribed to the Google Groups "Qubit Toolkit Commits" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/qubit-commits?hl=en.
