Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / fixDoubleRedirects.php
blobee9e4c6c613c44f8d7c5c9fb889d94d92c2b20bc
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 require_once __DIR__ . '/Maintenance.php';
30 /**
31 * Maintenance script that fixes double redirects.
33 * @ingroup Maintenance
35 class FixDoubleRedirects extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Script to fix double redirects' );
39 $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
40 $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
41 $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
44 public function execute() {
45 $async = $this->hasOption( 'async' );
46 $dryrun = $this->hasOption( 'dry-run' );
48 if ( $this->hasOption( 'title' ) ) {
49 $title = Title::newFromText( $this->getOption( 'title' ) );
50 if ( !$title || !$title->isRedirect() ) {
51 $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
53 } else {
54 $title = null;
57 $dbr = $this->getDB( DB_REPLICA );
59 // See also SpecialDoubleRedirects
60 $tables = [
61 'redirect',
62 'pa' => 'page',
63 'pb' => 'page',
65 $fields = [
66 'pa.page_namespace AS pa_namespace',
67 'pa.page_title AS pa_title',
68 'pb.page_namespace AS pb_namespace',
69 'pb.page_title AS pb_title',
71 $conds = [
72 'rd_from = pa.page_id',
73 'rd_namespace = pb.page_namespace',
74 'rd_title = pb.page_title',
75 // T42352
76 'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
77 'pb.page_is_redirect' => 1,
80 if ( $title != null ) {
81 $conds['pb.page_namespace'] = $title->getNamespace();
82 $conds['pb.page_title'] = $title->getDBkey();
84 // TODO: support batch querying
86 $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
88 if ( !$res->numRows() ) {
89 $this->output( "No double redirects found.\n" );
91 return;
94 $jobs = [];
95 $processedTitles = "\n";
96 $n = 0;
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 );
101 $processedTitles .= "* [[$titleA]]\n";
103 $job = new DoubleRedirectJob( $titleA, [
104 'reason' => 'maintenance',
105 'redirTitle' => $titleB->getPrefixedDBkey()
106 ] );
108 if ( !$async ) {
109 $success = ( $dryrun ? true : $job->run() );
110 if ( !$success ) {
111 $this->error( "Error fixing " . $titleA->getPrefixedText()
112 . ": " . $job->getLastError() . "\n" );
114 } else {
115 $jobs[] = $job;
116 // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
117 if ( count( $jobs ) > 10000 ) {
118 $this->queueJobs( $jobs, $dryrun );
119 $jobs = [];
123 if ( ++$n % 100 == 0 ) {
124 $this->output( "$n...\n" );
128 if ( count( $jobs ) ) {
129 $this->queueJobs( $jobs, $dryrun );
131 $this->output( "$n double redirects processed" . $processedTitles . "\n" );
134 protected function queueJobs( $jobs, $dryrun = false ) {
135 $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
136 JobQueueGroup::singleton()->push( $dryrun ? [] : $jobs );
140 $maintClass = FixDoubleRedirects::class;
141 require_once RUN_MAINTENANCE_IF_MAIN;