Fix syntax error from r55287
[lhc/web/wiklou.git] / maintenance / convertLinks.inc
index 3a25930..7c7b8af 100644 (file)
@@ -1,12 +1,24 @@
 <?php
+/**
+ * @file
+ * @todo document
+ * @ingroup Maintenance
+ */
 
+/** */
 function convertLinks() {
-       print "Converting links table to ID-ID...\n";
-       
+       global $wgDBtype;
+       if( $wgDBtype == 'postgres' ) {
+               wfOut( "Links table already ok on Postgres.\n" );
+               return;
+       }
+
+       wfOut( "Converting links table to ID-ID...\n" );
+
        global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
        global $noKeys, $logPerformance, $fh;
-       
-       $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
+
+       $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
        $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
 
        $reportCurReadProgress = true; #whether or not to give progress reports while reading IDs from cur table
@@ -31,32 +43,38 @@ function convertLinks() {
        $perfLogFilename = "convLinksPerf.txt";
        #--------------------------------------------------------------------
 
-       $res = wfQuery( "SELECT l_from FROM links LIMIT 1", DB_READ );
-       if ( mysql_field_type( $res, 0 ) == "int" ) {
-               print "Schema already converted\n";
+       $dbw = wfGetDB( DB_MASTER );
+       list ($cur, $links, $links_temp, $links_backup) = $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
+       
+       // Get database-agnostic limit clause
+       $sql_limit = $dbw->limitResult( "SELECT l_from FROM $links", 1 );
+       $res = $dbw->query(  $sql_limit );
+       if ( $dbw->fieldType( $res, 0 ) == "int" ) {
+               wfOut( "Schema already converted\n" );
                return;
        }
-       
-       $res = wfQuery( "SELECT COUNT(*) AS count FROM links", DB_WRITE );
-       $row = wfFetchObject($res);
+
+       $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
+       $row = $dbw->fetchObject($res);
        $numRows = $row->count;
-       wfFreeResult( $res );
+       $dbw->freeResult( $res );
 
        if ( $numRows == 0 ) {
-               print "Updating schema (no rows to convert)...\n";
+               wfOut( "Updating schema (no rows to convert)...\n" );
                createTempTable();
        } else {
                if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
                $baseTime = $startTime = getMicroTime();
                # Create a title -> cur_id map
-               print "Loading IDs from cur table...\n";
+               wfOut( "Loading IDs from $cur table...\n" );
                performanceLog ( "Reading $numRows rows from cur table...\n" );
                performanceLog ( "rows read vs seconds elapsed:\n" );
-               wfBufferSQLResults( false );
-               $res = wfQuery( "SELECT cur_namespace,cur_title,cur_id FROM cur", DB_WRITE );
+
+               $dbw->bufferResults( false );
+               $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
                $ids = array();
 
-               while ( $row = wfFetchObject( $res ) ) {
+               while ( $row = $dbw->fetchObject( $res ) ) {
                        $title = $row->cur_title;
                        if ( $row->cur_namespace ) {
                                $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
@@ -66,13 +84,13 @@ function convertLinks() {
                        if ($reportCurReadProgress) {
                                if (($curRowsRead % $curReadReportInterval) == 0) {
                                        performanceLog( $curRowsRead . " " . (getMicroTime() - $baseTime) . "\n" );
-                                       print "\t$curRowsRead rows of cur table read.\n";       
+                                       wfOut( "\t$curRowsRead rows of $cur table read.\n" );
                                }
                        }
                }
-               wfFreeResult( $res );
-               wfBufferSQLResults( true );
-               print "Finished loading IDs.\n\n";
+               $dbw->freeResult( $res );
+               $dbw->bufferResults( true );
+               wfOut( "Finished loading IDs.\n\n" );
                performanceLog( "Took " . (getMicroTime() - $baseTime) . " seconds to load IDs.\n\n" );
        #--------------------------------------------------------------------
 
@@ -81,22 +99,23 @@ function convertLinks() {
                createTempTable();
                performanceLog( "Resetting timer.\n\n" );
                $baseTime = getMicroTime();
-               print "Processing $numRows rows from links table...\n";
-               performanceLog( "Processing $numRows rows from links table...\n" );
+               wfOut( "Processing $numRows rows from $links table...\n" );
+               performanceLog( "Processing $numRows rows from $links table...\n" );
                performanceLog( "rows inserted vs seconds elapsed:\n" );
-               
+
                for ($rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval) {
-                       $sqlRead = "SELECT * FROM links ".wfLimitResult($linksConvInsertInterval,$rowOffset);
-                       $res = wfQuery($sqlRead, DB_READ);
+                       $sqlRead = "SELECT * FROM $links ";
+                       $sqlRead = $dbw->limitResult($sqlRead, $linksConvInsertInterval,$rowOffset);
+                       $res = $dbw->query($sqlRead);
                        if ( $noKeys ) {
-                               $sqlWrite = array("INSERT INTO links_temp(l_from,l_to) VALUES ");
+                               $sqlWrite = array("INSERT INTO $links_temp (l_from,l_to) VALUES ");
                        } else {
-                               $sqlWrite = array("INSERT IGNORE INTO links_temp(l_from,l_to) VALUES ");
+                               $sqlWrite = array("INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES ");
                        }
-                       
+
                        $tuplesAdded = 0; # no tuples added to INSERT yet
-                       while ( $row = wfFetchObject($res) ) {
-                               $fromTitle = $row->l_from; 
+                       while ( $row = $dbw->fetchObject($res) ) {
+                               $fromTitle = $row->l_from;
                                if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
                                        $from = $ids[$fromTitle];
                                        $to = $row->l_to;
@@ -104,25 +123,25 @@ function convertLinks() {
                                                $sqlWrite[] = ",";
                                        }
                                        $sqlWrite[] = "($from,$to)";
-                                       $tuplesAdded++;                         
+                                       $tuplesAdded++;
                                } else { # invalid title
                                        $numBadLinks++;
                                }
                        }
-                       wfFreeResult($res);
-                       #print "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n";
+                       $dbw->freeResult($res);
+                       #wfOut( "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n" );
                        if ( $tuplesAdded != 0  ) {
                                if ($reportLinksConvProgress) {
-                                       print "Inserting $tuplesAdded tuples into links_temp...";
+                                       wfOut( "Inserting $tuplesAdded tuples into $links_temp..." );
                                }
-                               wfQuery( implode("",$sqlWrite) , DB_WRITE );
+                               $dbw->query( implode("",$sqlWrite) );
                                $totalTuplesInserted += $tuplesAdded;
                                if ($reportLinksConvProgress)
-                                       print " done. Total $totalTuplesInserted tuples inserted.\n";
+                                       wfOut( " done. Total $totalTuplesInserted tuples inserted.\n" );
                                        performanceLog( $totalTuplesInserted . " " . (getMicroTime() - $baseTime) . "\n"  );
                        }
                }
-               print "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n";
+               wfOut( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n" );
                performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
                performanceLog( "Total execution time: " . (getMicroTime() - $startTime) . " seconds.\n" );
                if ( $logPerformance ) { fclose ( $fh ); }
@@ -132,25 +151,25 @@ function convertLinks() {
        if ( $overwriteLinksTable ) {
                $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
                if (!($dbConn->isOpen())) {
-                       print "Opening connection to database failed.\n";
+                       wfOut( "Opening connection to database failed.\n" );
                        return;
                }
                # Check for existing links_backup, and delete it if it exists.
-               print "Dropping backup links table if it exists...";
-               $dbConn->query( "DROP TABLE IF EXISTS links_backup", DB_WRITE);
-               print " done.\n";
-               
+               wfOut( "Dropping backup links table if it exists..." );
+               $dbConn->query( "DROP TABLE IF EXISTS $links_backup", DB_MASTER);
+               wfOut( " done.\n" );
+
                # Swap in the new table, and move old links table to links_backup
-               print "Swapping tables 'links' to 'links_backup'; 'links_temp' to 'links'...";
-               $dbConn->query( "RENAME TABLE links TO links_backup, links_temp TO links", DB_WRITE );
-               print " done.\n\n";
-               
+               wfOut( "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
+               $dbConn->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", DB_MASTER );
+               wfOut( " done.\n\n" );
+
                $dbConn->close();
-               print "Conversion complete. The old table remains at links_backup;\n";
-               print "delete at your leisure.\n";
+               wfOut( "Conversion complete. The old table remains at $links_backup;\n" );
+               wfOut( "delete at your leisure.\n" );
        } else {
-               print "Conversion complete.  The converted table is at links_temp;\n";
-               print "the original links table is unchanged.\n";
+               wfOut( "Conversion complete.  The converted table is at $links_temp;\n" );
+               wfOut( "the original links table is unchanged.\n" );
        }
 }
 
@@ -160,29 +179,30 @@ function createTempTable() {
        global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
        global $noKeys;
        $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
-       
+
        if (!($dbConn->isOpen())) {
-               print "Opening connection to database failed.\n";
+               wfOut( "Opening connection to database failed.\n" );
                return;
        }
-       
-       print "Dropping temporary links table if it exists...";
-       $dbConn->query( "DROP TABLE IF EXISTS links_temp", DB_WRITE);
-       print " done.\n";
-       
-       print "Creating temporary links table...";
+       $links_temp = $dbConn->tableName( 'links_temp' );
+
+       wfOut( "Dropping temporary links table if it exists..." );
+       $dbConn->query( "DROP TABLE IF EXISTS $links_temp");
+       wfOut( " done.\n" );
+
+       wfOut( "Creating temporary links table..." );
        if ( $noKeys ) {
-               $dbConn->query( "CREATE TABLE links_temp ( " .
+               $dbConn->query( "CREATE TABLE $links_temp ( " .
                "l_from int(8) unsigned NOT NULL default '0', " .
-               "l_to int(8) unsigned NOT NULL default '0')", DB_WRITE);
+               "l_to int(8) unsigned NOT NULL default '0')");
        } else {
-               $dbConn->query( "CREATE TABLE links_temp ( " .
+               $dbConn->query( "CREATE TABLE $links_temp ( " .
                "l_from int(8) unsigned NOT NULL default '0', " .
                "l_to int(8) unsigned NOT NULL default '0', " .
                "UNIQUE KEY l_from(l_from,l_to), " .
-               "KEY (l_to))", DB_WRITE);
+               "KEY (l_to))");
        }
-       print " done.\n\n";
+       wfOut( " done.\n\n" );
 }
 
 function performanceLog( $text ) {
@@ -196,7 +216,3 @@ function getMicroTime() { # return time in seconds, with microsecond accuracy
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
 }
-
-
-
-?>