Revert "merged master"
[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( dirname( __FILE__ ) . '/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 $tables = array( 'redirect', 'pa' => 'page', 'pb' => 'page' );
59 $fields = array(
60 'pa.page_namespace AS pa_namespace',
61 'pa.page_title AS pa_title',
62 'pb.page_namespace AS pb_namespace',
63 'pb.page_title AS pb_title',
64 );
65 $conds = array(
66 'rd_from = pa.page_id',
67 'rd_namespace = pb.page_namespace',
68 'rd_title = pb.page_title',
69 'pb.page_is_redirect' => 1,
70 );
71
72 if ( isset( $title ) ) {
73 $conds['pb.page_namespace'] = $title->getNamespace();
74 $conds['pb.page_title'] = $title->getDBkey();
75 }
76 // TODO: support batch querying
77
78 $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
79
80 if ( !$res->numRows() ) {
81 $this->output( "No double redirects found.\n" );
82 return;
83 }
84
85 $jobs = array();
86 $n = 0;
87 foreach ( $res as $row ) {
88 $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
89 $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
90
91 $job = new DoubleRedirectJob( $titleA, array( 'reason' => 'maintenance', 'redirTitle' => $titleB->getPrefixedDBkey() ) );
92
93 if ( !$async ) {
94 $success = ( $dryrun ? true : $job->run() );
95 if ( !$success ) {
96 $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" );
97 }
98 } else {
99 $jobs[] = $job;
100 // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
101 if ( count( $jobs ) > 10000 ) {
102 $this->queueJobs( $jobs, $dryrun );
103 $jobs = array();
104 }
105 }
106
107 if ( ++$n % 100 == 0 ) {
108 $this->output( "$n...\n" );
109 }
110 }
111
112 if ( count( $jobs ) ) {
113 $this->queueJobs( $jobs, $dryrun );
114 }
115 $this->output( "$n double redirects processed.\n" );
116 }
117
118 protected function queueJobs( $jobs, $dryrun = false ) {
119 $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
120 Job::batchInsert( $dryrun ? array() : $jobs );
121 }
122 }
123
124 $maintClass = "FixDoubleRedirects";
125 require_once( RUN_MAINTENANCE_IF_MAIN );