Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.21% covered (warning)
84.21%
64 / 76
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixDoubleRedirects
84.21% covered (warning)
84.21%
64 / 76
66.67% covered (warning)
66.67%
2 / 3
21.57
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 execute
82.61% covered (warning)
82.61%
57 / 69
0.00% covered (danger)
0.00%
0 / 1
18.52
 queueJobs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Fix double redirects.
4 *
5 * Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
6 * https://www.mediawiki.org/
7 *
8 * @license GPL-2.0-or-later
9 * @file
10 * @author Ilmari Karonen <nospam@vyznev.net>
11 * @ingroup Maintenance
12 */
13
14use MediaWiki\JobQueue\Jobs\DoubleRedirectJob;
15use MediaWiki\Maintenance\Maintenance;
16use MediaWiki\Title\Title;
17
18// @codeCoverageIgnoreStart
19require_once __DIR__ . '/Maintenance.php';
20// @codeCoverageIgnoreEnd
21
22/**
23 * Maintenance script that fixes double redirects.
24 *
25 * @ingroup Maintenance
26 */
27class FixDoubleRedirects extends Maintenance {
28    public function __construct() {
29        parent::__construct();
30        $this->addDescription( 'Script to fix double redirects' );
31        $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
32        $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
33        $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
34    }
35
36    public function execute() {
37        $async = $this->hasOption( 'async' );
38        $dryrun = $this->hasOption( 'dry-run' );
39
40        if ( $this->hasOption( 'title' ) ) {
41            $title = Title::newFromText( $this->getOption( 'title' ) );
42            if ( !$title || !$title->isRedirect() ) {
43                $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
44            }
45        } else {
46            $title = null;
47        }
48
49        $dbr = $this->getReplicaDB();
50
51        // See also SpecialDoubleRedirects
52        // TODO: support batch querying
53        $queryBuilder = $dbr->newSelectQueryBuilder()
54            ->select( [
55                'pa_namespace' => 'pa.page_namespace',
56                'pa_title' => 'pa.page_title',
57                'pb_namespace' => 'pb.page_namespace',
58                'pb_title' => 'pb.page_title',
59            ] )
60            ->from( 'redirect' )
61            ->join( 'page', 'pa', 'rd_from = pa.page_id' )
62            ->join( 'page', 'pb', [ 'rd_namespace = pb.page_namespace', 'rd_title = pb.page_title' ] )
63            // T42352
64            ->where( [ 'rd_interwiki' => '', 'pb.page_is_redirect' => 1 ] );
65
66        if ( $title != null ) {
67            $queryBuilder->andWhere( [
68                'pb.page_namespace' => $title->getNamespace(),
69                'pb.page_title' => $title->getDBkey()
70            ] );
71        }
72        $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
73
74        if ( !$res->numRows() ) {
75            $this->output( "No double redirects found.\n" );
76
77            return;
78        }
79
80        $jobs = [];
81        $processedTitles = "\n";
82        $n = 0;
83        $services = $this->getServiceContainer();
84        foreach ( $res as $row ) {
85            $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
86            $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
87            if ( !$titleA->canExist() || !$titleB->canExist() ) {
88                $this->error( "Cannot fix redirect from" .
89                    ( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
90                    . " to new" .
91                    ( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
92                    . "\n"
93                );
94                continue;
95            }
96
97            $processedTitles .= "* [[$titleA]]\n";
98
99            $job = new DoubleRedirectJob(
100                $titleA,
101                [
102                    'reason' => 'maintenance',
103                    'redirTitle' => $titleB->getPrefixedDBkey()
104                ],
105                $services->getRevisionLookup(),
106                $services->getMagicWordFactory(),
107                $services->getWikiPageFactory()
108            );
109
110            if ( !$async ) {
111                $success = ( $dryrun ? true : $job->run() );
112                if ( !$success ) {
113                    $this->error( "Error fixing " . $titleA->getPrefixedText()
114                        . ": " . $job->getLastError() . "\n" );
115                }
116            } else {
117                $jobs[] = $job;
118                if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
119                    $this->queueJobs( $jobs, $dryrun );
120                    $jobs = [];
121                }
122            }
123
124            if ( ++$n % 100 == 0 ) {
125                $this->output( "$n...\n" );
126            }
127        }
128
129        if ( count( $jobs ) ) {
130            $this->queueJobs( $jobs, $dryrun );
131        }
132        $this->output( "$n double redirects processed" . $processedTitles . "\n" );
133    }
134
135    protected function queueJobs( array $jobs, bool $dryrun = false ) {
136        $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
137        $this->getServiceContainer()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
138    }
139}
140
141// @codeCoverageIgnoreStart
142$maintClass = FixDoubleRedirects::class;
143require_once RUN_MAINTENANCE_IF_MAIN;
144// @codeCoverageIgnoreEnd