Various simple optimizations for the chunked upload process.
[lhc/web/wiklou.git] / includes / upload / UploadStash.php
index bbd9c44..09bcaea 100644 (file)
@@ -152,7 +152,7 @@ class UploadStash {
        /**
         * Getter for file metadata.
         *
-        * @param key String: key under which file information is stored
+        * @param $key String: key under which file information is stored
         * @return Array
         */
        public function getMetadata ( $key ) {
@@ -163,7 +163,7 @@ class UploadStash {
        /**
         * Getter for fileProps
         *
-        * @param key String: key under which file information is stored
+        * @param $key String: key under which file information is stored
         * @return Array
         */
        public function getFileProps ( $key ) {
@@ -182,7 +182,7 @@ class UploadStash {
         * @return UploadStashFile: file, or null on failure
         */
        public function stashFile( $path, $sourceType = null ) {
-               if ( ! file_exists( $path ) ) {
+               if ( !is_file( $path ) ) {
                        wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
                        throw new UploadStashBadPathException( "path doesn't exist" );
                }
@@ -192,12 +192,10 @@ class UploadStash {
                // we will be initializing from some tmpnam files that don't have extensions.
                // most of MediaWiki assumes all uploaded files have good extensions. So, we fix this.
                $extension = self::getExtensionForPath( $path );
-               if ( ! preg_match( "/\\.\\Q$extension\\E$/", $path ) ) {
+               if ( !preg_match( "/\\.\\Q$extension\\E$/", $path ) ) {
                        $pathWithGoodExtension = "$path.$extension";
-                       if ( ! rename( $path, $pathWithGoodExtension ) ) {
-                               throw new UploadStashFileException( "couldn't rename $path to have a better extension at $pathWithGoodExtension" );
-                       }
-                       $path = $pathWithGoodExtension;
+               } else {
+                       $pathWithGoodExtension = $path;
                }
 
                // If no key was supplied, make one.  a mysql insertid would be totally reasonable here, except
@@ -221,7 +219,7 @@ class UploadStash {
                wfDebug( __METHOD__ . " key for '$path': $key\n" );
 
                // if not already in a temporary area, put it there
-               $storeStatus = $this->repo->storeTemp( basename( $path ), $path );
+               $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ), $path );
 
                if ( ! $storeStatus->isOK() ) {
                        // It is a convention in MediaWiki to only return one error per API exception, even if multiple errors
@@ -244,9 +242,6 @@ class UploadStash {
                }
                $stashPath = $storeStatus->value;
 
-               // we have renamed the file so we have to cleanup once done
-               unlink($path);
-
                // fetch the current user ID
                if ( !$this->isLoggedIn ) {
                        throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
@@ -319,8 +314,8 @@ class UploadStash {
        /**
         * Remove a particular file from the stash.  Also removes it from the repo.
         *
-        * @throws UploadStashNotLoggedInException
-        * @throws UploadStashWrongOwnerException
+        * @param $key
+        * @throws UploadStashNoSuchKeyException|UploadStashNotLoggedInException|UploadStashWrongOwnerException
         * @return boolean: success
         */
        public function removeFile( $key ) {
@@ -416,6 +411,8 @@ class UploadStash {
         * with an extension.
         * XXX this is somewhat redundant with the checks that ApiUpload.php does with incoming
         * uploads versus the desired filename. Maybe we can get that passed to us...
+        * @param $path
+        * @throws UploadStashFileException
         * @return string
         */
        public static function getExtensionForPath( $path ) {