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