MediaWiki master
AssembleUploadChunksJob.php
Go to the documentation of this file.
1<?php
25use Wikimedia\ScopedCallback;
26
34 public function __construct( array $params ) {
35 parent::__construct( 'AssembleUploadChunks', $params );
36 $this->removeDuplicates = true;
37 }
38
39 public function run() {
40 $scope = RequestContext::importScopedSession( $this->params['session'] );
41 $this->addTeardownCallback( static function () use ( &$scope ) {
42 ScopedCallback::consume( $scope ); // T126450
43 } );
44
45 $logger = LoggerFactory::getInstance( 'upload' );
46 $context = RequestContext::getMain();
47 $user = $context->getUser();
48 try {
49 if ( !$user->isRegistered() ) {
50 $this->setLastError( "Could not load the author user from session." );
51
52 return false;
53 }
54
55 // TODO add some sort of proper locking maybe
56 $startingStatus = UploadBase::getSessionStatus( $user, $this->params['filekey'] );
57 if (
58 !$startingStatus ||
59 ( $startingStatus['result'] ?? '' ) !== 'Poll' ||
60 ( $startingStatus['stage'] ?? '' ) !== 'queued'
61 ) {
62 $logger->warning( "Tried to assemble upload that is in stage {stage}/{result}",
63 [
64 'stage' => $startingStatus['stage'] ?? '-',
65 'result' => $startingStatus['result'] ?? '-',
66 'status' => (string)( $startingStatus['status'] ?? '-' ),
67 'filekey' => $this->params['filekey'],
68 'filename' => $this->params['filename'],
69 'user' => $user->getName(),
70 ]
71 );
72 // If it is marked as currently in progress, abort. Otherwise
73 // assume it is some sort of replag issue or maybe a retry even
74 // though retries are impossible and just warn.
75 if (
76 $startingStatus &&
77 $startingStatus['stage'] === 'assembling' &&
78 $startingStatus['result'] !== 'Failure'
79 ) {
80 $this->setLastError( __METHOD__ . " already in progress" );
81 return false;
82 }
83 }
84 UploadBase::setSessionStatus(
85 $user,
86 $this->params['filekey'],
87 [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
88 );
89
90 $upload = new UploadFromChunks( $user );
91 $upload->continueChunks(
92 $this->params['filename'],
93 $this->params['filekey'],
94 new WebRequestUpload( $context->getRequest(), 'null' )
95 );
96 if (
97 isset( $this->params['filesize'] ) &&
98 $this->params['filesize'] !== (int)$upload->getOffset()
99 ) {
100 // Check to make sure we are not executing prior to the API's
101 // transaction being committed. (T350917)
102 throw new UnexpectedValueException(
103 "UploadStash file size does not match job's. Potential mis-nested transaction?"
104 );
105 }
106 // Combine all of the chunks into a local file and upload that to a new stash file
107 $status = $upload->concatenateChunks();
108 if ( !$status->isGood() ) {
109 UploadBase::setSessionStatus(
110 $user,
111 $this->params['filekey'],
112 [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
113 );
114 $logger->info( "Chunked upload assembly job failed for {filekey} because {status}",
115 [
116 'filekey' => $this->params['filekey'],
117 'filename' => $this->params['filename'],
118 'user' => $user->getName(),
119 'status' => (string)$status
120 ]
121 );
122 // the chunks did not get assembled, but this should not be considered a job
123 // failure - they simply didn't pass verification for some reason, and that
124 // reason is stored in above session to inform the clients
125 return true;
126 }
127
128 // We can only get warnings like 'duplicate' after concatenating the chunks
129 $status = Status::newGood();
130 $status->value = [
131 'warnings' => UploadBase::makeWarningsSerializable(
132 $upload->checkWarnings( $user )
133 )
134 ];
135
136 // We have a new filekey for the fully concatenated file
137 $newFileKey = $upload->getStashFile()->getFileKey();
138
139 // Remove the old stash file row and first chunk file
140 // Note: This does not delete the chunks, only the stash file
141 // which is same as first chunk but with a different name.
142 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
143
144 // Build the image info array while we have the local reference handy
145 $apiUpload = ApiUpload::getDummyInstance();
146 $imageInfo = $apiUpload->getUploadImageInfo( $upload );
147
148 // Cleanup any temporary local file
149 $upload->cleanupTempFile();
150
151 // Cache the info so the user doesn't have to wait forever to get the final info
152 UploadBase::setSessionStatus(
153 $user,
154 $this->params['filekey'],
155 [
156 'result' => 'Success',
157 'stage' => 'assembling',
158 'filekey' => $newFileKey,
159 'imageinfo' => $imageInfo,
160 'status' => $status
161 ]
162 );
163 $logger->info( "{filekey} successfully assembled into {newkey}",
164 [
165 'filekey' => $this->params['filekey'],
166 'newkey' => $newFileKey,
167 'filename' => $this->params['filename'],
168 'user' => $user->getName(),
169 'status' => (string)$status
170 ]
171 );
172 } catch ( Exception $e ) {
173 UploadBase::setSessionStatus(
174 $user,
175 $this->params['filekey'],
176 [
177 'result' => 'Failure',
178 'stage' => 'assembling',
179 'status' => Status::newFatal( 'api-error-stashfailed' )
180 ]
181 );
182 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
183 // To be extra robust.
184 MWExceptionHandler::rollbackPrimaryChangesAndLog( $e );
185
186 return false;
187 }
188
189 return true;
190 }
191
192 public function getDeduplicationInfo() {
193 $info = parent::getDeduplicationInfo();
194 if ( is_array( $info['params'] ) ) {
195 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
196 }
197
198 return $info;
199 }
200
201 public function allowRetries() {
202 return false;
203 }
204}
array $params
The job parameters.
addTeardownCallback( $callback)
This is actually implemented in the Job class.
setLastError( $error)
This is actually implemented in the Job class.
static getDummyInstance()
Assemble the segments of a chunked upload.
allowRetries()
In some setups (i.e. when using change-propagation) jobs may still be retried even when this is false...
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Describe and execute a background job.
Definition Job.php:40
Group all the pieces relevant to the context of a request into one instance.
Create PSR-3 logger objects.
Object to access the $_FILES array.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
cleanupTempFile()
If we've modified the upload file, then we need to manually remove it on exit to clean up.
checkWarnings( $user=null)
Check for non fatal problems with the file.
Implements uploading from chunks.
Interface for generic jobs only uses the parameters field and are JSON serializable.