Drush command to remove circular redirects

Created on 7 February 2024, 10 months ago
Updated 13 September 2024, 2 months ago

Problem/Motivation

It'd be useful to have a Drush command to purge circular redirects.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States DamienMcKenna NH, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @DamienMcKenna
  • 🇺🇸United States DamienMcKenna NH, USA

    This logic works for me, it could be turned into a command.

      /**
       * Purge circular redirects.
       *
       * @command mymodule:purge-circular-redirects
       */
      public function fixCircularRedirects() {
        $db = \Drupal::database();
        $records = $db->query("SELECT redirect.rid
          FROM {redirect}
          INNER JOIN {path_alias}
          	ON CONCAT('/', redirect.redirect_source__path) = path_alias.alias")
          ->fetchCol();
        if (!empty($records)) {
          $this->logger->notice($this->t('@count redirect records to purge: @list', [
            '@count' => count($records),
            '@list' => implode(', ', $records),
          ]));
          $db->delete('redirect')
            ->condition('rid', $records, 'IN')
            ->execute();
          $this->logger->notice($this->t('@count redirect records purged.', [
            '@count' => count($records),
          ]));
        }
        else {
          $this->logger->notice($this->t('No circiular redirects found.'));
        }
      }
    
  • First commit to issue fork.
  • Pipeline finished with Success
    4 months ago
    Total: 180s
    #227809
  • Pipeline finished with Success
    4 months ago
    Total: 199s
    #227845
  • Status changed to Needs review 4 months ago
  • Hello everyone, I have created this Drupal Drush command which facilitates the management of circular redirects using Drush as mentioned in the issue. Firstly, It retrieves all redirect entities from the database using EntityTypeManagerInterface, constructs a map linking source URLs to their corresponding destinations, identifies circular redirect relationships where a redirect points back to its own source. I have also logged each detected circular redirect using Psr\Log\LoggerInterface and provides a user prompt to confirm the delete operation. Upon confirmation, it deletes all identified circular redirect records.

    Drush command for the purging circular redirects: drush redirect:purge-circular-redirects

    Please review this.

  • Assigned to sourav_paul
  • Issue was unassigned.
  • Status changed to Needs work 3 months ago
  • 🇮🇳India sourav_paul Kolkata

    MR was applied successfully but got a fatal error on running the command drush redirect:purge-circular-redirects
    Using redirect 8.x-1.10 & D10.3
    Sharing ss:

    Please fix the logger issue...

  • Status changed to Needs review 2 months ago
  • @sourav_paul Hello I have resolved the fatal error and tested the same in d10 and d11, the circular redirects are successfully purging. Please review this once.
    Moving this to Needs Review

  • Pipeline finished with Success
    2 months ago
    Total: 519s
    #281911
  • 🇮🇳India sourav_paul Kolkata

    Thanks @uttam for your contribution. I've retested the MR, which was applied cleanly.
    Drush command working as expected.

    Attaching ss:

    Hence Moving it to RTBC.

Production build 0.71.5 2024