* (bug 21503) There's now a "reason" field when creating account for other users
[lhc/web/wiklou.git] / maintenance / convertLinks.php
1 <?php
2 /**
3 * Convert from the old links schema (string->ID) to the new schema (ID->ID)
4 * The wiki should be put into read-only mode while this script executes
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class ConvertLinks extends Maintenance {
27
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Convert from the old links schema (string->ID) to the new schema (ID->ID)
31 The wiki should be put into read-only mode while this script executes";
32 }
33
34 public function execute() {
35 global $wgDBtype;
36 if ( $wgDBtype == 'postgres' ) {
37 $this->output( "Links table already ok on Postgres.\n" );
38 return;
39 }
40
41 $this->output( "Converting links table to ID-ID...\n" );
42
43 global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
44 global $noKeys, $logPerformance, $fh;
45
46 $tuplesAdded = $numBadLinks = $curRowsRead = 0; # counters etc
47 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
48
49 $reportCurReadProgress = true; # whether or not to give progress reports while reading IDs from cur table
50 $curReadReportInterval = 1000; # number of rows between progress reports
51
52 $reportLinksConvProgress = true; # whether or not to give progress reports during conversion
53 $linksConvInsertInterval = 1000; # number of rows per INSERT
54
55 $initialRowOffset = 0;
56 # $finalRowOffset = 0; # not used yet; highest row number from links table to process
57
58 # Overwrite the old links table with the new one. If this is set to false,
59 # the new table will be left at links_temp.
60 $overwriteLinksTable = true;
61
62 # Don't create keys, and so allow duplicates in the new links table.
63 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
64 $noKeys = false;
65
66
67 $logPerformance = false; # output performance data to a file
68 $perfLogFilename = "convLinksPerf.txt";
69 # --------------------------------------------------------------------
70
71 $dbw = wfGetDB( DB_MASTER );
72 list ( $cur, $links, $links_temp, $links_backup ) = $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
73
74 $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
75 if ( $dbw->fieldType( $res, 0 ) == "int" ) {
76 $this->output( "Schema already converted\n" );
77 return;
78 }
79
80 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
81 $row = $dbw->fetchObject( $res );
82 $numRows = $row->count;
83 $dbw->freeResult( $res );
84
85 if ( $numRows == 0 ) {
86 $this->output( "Updating schema (no rows to convert)...\n" );
87 $this->createTempTable();
88 } else {
89 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
90 $baseTime = $startTime = $this->getMicroTime();
91 # Create a title -> cur_id map
92 $this->output( "Loading IDs from $cur table...\n" );
93 $this->performanceLog ( "Reading $numRows rows from cur table...\n" );
94 $this->performanceLog ( "rows read vs seconds elapsed:\n" );
95
96 $dbw->bufferResults( false );
97 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
98 $ids = array();
99
100 while ( $row = $dbw->fetchObject( $res ) ) {
101 $title = $row->cur_title;
102 if ( $row->cur_namespace ) {
103 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
104 }
105 $ids[$title] = $row->cur_id;
106 $curRowsRead++;
107 if ( $reportCurReadProgress ) {
108 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
109 $this->performanceLog( $curRowsRead . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
110 $this->output( "\t$curRowsRead rows of $cur table read.\n" );
111 }
112 }
113 }
114 $dbw->freeResult( $res );
115 $dbw->bufferResults( true );
116 $this->output( "Finished loading IDs.\n\n" );
117 $this->performanceLog( "Took " . ( $this->getMicroTime() - $baseTime ) . " seconds to load IDs.\n\n" );
118 # --------------------------------------------------------------------
119
120 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
121 # convert, and write to the new table.
122 $this->createTempTable();
123 $this->performanceLog( "Resetting timer.\n\n" );
124 $baseTime = $this->getMicroTime();
125 $this->output( "Processing $numRows rows from $links table...\n" );
126 $this->performanceLog( "Processing $numRows rows from $links table...\n" );
127 $this->performanceLog( "rows inserted vs seconds elapsed:\n" );
128
129 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval ) {
130 $sqlRead = "SELECT * FROM $links ";
131 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
132 $res = $dbw->query( $sqlRead );
133 if ( $noKeys ) {
134 $sqlWrite = array( "INSERT INTO $links_temp (l_from,l_to) VALUES " );
135 } else {
136 $sqlWrite = array( "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " );
137 }
138
139 $tuplesAdded = 0; # no tuples added to INSERT yet
140 while ( $row = $dbw->fetchObject( $res ) ) {
141 $fromTitle = $row->l_from;
142 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
143 $from = $ids[$fromTitle];
144 $to = $row->l_to;
145 if ( $tuplesAdded != 0 ) {
146 $sqlWrite[] = ",";
147 }
148 $sqlWrite[] = "($from,$to)";
149 $tuplesAdded++;
150 } else { # invalid title
151 $numBadLinks++;
152 }
153 }
154 $dbw->freeResult( $res );
155 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n" );
156 if ( $tuplesAdded != 0 ) {
157 if ( $reportLinksConvProgress ) {
158 $this->output( "Inserting $tuplesAdded tuples into $links_temp..." );
159 }
160 $dbw->query( implode( "", $sqlWrite ) );
161 $totalTuplesInserted += $tuplesAdded;
162 if ( $reportLinksConvProgress )
163 $this->output( " done. Total $totalTuplesInserted tuples inserted.\n" );
164 $this->performanceLog( $totalTuplesInserted . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
165 }
166 }
167 $this->output( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n" );
168 $this->performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
169 $this->performanceLog( "Total execution time: " . ( $this->getMicroTime() - $startTime ) . " seconds.\n" );
170 if ( $logPerformance ) { fclose ( $fh ); }
171 }
172 # --------------------------------------------------------------------
173
174 if ( $overwriteLinksTable ) {
175 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
176 if ( !( $dbConn->isOpen() ) ) {
177 $this->output( "Opening connection to database failed.\n" );
178 return;
179 }
180 # Check for existing links_backup, and delete it if it exists.
181 $this->output( "Dropping backup links table if it exists..." );
182 $dbConn->query( "DROP TABLE IF EXISTS $links_backup", DB_MASTER );
183 $this->output( " done.\n" );
184
185 # Swap in the new table, and move old links table to links_backup
186 $this->output( "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
187 $dbConn->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", DB_MASTER );
188 $this->output( " done.\n\n" );
189
190 $dbConn->close();
191 $this->output( "Conversion complete. The old table remains at $links_backup;\n" );
192 $this->output( "delete at your leisure.\n" );
193 } else {
194 $this->output( "Conversion complete. The converted table is at $links_temp;\n" );
195 $this->output( "the original links table is unchanged.\n" );
196 }
197 }
198
199 private function createTempTable() {
200 global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
201 global $noKeys;
202 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
203
204 if ( !( $dbConn->isOpen() ) ) {
205 $this->output( "Opening connection to database failed.\n" );
206 return;
207 }
208 $links_temp = $dbConn->tableName( 'links_temp' );
209
210 $this->output( "Dropping temporary links table if it exists..." );
211 $dbConn->query( "DROP TABLE IF EXISTS $links_temp" );
212 $this->output( " done.\n" );
213
214 $this->output( "Creating temporary links table..." );
215 if ( $noKeys ) {
216 $dbConn->query( "CREATE TABLE $links_temp ( " .
217 "l_from int(8) unsigned NOT NULL default '0', " .
218 "l_to int(8) unsigned NOT NULL default '0')" );
219 } else {
220 $dbConn->query( "CREATE TABLE $links_temp ( " .
221 "l_from int(8) unsigned NOT NULL default '0', " .
222 "l_to int(8) unsigned NOT NULL default '0', " .
223 "UNIQUE KEY l_from(l_from,l_to), " .
224 "KEY (l_to))" );
225 }
226 $this->output( " done.\n\n" );
227 }
228
229 private function performanceLog( $text ) {
230 global $logPerformance, $fh;
231 if ( $logPerformance ) {
232 fwrite( $fh, $text );
233 }
234 }
235
236 private function getMicroTime() { # return time in seconds, with microsecond accuracy
237 list( $usec, $sec ) = explode( " ", microtime() );
238 return ( (float)$usec + (float)$sec );
239 }
240 }
241
242 $maintClass = "ConvertLinks";
243 require_once( DO_MAINTENANCE );