Merge "Introduce new schema flags and use them in RevisionStore."
[lhc/web/wiklou.git] / includes / Storage / RevisionStore.php
index 3f852b0..119af1d 100644 (file)
@@ -83,6 +83,7 @@ class RevisionStore
 
        /**
         * @var boolean
+        * @see $wgContentHandlerUseDB
         */
        private $contentHandlerUseDB = true;
 
@@ -121,7 +122,7 @@ class RevisionStore
         */
        private $slotRoleStore;
 
-       /** @var int One of the MIGRATION_* constants */
+       /** @var int An appropriate combination of SCHEMA_COMPAT_XXX flags. */
        private $mcrMigrationStage;
 
        /**
@@ -133,9 +134,11 @@ class RevisionStore
         * @param CommentStore $commentStore
         * @param NameTableStore $contentModelStore
         * @param NameTableStore $slotRoleStore
-        * @param int $migrationStage
+        * @param int $mcrMigrationStage An appropriate combination of SCHEMA_COMPAT_XXX flags
         * @param ActorMigration $actorMigration
         * @param bool|string $wikiId
+        *
+        * @throws MWException if $mcrMigrationStage or $wikiId is invalid.
         */
        public function __construct(
                LoadBalancer $loadBalancer,
@@ -144,16 +147,39 @@ class RevisionStore
                CommentStore $commentStore,
                NameTableStore $contentModelStore,
                NameTableStore $slotRoleStore,
-               $migrationStage,
+               $mcrMigrationStage,
                ActorMigration $actorMigration,
                $wikiId = false
        ) {
                Assert::parameterType( 'string|boolean', $wikiId, '$wikiId' );
-               Assert::parameterType( 'integer', $migrationStage, '$migrationStage' );
-
-               if ( $migrationStage > MIGRATION_WRITE_BOTH ) {
-                       throw new InvalidArgumentException( 'New schema is not fully supported yet' );
-               }
+               Assert::parameterType( 'integer', $mcrMigrationStage, '$mcrMigrationStage' );
+               Assert::parameter(
+                       ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH ) !== SCHEMA_COMPAT_READ_BOTH,
+                       '$mcrMigrationStage',
+                       'Reading from the old and the new schema at the same time is not supported.'
+               );
+               Assert::parameter(
+                       ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH ) !== 0,
+                       '$mcrMigrationStage',
+                       'Reading needs to be enabled for the old or the new schema.'
+               );
+               Assert::parameter(
+                       ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_BOTH ) !== 0,
+                       '$mcrMigrationStage',
+                       'Writing needs to be enabled for the old or the new schema.'
+               );
+               Assert::parameter(
+                       ( $mcrMigrationStage & SCHEMA_COMPAT_READ_OLD ) === 0
+                       || ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) !== 0,
+                       '$mcrMigrationStage',
+                       'Cannot read the old schema when not also writing it.'
+               );
+               Assert::parameter(
+                       ( $mcrMigrationStage & SCHEMA_COMPAT_READ_NEW ) === 0
+                       || ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) !== 0,
+                       '$mcrMigrationStage',
+                       'Cannot read the new schema when not also writing it.'
+               );
 
                $this->loadBalancer = $loadBalancer;
                $this->blobStore = $blobStore;
@@ -161,12 +187,21 @@ class RevisionStore
                $this->commentStore = $commentStore;
                $this->contentModelStore = $contentModelStore;
                $this->slotRoleStore = $slotRoleStore;
-               $this->mcrMigrationStage = $migrationStage;
+               $this->mcrMigrationStage = $mcrMigrationStage;
                $this->actorMigration = $actorMigration;
                $this->wikiId = $wikiId;
                $this->logger = new NullLogger();
        }
 
+       /**
+        * @param int $flags A combination of SCHEMA_COMPAT_XXX flags.
+        * @return bool True if all the given flags were set in the $mcrMigrationStage
+        *         parameter passed to the constructor.
+        */
+       private function hasMcrSchemaFlags( $flags ) {
+               return ( $this->mcrMigrationStage & $flags ) === $flags;
+       }
+
        public function setLogger( LoggerInterface $logger ) {
                $this->logger = $logger;
        }
@@ -186,14 +221,19 @@ class RevisionStore
        }
 
        /**
+        * @see $wgContentHandlerUseDB
         * @param bool $contentHandlerUseDB
         * @throws MWException
         */
        public function setContentHandlerUseDB( $contentHandlerUseDB ) {
-               if ( !$contentHandlerUseDB && $this->mcrMigrationStage > MIGRATION_OLD ) {
-                       throw new MWException(
-                               'Content model must be stored in the database for multi content revision migration.'
-                       );
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW )
+                       || $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW )
+               ) {
+                       if ( !$contentHandlerUseDB ) {
+                               throw new MWException(
+                                       'Content model must be stored in the database for multi content revision migration.'
+                               );
+                       }
                }
                $this->contentHandlerUseDB = $contentHandlerUseDB;
        }
@@ -365,15 +405,31 @@ class RevisionStore
                // TODO: pass in a DBTransactionContext instead of a database connection.
                $this->checkDatabaseWikiId( $dbw );
 
-               if ( !$rev->getSlotRoles() ) {
-                       throw new InvalidArgumentException( 'At least one slot needs to be defined!' );
+               $slotRoles = $rev->getSlotRoles();
+
+               // Make sure the main slot is always provided throughout migration
+               if ( !in_array( 'main', $slotRoles ) ) {
+                       throw new InvalidArgumentException(
+                               'main slot must be provided'
+                       );
                }
 
-               // RevisionStore currently only supports writing a single slot
-               if ( $rev->getSlotRoles() !== [ 'main' ] ) {
-                       throw new InvalidArgumentException( 'Only the main slot is supported for now!' );
+               // While inserting into the old schema make sure only the main slot is allowed.
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD ) && $slotRoles !== [ 'main' ] ) {
+                       throw new InvalidArgumentException(
+                               'Only the main slot is supported when writing to the pre-MCR schema!'
+                       );
                }
 
+               // Checks
+               $this->failOnNull( $rev->getSize(), 'size field' );
+               $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
+               $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
+               $comment = $this->failOnNull( $rev->getComment( RevisionRecord::RAW ), 'comment' );
+               $user = $this->failOnNull( $rev->getUser( RevisionRecord::RAW ), 'user' );
+               $this->failOnNull( $user->getId(), 'user field' );
+               $this->failOnEmpty( $user->getName(), 'user_text field' );
+
                // TODO: we shouldn't need an actual Title here.
                $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
                $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
@@ -382,46 +438,147 @@ class RevisionStore
                        ? $this->getPreviousRevisionId( $dbw, $rev )
                        : $rev->getParentId();
 
-               // Record the text (or external storage URL) to the blob store
-               $mainSlot = $rev->getSlot( 'main', RevisionRecord::RAW );
+               /** @var RevisionRecord $rev */
+               $rev = $dbw->doAtomicSection(
+                       __METHOD__,
+                       function ( IDatabase $dbw, $fname ) use (
+                               $rev,
+                               $user,
+                               $comment,
+                               $title,
+                               $pageId,
+                               $parentId
+                       ) {
+                               return $this->insertRevisionInternal(
+                                       $rev,
+                                       $dbw,
+                                       $user,
+                                       $comment,
+                                       $title,
+                                       $pageId,
+                                       $parentId
+                               );
+                       }
+               );
 
-               $size = $this->failOnNull( $rev->getSize(), 'size field' );
-               $sha1 = $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
+               // sanity checks
+               Assert::postcondition( $rev->getId() > 0, 'revision must have an ID' );
+               Assert::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
+               Assert::postcondition(
+                       $rev->getComment( RevisionRecord::RAW ) !== null,
+                       'revision must have a comment'
+               );
+               Assert::postcondition(
+                       $rev->getUser( RevisionRecord::RAW ) !== null,
+                       'revision must have a user'
+               );
 
-               $dbw->startAtomic( __METHOD__ );
+               // Trigger exception if the main slot is missing.
+               // Technically, this could go away after MCR migration: while
+               // calling code may require a main slot to exist, RevisionStore
+               // really should not know or care about that requirement.
+               $rev->getSlot( 'main', RevisionRecord::RAW );
+
+               foreach ( $slotRoles as $role ) {
+                       $slot = $rev->getSlot( $role, RevisionRecord::RAW );
+                       Assert::postcondition(
+                               $slot->getContent() !== null,
+                               $role .  ' slot must have content'
+                       );
+                       Assert::postcondition(
+                               $slot->hasRevision(),
+                               $role .  ' slot must have a revision associated'
+                       );
+               }
 
-               if ( !$mainSlot->hasAddress() ) {
-                       $content = $mainSlot->getContent();
-                       $format = $content->getDefaultFormat();
-                       $model = $content->getModel();
+               Hooks::run( 'RevisionRecordInserted', [ $rev ] );
 
-                       $this->checkContentModel( $content, $title );
+               // TODO: deprecate in 1.32!
+               $legacyRevision = new Revision( $rev );
+               Hooks::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
 
-                       $data = $content->serialize( $format );
+               return $rev;
+       }
 
-                       // Hints allow the blob store to optimize by "leaking" application level information to it.
-                       // TODO: with the new MCR storage schema, we rev_id have this before storing the blobs.
-                       // When we have it, add rev_id as a hint. Can be used with rev_parent_id for
-                       // differential storage or compression of subsequent revisions.
-                       $blobHints = [
-                               BlobStore::DESIGNATION_HINT => 'page-content', // BlobStore may be used for other things too.
-                               BlobStore::PAGE_HINT => $pageId,
-                               BlobStore::ROLE_HINT => $mainSlot->getRole(),
-                               BlobStore::PARENT_HINT => $parentId,
-                               BlobStore::SHA1_HINT => $mainSlot->getSha1(),
-                               BlobStore::MODEL_HINT => $model,
-                               BlobStore::FORMAT_HINT => $format,
-                       ];
+       private function insertRevisionInternal(
+               RevisionRecord $rev,
+               IDatabase $dbw,
+               User $user,
+               CommentStoreComment $comment,
+               Title $title,
+               $pageId,
+               $parentId
+       ) {
+               $slotRoles = $rev->getSlotRoles();
 
-                       $blobAddress = $this->blobStore->storeBlob( $data, $blobHints );
-               } else {
-                       $blobAddress = $mainSlot->getAddress();
-                       $model = $mainSlot->getModel();
-                       $format = $mainSlot->getFormat();
+               $revisionRow = $this->insertRevisionRowOn(
+                       $dbw,
+                       $rev,
+                       $title,
+                       $parentId
+               );
+
+               $revisionId = $revisionRow['rev_id'];
+
+               $blobHints = [
+                       BlobStore::PAGE_HINT => $pageId,
+                       BlobStore::REVISION_HINT => $revisionId,
+                       BlobStore::PARENT_HINT => $parentId,
+               ];
+
+               $newSlots = [];
+               foreach ( $slotRoles as $role ) {
+                       $slot = $rev->getSlot( $role, RevisionRecord::RAW );
+
+                       if ( $slot->hasRevision() ) {
+                               // If the SlotRecord already has a revision ID set, this means it already exists
+                               // in the database, and should already belong to the current revision.
+                               // TODO: properly abort transaction if the assertion fails!
+                               Assert::parameter(
+                                       $slot->getRevision() === $revisionId,
+                                       'slot role ' . $slot->getRole(),
+                                       'Existing slot should belong to revision '
+                                       . $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!'
+                               );
+
+                               // Slot exists, nothing to do, move along.
+                               // This happens when restoring archived revisions.
+
+                               $newSlots[$role] = $slot;
+
+                               // Write the main slot's text ID to the revision table for backwards compatibility
+                               if ( $slot->getRole() === 'main'
+                                       && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD )
+                               ) {
+                                       $blobAddress = $slot->getAddress();
+                                       $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
+                               }
+                       } else {
+                               $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $title, $blobHints );
+                       }
                }
 
-               $textId = $this->blobStore->getTextIdFromAddress( $blobAddress );
+               $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
+
+               $rev = new RevisionStoreRecord(
+                       $title,
+                       $user,
+                       $comment,
+                       (object)$revisionRow,
+                       new RevisionSlots( $newSlots ),
+                       $this->wikiId
+               );
+
+               return $rev;
+       }
 
+       /**
+        * @param IDatabase $dbw
+        * @param int $revisionId
+        * @param string &$blobAddress (may change!)
+        */
+       private function updateRevisionTextId( IDatabase $dbw, $revisionId, &$blobAddress ) {
+               $textId = $this->blobStore->getTextIdFromAddress( $blobAddress );
                if ( !$textId ) {
                        throw new LogicException(
                                'Blob address not supported in 1.29 database schema: ' . $blobAddress
@@ -432,158 +589,220 @@ class RevisionStore
                // may be a new value, not anything already contained in $blobAddress.
                $blobAddress = SqlBlobStore::makeAddressFromTextId( $textId );
 
-               $comment = $this->failOnNull( $rev->getComment( RevisionRecord::RAW ), 'comment' );
-               $user = $this->failOnNull( $rev->getUser( RevisionRecord::RAW ), 'user' );
-               $timestamp = $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
-
-               // Checks.
-               $this->failOnNull( $user->getId(), 'user field' );
-               $this->failOnEmpty( $user->getName(), 'user_text field' );
-
-               // Record the edit in revisions
-               $revisionRow = [
-                       'rev_page'       => $pageId,
-                       'rev_parent_id'  => $parentId,
-                       'rev_minor_edit' => $rev->isMinor() ? 1 : 0,
-                       'rev_timestamp'  => $dbw->timestamp( $timestamp ),
-                       'rev_deleted'    => $rev->getVisibility(),
-                       'rev_len'        => $size,
-                       'rev_sha1'       => $sha1,
-               ];
-
-               if ( $rev->getId() !== null ) {
-                       // Needed to restore revisions with their original ID
-                       $revisionRow['rev_id'] = $rev->getId();
-               }
-
-               list( $commentFields, $commentCallback ) =
-                       $this->commentStore->insertWithTempTable( $dbw, 'rev_comment', $comment );
-               $revisionRow += $commentFields;
-
-               list( $actorFields, $actorCallback ) =
-                       $this->actorMigration->getInsertValuesWithTempTable( $dbw, 'rev_user', $user );
-               $revisionRow += $actorFields;
-
-               if ( $this->mcrMigrationStage <= MIGRATION_WRITE_BOTH ) {
-                       $revisionRow['rev_text_id'] = $textId;
-
-                       // MCR migration note: rev_content_model and rev_content_format will go away
-                       if ( $this->contentHandlerUseDB ) {
-                               $defaultModel = ContentHandler::getDefaultModelFor( $title );
-                               $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
+               $dbw->update(
+                       'revision',
+                       [ 'rev_text_id' => $textId ],
+                       [ 'rev_id' => $revisionId ],
+                       __METHOD__
+               );
+       }
 
-                               $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
-                               $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
-                       }
+       /**
+        * @param IDatabase $dbw
+        * @param int $revisionId
+        * @param SlotRecord $protoSlot
+        * @param Title $title
+        * @param array $blobHints See the BlobStore::XXX_HINT constants
+        * @return SlotRecord
+        */
+       private function insertSlotOn(
+               IDatabase $dbw,
+               $revisionId,
+               SlotRecord $protoSlot,
+               Title $title,
+               array $blobHints = []
+       ) {
+               if ( $protoSlot->hasAddress() ) {
+                       $blobAddress = $protoSlot->getAddress();
                } else {
-                       /**
-                        * rev_text_id has NOT NULL and no DEFAULT, so set to 0 when we are not writing to it.
-                        * WARNING: This should NOT be removed after migration until a schema change has been
-                        * made in WMF production giving rev_text_id a DEFAULT value of 0 (otherwise inserts
-                        * will fail)
-                        * Task: https://phabricator.wikimedia.org/T190148#4064625
-                        */
-                       $revisionRow['rev_text_id'] = 0;
+                       $blobAddress = $this->storeContentBlob( $protoSlot, $title, $blobHints );
                }
 
-               $dbw->insert( 'revision', $revisionRow, __METHOD__ );
-
-               $hasSlots = false;
-               $contentId = false;
+               // Write the main slot's text ID to the revision table for backwards compatibility
+               if ( $protoSlot->getRole() === 'main'
+                       && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD )
+               ) {
+                       $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
+               }
 
-               if ( isset( $revisionRow['rev_id'] ) ) {
-                       // Restoring a revision, slots should already exist,
-                       // unless the archive row wasn't migrated yet.
-                       if ( $this->mcrMigrationStage === MIGRATION_NEW ) {
-                               $hasSlots = true;
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
+                       if ( $protoSlot->hasContentId() ) {
+                               $contentId = $protoSlot->getContentId();
                        } else {
-                               $contentId = $this->findSlotContentId( $dbw, $revisionRow['rev_id'], 'main' );
-                               $hasSlots = (bool)$contentId;
+                               $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
                        }
-               } else {
-                       // not restoring a revision, use auto-increment value
-                       $revisionRow['rev_id'] = intval( $dbw->insertId() );
-               }
 
-               if ( $this->mcrMigrationStage > MIGRATION_OLD && $mainSlot->hasContentId() ) {
-                       // re-use content row of inherited slot!
-                       $contentId = $mainSlot->getContentId();
+                       $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
+               } else {
+                       $contentId = null;
                }
 
-               $revisionId = $revisionRow['rev_id'];
+               $savedSlot = SlotRecord::newSaved(
+                       $revisionId,
+                       $contentId,
+                       $blobAddress,
+                       $protoSlot
+               );
 
-               $commentCallback( $revisionId );
-               $actorCallback( $revisionId, $revisionRow );
+               return $savedSlot;
+       }
 
-               // Insert IP revision into ip_changes for use when querying for a range.
+       /**
+        * Insert IP revision into ip_changes for use when querying for a range.
+        * @param IDatabase $dbw
+        * @param User $user
+        * @param RevisionRecord $rev
+        * @param int $revisionId
+        */
+       private function insertIpChangesRow(
+               IDatabase $dbw,
+               User $user,
+               RevisionRecord $rev,
+               $revisionId
+       ) {
                if ( $user->getId() === 0 && IP::isValid( $user->getName() ) ) {
                        $ipcRow = [
                                'ipc_rev_id'        => $revisionId,
-                               'ipc_rev_timestamp' => $revisionRow['rev_timestamp'],
+                               'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
                                'ipc_hex'           => IP::toHex( $user->getName() ),
                        ];
                        $dbw->insert( 'ip_changes', $ipcRow, __METHOD__ );
                }
+       }
 
-               if ( $this->mcrMigrationStage >= MIGRATION_WRITE_BOTH ) {
+       /**
+        * @param IDatabase $dbw
+        * @param RevisionRecord $rev
+        * @param Title $title
+        * @param int $parentId
+        *
+        * @return array a revision table row
+        *
+        * @throws MWException
+        * @throws MWUnknownContentModelException
+        */
+       private function insertRevisionRowOn(
+               IDatabase $dbw,
+               RevisionRecord $rev,
+               Title $title,
+               $parentId
+       ) {
+               $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $title, $parentId );
 
-                       // Only insert slot rows for new revisions (not restored revisions).
-                       // Also, never insert content rows if not inserting slot rows.
-                       if ( !$hasSlots ) {
+               list( $commentFields, $commentCallback ) =
+                       $this->commentStore->insertWithTempTable(
+                               $dbw,
+                               'rev_comment',
+                               $rev->getComment( RevisionRecord::RAW )
+                       );
+               $revisionRow += $commentFields;
 
-                               // Only insert content rows for new content (not inherited content)
-                               if ( !$contentId ) {
-                                       Assert::invariant( !$hasSlots, 'Re-using slots, but not content ID is known' );
-                                       $contentId = $this->insertContentRowOn( $mainSlot, $dbw, $blobAddress );
-                               }
+               list( $actorFields, $actorCallback ) =
+                       $this->actorMigration->getInsertValuesWithTempTable(
+                               $dbw,
+                               'rev_user',
+                               $rev->getUser( RevisionRecord::RAW )
+                       );
+               $revisionRow += $actorFields;
 
-                               $this->insertSlotRowOn( $mainSlot, $dbw, $revisionId, $contentId );
-                       }
-               } else {
-                       $contentId = null;
+               $dbw->insert( 'revision', $revisionRow, __METHOD__ );
+
+               if ( !isset( $revisionRow['rev_id'] ) ) {
+                       // only if auto-increment was used
+                       $revisionRow['rev_id'] = intval( $dbw->insertId() );
                }
 
-               $dbw->endAtomic( __METHOD__ );
+               $commentCallback( $revisionRow['rev_id'] );
+               $actorCallback( $revisionRow['rev_id'], $revisionRow );
 
-               $newSlot = SlotRecord::newSaved( $revisionId, $contentId, $blobAddress, $mainSlot );
-               $slots = new RevisionSlots( [ 'main' => $newSlot ] );
+               return $revisionRow;
+       }
 
-               $rev = new RevisionStoreRecord(
-                       $title,
-                       $user,
-                       $comment,
-                       (object)$revisionRow,
-                       $slots,
-                       $this->wikiId
-               );
+       /**
+        * @param IDatabase $dbw
+        * @param RevisionRecord $rev
+        * @param Title $title
+        * @param int $parentId
+        *
+        * @return array [ 0 => array $revisionRow, 1 => callable  ]
+        * @throws MWException
+        * @throws MWUnknownContentModelException
+        */
+       private function getBaseRevisionRow(
+               IDatabase $dbw,
+               RevisionRecord $rev,
+               Title $title,
+               $parentId
+       ) {
+               // Record the edit in revisions
+               $revisionRow = [
+                       'rev_page'       => $rev->getPageId(),
+                       'rev_parent_id'  => $parentId,
+                       'rev_minor_edit' => $rev->isMinor() ? 1 : 0,
+                       'rev_timestamp'  => $dbw->timestamp( $rev->getTimestamp() ),
+                       'rev_deleted'    => $rev->getVisibility(),
+                       'rev_len'        => $rev->getSize(),
+                       'rev_sha1'       => $rev->getSha1(),
+               ];
 
-               $newSlot = $rev->getSlot( 'main', RevisionRecord::RAW );
+               if ( $rev->getId() !== null ) {
+                       // Needed to restore revisions with their original ID
+                       $revisionRow['rev_id'] = $rev->getId();
+               }
 
-               // sanity checks
-               Assert::postcondition( $rev->getId() > 0, 'revision must have an ID' );
-               Assert::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
-               Assert::postcondition(
-                       $rev->getComment( RevisionRecord::RAW ) !== null,
-                       'revision must have a comment'
-               );
-               Assert::postcondition(
-                       $rev->getUser( RevisionRecord::RAW ) !== null,
-                       'revision must have a user'
-               );
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD ) ) {
+                       // In non MCR mode this IF section will relate to the main slot
+                       $mainSlot = $rev->getSlot( 'main' );
+                       $model = $mainSlot->getModel();
+                       $format = $mainSlot->getFormat();
 
-               Assert::postcondition( $newSlot !== null, 'revision must have a main slot' );
-               Assert::postcondition(
-                       $newSlot->getAddress() !== null,
-                       'main slot must have an address'
-               );
+                       // MCR migration note: rev_content_model and rev_content_format will go away
+                       if ( $this->contentHandlerUseDB ) {
+                               $defaultModel = ContentHandler::getDefaultModelFor( $title );
+                               $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
 
-               Hooks::run( 'RevisionRecordInserted', [ $rev ] );
+                               $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
+                               $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
+                       }
+               }
 
-               // TODO: deprecate in 1.32!
-               $legacyRevision = new Revision( $rev );
-               Hooks::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
+               return $revisionRow;
+       }
 
-               return $rev;
+       /**
+        * @param SlotRecord $slot
+        * @param Title $title
+        * @param array $blobHints See the BlobStore::XXX_HINT constants
+        *
+        * @throws MWException
+        * @return string the blob address
+        */
+       private function storeContentBlob(
+               SlotRecord $slot,
+               Title $title,
+               array $blobHints = []
+       ) {
+               $content = $slot->getContent();
+               $format = $content->getDefaultFormat();
+               $model = $content->getModel();
+
+               $this->checkContent( $content, $title );
+
+               return $this->blobStore->storeBlob(
+                       $content->serialize( $format ),
+                       // These hints "leak" some information from the higher abstraction layer to
+                       // low level storage to allow for optimization.
+                       array_merge(
+                               $blobHints,
+                               [
+                                       BlobStore::DESIGNATION_HINT => 'page-content',
+                                       BlobStore::ROLE_HINT => $slot->getRole(),
+                                       BlobStore::SHA1_HINT => $slot->getSha1(),
+                                       BlobStore::MODEL_HINT => $model,
+                                       BlobStore::FORMAT_HINT => $format,
+                               ]
+                       )
+               );
        }
 
        /**
@@ -630,7 +849,7 @@ class RevisionStore
         * @throws MWException
         * @throws MWUnknownContentModelException
         */
-       private function checkContentModel( Content $content, Title $title ) {
+       private function checkContent( Content $content, Title $title ) {
                // Note: may return null for revisions that have not yet been inserted
 
                $model = $content->getModel();
@@ -681,16 +900,21 @@ class RevisionStore
         * Such revisions can for instance identify page rename
         * operations and other such meta-modifications.
         *
+        * @note: This method grabs a FOR UPDATE lock on the relevant row of the page table,
+        * to prevent a new revision from being inserted before the null revision has been written
+        * to the database.
+        *
         * MCR migration note: this replaces Revision::newNullRevision
         *
         * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
         * (or go away).
         *
-        * @param IDatabase $dbw
+        * @param IDatabase $dbw used for obtaining the lock on the page table row
         * @param Title $title Title of the page to read from
         * @param CommentStoreComment $comment RevisionRecord's summary
         * @param bool $minor Whether the revision should be considered as minor
         * @param User $user The user to attribute the revision to
+        *
         * @return RevisionRecord|null RevisionRecord or null on error
         */
        public function newNullRevision(
@@ -702,62 +926,34 @@ class RevisionStore
        ) {
                $this->checkDatabaseWikiId( $dbw );
 
-               $fields = [ 'page_latest', 'page_namespace', 'page_title',
-                       'rev_id', 'rev_text_id', 'rev_len', 'rev_sha1' ];
-
-               if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
-                       $fields[] = 'rev_text_id';
-                       if ( $this->contentHandlerUseDB ) {
-                               $fields[] = 'rev_content_model';
-                               $fields[] = 'rev_content_format';
-                       }
-               }
-
-               // Don't use getQueryInfo() top avoid locking extra tables, compare T191892.
-               // XXX: perhaps getQueryInfo() could support 'no-comment' and 'no-actor' as options.
-               $current = $dbw->selectRow(
-                       [ 'page', 'revision' ],
-                       $fields,
-                       [
-                               'page_id' => $title->getArticleID(),
-                       ],
+               // T51581: Lock the page table row to ensure no other process
+               // is adding a revision to the page at the same time.
+               // Avoid locking extra tables, compare T191892.
+               $pageLatest = $dbw->selectField(
+                       'page',
+                       'page_latest',
+                       [ 'page_id' => $title->getArticleID() ],
                        __METHOD__,
-                       [ 'FOR UPDATE' ], // T51581
-                       [ 'page' => [ 'JOIN', 'page_latest=rev_id' ] ]
+                       [ 'FOR UPDATE' ]
                );
 
-               if ( $current ) {
-                       $fields = [
-                               'page'        => $title->getArticleID(),
-                               'user_text'   => $user->getName(),
-                               'user'        => $user->getId(),
-                               'actor'       => $user->getActorId(),
-                               'comment'     => $comment,
-                               'minor_edit'  => $minor,
-                               'parent_id'   => $current->page_latest,
-                               'slot_origin' => $current->page_latest,
-                               'len'         => $current->rev_len,
-                               'sha1'        => $current->rev_sha1
-                       ];
+               if ( !$pageLatest ) {
+                       return null;
+               }
 
-                       if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
-                               $fields['text_id'] = $current->rev_text_id;
+               // Fetch the actual revision row, without locking all extra tables.
+               $oldRevision = $this->loadRevisionFromId( $dbw, $pageLatest );
 
-                               if ( $this->contentHandlerUseDB ) {
-                                       $fields['content_model'] = $current->rev_content_model;
-                                       $fields['content_format'] = $current->rev_content_format;
-                               }
-                       }
+               // Construct the new revision
+               $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
+               $newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision );
 
-                       $mainSlot = $this->emulateMainSlot_1_29( $fields, self::READ_LATEST, $title );
-                       $revision = new MutableRevisionRecord( $title, $this->wikiId );
-                       $this->initializeMutableRevisionFromArray( $revision, $fields );
-                       $revision->setSlot( $mainSlot );
-               } else {
-                       $revision = null;
-               }
+               $newRevision->setComment( $comment );
+               $newRevision->setUser( $user );
+               $newRevision->setTimestamp( $timestamp );
+               $newRevision->setMinorEdit( $minor );
 
-               return $revision;
+               return $newRevision;
        }
 
        /**
@@ -891,6 +1087,12 @@ class RevisionStore
                $blobFlags = null;
 
                if ( is_object( $row ) ) {
+                       if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW ) ) {
+                               // Don't emulate from a row when using the new schema.
+                               // Emulating from an array is still OK.
+                               throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
+                       }
+
                        // archive row
                        if ( !isset( $row->rev_id ) && ( isset( $row->ar_user ) || isset( $row->ar_actor ) ) ) {
                                $row = $this->mapArchiveFields( $row );
@@ -986,6 +1188,9 @@ class RevisionStore
                }
 
                if ( !$content ) {
+                       // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
+                       // is missing, but "empty revisions" with no content are used in some edge cases.
+
                        $content = function ( SlotRecord $slot )
                                use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
                        {
@@ -1009,8 +1214,6 @@ class RevisionStore
                                return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id, 'main' );
                        };
 
-               // use negative IDs for fake slot records.
-               $mainSlotRow->slot_id = -( $mainSlotRow->slot_revision_id );
                return new SlotRecord( $mainSlotRow, $content );
        }
 
@@ -1205,6 +1408,83 @@ class RevisionStore
                );
        }
 
+       /**
+        * @param int $revId The revision to load slots for.
+        * @param int $queryFlags
+        *
+        * @return SlotRecord[]
+        */
+       private function loadSlotRecords( $revId, $queryFlags ) {
+               $revQuery = self::getSlotsQueryInfo( [ 'content' ] );
+
+               list( $dbMode, $dbOptions ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
+               $db = $this->getDBConnectionRef( $dbMode );
+
+               $res = $db->select(
+                       $revQuery['tables'],
+                       $revQuery['fields'],
+                       [
+                               'slot_revision_id' => $revId,
+                       ],
+                       __METHOD__,
+                       $dbOptions,
+                       $revQuery['joins']
+               );
+
+               $slots = [];
+
+               foreach ( $res as $row ) {
+                       $contentCallback = function ( SlotRecord $slot ) use ( $queryFlags, $row ) {
+                               return $this->loadSlotContent( $slot, null, null, null, $queryFlags );
+                       };
+
+                       $slots[$row->role_name] = new SlotRecord( $row, $contentCallback );
+               }
+
+               if ( !isset( $slots['main'] ) ) {
+                       throw new RevisionAccessException(
+                               'Main slot of revision ' . $revId . ' not found in database!'
+                       );
+               };
+
+               return $slots;
+       }
+
+       /**
+        * Factory method for RevisionSlots.
+        *
+        * @note If other code has a need to construct RevisionSlots objects, this should be made
+        * public, since RevisionSlots instances should not be constructed directly.
+        *
+        * @param int $revId
+        * @param object $revisionRow
+        * @param int $queryFlags
+        * @param Title $title
+        *
+        * @return RevisionSlots
+        * @throws MWException
+        */
+       private function newRevisionSlots(
+               $revId,
+               $revisionRow,
+               $queryFlags,
+               Title $title
+       ) {
+               if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW ) ) {
+                       $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
+                       $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
+               } else {
+                       // XXX: do we need the same kind of caching here
+                       // that getKnownCurrentRevision uses (if $revId == page_latest?)
+
+                       $slots = new RevisionSlots( function () use( $revId, $queryFlags ) {
+                               return $this->loadSlotRecords( $revId, $queryFlags );
+                       } );
+               }
+
+               return $slots;
+       }
+
        /**
         * Make a fake revision object from an archive table row. This is queried
         * for permissions or even inserted (as in Special:Undelete)
@@ -1271,14 +1551,13 @@ class RevisionStore
                        // Legacy because $row may have come from self::selectFields()
                        ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'ar_comment', $row, true );
 
-               $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
-               $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
+               $slots = $this->newRevisionSlots( $row->ar_rev_id, $row, $queryFlags, $title );
 
                return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
        }
 
        /**
-        * @see RevisionFactory::newRevisionFromRow_1_29
+        * @see RevisionFactory::newRevisionFromRow
         *
         * MCR migration note: this replaces Revision::newFromRow
         *
@@ -1287,10 +1566,8 @@ class RevisionStore
         * @param Title|null $title
         *
         * @return RevisionRecord
-        * @throws MWException
-        * @throws RevisionAccessException
         */
-       private function newRevisionFromRow_1_29( $row, $queryFlags = 0, Title $title = null ) {
+       public function newRevisionFromRow( $row, $queryFlags = 0, Title $title = null ) {
                Assert::parameterType( 'object', $row, '$row' );
 
                if ( !$title ) {
@@ -1322,27 +1599,11 @@ class RevisionStore
                        // Legacy because $row may have come from self::selectFields()
                        ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'rev_comment', $row, true );
 
-               $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
-               $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
+               $slots = $this->newRevisionSlots( $row->rev_id, $row, $queryFlags, $title );
 
                return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
        }
 
-       /**
-        * @see RevisionFactory::newRevisionFromRow
-        *
-        * MCR migration note: this replaces Revision::newFromRow
-        *
-        * @param object $row
-        * @param int $queryFlags
-        * @param Title|null $title
-        *
-        * @return RevisionRecord
-        */
-       public function newRevisionFromRow( $row, $queryFlags = 0, Title $title = null ) {
-               return $this->newRevisionFromRow_1_29( $row, $queryFlags, $title );
-       }
-
        /**
         * Constructs a new MutableRevisionRecord based on the given associative array following
         * the MW1.29 convention for the Revision constructor.
@@ -1383,14 +1644,22 @@ class RevisionStore
 
                // if we have a content object, use it to set the model and type
                if ( !empty( $fields['content'] ) ) {
-                       if ( !( $fields['content'] instanceof Content ) ) {
-                               throw new MWException( 'content field must contain a Content object.' );
+                       if ( !( $fields['content'] instanceof Content ) && !is_array( $fields['content'] ) ) {
+                               throw new MWException(
+                                       'content field must contain a Content object or an array of Content objects.'
+                               );
+                       }
+               }
+
+               if ( !empty( $fields['text_id'] ) ) {
+                       if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
+                               throw new MWException( "The text_id field is only available in the pre-MCR schema" );
                        }
 
-                       if ( !empty( $fields['text_id'] ) ) {
+                       if ( !empty( $fields['content'] ) ) {
                                throw new MWException(
                                        "Text already stored in external store (id {$fields['text_id']}), " .
-                                       "can't serialize content object"
+                                       "can't specify content object"
                                );
                        }
                }
@@ -1415,11 +1684,17 @@ class RevisionStore
                        }
                }
 
-               $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
-
                $revision = new MutableRevisionRecord( $title, $this->wikiId );
                $this->initializeMutableRevisionFromArray( $revision, $fields );
-               $revision->setSlot( $mainSlot );
+
+               if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
+                       foreach ( $fields['content'] as $role => $content ) {
+                               $revision->setContent( $role, $content );
+                       }
+               } else {
+                       $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
+                       $revision->setSlot( $mainSlot );
+               }
 
                return $revision;
        }
@@ -1734,7 +2009,8 @@ class RevisionStore
        /**
         * Finds the ID of a content row for a given revision and slot role.
         * This can be used to re-use content rows even while the content ID
-        * is still missing from SlotRecords, in MIGRATION_WRITE_BOTH mode.
+        * is still missing from SlotRecords, when writing to both the old and
+        * the new schema during MCR schema migration.
         *
         * @todo remove after MCR schema migration is complete.
         *
@@ -1745,7 +2021,7 @@ class RevisionStore
         * @return int|null
         */
        private function findSlotContentId( IDatabase $db, $revId, $role ) {
-               if ( $this->mcrMigrationStage < MIGRATION_WRITE_BOTH ) {
+               if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
                        return null;
                }
 
@@ -1768,7 +2044,7 @@ class RevisionStore
 
        /**
         * Return the tables, fields, and join conditions to be selected to create
-        * a new revision object.
+        * a new RevisionStoreRecord object.
         *
         * MCR migration note: this replaces Revision::getQueryInfo
         *
@@ -1780,7 +2056,9 @@ class RevisionStore
         * @param array $options Any combination of the following strings
         *  - 'page': Join with the page table, and select fields to identify the page
         *  - 'user': Join with the user table, and select the user name
-        *  - 'text': Join with the text table, and select fields to load page text
+        *  - 'text': Join with the text table, and select fields to load page text. This
+        *    option is deprecated in MW 1.32 when the MCR migration flag SCHEMA_COMPAT_WRITE_NEW
+        *    is set, and disallowed when SCHEMA_COMPAT_READ_OLD is not set.
         *
         * @return array With three keys:
         *  - tables: (string[]) to include in the `$table` to `IDatabase->select()`
@@ -1816,7 +2094,7 @@ class RevisionStore
                $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
                $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
 
-               if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
                        $ret['fields'][] = 'rev_text_id';
 
                        if ( $this->contentHandlerUseDB ) {
@@ -1848,8 +2126,13 @@ class RevisionStore
                }
 
                if ( in_array( 'text', $options, true ) ) {
-                       if ( $this->mcrMigrationStage === MIGRATION_NEW ) {
+                       if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD ) ) {
                                throw new InvalidArgumentException( 'text table can no longer be joined directly' );
+                       } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
+                               // NOTE: even when this class is set to not read from the old schema, callers
+                               // should still be able to join against the text table, as long as we are still
+                               // writing the old schema for compatibility.
+                               wfDeprecated( __METHOD__ . ' with `text` option', '1.32' );
                        }
 
                        $ret['tables'][] = 'text';
@@ -1865,7 +2148,77 @@ class RevisionStore
 
        /**
         * Return the tables, fields, and join conditions to be selected to create
-        * a new archived revision object.
+        * a new SlotRecord.
+        *
+        * @since 1.32
+        *
+        * @param array $options Any combination of the following strings
+        *  - 'content': Join with the content table, and select content meta-data fields
+        *
+        * @return array With three keys:
+        *  - tables: (string[]) to include in the `$table` to `IDatabase->select()`
+        *  - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
+        *  - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
+        */
+       public function getSlotsQueryInfo( $options = [] ) {
+               $ret = [
+                       'tables' => [],
+                       'fields' => [],
+                       'joins'  => [],
+               ];
+
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
+                       $db = $this->getDBConnectionRef( DB_REPLICA );
+                       $ret['tables']['slots'] = 'revision';
+
+                       $ret['fields']['slot_revision_id'] = 'slots.rev_id';
+                       $ret['fields']['slot_content_id'] = 'NULL';
+                       $ret['fields']['slot_origin'] = 'slots.rev_id';
+                       $ret['fields']['role_name'] = $db->addQuotes( 'main' );
+
+                       if ( in_array( 'content', $options, true ) ) {
+                               $ret['fields']['content_size'] = 'slots.rev_len';
+                               $ret['fields']['content_sha1'] = 'slots.rev_sha1';
+                               $ret['fields']['content_address']
+                                       = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'slots.rev_text_id' ] );
+
+                               if ( $this->contentHandlerUseDB ) {
+                                       $ret['fields']['model_name'] = 'slots.rev_content_model';
+                               } else {
+                                       $ret['fields']['model_name'] = 'NULL';
+                               }
+                       }
+               } else {
+                       $ret['tables'][] = 'slots';
+                       $ret['tables'][] = 'slot_roles';
+                       $ret['fields'] = array_merge( $ret['fields'], [
+                               'slot_revision_id',
+                               'slot_content_id',
+                               'slot_origin',
+                               'role_name'
+                       ] );
+                       $ret['joins']['slot_roles'] = [ 'INNER JOIN', [ 'slot_role_id = role_id' ] ];
+
+                       if ( in_array( 'content', $options, true ) ) {
+                               $ret['tables'][] = 'content';
+                               $ret['tables'][] = 'content_models';
+                               $ret['fields'] = array_merge( $ret['fields'], [
+                                       'content_size',
+                                       'content_sha1',
+                                       'content_address',
+                                       'model_name'
+                               ] );
+                               $ret['joins']['content'] = [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ];
+                               $ret['joins']['content_models'] = [ 'INNER JOIN', [ 'content_model = model_id' ] ];
+                       }
+               }
+
+               return $ret;
+       }
+
+       /**
+        * Return the tables, fields, and join conditions to be selected to create
+        * a new RevisionArchiveRecord object.
         *
         * MCR migration note: this replaces Revision::getArchiveQueryInfo
         *
@@ -1897,7 +2250,7 @@ class RevisionStore
                        'joins' => $commentQuery['joins'] + $actorQuery['joins'],
                ];
 
-               if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
+               if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
                        $ret['fields'][] = 'ar_text_id';
 
                        if ( $this->contentHandlerUseDB ) {