You are here

function migrate_migrations in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 migrate.module \migrate_migrations()

Retrieve a list of all active migrations, ordered by dependencies. To be recognized, a class must be non-abstract, and derived from MigrationBase.

Return value

Array of migration objects, keyed by the machine name.

8 calls to migrate_migrations()
drush_migrate_get_migrations in ./migrate.drush.inc
drush_migrate_migrations in ./migrate.drush.inc
drush_migrate_status in ./migrate.drush.inc
A simplified version of the dashboard page.
migrate_ui_requirements in migrate_ui/migrate_ui.install
Implementation of hook_requirements(). Checks installation requirements and do status reporting. TODO: Share code with migrate_migration_info
migrate_update_6021 in ./migrate.install
Update map tables to reflect change of needs_update to a status column.

... See full list

File

./migrate.module, line 27

Code

function migrate_migrations() {
  static $migrations = array();
  if (!empty($migrations)) {
    return $migrations;
  }

  // Get list of modules implementing Migrate API - mainly, we're looking to
  // make sure any dynamic migrations defined in hook_migrate_api() get registered.
  migrate_get_module_apis(TRUE);
  $dependent_migrations = array();
  $required_migrations = array();
  $result = db_select('migrate_status', 'ms')
    ->fields('ms', array(
    'machine_name',
    'class_name',
    'arguments',
  ))
    ->execute();
  foreach ($result as $row) {
    if (class_exists($row->class_name)) {
      $reflect = new ReflectionClass($row->class_name);
      if (!$reflect
        ->isAbstract() && $reflect
        ->isSubclassOf('MigrationBase')) {
        $arguments = unserialize($row->arguments);
        if (!$arguments || !is_array($arguments)) {
          $arguments = array();
        }
        $migration = MigrationBase::getInstance($row->machine_name, $row->class_name, $arguments);
        $dependencies = $migration
          ->getDependencies();
        if (count($dependencies) > 0) {

          // Set classes with dependencies aside for reordering
          $dependent_migrations[$row->machine_name] = $migration;
          $required_migrations += $dependencies;
        }
        else {

          // No dependencies, just add
          $migrations[$row->machine_name] = $migration;
        }
      }
      else {
        MigrationBase::displayMessage(t('Class !class is no longer a valid concrete migration class', array(
          '!class' => $row->class_name,
        )));
      }
    }
    else {
      MigrationBase::displayMessage(t('Class !class no longer exists', array(
        '!class' => $row->class_name,
      )));
    }
  }

  // Scan modules with dependencies - we'll take 20 passes at it before
  // giving up
  // TODO: Can we share code with _migrate_class_list()?
  $iterations = 0;
  while (count($dependent_migrations) > 0) {
    if ($iterations++ > 20) {
      $migration_names = implode(',', array_keys($dependent_migrations));
      throw new MigrateException(t('Failure to sort migration list - most likely due ' . 'to circular dependencies involving !migration_names', array(
        '!migration_names' => $migration_names,
      )));
    }
    foreach ($dependent_migrations as $name => $migration) {
      $ready = TRUE;

      // Scan all the dependencies for this class and make sure they're all
      // in the final list
      foreach ($migration
        ->getDependencies() as $dependency) {
        if (!isset($migrations[$dependency])) {
          $ready = FALSE;
          break;
        }
      }
      if ($ready) {

        // Yes they are! Move this class to the final list
        $migrations[$name] = $migration;
        unset($dependent_migrations[$name]);
      }
    }
  }

  // The migrations are now ordered according to their own dependencies - now order
  // them by group
  $groups = MigrateGroup::groups();

  // Seed the final list by properly-ordered groups.
  $final_migrations = array();
  foreach ($groups as $name => $group) {
    $final_migrations[$name] = array();
  }

  // Fill in the grouped list
  foreach ($migrations as $machine_name => $migration) {
    $final_migrations[$migration
      ->getGroup()
      ->getName()][$machine_name] = $migration;
  }

  // Then flatten the list
  $migrations = array();
  foreach ($final_migrations as $group_name => $group_migrations) {
    foreach ($group_migrations as $machine_name => $migration) {
      $migrations[$machine_name] = $migration;
    }
  }
  return $migrations;
}