Implement extension registration from an extension.json file
[mediawiki.git] / includes / jobqueue / jobs / AssembleUploadChunksJob.php
blobcc28a019c050686ef9180244df0b320910b85622
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
24 /**
25 * Assemble the segments of a chunked upload.
27 * @ingroup Upload
29 class AssembleUploadChunksJob extends Job {
30 public function __construct( $title, $params ) {
31 parent::__construct( 'AssembleUploadChunks', $title, $params );
32 $this->removeDuplicates = true;
35 public function run() {
36 $scope = RequestContext::importScopedSession( $this->params['session'] );
37 $context = RequestContext::getMain();
38 $user = $context->getUser();
39 try {
40 if ( !$user->isLoggedIn() ) {
41 $this->setLastError( "Could not load the author user from session." );
43 return false;
46 UploadBase::setSessionStatus(
47 $user,
48 $this->params['filekey'],
49 array( 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() )
52 $upload = new UploadFromChunks( $user );
53 $upload->continueChunks(
54 $this->params['filename'],
55 $this->params['filekey'],
56 $context->getRequest()
59 // Combine all of the chunks into a local file and upload that to a new stash file
60 $status = $upload->concatenateChunks();
61 if ( !$status->isGood() ) {
62 UploadBase::setSessionStatus(
63 $user,
64 $this->params['filekey'],
65 array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status )
67 $this->setLastError( $status->getWikiText() );
69 return false;
72 // We have a new filekey for the fully concatenated file
73 $newFileKey = $upload->getLocalFile()->getFileKey();
75 // Remove the old stash file row and first chunk file
76 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
78 // Build the image info array while we have the local reference handy
79 $apiMain = new ApiMain(); // dummy object (XXX)
80 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
82 // Cleanup any temporary local file
83 $upload->cleanupTempFile();
85 // Cache the info so the user doesn't have to wait forever to get the final info
86 UploadBase::setSessionStatus(
87 $user,
88 $this->params['filekey'],
89 array(
90 'result' => 'Success',
91 'stage' => 'assembling',
92 'filekey' => $newFileKey,
93 'imageinfo' => $imageInfo,
94 'status' => Status::newGood()
97 } catch ( MWException $e ) {
98 UploadBase::setSessionStatus(
99 $user,
100 $this->params['filekey'],
101 array(
102 'result' => 'Failure',
103 'stage' => 'assembling',
104 'status' => Status::newFatal( 'api-error-stashfailed' )
107 $this->setLastError( get_class( $e ) . ": " . $e->getText() );
108 // To be extra robust.
109 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
111 return false;
114 return true;
117 public function getDeduplicationInfo() {
118 $info = parent::getDeduplicationInfo();
119 if ( is_array( $info['params'] ) ) {
120 $info['params'] = array( 'filekey' => $info['params']['filekey'] );
123 return $info;
126 public function allowRetries() {
127 return false;