Merge "Remove fix for a 5.3 problem"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / AssembleUploadChunksJob.php
1 <?php
2 /**
3 * Assemble the segments of a chunked upload.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Upload
22 */
23
24 /**
25 * Assemble the segments of a chunked upload.
26 *
27 * @ingroup Upload
28 */
29 class AssembleUploadChunksJob extends Job {
30 public function __construct( Title $title, array $params ) {
31 parent::__construct( 'AssembleUploadChunks', $title, $params );
32 $this->removeDuplicates = true;
33 }
34
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." );
43
44 return false;
45 }
46
47 UploadBase::setSessionStatus(
48 $user,
49 $this->params['filekey'],
50 [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
51 );
52
53 $upload = new UploadFromChunks( $user );
54 $upload->continueChunks(
55 $this->params['filename'],
56 $this->params['filekey'],
57 new WebRequestUpload( $context->getRequest(), 'null' )
58 );
59
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 [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
67 );
68 $this->setLastError( $status->getWikiText() );
69
70 return false;
71 }
72
73 // We have a new filekey for the fully concatenated file
74 $newFileKey = $upload->getLocalFile()->getFileKey();
75
76 // Remove the old stash file row and first chunk file
77 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
78
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() );
82
83 // Cleanup any temporary local file
84 $upload->cleanupTempFile();
85
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 [
91 'result' => 'Success',
92 'stage' => 'assembling',
93 'filekey' => $newFileKey,
94 'imageinfo' => $imageInfo,
95 'status' => Status::newGood()
96 ]
97 );
98 } catch ( Exception $e ) {
99 UploadBase::setSessionStatus(
100 $user,
101 $this->params['filekey'],
102 [
103 'result' => 'Failure',
104 'stage' => 'assembling',
105 'status' => Status::newFatal( 'api-error-stashfailed' )
106 ]
107 );
108 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
109 // To be extra robust.
110 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
111
112 return false;
113 }
114
115 return true;
116 }
117
118 public function getDeduplicationInfo() {
119 $info = parent::getDeduplicationInfo();
120 if ( is_array( $info['params'] ) ) {
121 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
122 }
123
124 return $info;
125 }
126
127 public function allowRetries() {
128 return false;
129 }
130 }