Add pf_memory field
[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 * @addtogroup maintenance
29 */
30
31 $options = array( 'dry-run' );
32
33 require_once( 'commandLine.inc' );
34 require_once( 'FiveUpgrade.inc' );
35
36 class CapsCleanup extends FiveUpgrade {
37 function CapsCleanup( $dryrun = false, $namespace=0 ) {
38 parent::FiveUpgrade();
39
40 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
41 $this->dryrun = $dryrun;
42 $this->namespace = intval( $namespace );
43 }
44
45 function cleanup() {
46 global $wgCapitalLinks;
47 if( $wgCapitalLinks ) {
48 echo "\$wgCapitalLinks is on -- no need for caps links cleanup.\n";
49 return false;
50 }
51
52 $this->runTable( 'page', 'WHERE page_namespace=' . $this->namespace,
53 array( &$this, 'processPage' ) );
54 }
55
56 function init( $count, $table ) {
57 $this->processed = 0;
58 $this->updated = 0;
59 $this->count = $count;
60 $this->startTime = wfTime();
61 $this->table = $table;
62 }
63
64 function progress( $updated ) {
65 $this->updated += $updated;
66 $this->processed++;
67 if( $this->processed % 100 != 0 ) {
68 return;
69 }
70 $portion = $this->processed / $this->count;
71 $updateRate = $this->updated / $this->processed;
72
73 $now = wfTime();
74 $delta = $now - $this->startTime;
75 $estimatedTotalTime = $delta / $portion;
76 $eta = $this->startTime + $estimatedTotalTime;
77
78 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
79 wfTimestamp( TS_DB, intval( $now ) ),
80 $portion * 100.0,
81 $this->table,
82 wfTimestamp( TS_DB, intval( $eta ) ),
83 $this->processed,
84 $this->count,
85 $this->processed / $delta,
86 $updateRate * 100.0 );
87 flush();
88 }
89
90 function runTable( $table, $where, $callback ) {
91 $fname = 'CapsCleanup::buildTable';
92
93 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
94 $this->init( $count, 'page' );
95 $this->log( "Processing $table..." );
96
97 $tableName = $this->dbr->tableName( $table );
98 $sql = "SELECT * FROM $tableName $where";
99 $result = $this->dbr->query( $sql, $fname );
100
101 while( $row = $this->dbr->fetchObject( $result ) ) {
102 call_user_func( $callback, $row );
103 }
104 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
105 $this->dbr->freeResult( $result );
106 }
107
108 function processPage( $row ) {
109 global $wgContLang;
110
111 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
112 $display = $current->getPrefixedText();
113 $upper = $row->page_title;
114 $lower = $wgContLang->lcfirst( $row->page_title );
115 if( $upper == $lower ) {
116 $this->log( "\"$display\" already lowercase." );
117 return $this->progress( 0 );
118 }
119
120 $target = Title::makeTitle( $row->page_namespace, $lower );
121 $targetDisplay = $target->getPrefixedText();
122 if( $target->exists() ) {
123 $this->log( "\"$display\" skipped; \"$targetDisplay\" already exists" );
124 return $this->progress( 0 );
125 }
126
127 if( $this->dryrun ) {
128 $this->log( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED" );
129 $ok = true;
130 } else {
131 $ok = $current->moveTo( $target, false, 'Converting page titles to lowercase' );
132 $this->log( "\"$display\" -> \"$targetDisplay\": $ok" );
133 }
134 if( $ok === true ) {
135 $this->progress( 1 );
136
137 if( $row->page_namespace == $this->namespace ) {
138 $talk = $target->getTalkPage();
139 $row->page_namespace = $talk->getNamespace();
140 if( $talk->exists() ) {
141 return $this->processPage( $row );
142 }
143 }
144 } else {
145 $this->progress( 0 );
146 }
147 }
148
149 }
150
151 $wgUser->setName( 'Conversion script' );
152 $ns = isset( $options['namespace'] ) ? $options['namespace'] : 0;
153 $caps = new CapsCleanup( isset( $options['dry-run'] ), $ns );
154 $caps->cleanup();
155
156