You are here

protected function Migration::import in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 includes/migration.inc \Migration::import()

Perform an import operation - migrate items from source to destination.

File

includes/migration.inc, line 540
Defines the base class for import/rollback processes.

Class

Migration
The base class for all import objects. This is where most of the smarts of the migrate module resides. Migrations are created by deriving from this class, and in the constructor (after calling parent::__construct()) initializing at a minimum the name,…

Code

protected function import() {
  $return = MigrationBase::RESULT_COMPLETED;
  try {
    $this->source
      ->rewind();
  } catch (Exception $e) {
    self::displayMessage(t('Migration failed with source plugin exception: !e', array(
      '!e' => $e
        ->getMessage(),
    )));
    return MigrationBase::RESULT_FAILED;
  }
  while ($this->source
    ->valid()) {
    $data_row = $this->source
      ->current();
    $this->currentSourceKey = $this->source
      ->getCurrentKey();

    // Wipe old messages
    $this->map
      ->delete($this->currentSourceKey, TRUE);
    $this->sourceValues = $data_row;
    $this
      ->applyMappings();
    try {
      migrate_instrument_start('destination import', TRUE);
      $ids = $this->destination
        ->import($this->destinationValues, $this->sourceValues);
      migrate_instrument_stop('destination import');
      if ($ids) {
        $this->map
          ->saveIDMapping($this->sourceValues, $ids, $this->needsUpdate, $this->rollbackAction);
        $this->successes_since_feedback++;
        $this->total_successes++;
      }
      else {
        $this->map
          ->saveIDMapping($this->sourceValues, array(), MigrateMap::STATUS_FAILED, $this->rollbackAction);
        $message = t('New object was not saved, no error provided');
        $this
          ->saveMessage($message);
        self::displayMessage($message);
      }
    } catch (MigrateException $e) {
      $this->map
        ->saveIDMapping($this->sourceValues, array(), MigrateMap::STATUS_FAILED, $this->rollbackAction);
      $this
        ->saveMessage($e
        ->getMessage(), $e
        ->getLevel());
      self::displayMessage($e
        ->getMessage());
    } catch (Exception $e) {
      $this->map
        ->saveIDMapping($this->sourceValues, array(), MigrateMap::STATUS_FAILED, $this->rollbackAction);
      $this
        ->handleException($e);
    }
    $this->total_processed++;
    $this->processed_since_feedback++;
    if ($this->highwaterField) {
      $this
        ->saveHighwater($this->sourceValues->{$this->highwaterField['name']});
    }

    // Reset row properties.
    unset($this->sourceValues, $this->destinationValues);
    $this->needsUpdate = MigrateMap::STATUS_IMPORTED;

    // TODO: Temporary. Remove when http://drupal.org/node/375494 is committed.
    // TODO: Should be done in MigrateDestinationEntity
    if (!empty($this->destination->entityType)) {
      entity_get_controller($this->destination->entityType)
        ->resetCache();
    }
    if ($this
      ->timeOptionExceeded()) {
      break;
    }
    if (($return = $this
      ->checkStatus()) != MigrationBase::RESULT_COMPLETED) {
      break;
    }
    if ($this
      ->itemOptionExceeded()) {
      break;
    }
    try {
      $this->source
        ->next();
    } catch (Exception $e) {
      self::displayMessage(t('Migration failed with source plugin exception: !e', array(
        '!e' => $e
          ->getMessage(),
      )));
      return MigrationBase::RESULT_FAILED;
    }
  }
  $this
    ->progressMessage($return);
  return $return;
}