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