You are here

public function MigrateToolsCommands::messages in Migrate Tools 8.5

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

View any messages associated with a migration.

@command migrate:messages

@option csv Export messages as a CSV (deprecated) @option idlist Comma-separated list of IDs to import @option idlist-delimiter The delimiter for records

@default $options []

@usage migrate:messages MyNode Show all messages for the MyNode migration

@validate-module-enabled migrate_tools

@aliases mmsg,migrate-messages

@field-labels source_ids_hash: Source IDs Hash source_ids: Source ID(s) destination_ids: Destination ID(s) level: Level message: Message @default-fields source_ids_hash,source_ids,destination_ids,level,message

Parameters

string $migration_id: ID of the migration.

array $options: Additional options for the command.

Return value

\Consolidation\OutputFormatters\StructuredData\RowsOfFields Source fields of the given migration formatted as a table.

File

src/Commands/MigrateToolsCommands.php, line 653

Class

MigrateToolsCommands
Migrate Tools drush commands.

Namespace

Drupal\migrate_tools\Commands

Code

public function messages($migration_id, array $options = [
  'csv' => FALSE,
  'idlist' => self::REQ,
  'idlist-delimiter' => MigrateTools::DEFAULT_ID_LIST_DELIMITER,
]) {

  /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
  $migration = $this->migrationPluginManager
    ->createInstance($migration_id);
  if (!$migration) {
    $error = dt('Migration @id does not exist', [
      '@id' => $migration_id,
    ]);
    $this
      ->logger()
      ->error($error);
    throw new \Exception($error);
  }
  $id_list = MigrateTools::buildIdList($options);

  /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface|\Drupal\migrate_tools\IdMapFilter $map */
  $map = new IdMapFilter($migration
    ->getIdMap(), $id_list);
  $source_id_keys = $this
    ->getSourceIdKeys($map);
  $table = [];

  // TODO: Remove after 8.7 support goes away.
  $iterator_method = method_exists($map, 'getMessages') ? 'getMessages' : 'getMessageIterator';
  foreach ($map
    ->{$iterator_method}() as $row) {
    unset($row->msgid);
    $array_row = (array) $row;

    // If the message includes useful IDs don't print the hash.
    if (count($source_id_keys) === count(array_intersect_key($source_id_keys, $array_row))) {
      unset($array_row['source_ids_hash']);
    }
    $source_ids = $destination_ids = [];
    foreach ($array_row as $name => $item) {
      if (substr($name, 0, 4) === 'src_') {
        $source_ids[$name] = $item;
      }
      if (substr($name, 0, 5) === 'dest_') {
        $destination_ids[$name] = $item;
      }
    }
    $array_row['source_ids'] = implode(', ', $source_ids);
    $array_row['destination_ids'] = implode(', ', $destination_ids);
    $table[] = $array_row;
  }
  if (empty($table)) {
    $this
      ->logger()
      ->notice(dt('No messages for this migration'));
    return NULL;
  }
  if ($options['csv']) {
    fputcsv(STDOUT, array_keys($table[0]));
    foreach ($table as $row) {
      fputcsv(STDOUT, $row);
    }
    $this
      ->logger()
      ->notice('--csv option is deprecated in 4.5 and is removed from 5.0. Use \'--format=csv\' instead.');
    @trigger_error('--csv option is deprecated in migrate_tool:8.x-4.5 and is removed from migrate_tool:8.x-5.0. Use \'--format=csv\' instead. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
    return NULL;
  }
  return new RowsOfFields($table);
}