Author: mcantelon Date: Wed Feb 1 16:47:17 2012 New Revision: 10789 Log: CLI tool for importing authority records.
Added: trunk/lib/task/import/csvAuthorityRecordImportTask.class.php Added: trunk/lib/task/import/csvAuthorityRecordImportTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/import/csvAuthorityRecordImportTask.class.php Wed Feb 1 16:47:17 2012 (r10789) @@ -0,0 +1,148 @@ +<?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 authoriy record 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 csvAuthorityRecordImportTask extends csvImportBaseTask +{ + protected $namespace = 'csv'; + protected $name = 'authority-import'; + protected $briefDescription = 'Import csv authority record data'; + protected $detailedDescription = <<<EOF +Import CSV data +EOF; + + /** + * @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(); + + // Load taxonomies into variables to avoid use of magic numbers + $termData = QubitFlatfileImport::loadTermsFromTaxonomies(array( + QubitTaxonomy::NOTE_TYPE_ID => 'noteTypes', + QubitTaxonomy::ACTOR_ENTITY_TYPE_ID => 'actorTypes' + )); + + // Define import + $import = new QubitFlatfileImport(array( + /* What type of object are we importing? */ + 'className' => 'QubitActor', + + /* 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, + 'actorTypes' => $termData['actorTypes'] + ), + 'columnNames' => fgetcsv($fh, 60000), // 1st row supplies column names/order + '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( + 'AuthorizedName' => 'authorizedFormOfName', + 'History' => 'history', + 'DatesOfExistence' => 'datesOfExistence' + ), + + /* import columns that can be added as QubitNote objects */ + 'noteMap' => array( + 'notes' => array( + 'typeId' => array_search('Maintenance note', $termData['noteTypes']) + ) + ), + + /* these values get stored to the rowStatusVars array */ + 'variableColumns' => array( + 'EntityType' + ), + + /* import logic to execute before saving accession */ + 'preSaveLogic' => function(&$self) + { + if ($self->object) + { + if ($self->rowStatusVars['EntityType']) + { + $entityTypes = $self->getStatus('actorTypes'); + $entityType = ucfirst(strtolower($self->rowStatusVars['EntityType'])); + $entityTypeId = array_search($entityType, $entityTypes); + if ($entityTypeId) + { + $self->object->entityTypeId = $entityTypeId; + } else { + throw new sfException($entityType .' is not a valid actor entity type.'); + } + } + } + }, + )); + + $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.
