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