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