3 function convertLinks() {
4 global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
6 $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
7 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
9 $reportCurReadProgress = true; #whether or not to give progress reports while reading IDs from cur table
10 $curReadReportInterval = 1000; #number of rows between progress reports
12 $reportLinksConvProgress = true; #whether or not to give progress reports during conversion
13 $linksConvInsertInterval = 1000; #number of rows per INSERT
15 $initialRowOffset = 0;
16 #$finalRowOffset = 0; # not used yet; highest row number from links table to process
18 # Overwrite the old links table with the new one. If this is set to false,
19 # the new table will be left at links_temp.
20 $overwriteLinksTable = true;
22 # Don't create keys, and so allow duplicates in the new links table.
23 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
27 $logPerformance = false; # output performance data to a file
28 $perfLogFilename = "convLinksPerf.txt";
29 #--------------------------------------------------------------------
31 $res = wfQuery( "SELECT COUNT(*) AS count FROM links", DB_WRITE );
32 $row = wfFetchObject($res);
33 $numRows = $row->count;
36 if ( $numRows == 0 ) {
37 print "No rows to convert. Updating schema...\n";
40 $row = wfFetchObject( $res );
41 if ( is_numeric( $row->l_from ) ) {
42 print "Schema already converted\n";
46 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
47 $baseTime = $startTime = getMicroTime();
48 # Create a title -> cur_id map
49 print "Loading IDs from cur table...\n";
50 performanceLog ( "Reading $numRows rows from cur table...\n" );
51 performanceLog ( "rows read vs seconds elapsed:\n" );
52 wfBufferSQLResults( false );
53 $res = wfQuery( "SELECT cur_namespace,cur_title,cur_id FROM cur", DB_WRITE );
56 while ( $row = wfFetchObject( $res ) ) {
57 $title = $row->cur_title;
58 if ( $row->cur_namespace ) {
59 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
61 $ids[$title] = $row->cur_id;
63 if ($reportCurReadProgress) {
64 if (($curRowsRead % $curReadReportInterval) == 0) {
65 performanceLog( $curRowsRead . " " . (getMicroTime() - $baseTime) . "\n" );
66 print "\t$curRowsRead rows of cur table read.\n";
71 wfBufferSQLResults( true );
72 print "Finished loading IDs.\n\n";
73 performanceLog( "Took " . (getMicroTime() - $baseTime) . " seconds to load IDs.\n\n" );
74 #--------------------------------------------------------------------
76 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
77 # convert, and write to the new table.
79 performanceLog( "Resetting timer.\n\n" );
80 $baseTime = getMicroTime();
81 print "Processing $numRows rows from links table...\n";
82 performanceLog( "Processing $numRows rows from links table...\n" );
83 performanceLog( "rows inserted vs seconds elapsed:\n" );
85 for ($rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval) {
86 $sqlRead = "SELECT * FROM links LIMIT $linksConvInsertInterval OFFSET $rowOffset";
87 $res = wfQuery($sqlRead, DB_READ);
89 $sqlWrite = array("INSERT INTO links_temp(l_from,l_to) VALUES ");
91 $sqlWrite = array("INSERT IGNORE INTO links_temp(l_from,l_to) VALUES ");
94 $tuplesAdded = 0; # no tuples added to INSERT yet
95 while ( $row = wfFetchObject($res) ) {
96 $fromTitle = $row->l_from;
97 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
98 $from = $ids[$fromTitle];
100 if ( $tuplesAdded != 0 ) {
103 $sqlWrite[] = "($from,$to)";
105 } else { # invalid title
110 #print "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n";
111 if ( $tuplesAdded != 0 ) {
112 if ($reportLinksConvProgress) {
113 print "Inserting $tuplesAdded tuples into links_temp...";
115 wfQuery( implode("",$sqlWrite) , DB_WRITE );
116 $totalTuplesInserted += $tuplesAdded;
117 if ($reportLinksConvProgress)
118 print " done. Total $totalTuplesInserted tuples inserted.\n";
119 performanceLog( $totalTuplesInserted . " " . (getMicroTime() - $baseTime) . "\n" );
122 print "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n";
123 performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
124 performanceLog( "Total execution time: " . (getMicroTime() - $startTime) . " seconds.\n" );
125 if ( $logPerformance ) { fclose ( $fh ); }
127 #--------------------------------------------------------------------
129 if ( $overwriteLinksTable ) {
130 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
131 if (!($dbConn->isOpen())) {
132 print "Opening connection to database failed.\n";
135 # Check for existing links_backup, and delete it if it exists.
136 print "Dropping backup links table if it exists...";
137 $dbConn->query( "DROP TABLE IF EXISTS links_backup", DB_WRITE);
140 # Swap in the new table, and move old links table to links_backup
141 print "Swapping tables 'links' to 'links_backup'; 'links_temp' to 'links'...";
142 $dbConn->query( "RENAME TABLE links TO links_backup, links_temp TO links", DB_WRITE );
146 print "Conversion complete. The old table remains at links_backup;\n";
147 print "delete at your leisure.\n";
149 print "Conversion complete. The converted table is at links_temp;\n";
150 print "the original links table is unchanged.\n";
153 #--------------------------------------------------------------------
155 function createTempTable() {
156 global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
158 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
160 if (!($dbConn->isOpen())) {
161 print "Opening connection to database failed.\n";
165 print "Dropping temporary links table if it exists...";
166 $dbConn->query( "DROP TABLE IF EXISTS links_temp", DB_WRITE);
169 print "Creating temporary links table...";
171 $dbConn->query( "CREATE TABLE links_temp ( " .
172 "l_from int(8) unsigned NOT NULL default '0', " .
173 "l_to int(8) unsigned NOT NULL default '0')", DB_WRITE);
175 $dbConn->query( "CREATE TABLE links_temp ( " .
176 "l_from int(8) unsigned NOT NULL default '0', " .
177 "l_to int(8) unsigned NOT NULL default '0', " .
178 "UNIQUE KEY l_from(l_from,l_to), " .
179 "KEY (l_to))", DB_WRITE);
184 function performanceLog( $text ) {
185 global $logPerformance, $fh;
186 if ( $logPerformance ) {
187 fwrite( $fh, $text );
191 function getMicroTime() { # return time in seconds, with microsecond accuracy
192 list($usec, $sec) = explode(" ", microtime());
193 return ((float)$usec + (float)$sec);