installer: Fix warningBox arguments in WebInstallerRestart
[lhc/web/wiklou.git] / maintenance / convertLinks.php
index c3ad46a..26f8182 100644 (file)
@@ -21,6 +21,8 @@
  * @ingroup Maintenance
  */
 
+use MediaWiki\MediaWikiServices;
+
 require_once __DIR__ . '/Maintenance.php';
 
 /**
@@ -36,9 +38,9 @@ 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->addDescription(
+                       '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(
@@ -66,7 +68,7 @@ class ConvertLinks extends Maintenance {
        }
 
        public function execute() {
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getDB( DB_MASTER );
 
                $type = $dbw->getType();
                if ( $type != 'mysql' ) {
@@ -75,8 +77,6 @@ class ConvertLinks extends Maintenance {
                        return;
                }
 
-               global $wgContLang;
-
                # counters etc
                $numBadLinks = $curRowsRead = 0;
 
@@ -100,10 +100,10 @@ class ConvertLinks extends Maintenance {
                # not used yet; highest row number from links table to process
                # $finalRowOffset = 0;
 
+               $this->logPerformance = $this->hasOption( 'logperformance' );
+               $perfLogFilename = $this->getArg( 1, "convLinksPerf.txt" );
                $overwriteLinksTable = !$this->hasOption( 'keep-links-table' );
                $noKeys = $this->hasOption( 'noKeys' );
-               $this->logPerformance = $this->hasOption( 'logperformance' );
-               $perfLogFilename = $this->getArg( 'perfLogFilename', "convLinksPerf.txt" );
 
                # --------------------------------------------------------------------
 
@@ -117,6 +117,7 @@ class ConvertLinks extends Maintenance {
                }
 
                $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
+               // @phan-suppress-next-line PhanUndeclaredMethod
                if ( $dbw->fieldType( $res, 0 ) == "int" ) {
                        $this->output( "Schema already converted\n" );
 
@@ -126,7 +127,6 @@ class ConvertLinks extends Maintenance {
                $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
                $row = $dbw->fetchObject( $res );
                $numRows = $row->count;
-               $dbw->freeResult( $res );
 
                if ( $numRows == 0 ) {
                        $this->output( "Updating schema (no rows to convert)...\n" );
@@ -145,30 +145,34 @@ class ConvertLinks extends Maintenance {
                        $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" );
-
-                       $dbw->bufferResults( false );
-                       $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
-                       $ids = array();
-
-                       foreach ( $res as $row ) {
-                               $title = $row->cur_title;
-                               if ( $row->cur_namespace ) {
-                                       $title = $wgContLang->getNsText( $row->cur_namespace ) . ":$title";
-                               }
-                               $ids[$title] = $row->cur_id;
-                               $curRowsRead++;
-                               if ( $reportCurReadProgress ) {
-                                       if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
-                                               $this->performanceLog(
-                                                       $fh,
-                                                       $curRowsRead . " " . ( microtime( true ) - $baseTime ) . "\n"
-                                               );
-                                               $this->output( "\t$curRowsRead rows of $cur table read.\n" );
+                       $contentLang = MediaWikiServices::getInstance()->getContentLanguage();
+
+                       $ids = [];
+                       $lastId = 0;
+                       do {
+                               $res = $dbw->query(
+                                       "SELECT cur_namespace,cur_title,cur_id FROM $cur " .
+                                       "WHERE cur_id > $lastId ORDER BY cur_id LIMIT 10000"
+                               );
+                               foreach ( $res as $row ) {
+                                       $title = $row->cur_title;
+                                       if ( $row->cur_namespace ) {
+                                               $title = $contentLang->getNsText( $row->cur_namespace ) . ":$title";
                                        }
+                                       $ids[$title] = $row->cur_id;
+                                       $curRowsRead++;
+                                       if ( $reportCurReadProgress ) {
+                                               if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
+                                                       $this->performanceLog(
+                                                               $fh,
+                                                               $curRowsRead . " " . ( microtime( true ) - $baseTime ) . "\n"
+                                                       );
+                                                       $this->output( "\t$curRowsRead rows of $cur table read.\n" );
+                                               }
+                                       }
+                                       $lastId = $row->cur_id;
                                }
-                       }
-                       $dbw->freeResult( $res );
-                       $dbw->bufferResults( true );
+                       } while ( $res->numRows() > 0 );
                        $this->output( "Finished loading IDs.\n\n" );
                        $this->performanceLog(
                                $fh,
@@ -193,9 +197,9 @@ class ConvertLinks extends Maintenance {
                                $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
                                $res = $dbw->query( $sqlRead );
                                if ( $noKeys ) {
-                                       $sqlWrite = array( "INSERT INTO $links_temp (l_from,l_to) VALUES " );
+                                       $sqlWrite = [ "INSERT INTO $links_temp (l_from,l_to) VALUES " ];
                                } else {
-                                       $sqlWrite = array( "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " );
+                                       $sqlWrite = [ "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " ];
                                }
 
                                $tuplesAdded = 0; # no tuples added to INSERT yet
@@ -213,7 +217,6 @@ class ConvertLinks extends Maintenance {
                                                $numBadLinks++;
                                        }
                                }
-                               $dbw->freeResult( $res );
                                # $this->output( "rowOffset: $rowOffset\ttuplesAdded: "
                                #       . "$tuplesAdded\tnumBadLinks: $numBadLinks\n" );
                                if ( $tuplesAdded != 0 ) {
@@ -267,7 +270,7 @@ class ConvertLinks extends Maintenance {
        }
 
        private function createTempTable() {
-               $dbConn = wfGetDB( DB_MASTER );
+               $dbConn = $this->getDB( DB_MASTER );
 
                if ( !( $dbConn->isOpen() ) ) {
                        $this->output( "Opening connection to database failed.\n" );
@@ -302,5 +305,5 @@ class ConvertLinks extends Maintenance {
        }
 }
 
-$maintClass = "ConvertLinks";
+$maintClass = ConvertLinks::class;
 require_once RUN_MAINTENANCE_IF_MAIN;