Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / fixDoubleRedirects.php
blobc2a010d502fe6dd895c41b3f2956eac19f582943
1 <?php
2 /**
3 * Fix double redirects.
5 * Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @author Ilmari Karonen <nospam@vyznev.net>
25 * @ingroup Maintenance
28 use MediaWiki\Maintenance\Maintenance;
29 use MediaWiki\Title\Title;
31 // @codeCoverageIgnoreStart
32 require_once __DIR__ . '/Maintenance.php';
33 // @codeCoverageIgnoreEnd
35 /**
36 * Maintenance script that fixes double redirects.
38 * @ingroup Maintenance
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' );
49 public function execute() {
50 $async = $this->hasOption( 'async' );
51 $dryrun = $this->hasOption( 'dry-run' );
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" );
58 } else {
59 $title = null;
62 $dbr = $this->getReplicaDB();
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 ] );
79 if ( $title != null ) {
80 $queryBuilder->andWhere( [
81 'pb.page_namespace' => $title->getNamespace(),
82 'pb.page_title' => $title->getDBkey()
83 ] );
85 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
87 if ( !$res->numRows() ) {
88 $this->output( "No double redirects found.\n" );
90 return;
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"
107 continue;
110 $processedTitles .= "* [[$titleA]]\n";
112 $job = new DoubleRedirectJob(
113 $titleA,
115 'reason' => 'maintenance',
116 'redirTitle' => $titleB->getPrefixedDBkey()
118 $services->getRevisionLookup(),
119 $services->getMagicWordFactory(),
120 $services->getWikiPageFactory()
123 if ( !$async ) {
124 $success = ( $dryrun ? true : $job->run() );
125 if ( !$success ) {
126 $this->error( "Error fixing " . $titleA->getPrefixedText()
127 . ": " . $job->getLastError() . "\n" );
129 } else {
130 $jobs[] = $job;
131 if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
132 $this->queueJobs( $jobs, $dryrun );
133 $jobs = [];
137 if ( ++$n % 100 == 0 ) {
138 $this->output( "$n...\n" );
142 if ( count( $jobs ) ) {
143 $this->queueJobs( $jobs, $dryrun );
145 $this->output( "$n double redirects processed" . $processedTitles . "\n" );
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 );
154 // @codeCoverageIgnoreStart
155 $maintClass = FixDoubleRedirects::class;
156 require_once RUN_MAINTENANCE_IF_MAIN;
157 // @codeCoverageIgnoreEnd