Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / Revision.php
index dca2e1b..3d4d161 100644 (file)
@@ -19,6 +19,9 @@
  *
  * @file
  */
+
+use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\ResultWrapper;
@@ -556,8 +559,6 @@ class Revision implements IDBAccessObject {
        }
 
        /**
-        * Constructor
-        *
         * @param object|array $row Either a database row or an array
         * @throws MWException
         * @access private
@@ -859,7 +860,7 @@ class Revision implements IDBAccessObject {
         *   Revision::FOR_PUBLIC       to be displayed to all users
         *   Revision::FOR_THIS_USER    to be displayed to the given user
         *   Revision::RAW              get the ID regardless of permissions
-        * @param User $user User object to check for, only if FOR_THIS_USER is passed
+        * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
         *   to the $audience parameter
         * @return int
         */
@@ -893,7 +894,7 @@ class Revision implements IDBAccessObject {
         *   Revision::FOR_PUBLIC       to be displayed to all users
         *   Revision::FOR_THIS_USER    to be displayed to the given user
         *   Revision::RAW              get the text regardless of permissions
-        * @param User $user User object to check for, only if FOR_THIS_USER is passed
+        * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
         *   to the $audience parameter
         * @return string
         */
@@ -937,7 +938,7 @@ class Revision implements IDBAccessObject {
         *   Revision::FOR_PUBLIC       to be displayed to all users
         *   Revision::FOR_THIS_USER    to be displayed to the given user
         *   Revision::RAW              get the text regardless of permissions
-        * @param User $user User object to check for, only if FOR_THIS_USER is passed
+        * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
         *   to the $audience parameter
         * @return string
         */
@@ -1001,7 +1002,7 @@ class Revision implements IDBAccessObject {
 
                return RecentChange::newFromConds(
                        [
-                               'rc_user_text' => $this->getUserText( Revision::RAW ),
+                               'rc_user_text' => $this->getUserText( self::RAW ),
                                'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
                                'rc_this_oldid' => $this->getId()
                        ],
@@ -1279,12 +1280,15 @@ class Revision implements IDBAccessObject {
                        if ( isset( $row->old_id ) && $wiki === false ) {
                                // Make use of the wiki-local revision text cache
                                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
-                               $text = $cache->getWithSetCallback(
+                               // The cached value should be decompressed, so handle that and return here
+                               return $cache->getWithSetCallback(
                                        $cache->makeKey( 'revisiontext', 'textid', $row->old_id ),
                                        self::getCacheTTL( $cache ),
-                                       function () use ( $url, $wiki ) {
+                                       function () use ( $url, $wiki, $flags ) {
                                                // No negative caching per Revision::loadText()
-                                               return ExternalStore::fetchFromURL( $url, [ 'wiki' => $wiki ] );
+                                               $text = ExternalStore::fetchFromURL( $url, [ 'wiki' => $wiki ] );
+
+                                               return self::decompressRevisionText( $text, $flags );
                                        },
                                        [ 'pcGroup' => self::TEXT_CACHE_GROUP, 'pcTTL' => $cache::TTL_PROC_LONG ]
                                );
@@ -1293,12 +1297,7 @@ class Revision implements IDBAccessObject {
                        }
                }
 
-               // If the text was fetched without an error, convert it
-               if ( $text !== false ) {
-                       $text = self::decompressRevisionText( $text, $flags );
-               }
-
-               return $text;
+               return self::decompressRevisionText( $text, $flags );
        }
 
        /**
@@ -1308,7 +1307,7 @@ class Revision implements IDBAccessObject {
         * data is compressed, and 'utf-8' if we're saving in UTF-8
         * mode.
         *
-        * @param mixed $text Reference to a text
+        * @param mixed &$text Reference to a text
         * @return string
         */
        public static function compressRevisionText( &$text ) {
@@ -1344,6 +1343,13 @@ class Revision implements IDBAccessObject {
         * @return string|bool Decompressed text, or false on failure
         */
        public static function decompressRevisionText( $text, $flags ) {
+               global $wgLegacyEncoding, $wgContLang;
+
+               if ( $text === false ) {
+                       // Text failed to be fetched; nothing to do
+                       return false;
+               }
+
                if ( in_array( 'gzip', $flags ) ) {
                        # Deal with optional compression of archived pages.
                        # This can be done periodically via maintenance/compressOld.php, and
@@ -1366,7 +1372,6 @@ class Revision implements IDBAccessObject {
                        $text = $obj->getText();
                }
 
-               global $wgLegacyEncoding;
                if ( $text !== false && $wgLegacyEncoding
                        && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
                ) {
@@ -1374,7 +1379,6 @@ class Revision implements IDBAccessObject {
                        # Upconvert on demand.
                        # ("utf8" checked for compatibility with some broken
                        #  conversion scripts 2008-12-30)
-                       global $wgContLang;
                        $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
                }
 
@@ -1462,7 +1466,7 @@ class Revision implements IDBAccessObject {
                                ? $this->getPreviousRevisionId( $dbw )
                                : $this->mParentId,
                        'rev_sha1'       => $this->mSha1 === null
-                               ? Revision::base36Sha1( $this->mText )
+                               ? self::base36Sha1( $this->mText )
                                : $this->mSha1,
                ];
 
@@ -1490,7 +1494,10 @@ class Revision implements IDBAccessObject {
 
                $dbw->insert( 'revision', $row, __METHOD__ );
 
-               $this->mId = $rev_id !== null ? $rev_id : $dbw->insertId();
+               if ( $this->mId === null ) {
+                       // Only if nextSequenceValue() was called
+                       $this->mId = $dbw->insertId();
+               }
 
                // Assertion to try to catch T92046
                if ( (int)$this->mId === 0 ) {
@@ -1548,7 +1555,7 @@ class Revision implements IDBAccessObject {
                        }
                }
 
-               $content = $this->getContent( Revision::RAW );
+               $content = $this->getContent( self::RAW );
                $prefixedDBkey = $title->getPrefixedDBkey();
                $revId = $this->mId;