Author: mcantelon Date: Thu Feb 9 16:37:03 2012 New Revision: 10834 Log: Added CLI tool for checking CSV before importing for empty columns and columns containing pipe characters.
Added: trunk/lib/task/import/csvCheckImportTask.class.php (contents, props changed) Added: trunk/lib/task/import/csvCheckImportTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/import/csvCheckImportTask.class.php Thu Feb 9 16:37:03 2012 (r10834) @@ -0,0 +1,112 @@ +<?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/>. + */ + +/** + * Check csv data + * + * @package symfony + * @subpackage task + * @author Mike Cantelon <[email protected]> + * @version SVN: $Id$ + */ +class csvCheckImportTask extends csvImportBaseTask +{ + protected $namespace = 'csv'; + protected $name = 'check-import'; + protected $briefDescription = 'Check CSV data, providing diagnostic info'; + + protected $detailedDescription = <<<EOF +Check CSV data, providing information about it +EOF; + + /** + * @see sfTask + */ + public function execute($arguments = array(), $options = array()) + { + $this->validateOptions($options); + + $skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0; + + if (false === $fh = fopen($arguments['filename'], 'rb')) + { + throw new sfException('You must specify a valid filename'); + } + + // get import definition + $import = new QubitFlatfileImport(array( + + 'status' => array( + 'nonEmptyColumns' => array() + ), + + 'saveLogic' => function(&$self) + { + foreach($self->status['row'] as $key => $value) + { + $value = $self->status['row'][$key]; + $column = $self->columnNames[$key]; + + // check if column isn't empty + if (trim($value)) + { + $self->status['nonEmptyColumns'][$column] = TRUE; + } + + // check for | character + if (substr_count($value, '|')) + { + $self->status['multiValueColumns'][$column] = TRUE; + } + } + }, + + 'completeLogic' => function(&$self) + { + print "\nAnalysis complete."; + + if (count($self->columnNames != count($self->status['nonEmptyColumns']))) + { + print "\n\nEmpty columns:\n"; + foreach($self->columnNames as $column) + { + if (!isset($self->status['nonEmptyColumns'][$column])) + { + print $column .' '; + } + } + } + + if (count($self->status['multiValueColumns'])) + { + print "\n\nMulti-value columns (contain \"|\" character):\n"; + + foreach(array_keys($self->status['multiValueColumns']) as $column) + { + print $column .' '; + } + } + + print "\n"; + } + )); + + $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.
