Follow up r72799.
[lhc/web/wiklou.git] / maintenance / FiveUpgrade.inc
index c32f1b2..fda34b9 100644 (file)
@@ -1,7 +1,9 @@
 <?php
+/**
+ * @file
+ * @ingroup Maintenance
+ */
 
-require_once( 'cleanupDupes.inc' );
-require_once( 'userDupes.inc' );
 require_once( 'updaters.inc' );
 
 define( 'MW_UPGRADE_COPY',     false );
@@ -9,12 +11,16 @@ define( 'MW_UPGRADE_ENCODE',   true  );
 define( 'MW_UPGRADE_NULL',     null  );
 define( 'MW_UPGRADE_CALLBACK', null  ); // for self-documentation only
 
+/**
+ * @ingroup Maintenance
+ */
 class FiveUpgrade {
        function FiveUpgrade() {
                $this->conversionTables = $this->prepareWindows1252();
 
-               $this->dbw =& $this->newConnection();
-               $this->dbr =& $this->streamConnection();
+               $this->loadBalancers = array();
+               $this->dbw = wfGetDB( DB_MASTER );
+               $this->dbr = $this->streamConnection();
 
                $this->cleanupSwaps = array();
                $this->emailAuth = false; # don't preauthenticate emails
@@ -42,14 +48,14 @@ class FiveUpgrade {
                        'ipblocks',
                        'recentchanges',
                        'querycache' );
-               foreach( $tables as $table ) {
-                       if( $this->doing( $table ) ) {
+               foreach ( $tables as $table ) {
+                       if ( $this->doing( $table ) ) {
                                $method = 'upgrade' . ucfirst( $table );
                                $this->$method();
                        }
                }
 
-               if( $this->doing( 'cleanup' ) ) {
+               if ( $this->doing( 'cleanup' ) ) {
                        $this->upgradeCleanup();
                }
        }
@@ -60,12 +66,23 @@ class FiveUpgrade {
         * @return Database
         * @access private
         */
-       function &newConnection() {
-               global $wgDBadminuser, $wgDBadminpassword;
-               global $wgDBserver, $wgDBname;
-               $db = new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
+       function newConnection() {
+               $lb = wfGetLBFactory()->newMainLB();
+               $db = $lb->getConnection( DB_MASTER );
+               
+               $this->loadBalancers[] = $lb;
                return $db;
        }
+       
+       /**
+        * Commit transactions and close the connections when we're done...
+        */
+       function close() {
+               foreach ( $this->loadBalancers as $lb ) {
+                       $lb->commitMasterChanges();
+                       $lb->closeAll();
+               }
+       }
 
        /**
         * Open a second connection to the master server, with buffering off.
@@ -74,12 +91,16 @@ class FiveUpgrade {
         * @return Database
         * @access private
         */
-       function &streamConnection() {
+       function streamConnection() {
+               global $wgDBtype;
+
                $timeout = 3600 * 24;
-               $db =& $this->newConnection();
+               $db = $this->newConnection();
                $db->bufferResults( false );
-               $db->query( "SET net_read_timeout=$timeout" );
-               $db->query( "SET net_write_timeout=$timeout" );
+               if ( $wgDBtype == 'mysql' ) {
+                       $db->query( "SET net_read_timeout=$timeout" );
+                       $db->query( "SET net_write_timeout=$timeout" );
+               }
                return $db;
        }
 
@@ -96,41 +117,41 @@ class FiveUpgrade {
                # Mappings from:
                # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
                static $cp1252 = array(
-                       0x80 => 0x20AC, #EURO SIGN
-                       0x81 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
-                       0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
-                       0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
-                       0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
-                       0x85 => 0x2026, #HORIZONTAL ELLIPSIS
-                       0x86 => 0x2020, #DAGGER
-                       0x87 => 0x2021, #DOUBLE DAGGER
-                       0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
-                       0x89 => 0x2030, #PER MILLE SIGN
-                       0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
-                       0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
-                       0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
-                       0x8D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
-                       0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
-                       0x8F => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
-                       0x90 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
-                       0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
-                       0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
-                       0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
-                       0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
-                       0x95 => 0x2022, #BULLET
-                       0x96 => 0x2013, #EN DASH
-                       0x97 => 0x2014, #EM DASH
-                       0x98 => 0x02DC, #SMALL TILDE
-                       0x99 => 0x2122, #TRADE MARK SIGN
-                       0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
-                       0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
-                       0x9C => 0x0153, #LATIN SMALL LIGATURE OE
-                       0x9D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
-                       0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
-                       0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
+                       0x80 => 0x20AC, # EURO SIGN
+                       0x81 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
+                       0x82 => 0x201A, # SINGLE LOW-9 QUOTATION MARK
+                       0x83 => 0x0192, # LATIN SMALL LETTER F WITH HOOK
+                       0x84 => 0x201E, # DOUBLE LOW-9 QUOTATION MARK
+                       0x85 => 0x2026, # HORIZONTAL ELLIPSIS
+                       0x86 => 0x2020, # DAGGER
+                       0x87 => 0x2021, # DOUBLE DAGGER
+                       0x88 => 0x02C6, # MODIFIER LETTER CIRCUMFLEX ACCENT
+                       0x89 => 0x2030, # PER MILLE SIGN
+                       0x8A => 0x0160, # LATIN CAPITAL LETTER S WITH CARON
+                       0x8B => 0x2039, # SINGLE LEFT-POINTING ANGLE QUOTATION MARK
+                       0x8C => 0x0152, # LATIN CAPITAL LIGATURE OE
+                       0x8D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
+                       0x8E => 0x017D, # LATIN CAPITAL LETTER Z WITH CARON
+                       0x8F => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
+                       0x90 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
+                       0x91 => 0x2018, # LEFT SINGLE QUOTATION MARK
+                       0x92 => 0x2019, # RIGHT SINGLE QUOTATION MARK
+                       0x93 => 0x201C, # LEFT DOUBLE QUOTATION MARK
+                       0x94 => 0x201D, # RIGHT DOUBLE QUOTATION MARK
+                       0x95 => 0x2022, # BULLET
+                       0x96 => 0x2013, # EN DASH
+                       0x97 => 0x2014, # EM DASH
+                       0x98 => 0x02DC, # SMALL TILDE
+                       0x99 => 0x2122, # TRADE MARK SIGN
+                       0x9A => 0x0161, # LATIN SMALL LETTER S WITH CARON
+                       0x9B => 0x203A, # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
+                       0x9C => 0x0153, # LATIN SMALL LIGATURE OE
+                       0x9D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
+                       0x9E => 0x017E, # LATIN SMALL LETTER Z WITH CARON
+                       0x9F => 0x0178, # LATIN CAPITAL LETTER Y WITH DIAERESIS
                        );
                $pairs = array();
-               for( $i = 0; $i < 0x100; $i++ ) {
+               for ( $i = 0; $i < 0x100; $i++ ) {
                        $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
                        $pairs[chr( $i )] = codepointToUtf8( $unicode );
                }
@@ -198,7 +219,7 @@ class FiveUpgrade {
         * @access private
         */
        function addChunk( &$chunk, $key = null ) {
-               if( count( $chunk ) >= $this->chunkSize ) {
+               if ( count( $chunk ) >= $this->chunkSize ) {
                        $this->insertChunk( $chunk );
 
                        $this->chunkCount += count( $chunk );
@@ -206,7 +227,7 @@ class FiveUpgrade {
                        $delta = $now - $this->chunkStartTime;
                        $rate = $this->chunkCount / $delta;
 
-                       if( is_null( $key ) ) {
+                       if ( is_null( $key ) ) {
                                $completed = $this->chunkCount;
                        } else {
                                $completed = $key;
@@ -237,7 +258,7 @@ class FiveUpgrade {
         */
        function lastChunk( &$chunk ) {
                $n = count( $chunk );
-               if( $n > 0 ) {
+               if ( $n > 0 ) {
                        $this->insertChunk( $chunk );
                }
                $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
@@ -293,27 +314,26 @@ class FiveUpgrade {
                        $fname );
 
                $add = array();
-               while( $row = $this->dbr->fetchObject( $result ) ) {
+               while ( $row = $this->dbr->fetchObject( $result ) ) {
                        $copy = array();
-                       foreach( $fields as $field => $source ) {
-                               if( $source === MW_UPGRADE_COPY ) {
+                       foreach ( $fields as $field => $source ) {
+                               if ( $source === MW_UPGRADE_COPY ) {
                                        $copy[$field] = $row->$field;
-                               } elseif( $source === MW_UPGRADE_ENCODE ) {
+                               } elseif ( $source === MW_UPGRADE_ENCODE ) {
                                        $copy[$field] = $this->conv( $row->$field );
-                               } elseif( $source === MW_UPGRADE_NULL ) {
+                               } elseif ( $source === MW_UPGRADE_NULL ) {
                                        $copy[$field] = null;
                                } else {
                                        $this->log( "Unknown field copy type: $field => $source" );
                                }
                        }
-                       if( is_callable( $callback ) ) {
+                       if ( is_callable( $callback ) ) {
                                $copy = call_user_func( $callback, $row, $copy );
                        }
                        $add[] = $copy;
                        $this->addChunk( $add );
                }
                $this->lastChunk( $add );
-               $this->dbr->freeResult( $result );
 
                $this->log( "Done converting $name." );
                $this->cleanupSwaps[] = $name;
@@ -323,20 +343,20 @@ class FiveUpgrade {
                $fname = "FiveUpgrade::upgradePage";
                $chunksize = 100;
 
-               if( $this->dbw->tableExists( 'page' ) ) {
+               if ( $this->dbw->tableExists( 'page' ) ) {
                        $this->log( 'Page table already exists; aborting.' );
                        die( -1 );
                }
 
                $this->log( "Checking cur table for unique title index and applying if necessary" );
-               checkDupes( true );
+               $this->checkDupes();
 
                $this->log( "...converting from cur/old to page/revision/text DB structure." );
 
-               list ($cur, $old, $page, $revision, $text) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
+               list ( $cur, $old, $page, $revision, $text ) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
 
                $this->log( "Creating page and revision tables..." );
-               $this->dbw->query("CREATE TABLE $page (
+               $this->dbw->query( "CREATE TABLE $page (
                        page_id int(8) unsigned NOT NULL auto_increment,
                        page_namespace int NOT NULL,
                        page_title varchar(255) binary NOT NULL,
@@ -354,7 +374,7 @@ class FiveUpgrade {
                        INDEX (page_random),
                        INDEX (page_len)
                        ) TYPE=InnoDB", $fname );
-               $this->dbw->query("CREATE TABLE $revision (
+               $this->dbw->query( "CREATE TABLE $revision (
                        rev_id int(8) unsigned NOT NULL auto_increment,
                        rev_page int(8) unsigned NOT NULL,
                        rev_text_id int(8) unsigned NOT NULL,
@@ -377,7 +397,7 @@ class FiveUpgrade {
                $this->log( "Last old record is {$maxold}" );
 
                global $wgLegacySchemaConversion;
-               if( $wgLegacySchemaConversion ) {
+               if ( $wgLegacySchemaConversion ) {
                        // Create HistoryBlobCurStub entries.
                        // Text will be pulled from the leftover 'cur' table at runtime.
                        echo "......Moving metadata from cur; using blob references to text in cur table.\n";
@@ -407,7 +427,7 @@ class FiveUpgrade {
                        FROM $cur
                        ORDER BY cur_id", $fname );
                $add = array();
-               while( $row = $this->dbr->fetchObject( $result ) ) {
+               while ( $row = $this->dbr->fetchObject( $result ) ) {
                        $add[] = array(
                                'old_namespace'  => $row->cur_namespace,
                                'old_title'      => $row->cur_title,
@@ -421,15 +441,14 @@ class FiveUpgrade {
                        $this->addChunk( $add, $row->cur_id );
                }
                $this->lastChunk( $add );
-               $this->dbr->freeResult( $result );
 
                /**
                 * Copy revision metadata from old into revision.
                 * We'll also do UTF-8 conversion of usernames and comments.
                 */
-               #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
-               #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
-               #$countold = $this->dbw->selectField( 'old', 'count(old_id)', '', $fname );
+               # $newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
+               # $this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
+               # $countold = $this->dbw->selectField( 'old', 'count(old_id)', '', $fname );
                $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
                $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
 
@@ -441,7 +460,7 @@ class FiveUpgrade {
                        $fname );
 
                $add = array();
-               while( $row = $this->dbr->fetchObject( $result ) ) {
+               while ( $row = $this->dbr->fetchObject( $result ) ) {
                        $add[] = array(
                                'rev_id'         =>              $row->old_id,
                                'rev_page'       =>              $row->cur_id,
@@ -454,7 +473,6 @@ class FiveUpgrade {
                        $this->addChunk( $add );
                }
                $this->lastChunk( $add );
-               $this->dbr->freeResult( $result );
 
 
                /**
@@ -470,7 +488,7 @@ class FiveUpgrade {
                        WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
                        ORDER BY cur_id", $fname );
                $add = array();
-               while( $row = $this->dbr->fetchObject( $result ) ) {
+               while ( $row = $this->dbr->fetchObject( $result ) ) {
                        $add[] = array(
                                'page_id'           =>              $row->cur_id,
                                'page_namespace'    =>              $row->cur_namespace,
@@ -483,11 +501,10 @@ class FiveUpgrade {
                                'page_touched'      =>              $this->dbw->timestamp(),
                                'page_latest'       =>              $row->rev_id,
                                'page_len'          =>              $row->len );
-                       #$this->addChunk( $add, $row->cur_id );
+                       # $this->addChunk( $add, $row->cur_id );
                        $this->addChunk( $add );
                }
                $this->lastChunk( $add );
-               $this->dbr->freeResult( $result );
 
                $this->log( "...done with cur/old -> page/revision." );
        }
@@ -495,15 +512,15 @@ class FiveUpgrade {
        function upgradeLinks() {
                $fname = 'FiveUpgrade::upgradeLinks';
                $chunksize = 200;
-               list ($links, $brokenlinks, $pagelinks, $cur) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
+               list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
 
                $this->log( 'Checking for interwiki table change in case of bogus items...' );
-               if( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
+               if ( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
                        $this->log( 'interwiki has iw_trans.' );
                } else {
                        global $IP;
                        $this->log( 'adding iw_trans...' );
-                       dbsource( $IP . '/maintenance/archives/patch-interwiki-trans.sql', $this->dbw );
+                       $this->dbw->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
                        $this->log( 'added iw_trans.' );
                }
 
@@ -527,14 +544,14 @@ CREATE TABLE $pagelinks (
 
                $this->log( 'Importing live links -> pagelinks' );
                $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
-               if( $nlinks ) {
+               if ( $nlinks ) {
                        $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
                        $result = $this->dbr->query( "
                          SELECT l_from,cur_namespace,cur_title
                                FROM $links, $cur
                                WHERE l_to=cur_id", $fname );
                        $add = array();
-                       while( $row = $this->dbr->fetchObject( $result ) ) {
+                       while ( $row = $this->dbr->fetchObject( $result ) ) {
                                $add[] = array(
                                        'pl_from'      =>              $row->l_from,
                                        'pl_namespace' =>              $row->cur_namespace,
@@ -548,16 +565,16 @@ CREATE TABLE $pagelinks (
 
                $this->log( 'Importing brokenlinks -> pagelinks' );
                $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', $fname );
-               if( $nbrokenlinks ) {
+               if ( $nbrokenlinks ) {
                        $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
                        $result = $this->dbr->query(
                                "SELECT bl_from, bl_to FROM $brokenlinks",
                                $fname );
                        $add = array();
-                       while( $row = $this->dbr->fetchObject( $result ) ) {
+                       while ( $row = $this->dbr->fetchObject( $result ) ) {
                                $pagename = $this->conv( $row->bl_to );
                                $title = Title::newFromText( $pagename );
-                               if( is_null( $title ) ) {
+                               if ( is_null( $title ) ) {
                                        $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
                                } else {
                                        $add[] = array(
@@ -578,11 +595,11 @@ CREATE TABLE $pagelinks (
        function upgradeUser() {
                // Apply unique index, if necessary:
                $duper = new UserDupes( $this->dbw );
-               if( $duper->hasUniqueIndex() ) {
+               if ( $duper->hasUniqueIndex() ) {
                        $this->log( "Already have unique user_name index." );
                } else {
                        $this->log( "Clearing user duplicates..." );
-                       if( !$duper->clearDupes() ) {
+                       if ( !$duper->clearDupes() ) {
                                $this->log( "WARNING: Duplicate user accounts, may explode!" );
                        }
                }
@@ -674,7 +691,7 @@ END;
 
        function imageCallback( $row, $copy ) {
                global $options;
-               if( !isset( $options['noimage'] ) ) {
+               if ( !isset( $options['noimage'] ) ) {
                        // Fill in the new image info fields
                        $info = $this->imageInfo( $row->img_name );
 
@@ -693,10 +710,7 @@ END;
                return $copy;
        }
 
-       function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
-               if( is_null( $basename ) ) $basename = $name;
-               $dir = call_user_func( $subdirCallback, $basename );
-               $filename = $dir . '/' . $name;
+       function imageInfo( $filename ) {
                $info = array(
                        'width'  => 0,
                        'height' => 0,
@@ -705,26 +719,19 @@ END;
                        'major'  => '',
                        'minor'  => '' );
 
-               $magic =& wfGetMimeMagic();
+               $magic = MimeMagic::singleton();
                $mime = $magic->guessMimeType( $filename, true );
                list( $info['major'], $info['minor'] ) = explode( '/', $mime );
 
                $info['media'] = $magic->getMediaType( $filename, $mime );
 
-               # Height and width
-               $gis = false;
-               if( $mime == 'image/svg' ) {
-                       $gis = wfGetSVGsize( $filename );
-               } elseif( $magic->isPHPImageType( $mime ) ) {
-                       $gis = getimagesize( $filename );
-               } else {
-                       $this->log( "Surprising mime type: $mime" );
-               }
-               if( $gis ) {
-                       $info['width' ] = $gis[0];
-                       $info['height'] = $gis[1];
-               }
-               if( isset( $gis['bits'] ) ) {
+               $image = UnregisteredLocalFile::newFromPath( $filename, $mime );
+
+               $info['width'] = $image->getWidth();
+               $info['height'] = $image->getHeight();
+
+               $gis = $image->getImageSize( $filename );
+               if ( isset( $gis['bits'] ) ) {
                        $info['bits'] = $gis['bits'];
                }
 
@@ -750,22 +757,22 @@ END;
         * @param string $basename pre-conversion base filename for dir hashing, if an archive
         * @access private
         */
-       function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
+       function renameFile( $oldname, $subdirCallback = 'wfImageDir', $basename = null ) {
                $newname = $this->conv( $oldname );
-               if( $newname == $oldname ) {
+               if ( $newname == $oldname ) {
                        // No need to rename; another field triggered this row.
                        return false;
                }
 
-               if( is_null( $basename ) ) $basename = $oldname;
+               if ( is_null( $basename ) ) $basename = $oldname;
                $ubasename = $this->conv( $basename );
                $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
                $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
 
                $this->log( "$oldpath -> $newpath" );
-               if( rename( $oldpath, $newpath ) ) {
+               if ( rename( $oldpath, $newpath ) ) {
                        $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
-                       if( !symlink( $relpath, $oldpath ) ) {
+                       if ( !symlink( $relpath, $oldpath ) ) {
                                $this->log( "... symlink failed!" );
                        }
                        return $newname;
@@ -816,7 +823,7 @@ END;
 
        function oldimageCallback( $row, $copy ) {
                global $options;
-               if( !isset( $options['noimage'] ) ) {
+               if ( !isset( $options['noimage'] ) ) {
                        // Fill in the new image info fields
                        $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
                        $copy['oi_width' ] = $info['width' ];
@@ -835,7 +842,7 @@ END;
                $fname = 'FiveUpgrade::upgradeWatchlist';
                $chunksize = 100;
 
-               list ($watchlist, $watchlist_temp) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
+               list ( $watchlist, $watchlist_temp ) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
 
                $this->log( 'Migrating watchlist table to watchlist_temp...' );
                $this->dbw->query(
@@ -873,30 +880,29 @@ END;
                        $fname );
 
                $add = array();
-               while( $row = $this->dbr->fetchObject( $result ) ) {
+               while ( $row = $this->dbr->fetchObject( $result ) ) {
                        $add[] = array(
-                               'wl_user'      =>                        $row->wl_user,
-                               'wl_namespace' => Namespace::getSubject( $row->wl_namespace ),
-                               'wl_title'     =>           $this->conv( $row->wl_title ),
-                               'wl_notificationtimestamp' =>            '0' );
+                               'wl_user'      =>                          $row->wl_user,
+                               'wl_namespace' => MWNamespace::getSubject( $row->wl_namespace ),
+                               'wl_title'     =>             $this->conv( $row->wl_title ),
+                               'wl_notificationtimestamp' =>              '0' );
                        $this->addChunk( $add );
 
                        $add[] = array(
-                               'wl_user'      =>                        $row->wl_user,
-                               'wl_namespace' =>    Namespace::getTalk( $row->wl_namespace ),
-                               'wl_title'     =>           $this->conv( $row->wl_title ),
-                               'wl_notificationtimestamp' =>            '0' );
+                               'wl_user'      =>                          $row->wl_user,
+                               'wl_namespace' =>    MWNamespace::getTalk( $row->wl_namespace ),
+                               'wl_title'     =>             $this->conv( $row->wl_title ),
+                               'wl_notificationtimestamp' =>              '0' );
                        $this->addChunk( $add );
                }
                $this->lastChunk( $add );
-               $this->dbr->freeResult( $result );
 
                $this->log( 'Done converting watchlist.' );
                $this->cleanupSwaps[] = 'watchlist';
        }
 
        function upgradeLogging() {
-               $tabledef = <<<END
+               $tabledef = <<<ENDS
 CREATE TABLE $1 (
   -- Symbolic keys for the general log type and the action type
   -- within the log. The output format will be controlled by the
@@ -926,7 +932,7 @@ CREATE TABLE $1 (
   KEY page_time (log_namespace, log_title, log_timestamp)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                $fields = array(
                        'log_type'      => MW_UPGRADE_COPY,
                        'log_action'    => MW_UPGRADE_COPY,
@@ -940,7 +946,7 @@ END;
        }
 
        function upgradeArchive() {
-               $tabledef = <<<END
+               $tabledef = <<<ENDS
 CREATE TABLE $1 (
   ar_namespace int NOT NULL default '0',
   ar_title varchar(255) binary NOT NULL default '',
@@ -960,7 +966,7 @@ CREATE TABLE $1 (
   KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                $fields = array(
                        'ar_namespace'  => MW_UPGRADE_COPY,
                        'ar_title'      => MW_UPGRADE_ENCODE,
@@ -978,22 +984,22 @@ END;
 
        function upgradeImagelinks() {
                global $wgUseLatin1;
-               if( $wgUseLatin1 ) {
-                       $tabledef = <<<END
+               if ( $wgUseLatin1 ) {
+                       $tabledef = <<<ENDS
 CREATE TABLE $1 (
   -- Key to page_id of the page containing the image / media link.
   il_from int(8) unsigned NOT NULL default '0',
 
   -- Filename of target image.
   -- This is also the page_title of the file's description page;
-  -- all such pages are in namespace 6 (NS_IMAGE).
+  -- all such pages are in namespace 6 (NS_FILE).
   il_to varchar(255) binary NOT NULL default '',
 
   UNIQUE KEY il_from(il_from,il_to),
   KEY (il_to)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                        $fields = array(
                                'il_from' => MW_UPGRADE_COPY,
                                'il_to'   => MW_UPGRADE_ENCODE );
@@ -1003,8 +1009,8 @@ END;
 
        function upgradeCategorylinks() {
                global $wgUseLatin1;
-               if( $wgUseLatin1 ) {
-                       $tabledef = <<<END
+               if ( $wgUseLatin1 ) {
+                       $tabledef = <<<ENDS
 CREATE TABLE $1 (
   cl_from int(8) unsigned NOT NULL default '0',
   cl_to varchar(255) binary NOT NULL default '',
@@ -1015,7 +1021,7 @@ CREATE TABLE $1 (
   KEY cl_sortkey(cl_to,cl_sortkey),
   KEY cl_timestamp(cl_to,cl_timestamp)
 ) TYPE=InnoDB
-END;
+ENDS;
                        $fields = array(
                                'cl_from'      => MW_UPGRADE_COPY,
                                'cl_to'        => MW_UPGRADE_ENCODE,
@@ -1027,8 +1033,8 @@ END;
 
        function upgradeIpblocks() {
                global $wgUseLatin1;
-               if( $wgUseLatin1 ) {
-                       $tabledef = <<<END
+               if ( $wgUseLatin1 ) {
+                       $tabledef = <<<ENDS
 CREATE TABLE $1 (
   ipb_id int(8) NOT NULL auto_increment,
   ipb_address varchar(40) binary NOT NULL default '',
@@ -1044,7 +1050,7 @@ CREATE TABLE $1 (
   INDEX ipb_user (ipb_user)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                        $fields = array(
                                'ipb_id'        => MW_UPGRADE_COPY,
                                'ipb_address'   => MW_UPGRADE_COPY,
@@ -1060,7 +1066,7 @@ END;
 
        function upgradeRecentchanges() {
                // There's a format change in the namespace field
-               $tabledef = <<<END
+               $tabledef = <<<ENDS
 CREATE TABLE $1 (
   rc_id int(8) NOT NULL auto_increment,
   rc_timestamp varchar(14) binary NOT NULL default '',
@@ -1098,7 +1104,7 @@ CREATE TABLE $1 (
   INDEX rc_ip (rc_ip)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                $fields = array(
                        'rc_id'             => MW_UPGRADE_COPY,
                        'rc_timestamp'      => MW_UPGRADE_COPY,
@@ -1124,7 +1130,7 @@ END;
 
        function upgradeQuerycache() {
                // There's a format change in the namespace field
-               $tabledef = <<<END
+               $tabledef = <<<ENDS
 CREATE TABLE $1 (
   -- A key name, generally the base name of of the special page.
   qc_type char(32) NOT NULL,
@@ -1139,7 +1145,7 @@ CREATE TABLE $1 (
   KEY (qc_type,qc_value)
 
 ) TYPE=InnoDB
-END;
+ENDS;
                $fields = array(
                        'qc_type'      => MW_UPGRADE_COPY,
                        'qc_value'     => MW_UPGRADE_COPY,
@@ -1148,6 +1154,101 @@ END;
                $this->copyTable( 'querycache', $tabledef, $fields );
        }
 
+       /**
+        * Check for duplicate rows in "cur" table and move duplicates entries in
+        * "old" table.
+        *
+        * This was in cleanupDupes.inc before.
+        */
+       function checkDupes() {
+               $dbw = wfGetDB( DB_MASTER );
+               if ( $dbw->indexExists( 'cur', 'name_title' ) &&
+                       $dbw->indexUnique( 'cur', 'name_title' ) ) {
+                       echo wfWikiID() . ": cur table has the current unique index; no duplicate entries.\n";
+                       return;
+               } elseif ( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
+                       echo wfWikiID() . ": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
+                       return;
+               }
+
+               echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
+
+               $dbw = wfGetDB( DB_MASTER );
+               $cur = $dbw->tableName( 'cur' );
+               $old = $dbw->tableName( 'old' );
+               $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
+               echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
+               $res = $dbw->query( <<<END
+SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
+  FROM $cur
+ GROUP BY cur_namespace,cur_title
+HAVING c > 1
+END
+               );
+               $n = $dbw->numRows( $res );
+               echo "Found $n titles with duplicate entries.\n";
+               if ( $n > 0 ) {
+                       echo "Correcting...\n";
+                       while ( $row = $dbw->fetchObject( $res ) ) {
+                               $ns = intval( $row->cur_namespace );
+                               $title = $dbw->addQuotes( $row->cur_title );
+
+                               # Get the first responding ID; that'll be the one we keep.
+                               $id = $dbw->selectField( 'cur', 'cur_id', array(
+                                       'cur_namespace' => $row->cur_namespace,
+                                       'cur_title'     => $row->cur_title ) );
+
+                               echo "$ns:$row->cur_title (canonical ID $id)\n";
+                               if ( $id != $row->id ) {
+                                       echo "  ** minimum ID $row->id; ";
+                                       $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
+                                               'cur_id' => $row->id ) );
+                                       $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
+                                               'cur_id' => $id ) );
+                                       if ( $timeMin == $timeFirst ) {
+                                               echo "timestamps match at $timeFirst; ok\n";
+                                       } else {
+                                               echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
+                                               if ( $timeMin > $timeFirst ) {
+                                                       $id = $row->id;
+                                                       echo "keeping minimum: $id\n";
+                                               } else {
+                                                       echo "keeping first: $id\n";
+                                               }
+                                       }
+                               }
+
+                               $dbw->query( <<<END
+INSERT
+  INTO $old
+      (old_namespace, old_title,      old_text,
+       old_comment,   old_user,       old_user_text,
+       old_timestamp, old_minor_edit, old_flags,
+       inverse_timestamp)
+SELECT cur_namespace, cur_title,      cur_text,
+       cur_comment,   cur_user,       cur_user_text,
+       cur_timestamp, cur_minor_edit, '',
+       inverse_timestamp
+  FROM $cur
+ WHERE cur_namespace=$ns
+   AND cur_title=$title
+   AND cur_id != $id
+END
+                               );
+                               $dbw->query( <<<END
+DELETE
+  FROM $cur
+ WHERE cur_namespace=$ns
+   AND cur_title=$title
+   AND cur_id != $id
+END
+                                       );
+                       }
+               }
+               $dbw->query( 'UNLOCK TABLES' );
+               echo "Done.\n";
+       }
+
        /**
         * Rename all our temporary tables into final place.
         * We've left things in place so a read-only wiki can continue running
@@ -1156,7 +1257,7 @@ END;
        function upgradeCleanup() {
                $this->renameTable( 'old', 'text' );
 
-               foreach( $this->cleanupSwaps as $table ) {
+               foreach ( $this->cleanupSwaps as $table ) {
                        $this->swap( $table );
                }
        }
@@ -1175,5 +1276,3 @@ END;
        }
 
 }
-
-?>