Merge "Block same-file reuploads"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 8aea7ab..410a794 100644 (file)
@@ -43,7 +43,7 @@ use Wikimedia\Rdbms\IDatabase;
  * @ingroup FileAbstraction
  */
 class LocalFile extends File {
-       const VERSION = 10; // cache version
+       const VERSION = 11; // cache version
 
        const CACHE_FIELD_MAX_LEN = 1000;
 
@@ -351,9 +351,8 @@ class LocalFile extends File {
                static $results = [];
 
                if ( $prefix == '' ) {
-                       return $fields;
+                       return array_merge( $fields, [ 'description' ] );
                }
-
                if ( !isset( $results[$prefix] ) ) {
                        $prefixedFields = [];
                        foreach ( $fields as $field ) {
@@ -1129,11 +1128,9 @@ class LocalFile extends File {
 
                if ( $this->historyLine == 0 ) { // called for the first time, return line from cur
                        $this->historyRes = $dbr->select( 'image',
-                               [
-                                       '*',
-                                       "'' AS oi_archive_name",
-                                       '0 as oi_deleted',
-                                       'img_sha1'
+                               self::selectFields() + [
+                                       'oi_archive_name' => $dbr->addQuotes( '' ),
+                                       'oi_deleted' => 0,
                                ],
                                [ 'img_name' => $this->title->getDBkey() ],
                                $fname
@@ -1145,7 +1142,9 @@ class LocalFile extends File {
                                return false;
                        }
                } elseif ( $this->historyLine == 1 ) {
-                       $this->historyRes = $dbr->select( 'oldimage', '*',
+                       $this->historyRes = $dbr->select(
+                               'oldimage',
+                               OldLocalFile::selectFields(),
                                [ 'oi_name' => $this->title->getDBkey() ],
                                $fname,
                                [ 'ORDER BY' => 'oi_timestamp DESC' ]
@@ -1244,8 +1243,22 @@ class LocalFile extends File {
                        // Once the second operation goes through, then the current version was
                        // updated and we must therefore update the DB too.
                        $oldver = $status->value;
-                       if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) {
-                               $status->fatal( 'filenotfound', $srcPath );
+                       $uploadStatus = $this->recordUpload2(
+                               $oldver,
+                               $comment,
+                               $pageText,
+                               $props,
+                               $timestamp,
+                               $user,
+                               $tags
+                       );
+                       if ( !$uploadStatus->isOK() ) {
+                               if ( $uploadStatus->hasMessage( 'filenotfound' ) ) {
+                                       // update filenotfound error with more specific path
+                                       $status->fatal( 'filenotfound', $srcPath );
+                               } else {
+                                       $status->merge( $uploadStatus );
+                               }
                        }
                }
 
@@ -1275,7 +1288,7 @@ class LocalFile extends File {
 
                $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
 
-               if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) {
+               if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user )->isOK() ) {
                        return false;
                }
 
@@ -1295,7 +1308,7 @@ class LocalFile extends File {
         * @param string|bool $timestamp
         * @param null|User $user
         * @param string[] $tags
-        * @return bool
+        * @return Status
         */
        function recordUpload2(
                $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = []
@@ -1329,7 +1342,7 @@ class LocalFile extends File {
                if ( !$this->fileExists ) {
                        wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" );
 
-                       return false;
+                       return Status::newFatal( 'filenotfound', $this->getRel() );
                }
 
                $dbw->startAtomic( __METHOD__ );
@@ -1362,16 +1375,24 @@ class LocalFile extends File {
                $reupload = ( $dbw->affectedRows() == 0 );
 
                if ( $reupload ) {
+                       $row = $dbw->selectRow(
+                               'image',
+                               [ 'img_timestamp', 'img_sha1' ],
+                               [ 'img_name' => $this->getName() ],
+                               __METHOD__,
+                               [ 'LOCK IN SHARE MODE' ]
+                       );
+
+                       if ( $row && $row->img_sha1 === $this->sha1 ) {
+                               $dbw->endAtomic( __METHOD__ );
+                               wfDebug( __METHOD__ . ": File " . $this->getRel() . " already exists!\n" );
+                               $title = Title::newFromText( $this->getName(), NS_FILE );
+                               return Status::newFatal( 'fileexists-no-change', $title->getPrefixedText() );
+                       }
+
                        if ( $allowTimeKludge ) {
                                # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
-                               $ltimestamp = $dbw->selectField(
-                                       'image',
-                                       'img_timestamp',
-                                       [ 'img_name' => $this->getName() ],
-                                       __METHOD__,
-                                       [ 'LOCK IN SHARE MODE' ]
-                               );
-                               $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false;
+                               $lUnixtime = $row ? wfTimestamp( TS_UNIX, $row->img_timestamp ) : false;
                                # Avoid a timestamp that is not newer than the last version
                                # TODO: the image/oldimage tables should be like page/revision with an ID field
                                if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) {
@@ -1642,7 +1663,7 @@ class LocalFile extends File {
                # Invalidate cache for all pages using this file
                DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) );
 
-               return true;
+               return Status::newGood();
        }
 
        /**
@@ -2308,7 +2329,6 @@ class LocalFileDeleteBatch {
 
                $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
                $encUserId = $dbw->addQuotes( $this->user->getId() );
-               $encReason = $dbw->addQuotes( $this->reason );
                $encGroup = $dbw->addQuotes( 'deleted' );
                $ext = $this->file->getExtension();
                $dotExt = $ext === '' ? '' : ".$ext";
@@ -2351,7 +2371,10 @@ class LocalFileDeleteBatch {
                        ];
                        $joins = [];
 
-                       $fields += $commentStoreFaReason->insert( $dbw, $encReason );
+                       $fields += array_map(
+                               [ $dbw, 'addQuotes' ],
+                               $commentStoreFaReason->insert( $dbw, $this->reason )
+                       );
 
                        if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
                                $fields['fa_description'] = 'img_description';