Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / jobqueue / jobs / AssembleUploadChunksJob.php
blob4de19bc5ae67e8d161a899dcbba285fa7acbd766
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 $title, array $params ) {
31 parent::__construct( 'AssembleUploadChunks', $title, $params );
32 $this->removeDuplicates = true;
35 public function run() {
36 /** @noinspection PhpUnusedLocalVariableInspection */
37 $scope = RequestContext::importScopedSession( $this->params['session'] );
38 $context = RequestContext::getMain();
39 $user = $context->getUser();
40 try {
41 if ( !$user->isLoggedIn() ) {
42 $this->setLastError( "Could not load the author user from session." );
44 return false;
47 UploadBase::setSessionStatus(
48 $user,
49 $this->params['filekey'],
50 array( 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() )
53 $upload = new UploadFromChunks( $user );
54 $upload->continueChunks(
55 $this->params['filename'],
56 $this->params['filekey'],
57 new WebRequestUpload( $context->getRequest(), 'null' )
60 // Combine all of the chunks into a local file and upload that to a new stash file
61 $status = $upload->concatenateChunks();
62 if ( !$status->isGood() ) {
63 UploadBase::setSessionStatus(
64 $user,
65 $this->params['filekey'],
66 array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status )
68 $this->setLastError( $status->getWikiText() );
70 return false;
73 // We have a new filekey for the fully concatenated file
74 $newFileKey = $upload->getLocalFile()->getFileKey();
76 // Remove the old stash file row and first chunk file
77 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
79 // Build the image info array while we have the local reference handy
80 $apiMain = new ApiMain(); // dummy object (XXX)
81 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
83 // Cleanup any temporary local file
84 $upload->cleanupTempFile();
86 // Cache the info so the user doesn't have to wait forever to get the final info
87 UploadBase::setSessionStatus(
88 $user,
89 $this->params['filekey'],
90 array(
91 'result' => 'Success',
92 'stage' => 'assembling',
93 'filekey' => $newFileKey,
94 'imageinfo' => $imageInfo,
95 'status' => Status::newGood()
98 } catch ( Exception $e ) {
99 UploadBase::setSessionStatus(
100 $user,
101 $this->params['filekey'],
102 array(
103 'result' => 'Failure',
104 'stage' => 'assembling',
105 'status' => Status::newFatal( 'api-error-stashfailed' )
108 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
109 // To be extra robust.
110 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
112 return false;
115 return true;
118 public function getDeduplicationInfo() {
119 $info = parent::getDeduplicationInfo();
120 if ( is_array( $info['params'] ) ) {
121 $info['params'] = array( 'filekey' => $info['params']['filekey'] );
124 return $info;
127 public function allowRetries() {
128 return false;