Refactor convertLinks to not use DB::newFromParams() crap, make updaters use the...
[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 getDbType() {
35 return self::DB_ADMIN;
36 }
37
38 public function execute() {
39 global $wgDBtype;
40
41 $dbw = wfGetDB( DB_MASTER );
42
43 $type = $dbw->getType();
44 if ( $type != 'mysql' ) {
45 $this->output( "Link table conversion not necessary for $type\n" );
46 return;
47 }
48
49 global $wgLang, $noKeys, $logPerformance, $fh;
50
51 $tuplesAdded = $numBadLinks = $curRowsRead = 0; # counters etc
52 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
53
54 $reportCurReadProgress = true; # whether or not to give progress reports while reading IDs from cur table
55 $curReadReportInterval = 1000; # number of rows between progress reports
56
57 $reportLinksConvProgress = true; # whether or not to give progress reports during conversion
58 $linksConvInsertInterval = 1000; # number of rows per INSERT
59
60 $initialRowOffset = 0;
61 # $finalRowOffset = 0; # not used yet; highest row number from links table to process
62
63 # Overwrite the old links table with the new one. If this is set to false,
64 # the new table will be left at links_temp.
65 $overwriteLinksTable = true;
66
67 # Don't create keys, and so allow duplicates in the new links table.
68 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
69 $noKeys = false;
70
71
72 $logPerformance = false; # output performance data to a file
73 $perfLogFilename = "convLinksPerf.txt";
74 # --------------------------------------------------------------------
75
76 list ( $cur, $links, $links_temp, $links_backup ) = $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
77
78 if( $dbw->tableExists( 'pagelinks' ) ) {
79 $this->output( "...have pagelinks; skipping old links table updates\n" );
80 return;
81 }
82
83 $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
84 if ( $dbw->fieldType( $res, 0 ) == "int" ) {
85 $this->output( "Schema already converted\n" );
86 return;
87 }
88
89 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
90 $row = $dbw->fetchObject( $res );
91 $numRows = $row->count;
92 $dbw->freeResult( $res );
93
94 if ( $numRows == 0 ) {
95 $this->output( "Updating schema (no rows to convert)...\n" );
96 $this->createTempTable();
97 } else {
98 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
99 $baseTime = $startTime = $this->getMicroTime();
100 # Create a title -> cur_id map
101 $this->output( "Loading IDs from $cur table...\n" );
102 $this->performanceLog ( "Reading $numRows rows from cur table...\n" );
103 $this->performanceLog ( "rows read vs seconds elapsed:\n" );
104
105 $dbw->bufferResults( false );
106 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
107 $ids = array();
108
109 while ( $row = $dbw->fetchObject( $res ) ) {
110 $title = $row->cur_title;
111 if ( $row->cur_namespace ) {
112 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
113 }
114 $ids[$title] = $row->cur_id;
115 $curRowsRead++;
116 if ( $reportCurReadProgress ) {
117 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
118 $this->performanceLog( $curRowsRead . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
119 $this->output( "\t$curRowsRead rows of $cur table read.\n" );
120 }
121 }
122 }
123 $dbw->freeResult( $res );
124 $dbw->bufferResults( true );
125 $this->output( "Finished loading IDs.\n\n" );
126 $this->performanceLog( "Took " . ( $this->getMicroTime() - $baseTime ) . " seconds to load IDs.\n\n" );
127 # --------------------------------------------------------------------
128
129 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
130 # convert, and write to the new table.
131 $this->createTempTable();
132 $this->performanceLog( "Resetting timer.\n\n" );
133 $baseTime = $this->getMicroTime();
134 $this->output( "Processing $numRows rows from $links table...\n" );
135 $this->performanceLog( "Processing $numRows rows from $links table...\n" );
136 $this->performanceLog( "rows inserted vs seconds elapsed:\n" );
137
138 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval ) {
139 $sqlRead = "SELECT * FROM $links ";
140 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
141 $res = $dbw->query( $sqlRead );
142 if ( $noKeys ) {
143 $sqlWrite = array( "INSERT INTO $links_temp (l_from,l_to) VALUES " );
144 } else {
145 $sqlWrite = array( "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " );
146 }
147
148 $tuplesAdded = 0; # no tuples added to INSERT yet
149 while ( $row = $dbw->fetchObject( $res ) ) {
150 $fromTitle = $row->l_from;
151 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
152 $from = $ids[$fromTitle];
153 $to = $row->l_to;
154 if ( $tuplesAdded != 0 ) {
155 $sqlWrite[] = ",";
156 }
157 $sqlWrite[] = "($from,$to)";
158 $tuplesAdded++;
159 } else { # invalid title
160 $numBadLinks++;
161 }
162 }
163 $dbw->freeResult( $res );
164 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n" );
165 if ( $tuplesAdded != 0 ) {
166 if ( $reportLinksConvProgress ) {
167 $this->output( "Inserting $tuplesAdded tuples into $links_temp..." );
168 }
169 $dbw->query( implode( "", $sqlWrite ) );
170 $totalTuplesInserted += $tuplesAdded;
171 if ( $reportLinksConvProgress )
172 $this->output( " done. Total $totalTuplesInserted tuples inserted.\n" );
173 $this->performanceLog( $totalTuplesInserted . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
174 }
175 }
176 $this->output( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n" );
177 $this->performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
178 $this->performanceLog( "Total execution time: " . ( $this->getMicroTime() - $startTime ) . " seconds.\n" );
179 if ( $logPerformance ) { fclose ( $fh ); }
180 }
181 # --------------------------------------------------------------------
182
183 if ( $overwriteLinksTable ) {
184 # Check for existing links_backup, and delete it if it exists.
185 $this->output( "Dropping backup links table if it exists..." );
186 $dbw->query( "DROP TABLE IF EXISTS $links_backup", DB_MASTER );
187 $this->output( " done.\n" );
188
189 # Swap in the new table, and move old links table to links_backup
190 $this->output( "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
191 $dbw->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", DB_MASTER );
192 $this->output( " done.\n\n" );
193
194 $dbw->close();
195 $this->output( "Conversion complete. The old table remains at $links_backup;\n" );
196 $this->output( "delete at your leisure.\n" );
197 } else {
198 $this->output( "Conversion complete. The converted table is at $links_temp;\n" );
199 $this->output( "the original links table is unchanged.\n" );
200 }
201 }
202
203 private function createTempTable() {
204 global $noKeys;
205 $dbConn = wfGetDB( DB_MASTER );
206
207 if ( !( $dbConn->isOpen() ) ) {
208 $this->output( "Opening connection to database failed.\n" );
209 return;
210 }
211 $links_temp = $dbConn->tableName( 'links_temp' );
212
213 $this->output( "Dropping temporary links table if it exists..." );
214 $dbConn->query( "DROP TABLE IF EXISTS $links_temp" );
215 $this->output( " done.\n" );
216
217 $this->output( "Creating temporary links table..." );
218 if ( $noKeys ) {
219 $dbConn->query( "CREATE TABLE $links_temp ( " .
220 "l_from int(8) unsigned NOT NULL default '0', " .
221 "l_to int(8) unsigned NOT NULL default '0')" );
222 } else {
223 $dbConn->query( "CREATE TABLE $links_temp ( " .
224 "l_from int(8) unsigned NOT NULL default '0', " .
225 "l_to int(8) unsigned NOT NULL default '0', " .
226 "UNIQUE KEY l_from(l_from,l_to), " .
227 "KEY (l_to))" );
228 }
229 $this->output( " done.\n\n" );
230 }
231
232 private function performanceLog( $text ) {
233 global $logPerformance, $fh;
234 if ( $logPerformance ) {
235 fwrite( $fh, $text );
236 }
237 }
238
239 private function getMicroTime() { # return time in seconds, with microsecond accuracy
240 list( $usec, $sec ) = explode( " ", microtime() );
241 return ( (float)$usec + (float)$sec );
242 }
243 }
244
245 $maintClass = "ConvertLinks";
246 require_once( DO_MAINTENANCE );