Merge "changes: Avoid use of deprecated method Revision::userCanBitfield()"
[lhc/web/wiklou.git] / maintenance / storage / trackBlobs.php
index 2dd48ca..d9793b4 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 use MediaWiki\MediaWikiServices;
+use MediaWiki\Revision\SlotRecord;
 use Wikimedia\Rdbms\DBConnectionError;
 
 require __DIR__ . '/../commandLine.inc';
@@ -130,6 +131,8 @@ class TrackBlobs {
         *  Scan the revision table for rows stored in the specified clusters
         */
        function trackRevisions() {
+               global $wgMultiContentRevisionSchemaMigrationStage;
+
                $dbw = wfGetDB( DB_MASTER );
                $dbr = wfGetDB( DB_REPLICA );
 
@@ -141,20 +144,40 @@ class TrackBlobs {
 
                echo "Finding revisions...\n";
 
+               $fields = [ 'rev_id', 'rev_page', 'old_id', 'old_flags', 'old_text' ];
+               $options = [
+                       'ORDER BY' => 'rev_id',
+                       'LIMIT' => $this->batchSize
+               ];
+               $conds = [
+                       $textClause,
+                       'old_flags ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
+               ];
+               if ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_READ_OLD ) {
+                       $tables = [ 'revision', 'text' ];
+                       $conds = array_merge( [
+                               'rev_text_id=old_id',
+                       ], $conds );
+               } else {
+                       $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
+                       $tables = [ 'revision', 'slots', 'content', 'text' ];
+                       $conds = array_merge( [
+                               'rev_id=slot_revision_id',
+                               'slot_role_id=' . $slotRoleStore->getId( SlotRecord::MAIN ),
+                               'content_id=slot_content_id',
+                               'SUBSTRING(content_address, 1, 3)=' . $dbr->addQuotes( 'tt:' ),
+                               'SUBSTRING(content_address, 4)=old_id',
+                       ], $conds );
+               }
+
                while ( true ) {
-                       $res = $dbr->select( [ 'revision', 'text' ],
-                               [ 'rev_id', 'rev_page', 'old_id', 'old_flags', 'old_text' ],
-                               [
+                       $res = $dbr->select( $tables,
+                               $fields,
+                               array_merge( [
                                        'rev_id > ' . $dbr->addQuotes( $startId ),
-                                       'rev_text_id=old_id',
-                                       $textClause,
-                                       'old_flags ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
-                               ],
+                               ], $conds ),
                                __METHOD__,
-                               [
-                                       'ORDER BY' => 'rev_id',
-                                       'LIMIT' => $this->batchSize
-                               ]
+                               $options
                        );
                        if ( !$res->numRows() ) {
                                break;
@@ -209,7 +232,7 @@ class TrackBlobs {
                $pos = $dbw->getMasterPos();
                $dbr->masterPosWait( $pos, 100000 );
 
-               $textClause = $this->getTextClause( $this->clusters );
+               $textClause = $this->getTextClause();
                $startId = 0;
                $endId = $dbr->selectField( 'text', 'MAX(old_id)', '', __METHOD__ );
                $rowsInserted = 0;
@@ -302,9 +325,9 @@ class TrackBlobs {
                        $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                        $lb = $lbFactory->getExternalLB( $cluster );
                        try {
-                               $extDB = $lb->getConnection( DB_REPLICA );
+                               $extDB = $lb->getMaintenanceConnectionRef( DB_REPLICA );
                        } catch ( DBConnectionError $e ) {
-                               if ( strpos( $e->error, 'Unknown database' ) !== false ) {
+                               if ( strpos( $e->getMessage(), 'Unknown database' ) !== false ) {
                                        echo "No database on $cluster\n";
                                } else {
                                        echo "Error on $cluster: " . $e->getMessage() . "\n";
@@ -339,8 +362,8 @@ class TrackBlobs {
 
                                foreach ( $res as $row ) {
                                        gmp_setbit( $actualBlobs, $row->blob_id );
+                                       $startId = $row->blob_id;
                                }
-                               $startId = $row->blob_id;
 
                                ++$batchesDone;
                                if ( $batchesDone >= $this->reportingInterval ) {