Merge "Revision: Handle all return values of Title::newFromId"
[lhc/web/wiklou.git] / includes / Revision.php
index ea73a61..1d7e62c 100644 (file)
@@ -65,10 +65,14 @@ class Revision implements IDBAccessObject {
        }
 
        /**
+        * @param bool|string $wikiId The ID of the target wiki database. Use false for the local wiki.
+        *
         * @return SqlBlobStore
         */
-       protected static function getBlobStore() {
-               $store = MediaWikiServices::getInstance()->getBlobStore();
+       protected static function getBlobStore( $wiki = false ) {
+               $store = MediaWikiServices::getInstance()
+                       ->getBlobStoreFactory()
+                       ->newSqlBlobStore( $wiki );
 
                if ( !$store instanceof SqlBlobStore ) {
                        throw new RuntimeException(
@@ -146,8 +150,42 @@ class Revision implements IDBAccessObject {
         * @return Revision
         */
        public static function newFromArchiveRow( $row, $overrides = [] ) {
+               /**
+                * MCR Migration: https://phabricator.wikimedia.org/T183564
+                * This method used to overwrite attributes, then passed to Revision::__construct
+                * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
+                * So do a conversion here.
+                */
+               if ( array_key_exists( 'page', $overrides ) ) {
+                       $overrides['page_id'] = $overrides['page'];
+                       unset( $overrides['page'] );
+               }
+
+               /**
+                * We require a Title for both the Revision object and the RevisionRecord.
+                * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
+                * to fetch a title in order pass it into the Revision object.
+                */
+               $title = null;
+               if ( isset( $overrides['title'] ) ) {
+                       if ( !( $overrides['title'] instanceof Title ) ) {
+                               throw new MWException( 'title field override must contain a Title object.' );
+                       }
+
+                       $title = $overrides['title'];
+               }
+               if ( $title !== null ) {
+                       if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
+                               $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
+                       } else {
+                               throw new InvalidArgumentException(
+                                       'A Title or ar_namespace and ar_title must be given'
+                               );
+                       }
+               }
+
                $rec = self::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, null, $overrides );
-               return new Revision( $rec );
+               return new Revision( $rec, self::READ_NORMAL, $title );
        }
 
        /**
@@ -925,7 +963,7 @@ class Revision implements IDBAccessObject {
 
                $cacheKey = isset( $row->old_id ) ? ( 'tt:' . $row->old_id ) : null;
 
-               return self::getBlobStore()->expandBlob( $text, $flags, $cacheKey );
+               return self::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
        }
 
        /**
@@ -979,8 +1017,10 @@ class Revision implements IDBAccessObject {
 
                $this->mRecord = $rec;
 
+               // Avoid PHP 7.1 warning of passing $this by reference
+               $revision = $this;
                // TODO: hard-deprecate in 1.32 (or even 1.31?)
-               Hooks::run( 'RevisionInsertComplete', [ $this, null, null ] );
+               Hooks::run( 'RevisionInsertComplete', [ &$revision, null, null ] );
 
                return $rec->getId();
        }
@@ -1139,6 +1179,10 @@ class Revision implements IDBAccessObject {
                        ? $pageIdOrTitle
                        : Title::newFromID( $pageIdOrTitle );
 
+               if ( !$title ) {
+                       return false;
+               }
+
                $record = self::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
                return $record ? new Revision( $record ) : false;
        }