MediaWiki 1.40.4
AssembleUploadChunksJob.php
Go to the documentation of this file.
1<?php
23use Wikimedia\ScopedCallback;
24
32 public function __construct( Title $title, array $params ) {
33 parent::__construct( 'AssembleUploadChunks', $title, $params );
34 $this->removeDuplicates = true;
35 }
36
37 public function run() {
38 $scope = RequestContext::importScopedSession( $this->params['session'] );
39 $this->addTeardownCallback( static function () use ( &$scope ) {
40 ScopedCallback::consume( $scope ); // T126450
41 } );
42
43 $context = RequestContext::getMain();
44 $user = $context->getUser();
45 try {
46 if ( !$user->isRegistered() ) {
47 $this->setLastError( "Could not load the author user from session." );
48
49 return false;
50 }
51
52 UploadBase::setSessionStatus(
53 $user,
54 $this->params['filekey'],
55 [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
56 );
57
58 $upload = new UploadFromChunks( $user );
59 $upload->continueChunks(
60 $this->params['filename'],
61 $this->params['filekey'],
62 new WebRequestUpload( $context->getRequest(), 'null' )
63 );
64
65 // Combine all of the chunks into a local file and upload that to a new stash file
66 $status = $upload->concatenateChunks();
67 if ( !$status->isGood() ) {
68 UploadBase::setSessionStatus(
69 $user,
70 $this->params['filekey'],
71 [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
72 );
73
74 // the chunks did not get assembled, but this should not be considered a job
75 // failure - they simply didn't pass verification for some reason, and that
76 // reason is stored in above session to inform the clients
77 return true;
78 }
79
80 // We can only get warnings like 'duplicate' after concatenating the chunks
81 $status = Status::newGood();
82 $status->value = [
83 'warnings' => UploadBase::makeWarningsSerializable(
84 $upload->checkWarnings( $user )
85 )
86 ];
87
88 // We have a new filekey for the fully concatenated file
89 $newFileKey = $upload->getStashFile()->getFileKey();
90
91 // Remove the old stash file row and first chunk file
92 $upload->stash->removeFileNoAuth( $this->params['filekey'] );
93
94 // Build the image info array while we have the local reference handy
95 $apiMain = new ApiMain(); // dummy object (XXX)
96 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
97
98 // Cleanup any temporary local file
99 $upload->cleanupTempFile();
100
101 // Cache the info so the user doesn't have to wait forever to get the final info
102 UploadBase::setSessionStatus(
103 $user,
104 $this->params['filekey'],
105 [
106 'result' => 'Success',
107 'stage' => 'assembling',
108 'filekey' => $newFileKey,
109 'imageinfo' => $imageInfo,
110 'status' => $status
111 ]
112 );
113 } catch ( Exception $e ) {
114 UploadBase::setSessionStatus(
115 $user,
116 $this->params['filekey'],
117 [
118 'result' => 'Failure',
119 'stage' => 'assembling',
120 'status' => Status::newFatal( 'api-error-stashfailed' )
121 ]
122 );
123 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
124 // To be extra robust.
125 MWExceptionHandler::rollbackPrimaryChangesAndLog( $e );
126
127 return false;
128 }
129
130 return true;
131 }
132
133 public function getDeduplicationInfo() {
134 $info = parent::getDeduplicationInfo();
135 if ( is_array( $info['params'] ) ) {
136 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
137 }
138
139 return $info;
140 }
141
142 public function allowRetries() {
143 return false;
144 }
145}
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:58
Assemble the segments of a chunked upload.
__construct(Title $title, array $params)
allowRetries()
bool Whether this job can be retried on failure by job runners 1.21to override
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Class to both describe a background job and handle jobs.
Definition Job.php:39
setLastError( $error)
Definition Job.php:429
addTeardownCallback( $callback)
Definition Job.php:355
Object to access the $_FILES array.
Represents a title within MediaWiki.
Definition Title.php:82
Implements uploading from chunks.