8864377fa339fabafbd403354cb1cda4a949c894
[lhc/web/wiklou.git] / maintenance / cleanupCaps.php
1 <?php
2 /*
3 * Script to clean up broken page links when somebody turns on $wgCapitalLinks.
4 *
5 * Usage: php cleanupCaps.php [--dry-run]
6 * Options:
7 * --dry-run don't actually try moving them
8 *
9 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @author Brion Vibber <brion at pobox.com>
28 * @package MediaWiki
29 * @subpackage maintenance
30 */
31
32 $options = array( 'dry-run' );
33
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
36
37 class CapsCleanup extends FiveUpgrade {
38 function CapsCleanup( $dryrun = false, $namespace=0 ) {
39 parent::FiveUpgrade();
40
41 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
42 $this->dryrun = $dryrun;
43 $this->namespace = intval( $namespace );
44 }
45
46 function cleanup() {
47 global $wgCapitalLinks;
48 if( $wgCapitalLinks ) {
49 echo "\$wgCapitalLinks is on -- no need for caps links cleanup.\n";
50 return false;
51 }
52
53 $this->runTable( 'page', 'WHERE page_namespace=' . $this->namespace,
54 array( &$this, 'processPage' ) );
55 }
56
57 function init( $count, $table ) {
58 $this->processed = 0;
59 $this->updated = 0;
60 $this->count = $count;
61 $this->startTime = wfTime();
62 $this->table = $table;
63 }
64
65 function progress( $updated ) {
66 $this->updated += $updated;
67 $this->processed++;
68 if( $this->processed % 100 != 0 ) {
69 return;
70 }
71 $portion = $this->processed / $this->count;
72 $updateRate = $this->updated / $this->processed;
73
74 $now = wfTime();
75 $delta = $now - $this->startTime;
76 $estimatedTotalTime = $delta / $portion;
77 $eta = $this->startTime + $estimatedTotalTime;
78
79 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
80 wfTimestamp( TS_DB, intval( $now ) ),
81 $portion * 100.0,
82 $this->table,
83 wfTimestamp( TS_DB, intval( $eta ) ),
84 $this->processed,
85 $this->count,
86 $this->processed / $delta,
87 $updateRate * 100.0 );
88 flush();
89 }
90
91 function runTable( $table, $where, $callback ) {
92 $fname = 'CapsCleanup::buildTable';
93
94 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
95 $this->init( $count, 'page' );
96 $this->log( "Processing $table..." );
97
98 $tableName = $this->dbr->tableName( $table );
99 $sql = "SELECT * FROM $tableName $where";
100 $result = $this->dbr->query( $sql, $fname );
101
102 while( $row = $this->dbr->fetchObject( $result ) ) {
103 call_user_func( $callback, $row );
104 }
105 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
106 $this->dbr->freeResult( $result );
107 }
108
109 function processPage( $row ) {
110 global $wgContLang;
111
112 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
113 $display = $current->getPrefixedText();
114 $upper = $row->page_title;
115 $lower = $wgContLang->lcfirst( $row->page_title );
116 if( $upper == $lower ) {
117 $this->log( "\"$display\" already lowercase." );
118 return $this->progress( 0 );
119 }
120
121 $target = Title::makeTitle( $row->page_namespace, $lower );
122 $targetDisplay = $target->getPrefixedText();
123 if( $target->exists() ) {
124 $this->log( "\"$display\" skipped; \"$targetDisplay\" already exists" );
125 return $this->progress( 0 );
126 }
127
128 if( $this->dryrun ) {
129 $this->log( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED" );
130 $ok = true;
131 } else {
132 $ok = $current->moveTo( $target, false, 'Converting page titles to lowercase' );
133 $this->log( "\"$display\" -> \"$targetDisplay\": $ok" );
134 }
135 if( $ok === true ) {
136 $this->progress( 1 );
137
138 if( $row->page_namespace == $this->namespace ) {
139 $talk = $target->getTalkPage();
140 $row->page_namespace = $talk->getNamespace();
141 if( $talk->exists() ) {
142 return $this->processPage( $row );
143 }
144 }
145 } else {
146 $this->progress( 0 );
147 }
148 }
149
150 }
151
152 $wgUser->setName( 'Conversion script' );
153 $ns = isset( $options['namespace'] ) ? $options['namespace'] : 0;
154 $caps = new CapsCleanup( isset( $options['dry-run'] ), $ns );
155 $caps->cleanup();
156
157 ?>