5 * @subpackage Maintenance
9 function convertLinks() {
10 print "Converting links table to ID-ID...\n";
12 global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
13 global $noKeys, $logPerformance, $fh;
15 $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
16 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
18 $reportCurReadProgress = true; #whether or not to give progress reports while reading IDs from cur table
19 $curReadReportInterval = 1000; #number of rows between progress reports
21 $reportLinksConvProgress = true; #whether or not to give progress reports during conversion
22 $linksConvInsertInterval = 1000; #number of rows per INSERT
24 $initialRowOffset = 0;
25 #$finalRowOffset = 0; # not used yet; highest row number from links table to process
27 # Overwrite the old links table with the new one. If this is set to false,
28 # the new table will be left at links_temp.
29 $overwriteLinksTable = true;
31 # Don't create keys, and so allow duplicates in the new links table.
32 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
36 $logPerformance = false; # output performance data to a file
37 $perfLogFilename = "convLinksPerf.txt";
38 #--------------------------------------------------------------------
40 $dbw =& wfGetDB( DB_MASTER );
41 extract( $dbw->tableNames( 'cur', 'links', 'links_temp', 'links_backup' ) );
43 $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
44 if ( $dbw->fieldType( $res, 0 ) == "int" ) {
45 print "Schema already converted\n";
49 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
50 $row = $dbw->fetchObject($res);
51 $numRows = $row->count;
52 $dbw->freeResult( $res );
54 if ( $numRows == 0 ) {
55 print "Updating schema (no rows to convert)...\n";
58 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
59 $baseTime = $startTime = getMicroTime();
60 # Create a title -> cur_id map
61 print "Loading IDs from $cur table...\n";
62 performanceLog ( "Reading $numRows rows from cur table...\n" );
63 performanceLog ( "rows read vs seconds elapsed:\n" );
65 $dbw->setBufferResults( false );
66 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
69 while ( $row = $dbw->fetchObject( $res ) ) {
70 $title = $row->cur_title;
71 if ( $row->cur_namespace ) {
72 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
74 $ids[$title] = $row->cur_id;
76 if ($reportCurReadProgress) {
77 if (($curRowsRead % $curReadReportInterval) == 0) {
78 performanceLog( $curRowsRead . " " . (getMicroTime() - $baseTime) . "\n" );
79 print "\t$curRowsRead rows of $cur table read.\n";
83 $dbw->freeResult( $res );
84 $dbw->setBufferResults( true );
85 print "Finished loading IDs.\n\n";
86 performanceLog( "Took " . (getMicroTime() - $baseTime) . " seconds to load IDs.\n\n" );
87 #--------------------------------------------------------------------
89 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
90 # convert, and write to the new table.
92 performanceLog( "Resetting timer.\n\n" );
93 $baseTime = getMicroTime();
94 print "Processing $numRows rows from $links table...\n";
95 performanceLog( "Processing $numRows rows from $links table...\n" );
96 performanceLog( "rows inserted vs seconds elapsed:\n" );
98 for ($rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval) {
99 $sqlRead = "SELECT * FROM $links ".wfLimitResult($linksConvInsertInterval,$rowOffset);
100 $res = $dbw->query($sqlRead);
102 $sqlWrite = array("INSERT INTO $links_temp (l_from,l_to) VALUES ");
104 $sqlWrite = array("INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES ");
107 $tuplesAdded = 0; # no tuples added to INSERT yet
108 while ( $row = $dbw->fetchObject($res) ) {
109 $fromTitle = $row->l_from;
110 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
111 $from = $ids[$fromTitle];
113 if ( $tuplesAdded != 0 ) {
116 $sqlWrite[] = "($from,$to)";
118 } else { # invalid title
122 $dbw->freeResult($res);
123 #print "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n";
124 if ( $tuplesAdded != 0 ) {
125 if ($reportLinksConvProgress) {
126 print "Inserting $tuplesAdded tuples into $links_temp...";
128 $dbw->query( implode("",$sqlWrite) );
129 $totalTuplesInserted += $tuplesAdded;
130 if ($reportLinksConvProgress)
131 print " done. Total $totalTuplesInserted tuples inserted.\n";
132 performanceLog( $totalTuplesInserted . " " . (getMicroTime() - $baseTime) . "\n" );
135 print "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n";
136 performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
137 performanceLog( "Total execution time: " . (getMicroTime() - $startTime) . " seconds.\n" );
138 if ( $logPerformance ) { fclose ( $fh ); }
140 #--------------------------------------------------------------------
142 if ( $overwriteLinksTable ) {
143 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
144 if (!($dbConn->isOpen())) {
145 print "Opening connection to database failed.\n";
148 # Check for existing links_backup, and delete it if it exists.
149 print "Dropping backup links table if it exists...";
150 $dbConn->query( "DROP TABLE IF EXISTS $links_backup", DB_MASTER);
153 # Swap in the new table, and move old links table to links_backup
154 print "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'...";
155 $dbConn->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", DB_MASTER );
159 print "Conversion complete. The old table remains at $links_backup;\n";
160 print "delete at your leisure.\n";
162 print "Conversion complete. The converted table is at $links_temp;\n";
163 print "the original links table is unchanged.\n";
167 #--------------------------------------------------------------------
169 function createTempTable() {
170 global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
172 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
174 if (!($dbConn->isOpen())) {
175 print "Opening connection to database failed.\n";
178 $links_temp = $dbConn->tableName( 'links_temp' );
180 print "Dropping temporary links table if it exists...";
181 $dbConn->query( "DROP TABLE IF EXISTS $links_temp");
184 print "Creating temporary links table...";
186 $dbConn->query( "CREATE TABLE $links_temp ( " .
187 "l_from int(8) unsigned NOT NULL default '0', " .
188 "l_to int(8) unsigned NOT NULL default '0')");
190 $dbConn->query( "CREATE TABLE $links_temp ( " .
191 "l_from int(8) unsigned NOT NULL default '0', " .
192 "l_to int(8) unsigned NOT NULL default '0', " .
193 "UNIQUE KEY l_from(l_from,l_to), " .
199 function performanceLog( $text ) {
200 global $logPerformance, $fh;
201 if ( $logPerformance ) {
202 fwrite( $fh, $text );
206 function getMicroTime() { # return time in seconds, with microsecond accuracy
207 list($usec, $sec) = explode(" ", microtime());
208 return ((float)$usec + (float)$sec);