Fixes LocalFile isMissing always returning true
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 878e82d..aa04fae 100644 (file)
@@ -634,7 +634,6 @@ class LocalFile extends File {
                }
 
                $this->fileExists = true;
-               $this->maybeUpgradeRow();
        }
 
        /**
@@ -659,7 +658,7 @@ class LocalFile extends File {
        /**
         * Upgrade a row if it needs it
         */
-       function maybeUpgradeRow() {
+       protected function maybeUpgradeRow() {
                global $wgUpdateCompatibleMetadata;
 
                if ( wfReadOnly() || $this->upgrading ) {
@@ -797,11 +796,14 @@ class LocalFile extends File {
        /** isVisible inherited */
 
        /**
+        * Checks if this file exists in its parent repo, as referenced by its
+        * virtual URL.
+        *
         * @return bool
         */
        function isMissing() {
                if ( $this->missing === null ) {
-                       list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() );
+                       $fileExists = $this->repo->fileExists( $this->getVirtualUrl() );
                        $this->missing = !$fileExists;
                }
 
@@ -1028,6 +1030,7 @@ class LocalFile extends File {
         */
        function purgeCache( $options = [] ) {
                // Refresh metadata cache
+               $this->maybeUpgradeRow();
                $this->purgeMetadataCache();
 
                // Delete thumbnails
@@ -1303,12 +1306,13 @@ class LocalFile extends File {
         *   (This doesn't check $user's permissions.)
         * @param bool $createNullRevision Set to false to avoid creation of a null revision on file
         *   upload, see T193621
+        * @param bool $revert If this file upload is a revert
         * @return Status On success, the value member contains the
         *     archive name, or an empty string if it was a new file.
         */
        function upload( $src, $comment, $pageText, $flags = 0, $props = false,
                $timestamp = false, $user = null, $tags = [],
-               $createNullRevision = true
+               $createNullRevision = true, $revert = false
        ) {
                if ( $this->getRepo()->getReadOnlyReason() !== false ) {
                        return $this->readOnlyFatalStatus();
@@ -1366,7 +1370,8 @@ class LocalFile extends File {
                                $timestamp,
                                $user,
                                $tags,
-                               $createNullRevision
+                               $createNullRevision,
+                               $revert
                        );
                        if ( !$uploadStatus->isOK() ) {
                                if ( $uploadStatus->hasMessage( 'filenotfound' ) ) {
@@ -1425,13 +1430,14 @@ class LocalFile extends File {
         * @param string[] $tags
         * @param bool $createNullRevision Set to false to avoid creation of a null revision on file
         *   upload, see T193621
+        * @param bool $revert If this file upload is a revert
         * @return Status
         */
        function recordUpload2(
                $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = [],
-               $createNullRevision = true
+               $createNullRevision = true, $revert = false
        ) {
-               global $wgCommentTableSchemaMigrationStage, $wgActorTableSchemaMigrationStage;
+               global $wgActorTableSchemaMigrationStage;
 
                if ( is_null( $user ) ) {
                        global $wgUser;
@@ -1528,6 +1534,7 @@ class LocalFile extends File {
                                'oi_width' => 'img_width',
                                'oi_height' => 'img_height',
                                'oi_bits' => 'img_bits',
+                               'oi_description_id' => 'img_description_id',
                                'oi_timestamp' => 'img_timestamp',
                                'oi_metadata' => 'img_metadata',
                                'oi_media_type' => 'img_media_type',
@@ -1537,39 +1544,6 @@ class LocalFile extends File {
                        ];
                        $joins = [];
 
-                       if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
-                               $fields['oi_description'] = 'img_description';
-                       }
-                       if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
-                               $fields['oi_description_id'] = 'img_description_id';
-                       }
-
-                       if ( $wgCommentTableSchemaMigrationStage !== MIGRATION_OLD &&
-                               $wgCommentTableSchemaMigrationStage !== MIGRATION_NEW
-                       ) {
-                               // 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_description' ],
-                                       [
-                                               'img_name' => $this->getName(),
-                                               'img_description_id' => 0,
-                                       ],
-                                       __METHOD__
-                               );
-                               foreach ( $res as $row ) {
-                                       $imgFields = $commentStore->insert( $dbw, 'img_description', $row->img_description );
-                                       $dbw->update(
-                                               'image',
-                                               $imgFields,
-                                               [ 'img_name' => $row->img_name ],
-                                               __METHOD__
-                                       );
-                               }
-                       }
-
                        if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
                                $fields['oi_user'] = 'img_user';
                                $fields['oi_user_text'] = 'img_user_text';
@@ -1634,8 +1608,16 @@ class LocalFile extends File {
                $wikiPage = new WikiFilePage( $descTitle );
                $wikiPage->setFile( $this );
 
+               // Determine log action. If reupload is done by reverting, use a special log_action.
+               if ( $revert === true ) {
+                       $logAction = 'revert';
+               } elseif ( $reupload === true ) {
+                       $logAction = 'overwrite';
+               } else {
+                       $logAction = 'upload';
+               }
                // Add the log entry...
-               $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' );
+               $logEntry = new ManualLogEntry( 'upload', $logAction );
                $logEntry->setTimestamp( $this->timestamp );
                $logEntry->setPerformer( $user );
                $logEntry->setComment( $comment );
@@ -1859,8 +1841,13 @@ class LocalFile extends File {
 
                $this->lock();
 
-               $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
-               $archiveRel = $this->getArchiveRel( $archiveName );
+               if ( $this->isOld() ) {
+                       $archiveRel = $dstRel;
+                       $archiveName = basename( $archiveRel );
+               } else {
+                       $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
+                       $archiveRel = $this->getArchiveRel( $archiveName );
+               }
 
                if ( $repo->hasSha1Storage() ) {
                        $sha1 = FileRepo::isVirtualUrl( $srcPath )
@@ -2470,7 +2457,7 @@ class LocalFileDeleteBatch {
        }
 
        protected function doDBInserts() {
-               global $wgCommentTableSchemaMigrationStage, $wgActorTableSchemaMigrationStage;
+               global $wgActorTableSchemaMigrationStage;
 
                $now = time();
                $dbw = $this->file->repo->getMasterDB();
@@ -2515,6 +2502,7 @@ class LocalFileDeleteBatch {
                                'fa_media_type' => 'img_media_type',
                                'fa_major_mime' => 'img_major_mime',
                                'fa_minor_mime' => 'img_minor_mime',
+                               'fa_description_id' => 'img_description_id',
                                'fa_timestamp' => 'img_timestamp',
                                'fa_sha1' => 'img_sha1'
                        ];
@@ -2525,39 +2513,6 @@ class LocalFileDeleteBatch {
                                $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason )
                        );
 
-                       if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
-                               $fields['fa_description'] = 'img_description';
-                       }
-                       if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
-                               $fields['fa_description_id'] = 'img_description_id';
-                       }
-
-                       if ( $wgCommentTableSchemaMigrationStage !== MIGRATION_OLD &&
-                               $wgCommentTableSchemaMigrationStage !== MIGRATION_NEW
-                       ) {
-                               // 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_description' ],
-                                       [
-                                               'img_name' => $this->file->getName(),
-                                               'img_description_id' => 0,
-                                       ],
-                                       __METHOD__
-                               );
-                               foreach ( $res as $row ) {
-                                       $imgFields = $commentStore->insert( $dbw, 'img_description', $row->img_description );
-                                       $dbw->update(
-                                               'image',
-                                               $imgFields,
-                                               [ 'img_name' => $row->img_name ],
-                                               __METHOD__
-                                       );
-                               }
-                       }
-
                        if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
                                $fields['fa_user'] = 'img_user';
                                $fields['fa_user_text'] = 'img_user_text';