You are here

public function MigrateToolsCommands::status in Migrate Tools 8.4

Same name and namespace in other branches
  1. 8.5 src/Commands/MigrateToolsCommands.php \Drupal\migrate_tools\Commands\MigrateToolsCommands::status()

List all migrations with current status.

@command migrate:status

@option group A comma-separated list of migration groups to list @option tag Name of the migration tag to list @option names-only Only return names, not all the details (faster) @option continue-on-failure When a migration fails, continue processing remaining migrations.

@default $options []

@usage migrate:status Retrieve status for all migrations @usage migrate:status --group=beer Retrieve status for all migrations in a given group @usage migrate:status --tag=user Retrieve status for all migrations with a given tag @usage migrate:status --group=beer --tag=user Retrieve status for all migrations in the beer group and with the user tag. @usage migrate:status beer_term,beer_node Retrieve status for specific migrations

@validate-module-enabled migrate_tools

@aliases ms, migrate-status

@field-labels group: Group id: Migration ID status: Status total: Total imported: Imported unprocessed: Unprocessed last_imported: Last Imported @default-fields group,id,status,total,imported,unprocessed,last_imported

Parameters

string $migration_names: Restrict to a comma-separated list of migrations (Optional).

array $options: Additional options for the command.

Return value

\Consolidation\OutputFormatters\StructuredData\RowsOfFields Migrations status formatted as table.

File

src/Commands/MigrateToolsCommands.php, line 127

Class

MigrateToolsCommands
Migrate Tools drush commands.

Namespace

Drupal\migrate_tools\Commands

Code

public function status($migration_names = '', array $options = [
  'group' => self::REQ,
  'tag' => self::REQ,
  'names-only' => FALSE,
  'continue-on-failure' => FALSE,
]) {
  $names_only = $options['names-only'];
  $migrations = $this
    ->migrationsList($migration_names, $options);
  $table = [];
  $errors = [];

  // Take it one group at a time, listing the migrations within each group.
  foreach ($migrations as $group_id => $migration_list) {

    /** @var \Drupal\migrate_plus\Entity\MigrationGroup $group */
    $group = $this->entityTypeManager
      ->getStorage('migration_group')
      ->load($group_id);
    $group_name = !empty($group) ? "{$group->label()} ({$group->id()})" : $group_id;
    foreach ($migration_list as $migration_id => $migration) {
      if ($names_only) {
        $table[] = [
          'group' => dt('Group: @name', [
            '@name' => $group_name,
          ]),
          'id' => $migration_id,
        ];
      }
      else {
        try {
          $map = $migration
            ->getIdMap();
          $imported = $map
            ->importedCount();
          $source_plugin = $migration
            ->getSourcePlugin();
        } catch (\Exception $e) {
          $error = dt('Failure retrieving information on @migration: @message', [
            '@migration' => $migration_id,
            '@message' => $e
              ->getMessage(),
          ]);
          $this
            ->logger()
            ->error($error);
          $errors[] = $error;
          continue;
        }
        try {
          $source_rows = $source_plugin
            ->count();

          // -1 indicates uncountable sources.
          if ($source_rows == -1) {
            $source_rows = dt('N/A');
            $unprocessed = dt('N/A');
          }
          else {
            $unprocessed = $source_rows - $map
              ->processedCount();
          }
        } catch (\Exception $e) {
          $this
            ->logger()
            ->error(dt('Could not retrieve source count from @migration: @message', [
            '@migration' => $migration_id,
            '@message' => $e
              ->getMessage(),
          ]));
          $source_rows = dt('N/A');
          $unprocessed = dt('N/A');
        }
        $status = $migration
          ->getStatusLabel();
        $migrate_last_imported_store = $this->keyValue
          ->get('migrate_last_imported');
        $last_imported = $migrate_last_imported_store
          ->get($migration
          ->id(), FALSE);
        if ($last_imported) {
          $last_imported = $this->dateFormatter
            ->format($last_imported / 1000, 'custom', 'Y-m-d H:i:s');
        }
        else {
          $last_imported = '';
        }
        $table[] = [
          'group' => $group_name,
          'id' => $migration_id,
          'status' => $status,
          'total' => $source_rows,
          'imported' => $imported,
          'unprocessed' => $unprocessed,
          'last_imported' => $last_imported,
        ];
      }
    }

    // Add empty row to separate groups, for readability.
    end($migrations);
    if ($group_id !== key($migrations)) {
      $table[] = [];
    }
  }

  // If any errors occurred, throw an exception.
  if (!empty($errors)) {
    throw new \Exception(implode(PHP_EOL, $errors));
  }
  return new RowsOfFields($table);
}