Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / jobqueue / jobs / AssembleUploadChunksJob.php
blobe2914be5e4dbad5ce5486f39e222c8856fd3235d
1 <?php
2 /**
3 * Assemble the segments of a chunked upload.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Upload
23 use Wikimedia\ScopedCallback;
25 /**
26 * Assemble the segments of a chunked upload.
28 * @ingroup Upload
30 class AssembleUploadChunksJob extends Job {
31 public function __construct( Title $title, array $params ) {
32 parent::__construct( 'AssembleUploadChunks', $title, $params );
33 $this->removeDuplicates = true;
36 public function run() {
37 $scope = RequestContext::importScopedSession( $this->params['session'] );
38 $this->addTeardownCallback( function () use ( &$scope ) {
39 ScopedCallback::consume( $scope ); // T126450
40 } );
42 $context = RequestContext::getMain();
43 $user = $context->getUser();
44 try {
45 if ( !$user->isLoggedIn() ) {
46 $this->setLastError( "Could not load the author user from session." );
48 return false;
51 UploadBase::setSessionStatus(
52 $user,
53 $this->params['filekey'],
54 [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
57 $upload = new UploadFromChunks( $user );
58 $upload->continueChunks(
59 $this->params['filename'],
60 $this->params['filekey'],
61 new WebRequestUpload( $context->getRequest(), 'null' )
64 // Combine all of the chunks into a local file and upload that to a new stash file
65 $status = $upload->concatenateChunks();
66 if ( !$status->isGood() ) {
67 UploadBase::setSessionStatus(
68 $user,
69 $this->params['filekey'],
70 [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
72 $this->setLastError( $status->getWikiText( false, false, 'en' ) );
74 return false;
77 // We can only get warnings like 'duplicate' after concatenating the chunks
78 $status = Status::newGood();
79 $status->value = [ 'warnings' => $upload->checkWarnings() ];
81 // We have a new filekey for the fully concatenated file
82 $newFileKey = $upload->getStashFile()->getFileKey();
84 // Remove the old stash file row and first chunk file
85 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
87 // Build the image info array while we have the local reference handy
88 $apiMain = new ApiMain(); // dummy object (XXX)
89 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
91 // Cleanup any temporary local file
92 $upload->cleanupTempFile();
94 // Cache the info so the user doesn't have to wait forever to get the final info
95 UploadBase::setSessionStatus(
96 $user,
97 $this->params['filekey'],
99 'result' => 'Success',
100 'stage' => 'assembling',
101 'filekey' => $newFileKey,
102 'imageinfo' => $imageInfo,
103 'status' => $status
106 } catch ( Exception $e ) {
107 UploadBase::setSessionStatus(
108 $user,
109 $this->params['filekey'],
111 'result' => 'Failure',
112 'stage' => 'assembling',
113 'status' => Status::newFatal( 'api-error-stashfailed' )
116 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
117 // To be extra robust.
118 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
120 return false;
123 return true;
126 public function getDeduplicationInfo() {
127 $info = parent::getDeduplicationInfo();
128 if ( is_array( $info['params'] ) ) {
129 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
132 return $info;
135 public function allowRetries() {
136 return false;