}
}
+ /**
+ * Returns whether this RevisionRecord is ready for insertion, that is, whether it contains all
+ * information needed to save it to the database. This should trivially be true for
+ * RevisionRecords loaded from the database.
+ *
+ * Note that this may return true even if getId() or getPage() return null or 0, since these
+ * are generally assigned while the revision is saved to the database, and may not be available
+ * before.
+ *
+ * @return bool
+ */
+ public function isReadyForInsertion() {
+ // NOTE: don't check getSize() and getSha1(), since that may cause the full content to
+ // be loaded in order to calculate the values. Just assume these methods will not return
+ // null if mSlots is not empty.
+
+ // NOTE: getId() and getPageId() may return null before a revision is saved, so don't
+ //check them.
+
+ return $this->getTimestamp() !== null
+ && $this->getComment( self::RAW ) !== null
+ && $this->getUser( self::RAW ) !== null
+ && $this->mSlots->getSlotRoles() !== [];
+ }
+
}
$this->failOnNull( $user->getId(), 'user field' );
$this->failOnEmpty( $user->getName(), 'user_text field' );
+ if ( !$rev->isReadyForInsertion() ) {
+ // This is here for future-proofing. At the time this check being added, it
+ // was redundant to the individual checks above.
+ throw new IncompleteRevisionException( 'Revision is incomplete' );
+ }
+
// 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
use MediaWikiTestCase;
use TextContent;
use Title;
+use User;
use WikitextContent;
/**
$record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
$record->setComment( $comment );
$record->setUser( $user );
+ $record->setTimestamp( '20101010000000' );
return $record;
}
$this->assertFalse( $record->hasSlot( 'c' ) );
}
+ public function provideNotReadyForInsertion() {
+ /** @var Title $title */
+ $title = $this->getMock( Title::class );
+
+ /** @var User $user */
+ $user = $this->getMock( User::class );
+
+ /** @var CommentStoreComment $comment */
+ $comment = $this->getMockBuilder( CommentStoreComment::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $content = new TextContent( 'Test' );
+
+ $rev = new MutableRevisionRecord( $title );
+ yield 'empty' => [ $rev ];
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setContent( 'main', $content );
+ $rev->setUser( $user );
+ $rev->setComment( $comment );
+ yield 'no timestamp' => [ $rev ];
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setUser( $user );
+ $rev->setComment( $comment );
+ $rev->setTimestamp( '20101010000000' );
+ yield 'no content' => [ $rev ];
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setContent( 'main', $content );
+ $rev->setComment( $comment );
+ $rev->setTimestamp( '20101010000000' );
+ yield 'no user' => [ $rev ];
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setUser( $user );
+ $rev->setContent( 'main', $content );
+ $rev->setTimestamp( '20101010000000' );
+ yield 'no comment' => [ $rev ];
+ }
+
+ /**
+ * @dataProvider provideNotReadyForInsertion
+ */
+ public function testNotReadyForInsertion( $rev ) {
+ $this->assertFalse( $rev->isReadyForInsertion() );
+ }
}