Handle failure to load content in Revision getSize, etc
[lhc/web/wiklou.git] / includes / Revision.php
index ea73a61..6db3710 100644 (file)
@@ -65,10 +65,14 @@ class Revision implements IDBAccessObject {
        }
 
        /**
+        * @param bool|string $wiki 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 = [] ) {
-               $rec = self::getRevisionStore()->newRevisionFromArchiveRow( $row, 0, null, $overrides );
-               return new Revision( $rec );
+               /**
+                * 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, $title, $overrides );
+               return new Revision( $rec, self::READ_NORMAL, $title );
        }
 
        /**
@@ -155,8 +193,9 @@ class Revision implements IDBAccessObject {
         *
         * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
         * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
-        * a MutableRevisionRecord should be constructed directly. RevisionStore::newRevisionFromArray()
-        * can be used as a temporary replacement, but should be avoided.
+        * a MutableRevisionRecord should be constructed directly.
+        * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
+        * but should be avoided.
         *
         * @param object|array $row
         * @return Revision
@@ -226,7 +265,8 @@ class Revision implements IDBAccessObject {
         * WARNING: Timestamps may in some circumstances not be unique,
         * so this isn't the best key to use.
         *
-        * @deprecated since 1.31, use RevisionStore::loadRevisionFromTimestamp() instead.
+        * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
+        *   or RevisionStore::loadRevisionFromTimestamp() instead.
         *
         * @param IDatabase $db
         * @param Title $title
@@ -234,7 +274,6 @@ class Revision implements IDBAccessObject {
         * @return Revision|null
         */
        public static function loadFromTimestamp( $db, $title, $timestamp ) {
-               // XXX: replace loadRevisionFromTimestamp by getRevisionByTimestamp?
                $rec = self::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
                return $rec === null ? null : new Revision( $rec );
        }
@@ -423,6 +462,9 @@ class Revision implements IDBAccessObject {
 
        /**
         * Do a batched query to get the parent revision lengths
+        *
+        * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
+        *
         * @param IDatabase $db
         * @param array $revIds
         * @return array
@@ -444,6 +486,8 @@ class Revision implements IDBAccessObject {
                if ( $row instanceof RevisionRecord ) {
                        $this->mRecord = $row;
                } elseif ( is_array( $row ) ) {
+                       // If no user is specified, fall back to using the global user object, to stay
+                       // compatible with pre-1.31 behavior.
                        if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
                                $row['user'] = $wgUser;
                        }
@@ -564,20 +608,27 @@ class Revision implements IDBAccessObject {
        /**
         * Returns the length of the text in this revision, or null if unknown.
         *
-        * @return int
+        * @return int|null
         */
        public function getSize() {
-               return $this->mRecord->getSize();
+               try {
+                       return $this->mRecord->getSize();
+               } catch ( RevisionAccessException $ex ) {
+                       return null;
+               }
        }
 
        /**
         * Returns the base36 sha1 of the content in this revision, or null if unknown.
         *
-        * @return string
+        * @return string|null
         */
        public function getSha1() {
-               // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
-               return $this->mRecord->getSha1();
+               try {
+                       return $this->mRecord->getSha1();
+               } catch ( RevisionAccessException $ex ) {
+                       return null;
+               }
        }
 
        /**
@@ -735,7 +786,7 @@ class Revision implements IDBAccessObject {
         * @return int Rcid of the unpatrolled row, zero if there isn't one
         */
        public function isUnpatrolled() {
-               return self::getRevisionStore()->isUnpatrolled( $this->mRecord );
+               return self::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord );
        }
 
        /**
@@ -879,8 +930,9 @@ class Revision implements IDBAccessObject {
         * @return Revision|null
         */
        public function getPrevious() {
-               $rec = self::getRevisionStore()->getPreviousRevision( $this->mRecord );
-               return $rec === null ? null : new Revision( $rec );
+               $title = $this->getTitle();
+               $rec = self::getRevisionStore()->getPreviousRevision( $this->mRecord, $title );
+               return $rec === null ? null : new Revision( $rec, self::READ_NORMAL, $title );
        }
 
        /**
@@ -889,8 +941,9 @@ class Revision implements IDBAccessObject {
         * @return Revision|null
         */
        public function getNext() {
-               $rec = self::getRevisionStore()->getNextRevision( $this->mRecord );
-               return $rec === null ? null : new Revision( $rec );
+               $title = $this->getTitle();
+               $rec = self::getRevisionStore()->getNextRevision( $this->mRecord, $title );
+               return $rec === null ? null : new Revision( $rec, self::READ_NORMAL, $title );
        }
 
        /**
@@ -925,7 +978,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 +1032,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 +1194,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;
        }