Merge "[JobQueue] Added a sanity check to catch ack() breakage."
[lhc/web/wiklou.git] / includes / upload / UploadFromChunks.php
index b0e5fb6..2b0128b 100644 (file)
@@ -37,7 +37,7 @@ class UploadFromChunks extends UploadFromFile {
         * @param $stash UploadStash
         * @param $repo FileRepo
         */
-       public function __construct( $user = false, $stash = false, $repo = false ) {
+       public function __construct( $user = null, $stash = false, $repo = false ) {
                // user object. sometimes this won't exist, as when running from cron.
                $this->user = $user;
 
@@ -60,6 +60,7 @@ class UploadFromChunks extends UploadFromFile {
 
                return true;
        }
+
        /**
         * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
         *
@@ -120,17 +121,24 @@ class UploadFromChunks extends UploadFromFile {
                // Get a 0-byte temp file to perform the concatenation at
                $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext );
                $tmpPath = $tmpFile
-                       ? $tmpFile->getPath()
+                       ? $tmpFile->bind( $this )->getPath() // keep alive with $this
                        : false; // fail in concatenate()
                // Concatenate the chunks at the temp file
+               $tStart = microtime( true );
                $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE );
+               $tAmount = microtime( true ) - $tStart;
                if( !$status->isOk() ){
                        return $status;
                }
+               wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds.\n" );
                // Update the mTempPath and mLocalFile
                // ( for FileUpload or normal Stash to take over )
                $this->mTempPath = $tmpPath; // file system path
-               $this->mLocalFile = parent::stashFile();
+               $tStart = microtime( true );
+               $this->mLocalFile = parent::stashFile( $this->user );
+               $tAmount = microtime( true ) - $tStart;
+               $this->mLocalFile->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo())
+               wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds.\n" );
 
                return $status;
        }
@@ -203,6 +211,9 @@ class UploadFromChunks extends UploadFromFile {
                                        $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
 
                $dbw = $this->repo->getMasterDb();
+               // Use a quick transaction since we will upload the full temp file into shared
+               // storage, which takes time for large files. We don't want to hold locks then.
+               $dbw->begin( __METHOD__ );
                $dbw->update(
                        'uploadstash',
                        array(
@@ -213,6 +224,7 @@ class UploadFromChunks extends UploadFromFile {
                        array( 'us_key' => $this->mFileKey ),
                        __METHOD__
                );
+               $dbw->commit( __METHOD__ );
        }
 
        /**