$this->mRecord = self::getRevisionStore()->newMutableRevisionFromArray(
$row,
$queryFlags,
- $title
+ $this->ensureTitle( $row, $queryFlags, $title )
);
} elseif ( is_object( $row ) ) {
$this->mRecord = self::getRevisionStore()->newRevisionFromRow(
$row,
$queryFlags,
- $title
+ $this->ensureTitle( $row, $queryFlags, $title )
);
} else {
throw new InvalidArgumentException(
}
}
+ /**
+ * Make sure we have *some* Title object for use by the constructor.
+ * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
+ *
+ * @param array|object $row
+ * @param int $queryFlags
+ * @param Title|null $title
+ *
+ * @return Title $title if not null, or a Title constructed from information in $row.
+ */
+ private function ensureTitle( $row, $queryFlags, $title = null ) {
+ if ( $title ) {
+ return $title;
+ }
+
+ if ( is_array( $row ) ) {
+ if ( isset( $row['title'] ) ) {
+ if ( !( $row['title'] instanceof Title ) ) {
+ throw new MWException( 'title field must contain a Title object.' );
+ }
+
+ return $row['title'];
+ }
+
+ $pageId = isset( $row['page'] ) ? $row['page'] : 0;
+ $revId = isset( $row['id'] ) ? $row['id'] : 0;
+ } else {
+ $pageId = isset( $row->rev_page ) ? $row->rev_page : 0;
+ $revId = isset( $row->rev_id ) ? $row->rev_id : 0;
+ }
+
+ try {
+ $title = self::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
+ } catch ( RevisionAccessException $ex ) {
+ // construct a dummy title!
+ wfLogWarning( __METHOD__ . ': ' . $ex->getMessage() );
+
+ // NOTE: this Title will only be used inside RevisionRecord
+ $title = Title::makeTitleSafe( NS_SPECIAL, "Badtitle/ID=$pageId" );
+ $title->resetArticleID( $pageId );
+ }
+
+ return $title;
+ }
+
/**
* @return RevisionRecord
*/
*
* MCR migration note: this corresponds to Revision::getTitle
*
+ * @note this method should be private, external use should be avoided!
+ *
* @param int|null $pageId
* @param int|null $revId
* @param int $queryFlags
* @return Title
* @throws RevisionAccessException
*/
- private function getTitle( $pageId, $revId, $queryFlags = 0 ) {
+ public function getTitle( $pageId, $revId, $queryFlags = 0 ) {
if ( !$pageId && !$revId ) {
throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
}
$this->assertNull( $rev->getContent(), 'no content object should be available' );
}
+ /**
+ * @covers Revision::__construct
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+ */
+ public function testConstructFromArrayWithBadPageId() {
+ MediaWiki\suppressWarnings();
+ $rev = new Revision( [ 'page' => 77777777 ] );
+ $this->assertSame( 77777777, $rev->getPage() );
+ MediaWiki\restoreWarnings();
+ }
+
public function provideConstructFromArray_userSetAsExpected() {
yield 'no user defaults to wgUser' => [
[
$assertions( $this, $rev );
}
+ /**
+ * @covers Revision::__construct
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+ */
+ public function testConstructFromRowWithBadPageId() {
+ MediaWiki\suppressWarnings();
+ $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
+ $this->assertSame( 77777777, $rev->getPage() );
+ MediaWiki\restoreWarnings();
+ }
+
public function provideGetRevisionText() {
yield 'Generic test' => [
'This is a goat of revision text.',