You are here

function drush_migrate_analyze in Migrate 6.2

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

Analyze the source fields for any passed migrations.

File

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

Code

function drush_migrate_analyze($args = NULL) {
  $migrations = drush_migrate_get_migrations($args);
  foreach ($migrations as $name => $migration) {

    // "Migrations" derived from MigrationBase won't have an analyze method.
    if (method_exists($migration, 'analyze')) {
      drush_print("\n" . dt('Analyzing @migration', array(
        '@migration' => $name,
      )) . "\n");
      $analysis = $migration
        ->analyze();
      if (!empty($analysis)) {
        foreach ($analysis as $field_name => $details) {
          if (!empty($details['description'])) {
            drush_print(dt('@name (@description):', array(
              '@name' => $field_name,
              '@description' => $details['description'],
            )));
          }
          else {
            drush_print(dt('@name:', array(
              '@name' => $field_name,
            )));
          }

          // Special handling in degenerate cases
          if (count($details['distinct_values']) == 1) {
            $value = trim(reset(array_keys($details['distinct_values'])));
            if ($value === '') {
              drush_print('  ' . dt('The field is always empty'));
            }
            else {
              drush_print('  ' . dt('Only one value present: @value', array(
                '@value' => $value,
              )));
            }
          }
          else {
            if ($details['is_numeric']) {
              drush_print('  ' . dt('Numeric field with a range of @min to @max', array(
                '@min' => $details['min_numeric'],
                '@max' => $details['max_numeric'],
              )));
            }
            else {
              drush_print('  ' . dt('String field with a length ranging from @min to @max', array(
                '@min' => $details['min_strlen'],
                '@max' => $details['max_strlen'],
              )));
            }
            $values = array();
            $header = NULL;

            // If the max of 10 tracked distinct values was reached, we assume
            // there are many values and treat them as samples. Under 10 this
            // may be an enumerated field, show all values with their counts.
            if (count($details['distinct_values']) < 10) {
              drush_print('  ' . dt('Distinct values:'));
              $header = array(
                '',
                dt('Value'),
                dt('Count'),
              );
              $values[] = $header;
            }
            else {
              drush_print('  ' . dt('Sample values:'));
            }
            ksort($details['distinct_values']);
            foreach ($details['distinct_values'] as $value => $count) {

              // Truncate long strings
              $value = substr($value, 0, 60);
              if (strlen($value) == 60) {
                $value .= dt('...');
              }
              $row = array(
                '    ',
                $value,
              );
              if (count($details['distinct_values']) < 10) {
                $row[] = $count;
              }
              $values[] = $row;
            }

            // No header for sample values.
            drush_print_table($values, !is_null($header));
          }
        }
      }
    }
  }
}