Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixDoubleRedirects
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 3
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 1
306
 queueJobs
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Fix double redirects.
4 *
5 * Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @author Ilmari Karonen <nospam@vyznev.net>
25 * @ingroup Maintenance
26 */
27
28use MediaWiki\Title\Title;
29
30require_once __DIR__ . '/Maintenance.php';
31
32/**
33 * Maintenance script that fixes double redirects.
34 *
35 * @ingroup Maintenance
36 */
37class FixDoubleRedirects extends Maintenance {
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription( 'Script to fix double redirects' );
41        $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
42        $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
43        $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
44    }
45
46    public function execute() {
47        $async = $this->hasOption( 'async' );
48        $dryrun = $this->hasOption( 'dry-run' );
49
50        if ( $this->hasOption( 'title' ) ) {
51            $title = Title::newFromText( $this->getOption( 'title' ) );
52            if ( !$title || !$title->isRedirect() ) {
53                $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
54            }
55        } else {
56            $title = null;
57        }
58
59        $dbr = $this->getReplicaDB();
60
61        // See also SpecialDoubleRedirects
62        // TODO: support batch querying
63        $queryBuilder = $dbr->newSelectQueryBuilder()
64            ->select( [
65                'pa_namespace' => 'pa.page_namespace',
66                'pa_title' => 'pa.page_title',
67                'pb_namespace' => 'pb.page_namespace',
68                'pb_title' => 'pb.page_title',
69            ] )
70            ->from( 'redirect' )
71            ->join( 'page', 'pa', 'rd_from = pa.page_id' )
72            ->join( 'page', 'pb', [ 'rd_namespace = pb.page_namespace', 'rd_title = pb.page_title' ] )
73            // T42352
74            ->where( [ 'rd_interwiki' => '', 'pb.page_is_redirect' => 1 ] );
75
76        if ( $title != null ) {
77            $queryBuilder->andWhere( [
78                'pb.page_namespace' => $title->getNamespace(),
79                'pb.page_title' => $title->getDBkey()
80            ] );
81        }
82        $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
83
84        if ( !$res->numRows() ) {
85            $this->output( "No double redirects found.\n" );
86
87            return;
88        }
89
90        $jobs = [];
91        $processedTitles = "\n";
92        $n = 0;
93        $services = $this->getServiceContainer();
94        foreach ( $res as $row ) {
95            $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
96            $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
97            if ( !$titleA->canExist() || !$titleB->canExist() ) {
98                $this->error( "Cannot fix redirect from" .
99                    ( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
100                    . " to new" .
101                    ( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
102                    . "\n"
103                );
104                continue;
105            }
106
107            $processedTitles .= "* [[$titleA]]\n";
108
109            $job = new DoubleRedirectJob(
110                $titleA,
111                [
112                    'reason' => 'maintenance',
113                    'redirTitle' => $titleB->getPrefixedDBkey()
114                ],
115                $services->getRevisionLookup(),
116                $services->getMagicWordFactory(),
117                $services->getWikiPageFactory()
118            );
119
120            if ( !$async ) {
121                $success = ( $dryrun ? true : $job->run() );
122                if ( !$success ) {
123                    $this->error( "Error fixing " . $titleA->getPrefixedText()
124                        . ": " . $job->getLastError() . "\n" );
125                }
126            } else {
127                $jobs[] = $job;
128                if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
129                    $this->queueJobs( $jobs, $dryrun );
130                    $jobs = [];
131                }
132            }
133
134            if ( ++$n % 100 == 0 ) {
135                $this->output( "$n...\n" );
136            }
137        }
138
139        if ( count( $jobs ) ) {
140            $this->queueJobs( $jobs, $dryrun );
141        }
142        $this->output( "$n double redirects processed" . $processedTitles . "\n" );
143    }
144
145    protected function queueJobs( $jobs, $dryrun = false ) {
146        $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
147        $this->getServiceContainer()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
148    }
149}
150
151$maintClass = FixDoubleRedirects::class;
152require_once RUN_MAINTENANCE_IF_MAIN;