Followup r92081: SQL fix for cleanupUploadStash.php on PostgreSQL (bug 32822)
[lhc/web/wiklou.git] / maintenance / fixDoubleRedirects.php
1 <?php
2 /**
3 * Script to fix double redirects.
4 *
5 * Copyright (C) 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 class FixDoubleRedirects extends Maintenance {
31 public function __construct() {
32 parent::__construct();
33 $this->mDescription = "Script to fix double redirects";
34 $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
35 $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
36 $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
37 }
38
39 public function execute() {
40 $async = $this->getOption( 'async', false );
41 $dryrun = $this->getOption( 'dry-run', false );
42 $title = $this->getOption( 'title' );
43
44 if ( isset( $title ) ) {
45 $title = Title::newFromText( $title );
46 if ( !$title || !$title->isRedirect() ) {
47 $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
48 }
49 }
50
51 $dbr = wfGetDB( DB_SLAVE );
52
53 $tables = array( 'redirect', 'pa' => 'page', 'pb' => 'page' );
54 $fields = array(
55 'pa.page_namespace AS pa_namespace',
56 'pa.page_title AS pa_title',
57 'pb.page_namespace AS pb_namespace',
58 'pb.page_title AS pb_title',
59 );
60 $conds = array(
61 'rd_from = pa.page_id',
62 'rd_namespace = pb.page_namespace',
63 'rd_title = pb.page_title',
64 'pb.page_is_redirect' => 1,
65 );
66
67 if ( isset( $title ) ) {
68 $conds['pb.page_namespace'] = $title->getNamespace();
69 $conds['pb.page_title'] = $title->getDBkey();
70 }
71 // TODO: support batch querying
72
73 $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
74
75 if ( !$res->numRows() ) {
76 $this->output( "No double redirects found.\n" );
77 return;
78 }
79
80 $jobs = array();
81 $n = 0;
82 foreach ( $res as $row ) {
83 $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
84 $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
85
86 $job = new DoubleRedirectJob( $titleA, array( 'reason' => 'maintenance', 'redirTitle' => $titleB->getPrefixedDBkey() ) );
87
88 if ( !$async ) {
89 $success = ( $dryrun ? true : $job->run() );
90 if ( !$success ) {
91 $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" );
92 }
93 } else {
94 $jobs[] = $job;
95 // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
96 if ( count( $jobs ) > 10000 ) {
97 $this->queueJobs( $jobs, $dryrun );
98 $jobs = array();
99 }
100 }
101
102 if ( ++$n % 100 == 0 ) {
103 $this->output( "$n...\n" );
104 }
105 }
106
107 if ( count( $jobs ) ) {
108 $this->queueJobs( $jobs, $dryrun );
109 }
110 $this->output( "$n double redirects processed.\n" );
111 }
112
113 protected function queueJobs( $jobs, $dryrun = false ) {
114 $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
115 Job::batchInsert( $dryrun ? array() : $jobs );
116 }
117 }
118
119 $maintClass = "FixDoubleRedirects";
120 require_once( RUN_MAINTENANCE_IF_MAIN );