You are here

function drush_migrate_tools_migration_list in Migrate Tools 8.3

Same name and namespace in other branches
  1. 8 migrate_tools.drush.inc \drush_migrate_tools_migration_list()
  2. 8.2 migrate_tools.drush.inc \drush_migrate_tools_migration_list()
  3. 8.4 migrate_tools.drush.inc \drush_migrate_tools_migration_list()

Retrieve a list of active migrations.

Parameters

string $migration_ids: Comma-separated list of migrations - if present, return only these migrations.

Return value

MigrationInterface[][] An array keyed by migration group, each value containing an array of migrations or an empty array if no migrations match the input criteria.

3 calls to drush_migrate_tools_migration_list()
drush_migrate_tools_migrate_import in ./migrate_tools.drush.inc
drush_migrate_tools_migrate_rollback in ./migrate_tools.drush.inc
drush_migrate_tools_migrate_status in ./migrate_tools.drush.inc

File

./migrate_tools.drush.inc, line 447
Command-line tools to aid performing and developing migrations.

Code

function drush_migrate_tools_migration_list($migration_ids = '') {

  // Filter keys must match the migration configuration property name.
  $filter['migration_group'] = drush_get_option('group') ? explode(',', drush_get_option('group')) : [];
  $filter['migration_tags'] = drush_get_option('tag') ? explode(',', drush_get_option('tag')) : [];
  $manager = \Drupal::service('plugin.manager.config_entity_migration');
  $plugins = $manager
    ->createInstances([]);
  $matched_migrations = [];

  // Get the set of migrations that may be filtered.
  if (empty($migration_ids)) {
    $matched_migrations = $plugins;
  }
  else {

    // Get the requested migrations.
    $migration_ids = explode(',', Unicode::strtolower($migration_ids));
    foreach ($plugins as $id => $migration) {
      if (in_array(Unicode::strtolower($id), $migration_ids)) {
        $matched_migrations[$id] = $migration;
      }
    }
  }

  // Filters the matched migrations if a group or a tag has been input.
  if (!empty($filter['migration_group']) || !empty($filter['migration_tags'])) {

    // Get migrations in any of the specified groups and with any of the
    // specified tags.
    foreach ($filter as $property => $values) {
      if (!empty($values)) {
        $filtered_migrations = [];
        foreach ($values as $search_value) {
          foreach ($matched_migrations as $id => $migration) {

            // Cast to array because migration_tags can be an array.
            $configured_values = (array) $migration
              ->get($property);
            $configured_id = in_array($search_value, $configured_values) ? $search_value : 'default';
            if (empty($search_value) || $search_value == $configured_id) {
              if (empty($migration_ids) || in_array(Unicode::strtolower($id), $migration_ids)) {
                $filtered_migrations[$id] = $migration;
              }
            }
          }
        }
        $matched_migrations = $filtered_migrations;
      }
    }
  }

  // Sort the matched migrations by group.
  if (!empty($matched_migrations)) {
    foreach ($matched_migrations as $id => $migration) {
      $configured_group_id = empty($migration
        ->get('migration_group')) ? 'default' : $migration
        ->get('migration_group');
      $migrations[$configured_group_id][$id] = $migration;
    }
  }
  return isset($migrations) ? $migrations : [];
}