Merge "Add List-Unsubscribe header to emails"
[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 $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." );
42
43 return false;
44 }
45
46 UploadBase::setSessionStatus(
47 $user,
48 $this->params['filekey'],
49 array( 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() )
50 );
51
52 $upload = new UploadFromChunks( $user );
53 $upload->continueChunks(
54 $this->params['filename'],
55 $this->params['filekey'],
56 $context->getRequest()
57 );
58
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 )
66 );
67 $this->setLastError( $status->getWikiText() );
68
69 return false;
70 }
71
72 // We have a new filekey for the fully concatenated file
73 $newFileKey = $upload->getLocalFile()->getFileKey();
74
75 // Remove the old stash file row and first chunk file
76 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
77
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() );
81
82 // Cleanup any temporary local file
83 $upload->cleanupTempFile();
84
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()
95 )
96 );
97 } catch ( Exception $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' )
105 )
106 );
107 $this->setLastError( get_class( $e ) . ": " . $e->getText() );
108 // To be extra robust.
109 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
110
111 return false;
112 }
113
114 return true;
115 }
116
117 public function getDeduplicationInfo() {
118 $info = parent::getDeduplicationInfo();
119 if ( is_array( $info['params'] ) ) {
120 $info['params'] = array( 'filekey' => $info['params']['filekey'] );
121 }
122
123 return $info;
124 }
125
126 public function allowRetries() {
127 return false;
128 }
129 }