phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / fixDoubleRedirects.php
1 <?php
2 /**
3 * Fix double redirects.
4 *
5 * Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
6 * http://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 require_once __DIR__ . '/Maintenance.php';
29
30 /**
31 * Maintenance script that fixes double redirects.
32 *
33 * @ingroup Maintenance
34 */
35 class FixDoubleRedirects extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "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' );
42 }
43
44 public function execute() {
45 $async = $this->getOption( 'async', false );
46 $dryrun = $this->getOption( 'dry-run', false );
47 $title = $this->getOption( 'title' );
48
49 if ( isset( $title ) ) {
50 $title = Title::newFromText( $title );
51 if ( !$title || !$title->isRedirect() ) {
52 $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
53 }
54 }
55
56 $dbr = wfGetDB( DB_SLAVE );
57
58 // See also SpecialDoubleRedirects
59 $tables = array(
60 'redirect',
61 'pa' => 'page',
62 'pb' => 'page',
63 );
64 $fields = array(
65 'pa.page_namespace AS pa_namespace',
66 'pa.page_title AS pa_title',
67 'pb.page_namespace AS pb_namespace',
68 'pb.page_title AS pb_title',
69 );
70 $conds = array(
71 'rd_from = pa.page_id',
72 'rd_namespace = pb.page_namespace',
73 'rd_title = pb.page_title',
74 '(rd_interwiki IS NULL OR rd_interwiki = "")', // bug 40352
75 'pb.page_is_redirect' => 1,
76 );
77
78 if ( isset( $title ) ) {
79 $conds['pb.page_namespace'] = $title->getNamespace();
80 $conds['pb.page_title'] = $title->getDBkey();
81 }
82 // TODO: support batch querying
83
84 $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
85
86 if ( !$res->numRows() ) {
87 $this->output( "No double redirects found.\n" );
88 return;
89 }
90
91 $jobs = array();
92 $processedTitles = "\n";
93 $n = 0;
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
98 $processedTitles .= "* [[$titleA]]\n";
99
100 $job = new DoubleRedirectJob( $titleA, array(
101 'reason' => 'maintenance',
102 'redirTitle' => $titleB->getPrefixedDBkey()
103 ) );
104
105 if ( !$async ) {
106 $success = ( $dryrun ? true : $job->run() );
107 if ( !$success ) {
108 $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" );
109 }
110 } else {
111 $jobs[] = $job;
112 // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
113 if ( count( $jobs ) > 10000 ) {
114 $this->queueJobs( $jobs, $dryrun );
115 $jobs = array();
116 }
117 }
118
119 if ( ++$n % 100 == 0 ) {
120 $this->output( "$n...\n" );
121 }
122 }
123
124 if ( count( $jobs ) ) {
125 $this->queueJobs( $jobs, $dryrun );
126 }
127 $this->output( "$n double redirects processed" . $processedTitles . "\n" );
128 }
129
130 protected function queueJobs( $jobs, $dryrun = false ) {
131 $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
132 JobQueueGroup::singleton()->push( $dryrun ? array() : $jobs );
133 }
134 }
135
136 $maintClass = "FixDoubleRedirects";
137 require_once RUN_MAINTENANCE_IF_MAIN;