1 system.admin.inc _system_utf8mb4_convert_batch($tables, &$context)

Executes a batch operation for system_utf8mb4_convert_form_submit().

Parameters

array $tables: Associative array of table names to update.

array $context: Batch array of contextual key/values.

File

core/modules/system/system.admin.inc, line 2585
Admin page callbacks for the system module.

Code

function _system_utf8mb4_convert_batch($tables, &$context) {
  global $databases;

  // Instantiate a character set converter object.
  $converter = new DatabaseCharsetConverter();

  // Initial run, prepare sandbox and results arrays.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($tables) + 1;
    $context['sandbox']['tables_to_process'] = $tables;
    $context['results']['converted'] = array();
    $context['results']['errors'] = array();

    // If converting the database itself fails, bail out entirely.
    try {
      $result = $converter->convertDatabase($databases['default']['default']['database']);
      if (!$result) {
        $context['results']['errors'][] = t('The database %name could not be converted.', array('%name' => $databases['default']['default']['database']));
        $context['finished'] = 1;
        return;
      }
    }
    catch (PDOException $e) {
      $context['results']['errors'] = $e->getMessage();
      $context['finished'] = 1;
      return;
    }
  }
  // Subsequent runs, convert each table.
  else {
    // When converting tables, record but continue after failures.
    $table_name = array_shift($context['sandbox']['tables_to_process']);
    try {
      $result = $converter->convertTable($table_name);
      if (!$result) {
        $context['results']['errors'][] = t('The table %name could not be converted.', array('%name' => $table_name));
      }
    }
    catch (PDOException $e) {
      $context['results']['errors'][] = $e->getMessage();
    }

    // Store result for post-processing in the finished callback.
    $context['results']['converted'][] = $table_name;
  }

  // Update our progress information.
  $context['sandbox']['progress']++;
  if (!empty($context['sandbox']['tables_to_process'])) {
    $context['message'] = t('Converting database table "@table_name"...', array('@table_name' => reset($context['sandbox']['tables_to_process'])));
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['message'] = t('Completing...');
    $context['finished'] = 1;
  }
}