You are here

function redirect_update_8100 in Redirect 8

Rehash redirects to account for case insensitivity.

File

./redirect.install, line 20
Update hooks for the Redirect module.

Code

function redirect_update_8100(&$sandbox) {

  // Loop through 100 redirects at a time.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_rid'] = 0;

    // Note, because MySQL can treat `foo = LOWER(foo)`, all records must be checked.
    $sandbox['max'] = Database::getConnection()
      ->query('SELECT COUNT(1) FROM {redirect}')
      ->fetchField();
  }
  $result = \Drupal::database()
    ->select('redirect', 'r')
    ->fields('r', [
    'rid',
    'redirect_source__path',
    'redirect_source__query',
    'language',
    'hash',
  ])
    ->condition('rid', $sandbox['current_rid'], '>')
    ->range(0, 100)
    ->orderBy('rid', 'ASC')
    ->execute();
  foreach ($result as $row) {
    $query = !empty($row->redirect_source__query) ? unserialize($row->redirect_source__query) : [];
    $new_hash = Redirect::generateHash($row->redirect_source__path, (array) $query, $row->language);
    if ($row->hash != $new_hash) {

      // Do a direct query to speed things up.
      $query = \Drupal::database();
      $query
        ->update('redirect')
        ->fields([
        'hash' => $new_hash,
      ])
        ->condition('rid', $row->rid)
        ->execute();
    }
    $sandbox['progress']++;
    $sandbox['current_rid'] = $row->rid;
  }

  // Reset caches.
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}