Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 3090632..6d29433 100644 (file)
@@ -202,44 +202,6 @@ class LocalFile extends File {
                }
        }
 
-       /**
-        * Fields in the image table
-        * @deprecated since 1.31, use self::getQueryInfo() instead.
-        * @return string[]
-        */
-       static function selectFields() {
-               global $wgActorTableSchemaMigrationStage;
-
-               wfDeprecated( __METHOD__, '1.31' );
-               if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
-                       // If code is using this instead of self::getQueryInfo(), there's a
-                       // decent chance it's going to try to directly access
-                       // $row->img_user or $row->img_user_text and we can't give it
-                       // useful values here once those aren't being used anymore.
-                       throw new BadMethodCallException(
-                               'Cannot use ' . __METHOD__
-                                       . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
-                       );
-               }
-
-               return [
-                       'img_name',
-                       'img_size',
-                       'img_width',
-                       'img_height',
-                       'img_metadata',
-                       'img_bits',
-                       'img_media_type',
-                       'img_major_mime',
-                       'img_minor_mime',
-                       'img_user',
-                       'img_user_text',
-                       'img_actor' => 'NULL',
-                       'img_timestamp',
-                       'img_sha1',
-               ] + MediaWikiServices::getInstance()->getCommentStore()->getFields( 'img_description' );
-       }
-
        /**
         * Return the tables, fields, and join conditions to be selected to create
         * a new localfile object.
@@ -490,6 +452,10 @@ class LocalFile extends File {
         * This covers fields that are sometimes not cached.
         */
        protected function loadExtraFromDB() {
+               if ( !$this->title ) {
+                       return; // Avoid hard failure when the file does not exist. T221812
+               }
+
                $fname = static::class . '::' . __FUNCTION__;
 
                # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
@@ -895,12 +861,24 @@ class LocalFile extends File {
        function getUser( $type = 'text' ) {
                $this->load();
 
-               if ( $type === 'object' ) {
-                       return $this->user;
-               } elseif ( $type === 'text' ) {
-                       return $this->user->getName();
-               } elseif ( $type === 'id' ) {
-                       return $this->user->getId();
+               if ( !$this->user ) {
+                       // If the file does not exist, $this->user will be null, see T221812.
+                       // Note: 'Unknown user' this is a reserved user name.
+                       if ( $type === 'object' ) {
+                               return User::newFromName( 'Unknown user', false );
+                       } elseif ( $type === 'text' ) {
+                               return 'Unknown user';
+                       } elseif ( $type === 'id' ) {
+                               return 0;
+                       }
+               } else {
+                       if ( $type === 'object' ) {
+                               return $this->user;
+                       } elseif ( $type === 'text' ) {
+                               return $this->user->getName();
+                       } elseif ( $type === 'id' ) {
+                               return $this->user->getId();
+                       }
                }
 
                throw new MWException( "Unknown type '$type'." );
@@ -914,9 +892,13 @@ class LocalFile extends File {
         * @since 1.27
         */
        public function getDescriptionShortUrl() {
+               if ( !$this->title ) {
+                       return null; // Avoid hard failure when the file does not exist. T221812
+               }
+
                $pageId = $this->title->getArticleID();
 
-               if ( $pageId !== null ) {
+               if ( $pageId ) {
                        $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
                        if ( $url !== false ) {
                                return $url;
@@ -1183,6 +1165,10 @@ class LocalFile extends File {
         * @return OldLocalFile[]
         */
        function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
+               if ( !$this->exists() ) {
+                       return []; // Avoid hard failure when the file does not exist. T221812
+               }
+
                $dbr = $this->repo->getReplicaDB();
                $oldFileQuery = OldLocalFile::getQueryInfo();
 
@@ -1236,9 +1222,13 @@ class LocalFile extends File {
         *  0      return line for current version
         *  1      query for old versions, return first one
         *  2, ... return next old version from above query
-        * @return bool
+        * @return stdClass|bool
         */
        public function nextHistoryLine() {
+               if ( !$this->exists() ) {
+                       return false; // Avoid hard failure when the file does not exist. T221812
+               }
+
                # Polymorphic function name to distinguish foreign and local fetches
                $fname = static::class . '::' . __FUNCTION__;
 
@@ -1449,8 +1439,6 @@ class LocalFile extends File {
                $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = [],
                $createNullRevision = true, $revert = false
        ) {
-               global $wgActorTableSchemaMigrationStage;
-
                if ( is_null( $user ) ) {
                        global $wgUser;
                        $user = $wgUser;
@@ -1553,40 +1541,10 @@ class LocalFile extends File {
                                'oi_major_mime' => 'img_major_mime',
                                'oi_minor_mime' => 'img_minor_mime',
                                'oi_sha1' => 'img_sha1',
+                               'oi_actor' => 'img_actor',
                        ];
                        $joins = [];
 
-                       if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
-                               $fields['oi_user'] = 'img_user';
-                               $fields['oi_user_text'] = 'img_user_text';
-                       }
-                       if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
-                               $fields['oi_actor'] = 'img_actor';
-                       }
-
-                       if (
-                               ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_BOTH ) === SCHEMA_COMPAT_WRITE_BOTH
-                       ) {
-                               // Upgrade any rows that are still old-style. Otherwise an upgrade
-                               // might be missed if a deletion happens while the migration script
-                               // is running.
-                               $res = $dbw->select(
-                                       [ 'image' ],
-                                       [ 'img_name', 'img_user', 'img_user_text' ],
-                                       [ 'img_name' => $this->getName(), 'img_actor' => 0 ],
-                                       __METHOD__
-                               );
-                               foreach ( $res as $row ) {
-                                       $actorId = User::newFromAnyId( $row->img_user, $row->img_user_text, null )->getActorId( $dbw );
-                                       $dbw->update(
-                                               'image',
-                                               [ 'img_actor' => $actorId ],
-                                               [ 'img_name' => $row->img_name, 'img_actor' => 0 ],
-                                               __METHOD__
-                                       );
-                               }
-                       }
-
                        # (T36993) Note: $oldver can be empty here, if the previous
                        # version of the file was broken. Allow registration of the new
                        # version to continue anyway, because that's better than having
@@ -2096,9 +2054,13 @@ class LocalFile extends File {
 
        /**
         * Get the URL of the file description page.
-        * @return string
+        * @return string|bool
         */
        function getDescriptionUrl() {
+               if ( !$this->title ) {
+                       return false; // Avoid hard failure when the file does not exist. T221812
+               }
+
                return $this->title->getLocalURL();
        }
 
@@ -2111,6 +2073,10 @@ class LocalFile extends File {
         * @return string|false
         */
        function getDescriptionText( Language $lang = null ) {
+               if ( !$this->title ) {
+                       return false; // Avoid hard failure when the file does not exist. T221812
+               }
+
                $store = MediaWikiServices::getInstance()->getRevisionStore();
                $revision = $store->getRevisionByTitle( $this->title, 0, Revision::READ_NORMAL );
                if ( !$revision ) {
@@ -2160,6 +2126,10 @@ class LocalFile extends File {
         * @return bool|string
         */
        public function getDescriptionTouched() {
+               if ( !$this->exists() ) {
+                       return false; // Avoid hard failure when the file does not exist. T221812
+               }
+
                // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
                // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
                // need to differentiate between null (uninitialized) and false (failed to load).