You are here

function drush_migrate_audit in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 migrate.drush.inc \drush_migrate_audit()

Display field mappings for a migration.

File

./migrate.drush.inc, line 669
Drush support for the migrate module

Code

function drush_migrate_audit($args = NULL) {
  try {
    $problem_descriptions = array(
      'noted_issues' => dt("Noted as an issue:"),
      'sources_unmapped' => dt("Source not used in a mapping:"),
      'sources_missing' => dt("Used as source field in mapping but not in source field list:"),
      'destinations_unmapped' => dt("Destination not used in a mapping:"),
      'destinations_missing' => dt("Used as destination field in mapping but not in destination field list:"),
    );
    $migrations = drush_migrate_get_migrations($args);
    foreach ($migrations as $name => $migration) {
      drush_print("\n" . dt('@migration Audit', array(
        '@migration' => $name,
      )) . "\n");
      $source = $migration
        ->getSource();
      $destination = $migration
        ->getDestination();
      if (empty($source)) {
        drush_print(dt('Missing a source.'));
      }
      if (empty($destination)) {
        drush_print(dt('Missing a destination.'));
      }
      $field_mappings = $migration
        ->getFieldMappings();
      $source_fields = $source
        ->fields();
      $destination_fields = $destination
        ->fields();
      $unused_sources = array();
      $used_destinations = array();
      $problems = array(
        'noted_issues' => array(),
        'sources_unmapped' => array(),
        'sources_missing' => array(),
        'destinations_unmapped' => array(),
        'destinations_missing' => array(),
      );
      foreach ($field_mappings as $mapping) {
        $source_field = $mapping
          ->getSourceField();
        $destination_field = $mapping
          ->getDestinationField();
        $used_sources[$source_field] = TRUE;
        $used_destinations[$destination_field] = TRUE;
        $issue_priority = $mapping
          ->getIssuePriority();
        if (!is_null($issue_priority) && $issue_priority != MigrateFieldMapping::ISSUE_PRIORITY_OK) {
          $problems['noted_issues'][] = array(
            dt('Source') => $source_field,
            dt('Destination') => $destination_field,
            dt('Priority') => MigrateFieldMapping::$priorities[$issue_priority],
            dt('Description') => $mapping
              ->getDescription(),
          );
        }

        // Validate source and destination fields actually exist
        if (!is_null($source_field) && !isset($source_fields[$source_field])) {
          $problems['sources_missing'][] = $source_field;
        }
        if (!is_null($destination_field) && !isset($destination_fields[$destination_field])) {
          $problems['destinations_missing'][] = $destination_field;
        }
      }
      $problems['sources_unmapped'] = array_diff_key($source_fields, $used_sources);
      $problems['destinations_unmapped'] = array_diff_key($destination_fields, $used_destinations);
      $problems = array_filter($problems);
      if (empty($problems)) {
        drush_print(dt('No problems found.') . "\n", 1);
      }
      else {
        foreach ($problems as $type => $some_problems) {
          drush_print($problem_descriptions[$type]);

          // Print arrays as tables.
          if (is_array($some_problems[0])) {
            $table = array_merge(array(
              array_keys($some_problems[0]),
            ), $some_problems);
            drush_print_table($table, TRUE);
          }
          else {
            foreach ($some_problems as $problem) {
              drush_print($problem, 1);
            }
            drush_print('');
          }
        }
      }
    }
  } catch (MigrateException $e) {
    drush_print($e
      ->getMessage());
    exit;
  }
}