You are here

function migrate_ui_batch in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 migrate_ui/migrate_ui.pages.inc \migrate_ui_batch()

Process all enabled migration processes in a browser, using the Batch API to break it into manageable chunks.

Parameters

$operation: Operation to perform - 'import', 'rollback', 'stop', or 'reset'.

$machine_name: Machine name of migration to process.

$limit: An array indicating the number of items to import or rollback, or the number of seconds to process. Should include 'unit' (either 'items' or 'seconds') and 'value'.

$context: Batch API context structure

1 string reference to 'migrate_ui_batch'
migrate_ui_dashboard_submit in migrate_ui/migrate_ui.pages.inc
Submit callback for the dashboard form.

File

migrate_ui/migrate_ui.pages.inc, line 309

Code

function migrate_ui_batch($operation, $machine_name, $limit, $force = FALSE, &$context) {

  // If we got a stop message, skip everything else
  if (isset($context['results']['stopped'])) {
    $context['finished'] = 1;
    return;
  }
  $migration = Migration::getInstance($machine_name);

  // Messages generated by migration processes will be captured in this global
  global $_migrate_messages;
  $_migrate_messages = array();
  Migration::setDisplayFunction('migrate_ui_capture_message');

  // Perform the requested operation
  switch ($operation) {
    case 'import':
      $result = $migration
        ->processImport(array(
        'limit' => $limit,
        'force' => $force,
      ));
      break;
    case 'rollback':
      $result = $migration
        ->processRollback(array(
        'limit' => $limit,
        'force' => $force,
      ));
      break;
  }
  switch ($result) {
    case Migration::RESULT_INCOMPLETE:

      // Default to half-done, in case we can't get a more precise fix
      $context['finished'] = 0.5;
      if (method_exists($migration, 'sourceCount')) {
        $total = $migration
          ->sourceCount();
        if ($total > 0 && method_exists($migration, 'importedCount')) {
          $processed = $migration
            ->processedCount();
          switch ($operation) {
            case 'import':
              $to_update = $migration
                ->updateCount();
              $context['finished'] = ($processed - $to_update) / $total;
              break;
            case 'rollback':
              $context['finished'] = ($total - $migration
                ->importedCount()) / $total;
              break;
          }
        }
      }
      break;
    case MigrationBase::RESULT_SKIPPED:
      $_migrate_messages[] = t("Skipped !name due to unfulfilled dependencies: !depends", array(
        '!name' => $machine_name,
        '!depends' => implode(", ", $migration
          ->incompleteDependencies()),
      ));
      $context['finished'] = 1;
      break;
    case MigrationBase::RESULT_STOPPED:
      $context['finished'] = 1;

      // Skip any further operations
      $context['results']['stopped'] = TRUE;
      break;
    default:
      $context['finished'] = 1;
      break;
  }

  // Add any messages generated in this batch to the cumulative list
  foreach ($_migrate_messages as $message) {
    $context['results'][] = $message;
  }

  // While in progress, show the cumulative list of messages
  $full_message = '';
  foreach ($context['results'] as $message) {
    $full_message .= $message . '<br />';
  }
  $context['message'] = $full_message;
}