Author: mcantelon Date: Fri Jan 13 18:23:18 2012 New Revision: 10671 Log: Added basis for accession import CLI tool.
Added: trunk/lib/task/csvAccessionImportTask.class.php Added: trunk/lib/task/csvAccessionImportTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/csvAccessionImportTask.class.php Fri Jan 13 18:23:18 2012 (r10671) @@ -0,0 +1,209 @@ +<?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 accession data + * + * @package symfony + * @subpackage task + * @author Mike Cantelon <[email protected]> + * @version SVN: $Id: csvImportTask.class.php 10666 2012-01-13 01:13:48Z mcantelon $ + */ +class csvImportTask extends sfBaseTask +{ + protected static + $count = 0; + + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'The input file (csv format).') + )); + + $this->addOptions(array( + new sfCommandOption('rows-until-update', null, sfCommandOption::PARAMETER_OPTIONAL, 'Output total rows imported every n rows.'), + new sfCommandOption('skip-rows', null, sfCommandOption::PARAMETER_OPTIONAL, 'Skip n rows before importing.'), + new sfCommandOption('error-log', null, sfCommandOption::PARAMETER_OPTIONAL, 'File to log errors to.'), + new sfCommandOption('source-name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Source name to use when inserting keymap entries.') + )); + + $this->namespace = 'csv'; + $this->name = 'accession-import'; + $this->briefDescription = 'Import csv acession data'; + + $this->detailedDescription = <<<EOF +Import CSV data +EOF; + } + + protected function validateOptions($options) + { + $numericOptions = array('rows-until-update', 'skip-rows'); + + foreach($numericOptions as $option) + { + if ($options[$option] && !is_numeric($options[$option])) + { + throw new sfException($option .' must be an integer'); + } + } + + if ($options['error-log'] && !is_dir(dirname($options['error-log']))) + { + throw new sfException('Path to error log is invalid.'); + } + } + + /** + * @see sfTask + */ + public function execute($arguments = array(), $options = array()) + { + $this->validateOptions($options); + + $skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0; + + if (!$options['source-name']) + { + print "WARNING: If you're importing multiple CSV files as part of the " + ."same import it's advisable to use the source-name CLI option to " + ."specify a source name (otherwise the filename will be used as a " + . "source name.\n"; + } + + $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(); + + // set default publishing status + $defaultStatusId = sfConfig::get( + 'app_defaultPubStatus', + QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID + ); + $defaultStatusTypeId = QubitTerm::STATUS_TYPE_PUBLICATION_ID; + + // create note term if it doesn't yet exist + QubitFlatfileImport::createOrFetchTerm( + QubitTaxonomy::NOTE_TYPE_ID, + 'Language note' + ); + + // Define import + $import = new QubitFlatfileImport(array( + /* How many rows should import until we display an import status update? */ + 'rowsUntilProgressDisplay' => $options['rows-until-update'], + + /* Where to log errors to */ + 'errorLog' => $options['error-log'], + + /* the status array is a place to put data that should be accessible + from closure logic using the getStatus method */ + 'status' => array( + 'sourceName' => $sourceName + ), + 'columnNames' => fgetcsv($fh, 60000), // 1st row supplies column names/order +// 'defaultStatusId' => $defaultStatusId, +// 'defaultStatusTypeId' => $defaultStatusTypeId, + 'ignoreColumns' => array( + ), + + /* import columns that map directory to QubitInformationObject properties */ + 'standardColumns' => array( + ), + + /* import columns that should be redirected to QubitInformationObject + properties (and optionally transformed) + + Example: + 'columnMap' => array( + 'Archival History' => 'archivalHistory', + 'Revision history' => array( + 'column' => 'revision', + 'transformationLogic' => function(&$self, $text) + { + return $self->appendWithLineBreakIfNeeded( + $self->object->revision, + $text + ); + } + ) + ), + */ + 'columnMap' => array( + ), + + /* these values get stored to the rowStatusVars array */ + 'variableColumns' => array( + 'ACCESSION NUMBER' + ), + + /* import logic to load accession */ + 'rowInitLogic' => function(&$self) + { + // TO-DO: work out less silly way of getting accession # + $accessionColumnIndex = array_search( + 'ACCESSION NUMBER', + $self->columnNames + ); + + $accessionNumber = $self->status['row'][$accessionColumnIndex]; + + // look up Qubit ID of accession using keymap + $statement = $self->sqlQuery( + "SELECT id FROM accession WHERE identifier=?", + $params = array($accessionNumber) + ); + + $result = $statement->fetch(PDO::FETCH_OBJ); + if ($result) + { + print 'Found '. $result->id ."\n"; + $self->object = QubitAccession::getById($result->id); + } else { + $self->object = FALSE; + $error = "Couldn't find accession # ". $accessionNumber; + $self->printAndLogError($error); + } + }, + + /* import logic to execute before saving accession */ + 'preSaveLogic' => function(&$self) + { + }, + + /* import logic to save accession */ + 'saveLogic' => function(&$self) + { + } + )); + + $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.
