registration: Have wfLoadExtension() (and similar) use the queue
[lhc/web/wiklou.git] / maintenance / convertLinks.php
index 6e6df24..221ebe3 100644 (file)
@@ -36,14 +36,29 @@ class ConvertLinks extends Maintenance {
 
        public function __construct() {
                parent::__construct();
-               $this->mDescription = "Convert from the old links schema (string->ID) to the new schema (ID->ID).
-The wiki should be put into read-only mode while this script executes";
+               $this->mDescription =
+                       "Convert from the old links schema (string->ID) to the new schema (ID->ID)."
+                               . "The wiki should be put into read-only mode while this script executes";
 
                $this->addArg( 'logperformance', "Log performance to perfLogFilename.", false );
-               $this->addArg( 'perfLogFilename', "Filename where performance is logged if --logperformance was set (defaults to 'convLinksPerf.txt').", false );
-               $this->addArg( 'keep-links-table', "Don't overwrite the old links table with the new one, leave the new table at links_temp.", false );
-               $this->addArg( 'nokeys', "Don't create keys, and so allow duplicates in the new links table.\n
-This gives a huge speed improvement for very large links tables which are MyISAM." /* (What about InnoDB?) */, false );
+               $this->addArg(
+                       'perfLogFilename',
+                       "Filename where performance is logged if --logperformance was set "
+                               . "(defaults to 'convLinksPerf.txt').",
+                       false
+               );
+               $this->addArg(
+                       'keep-links-table',
+                       "Don't overwrite the old links table with the new one, leave the new table at links_temp.",
+                       false
+               );
+               $this->addArg(
+                       'nokeys',
+                       /* (What about InnoDB?) */
+                       "Don't create keys, and so allow duplicates in the new links table.\n"
+                               . "This gives a huge speed improvement for very large links tables which are MyISAM.",
+                       false
+               );
        }
 
        public function getDbType() {
@@ -56,22 +71,34 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                $type = $dbw->getType();
                if ( $type != 'mysql' ) {
                        $this->output( "Link table conversion not necessary for $type\n" );
+
                        return;
                }
 
                global $wgContLang;
 
-               $numBadLinks = $curRowsRead = 0; # counters etc
-               $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
+               # counters etc
+               $numBadLinks = $curRowsRead = 0;
+
+               # total tuples INSERTed into links_temp
+               $totalTuplesInserted = 0;
+
+               # whether or not to give progress reports while reading IDs from cur table
+               $reportCurReadProgress = true;
 
-               $reportCurReadProgress = true; # whether or not to give progress reports while reading IDs from cur table
-               $curReadReportInterval = 1000; # number of rows between progress reports
+               # number of rows between progress reports
+               $curReadReportInterval = 1000;
 
-               $reportLinksConvProgress = true; # whether or not to give progress reports during conversion
-               $linksConvInsertInterval = 1000; # number of rows per INSERT
+               # whether or not to give progress reports during conversion
+               $reportLinksConvProgress = true;
+
+               # number of rows per INSERT
+               $linksConvInsertInterval = 1000;
 
                $initialRowOffset = 0;
-               # $finalRowOffset = 0; # not used yet; highest row number from links table to process
+
+               # not used yet; highest row number from links table to process
+               # $finalRowOffset = 0;
 
                $overwriteLinksTable = !$this->hasOption( 'keep-links-table' );
                $noKeys = $this->hasOption( 'noKeys' );
@@ -80,16 +107,19 @@ This gives a huge speed improvement for very large links tables which are MyISAM
 
                # --------------------------------------------------------------------
 
-               list( $cur, $links, $links_temp, $links_backup ) = $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
+               list( $cur, $links, $links_temp, $links_backup ) =
+                       $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
 
                if ( $dbw->tableExists( 'pagelinks' ) ) {
                        $this->output( "...have pagelinks; skipping old links table updates\n" );
+
                        return;
                }
 
                $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
                if ( $dbw->fieldType( $res, 0 ) == "int" ) {
                        $this->output( "Schema already converted\n" );
+
                        return;
                }
 
@@ -104,17 +134,17 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                } else {
                        $fh = false;
                        if ( $this->logPerformance ) {
-                               $fh = fopen ( $perfLogFilename, "w" );
+                               $fh = fopen( $perfLogFilename, "w" );
                                if ( !$fh ) {
                                        $this->error( "Couldn't open $perfLogFilename" );
                                        $this->logPerformance = false;
                                }
                        }
-                       $baseTime = $startTime = $this->getMicroTime();
+                       $baseTime = $startTime = microtime( true );
                        # Create a title -> cur_id map
                        $this->output( "Loading IDs from $cur table...\n" );
-                       $this->performanceLog ( $fh, "Reading $numRows rows from cur table...\n" );
-                       $this->performanceLog ( $fh, "rows read vs seconds elapsed:\n" );
+                       $this->performanceLog( $fh, "Reading $numRows rows from cur table...\n" );
+                       $this->performanceLog( $fh, "rows read vs seconds elapsed:\n" );
 
                        $dbw->bufferResults( false );
                        $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
@@ -129,7 +159,10 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                                $curRowsRead++;
                                if ( $reportCurReadProgress ) {
                                        if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
-                                               $this->performanceLog( $fh, $curRowsRead . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
+                                               $this->performanceLog(
+                                                       $fh,
+                                                       $curRowsRead . " " . ( microtime( true ) - $baseTime ) . "\n"
+                                               );
                                                $this->output( "\t$curRowsRead rows of $cur table read.\n" );
                                        }
                                }
@@ -137,7 +170,10 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                        $dbw->freeResult( $res );
                        $dbw->bufferResults( true );
                        $this->output( "Finished loading IDs.\n\n" );
-                       $this->performanceLog( $fh, "Took " . ( $this->getMicroTime() - $baseTime ) . " seconds to load IDs.\n\n" );
+                       $this->performanceLog(
+                               $fh,
+                               "Took " . ( microtime( true ) - $baseTime ) . " seconds to load IDs.\n\n"
+                       );
 
                        # --------------------------------------------------------------------
 
@@ -145,12 +181,14 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                        # convert, and write to the new table.
                        $this->createTempTable();
                        $this->performanceLog( $fh, "Resetting timer.\n\n" );
-                       $baseTime = $this->getMicroTime();
+                       $baseTime = microtime( true );
                        $this->output( "Processing $numRows rows from $links table...\n" );
                        $this->performanceLog( $fh, "Processing $numRows rows from $links table...\n" );
                        $this->performanceLog( $fh, "rows inserted vs seconds elapsed:\n" );
 
-                       for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval ) {
+                       for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows;
+                               $rowOffset += $linksConvInsertInterval
+                       ) {
                                $sqlRead = "SELECT * FROM $links ";
                                $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
                                $res = $dbw->query( $sqlRead );
@@ -176,7 +214,8 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                                        }
                                }
                                $dbw->freeResult( $res );
-                               # $this->output( "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n" );
+                               # $this->output( "rowOffset: $rowOffset\ttuplesAdded: "
+                               #       . "$tuplesAdded\tnumBadLinks: $numBadLinks\n" );
                                if ( $tuplesAdded != 0 ) {
                                        if ( $reportLinksConvProgress ) {
                                                $this->output( "Inserting $tuplesAdded tuples into $links_temp..." );
@@ -185,15 +224,25 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                                        $totalTuplesInserted += $tuplesAdded;
                                        if ( $reportLinksConvProgress ) {
                                                $this->output( " done. Total $totalTuplesInserted tuples inserted.\n" );
-                                               $this->performanceLog( $fh, $totalTuplesInserted . " " . ( $this->getMicroTime() - $baseTime ) . "\n" );
+                                               $this->performanceLog(
+                                                       $fh,
+                                                       $totalTuplesInserted . " " . ( microtime( true ) - $baseTime ) . "\n"
+                                               );
                                        }
                                }
                        }
-                       $this->output( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n" );
-                       $this->performanceLog( $fh, "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
-                       $this->performanceLog( $fh, "Total execution time: " . ( $this->getMicroTime() - $startTime ) . " seconds.\n" );
+                       $this->output( "$totalTuplesInserted valid titles and "
+                               . "$numBadLinks invalid titles were processed.\n\n" );
+                       $this->performanceLog(
+                               $fh,
+                               "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n"
+                       );
+                       $this->performanceLog(
+                               $fh,
+                               "Total execution time: " . ( microtime( true ) - $startTime ) . " seconds.\n"
+                       );
                        if ( $this->logPerformance ) {
-                               fclose ( $fh );
+                               fclose( $fh );
                        }
                }
                # --------------------------------------------------------------------
@@ -222,6 +271,7 @@ This gives a huge speed improvement for very large links tables which are MyISAM
 
                if ( !( $dbConn->isOpen() ) ) {
                        $this->output( "Opening connection to database failed.\n" );
+
                        return;
                }
                $links_temp = $dbConn->tableName( 'links_temp' );
@@ -233,14 +283,14 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                $this->output( "Creating temporary links table..." );
                if ( $this->hasOption( 'noKeys' ) ) {
                        $dbConn->query( "CREATE TABLE $links_temp ( " .
-                       "l_from int(8) unsigned NOT NULL default '0', " .
-                       "l_to int(8) unsigned NOT NULL default '0')" );
+                               "l_from int(8) unsigned NOT NULL default '0', " .
+                               "l_to int(8) unsigned NOT NULL default '0')" );
                } else {
                        $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))" );
+                               "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))" );
                }
                $this->output( " done.\n\n" );
        }
@@ -250,11 +300,6 @@ This gives a huge speed improvement for very large links tables which are MyISAM
                        fwrite( $fh, $text );
                }
        }
-
-       private function getMicroTime() { # return time in seconds, with microsecond accuracy
-               list( $usec, $sec ) = explode( " ", microtime() );
-               return ( (float)$usec + (float)$sec );
-       }
 }
 
 $maintClass = "ConvertLinks";