You are here

public function MigrateExecutable::import in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/MigrateExecutable.php \Drupal\migrate\MigrateExecutable::import()
  2. 9 core/modules/migrate/src/MigrateExecutable.php \Drupal\migrate\MigrateExecutable::import()

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

Return value

int Returns a value indicating the status of the import operation. The possible values are the 'RESULT_' constants defined in MigrationInterface.

Overrides MigrateExecutableInterface::import

See also

\Drupal\migrate\Plugin\MigrationInterface

File

core/modules/migrate/src/MigrateExecutable.php, line 148

Class

MigrateExecutable
Defines a migrate executable class.

Namespace

Drupal\migrate

Code

public function import() {

  // Only begin the import operation if the migration is currently idle.
  if ($this->migration
    ->getStatus() !== MigrationInterface::STATUS_IDLE) {
    $this->message
      ->display($this
      ->t('Migration @id is busy with another operation: @status', [
      '@id' => $this->migration
        ->id(),
      '@status' => $this
        ->t($this->migration
        ->getStatusLabel()),
    ]), 'error');
    return MigrationInterface::RESULT_FAILED;
  }
  $this
    ->getEventDispatcher()
    ->dispatch(new MigrateImportEvent($this->migration, $this->message), MigrateEvents::PRE_IMPORT);

  // Knock off migration if the requirements haven't been met.
  try {
    $this->migration
      ->checkRequirements();
  } catch (RequirementsException $e) {
    $this->message
      ->display($this
      ->t('Migration @id did not meet the requirements. @message @requirements', [
      '@id' => $this->migration
        ->id(),
      '@message' => $e
        ->getMessage(),
      '@requirements' => $e
        ->getRequirementsString(),
    ]), 'error');
    return MigrationInterface::RESULT_FAILED;
  }
  $this->migration
    ->setStatus(MigrationInterface::STATUS_IMPORTING);
  $source = $this
    ->getSource();
  try {
    $source
      ->rewind();
  } catch (\Exception $e) {
    $this->message
      ->display($this
      ->t('Migration failed with source plugin exception: @e in @file line @line', [
      '@e' => $e
        ->getMessage(),
      '@file' => $e
        ->getFile(),
      '@line' => $e
        ->getLine(),
    ]), 'error');
    $this->migration
      ->setStatus(MigrationInterface::STATUS_IDLE);
    return MigrationInterface::RESULT_FAILED;
  }

  // Get the process pipeline.
  $pipeline = FALSE;
  if ($source
    ->valid()) {
    try {
      $pipeline = $this->migration
        ->getProcessPlugins();
    } catch (MigrateException $e) {
      $row = $source
        ->current();
      $this->sourceIdValues = $row
        ->getSourceIdValues();
      $this
        ->getIdMap()
        ->saveIdMapping($row, [], $e
        ->getStatus());
      $this
        ->saveMessage($e
        ->getMessage(), $e
        ->getLevel());
    }
  }
  $return = MigrationInterface::RESULT_COMPLETED;
  if ($pipeline) {
    $id_map = $this
      ->getIdMap();
    $destination = $this->migration
      ->getDestinationPlugin();
    while ($source
      ->valid()) {
      $row = $source
        ->current();
      $this->sourceIdValues = $row
        ->getSourceIdValues();
      try {
        foreach ($pipeline as $destination_property_name => $plugins) {
          $this
            ->processPipeline($row, $destination_property_name, $plugins, NULL);
        }
        $save = TRUE;
      } catch (MigrateException $e) {
        $this
          ->getIdMap()
          ->saveIdMapping($row, [], $e
          ->getStatus());
        $msg = sprintf("%s:%s:%s", $this->migration
          ->getPluginId(), $destination_property_name, $e
          ->getMessage());
        $this
          ->saveMessage($msg, $e
          ->getLevel());
        $save = FALSE;
      } catch (MigrateSkipRowException $e) {
        if ($e
          ->getSaveToMap()) {
          $id_map
            ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_IGNORED);
        }
        if ($message = trim($e
          ->getMessage())) {
          $msg = sprintf("%s:%s: %s", $this->migration
            ->getPluginId(), $destination_property_name, $message);
          $this
            ->saveMessage($msg, MigrationInterface::MESSAGE_INFORMATIONAL);
        }
        $save = FALSE;
      }
      if ($save) {
        try {
          $this
            ->getEventDispatcher()
            ->dispatch(new MigratePreRowSaveEvent($this->migration, $this->message, $row), MigrateEvents::PRE_ROW_SAVE);
          $destination_ids = $id_map
            ->lookupDestinationIds($this->sourceIdValues);
          $destination_id_values = $destination_ids ? reset($destination_ids) : [];
          $destination_id_values = $destination
            ->import($row, $destination_id_values);
          $this
            ->getEventDispatcher()
            ->dispatch(new MigratePostRowSaveEvent($this->migration, $this->message, $row, $destination_id_values), MigrateEvents::POST_ROW_SAVE);
          if ($destination_id_values) {

            // We do not save an idMap entry for config.
            if ($destination_id_values !== TRUE) {
              $id_map
                ->saveIdMapping($row, $destination_id_values, $this->sourceRowStatus, $destination
                ->rollbackAction());
            }
          }
          else {
            $id_map
              ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
            if (!$id_map
              ->messageCount()) {
              $message = $this
                ->t('New object was not saved, no error provided');
              $this
                ->saveMessage($message);
              $this->message
                ->display($message);
            }
          }
        } catch (MigrateException $e) {
          $this
            ->getIdMap()
            ->saveIdMapping($row, [], $e
            ->getStatus());
          $this
            ->saveMessage($e
            ->getMessage(), $e
            ->getLevel());
        } catch (\Exception $e) {
          $this
            ->getIdMap()
            ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
          $this
            ->handleException($e);
        }
      }
      $this->sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;

      // Check for memory exhaustion.
      if (($return = $this
        ->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
        break;
      }

      // If anyone has requested we stop, return the requested result.
      if ($this->migration
        ->getStatus() == MigrationInterface::STATUS_STOPPING) {
        $return = $this->migration
          ->getInterruptionResult();
        $this->migration
          ->clearInterruptionResult();
        break;
      }
      try {
        $source
          ->next();
      } catch (\Exception $e) {
        $this->message
          ->display($this
          ->t('Migration failed with source plugin exception: @e in @file line @line', [
          '@e' => $e
            ->getMessage(),
          '@file' => $e
            ->getFile(),
          '@line' => $e
            ->getLine(),
        ]), 'error');
        $this->migration
          ->setStatus(MigrationInterface::STATUS_IDLE);
        return MigrationInterface::RESULT_FAILED;
      }
    }
  }
  $this
    ->getEventDispatcher()
    ->dispatch(new MigrateImportEvent($this->migration, $this->message), MigrateEvents::POST_IMPORT);
  $this->migration
    ->setStatus(MigrationInterface::STATUS_IDLE);
  return $return;
}