only in patch2: unchanged: --- /dev/null +++ b/redirect.post_update.php @@ -0,0 +1,15 @@ +manager->getStorage('redirect')->loadMultiple($redirect_ids); } + + /** + * Regenerate the redirect hash as part of a Drupal update. + * + * @param array $sandbox + * Stores information for batch updates. + * + * @see hook_post_update_NAME() + */ + public static function regenerateRedirectHash(array &$sandbox) { + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['updated'] = 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(); + } + $batch_size = Settings::get('entity_update_batch_size', 50); + + $result = \Drupal::database()->select('redirect', 'r') + ->fields('r', [ + 'rid', 'redirect_source__path', 'redirect_source__query', 'language', 'hash', + ]) + ->condition('rid', $sandbox['current_rid'], '>') + ->range(0, $batch_size) + ->orderBy('rid') + ->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['updated']++; + } + $sandbox['progress']++; + $sandbox['current_rid'] = $row->rid; + } + // Reset caches. + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + + if ($sandbox['#finished'] >= 1) { + if ($sandbox['updated']) { + // Invalidate 'redirect_list' cache tag when a redirect hash was + // updated. + Cache::invalidateTags(['redirect_list']); + } + + $message = \Drupal::translation()->formatPlural( + $sandbox['updated'], + '@count redirect was updated.', + '@count redirects were updated.', + ['@count' => $sandbox['updated']] + ); + \Drupal::messenger()->addStatus($message); + + return $message; + } + return NULL; + } + }