Author: mcantelon Date: Thu Mar 29 17:00:10 2012 New Revision: 11314 Log: Added event import tool.
Added: trunk/lib/task/import/csvEventImportTask.class.php Added: trunk/lib/task/import/csvEventImportTask.class.php ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/lib/task/import/csvEventImportTask.class.php Thu Mar 29 17:00:10 2012 (r11314) @@ -0,0 +1,224 @@ +<?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 event 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 csvEventRecordImportTask extends csvImportBaseTask +{ + protected $namespace = 'csv'; + protected $name = 'event-import'; + protected $briefDescription = 'Import csv event record data'; + protected $detailedDescription = <<<EOF +Import CSV event record data +EOF; + + /** + * @see csvImportBaseTask + */ + protected function configure() + { + parent::configure(); + + $this->addOptions(array( + new sfCommandOption('event-types', null, sfCommandOption::PARAMETER_OPTIONAL, 'Event type terms to create, if they do not yet exist, before import.') + )); + } + + /** + * @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::EVENT_TYPE_ID => 'eventTypes' + )); + + // add new event types + foreach(array('Authored', 'Edited', 'Compiled') as $termName) + { + QubitFlatfileImport::createOrFetchTerm( + QubitTaxonomy::EVENT_TYPE_ID, + $termName + ); + } + + $subjectTable = 'actor_i18n'; + $subjectKeyColumn = 'authorized_form_of_name'; + $subjectValueColumn = 'Actor'; + + $objectTable = 'information_object_i18n'; + $objectKeyColumn = 'sources'; + $objectValueColumn = 'CS_RecordID'; + + $relationTypeColumn = 'ActorRole'; + + $import = new QubitFlatfileImport(array( + + 'status' => array( + 'eventTypes' => $termData['eventTypes'], + 'subjectTable' => $subjectTable, + 'subjectKeyColumn' => $subjectKeyColumn, + 'subjectValueColumn' => $subjectValueColumn, + 'objectTable' => $objectTable, + 'objectKeyColumn' => $objectKeyColumn, + 'objectValueColumn' => $objectValueColumn, + 'relationTypeColumn' => $relationTypeColumn, + 'dataCached' => FALSE, + 'subjectKeys' => array(), + 'objectKeys' => array(), + 'goodSubjects' => 0, + 'badSubjects' => 0, + 'goodObjects' => 0, + 'badObjects' => 0 + ), + + 'errorLog' => $options['error-log'], + + 'saveLogic' => function(&$self) + { + if (!$self->status['dataCached']) + { + // cache key -> id associations + $self->status['subjectKeys'] = getNameIdArrayFromTable( + $self, + $self->status['subjectTable'], + $self->status['subjectKeyColumn'], + 'id' + ); + + $self->status['objectKeys'] = getNameIdArrayFromTable( + $self, + $self->status['objectTable'], + $self->status['objectKeyColumn'], + 'id' + ); + + $self->status['dataCached'] = TRUE; + } + + $subjectKey = trim($self->columnValue($self->status['subjectValueColumn'])); + $subjectId = FALSE; + if ($subjectKey) + { + if (isset($self->status['subjectKeys'][$subjectKey])) + { + $subjectId = $self->status['subjectKeys'][$subjectKey]; + } + } + + if ($subjectId) + { + $self->status['goodSubjects']++; + + $objectKey = trim($self->columnValue($self->status['objectValueColumn'])); + $objectId = FALSE; + if ($objectKey) + { + if (isset($self->status['objectKeys'][$objectKey])) + { + $objectId = $self->status['objectKeys'][$objectKey]; + } + } + + if ($objectId) { + $self->status['goodObjects']++; + + $type = $self->columnValue($self->status['relationTypeColumn']); + print 'Relate '. $subjectId .' to '. $objectId .' as '. $type .".\n"; + + $typeId = array_search($type, $self->status['eventTypes']); + + if (!$typeId) + { + print "Term does not exist.\n"; + exit(); + } + + $event = new QubitEvent; + $event->informationObjectId = $objectId; + $event->typeId = $typeId; + $event->actorId = $subjectId; + $event->save(); + } else { + $self->status['badObjects']++; + print 'ERROR: object '. $objectKey ." not found.\n"; + } + } + else { + $self->status['badSubjects']++; + print 'ERROR: subject '. $subjectKey ." not found.\n"; + } + }, + + 'completeLogic' => function(&$self) + { + print "Import complete.\n"; + print "Good subjects: ". $self->status['goodSubjects'] ."\n"; + print "Bad subjects: ". $self->status['badSubjects'] ."\n"; + print "Good objects: ". $self->status['goodObjects'] ."\n"; + print "Bad objects: ". $self->status['badObjects'] ."\n"; + } + )); + $import->csv($fh, $skipRows); + } +} + +function getNameIdArrayFromTable(&$self, $tableName, $keyColumn, $idColumn) +{ + $names = array(); + + $query = "SELECT ". $keyColumn .", ". $idColumn ." FROM ". $tableName; + + $statement = $self->sqlQuery($query); + + if (!$statement) + { + print 'DB error'; exit(); + } + + $actors = array(); + while($subject = $statement->fetch(PDO::FETCH_OBJ)) + { + $names[$subject->$keyColumn] = $subject->id; + } + return $names; +} -- 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.
