MediaWiki  master
AssembleUploadChunksJob.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\ScopedCallback;
25 
33  public function __construct( Title $title, array $params ) {
34  parent::__construct( 'AssembleUploadChunks', $title, $params );
35  $this->removeDuplicates = true;
36  }
37 
38  public function run() {
39  $scope = RequestContext::importScopedSession( $this->params['session'] );
40  $this->addTeardownCallback( static function () use ( &$scope ) {
41  ScopedCallback::consume( $scope ); // T126450
42  } );
43 
44  $context = RequestContext::getMain();
45  $user = $context->getUser();
46  try {
47  if ( !$user->isRegistered() ) {
48  $this->setLastError( "Could not load the author user from session." );
49 
50  return false;
51  }
52 
54  $user,
55  $this->params['filekey'],
56  [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
57  );
58 
59  $upload = new UploadFromChunks( $user );
60  $upload->continueChunks(
61  $this->params['filename'],
62  $this->params['filekey'],
63  new WebRequestUpload( $context->getRequest(), 'null' )
64  );
65 
66  // Combine all of the chunks into a local file and upload that to a new stash file
67  $status = $upload->concatenateChunks();
68  if ( !$status->isGood() ) {
70  $user,
71  $this->params['filekey'],
72  [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
73  );
74 
75  // the chunks did not get assembled, but this should not be considered a job
76  // failure - they simply didn't pass verification for some reason, and that
77  // reason is stored in above session to inform the clients
78  return true;
79  }
80 
81  // We can only get warnings like 'duplicate' after concatenating the chunks
82  $status = Status::newGood();
83  $status->value = [
85  $upload->checkWarnings( $user )
86  )
87  ];
88 
89  // We have a new filekey for the fully concatenated file
90  $newFileKey = $upload->getStashFile()->getFileKey();
91 
92  // Remove the old stash file row and first chunk file
93  $upload->stash->removeFileNoAuth( $this->params['filekey'] );
94 
95  // Build the image info array while we have the local reference handy
96  $apiMain = new ApiMain(); // dummy object (XXX)
97  $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
98 
99  // Cleanup any temporary local file
100  $upload->cleanupTempFile();
101 
102  // Cache the info so the user doesn't have to wait forever to get the final info
104  $user,
105  $this->params['filekey'],
106  [
107  'result' => 'Success',
108  'stage' => 'assembling',
109  'filekey' => $newFileKey,
110  'imageinfo' => $imageInfo,
111  'status' => $status
112  ]
113  );
114  } catch ( Exception $e ) {
116  $user,
117  $this->params['filekey'],
118  [
119  'result' => 'Failure',
120  'stage' => 'assembling',
121  'status' => Status::newFatal( 'api-error-stashfailed' )
122  ]
123  );
124  $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
125  // To be extra robust.
127 
128  return false;
129  }
130 
131  return true;
132  }
133 
134  public function getDeduplicationInfo() {
135  $info = parent::getDeduplicationInfo();
136  if ( is_array( $info['params'] ) ) {
137  $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
138  }
139 
140  return $info;
141  }
142 
143  public function allowRetries() {
144  return false;
145  }
146 }
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:64
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.21Stability: stableto 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:40
Title $title
Definition: Job.php:51
setLastError( $error)
Definition: Job.php:432
addTeardownCallback( $callback)
Definition: Job.php:354
array $params
Array of job parameters.
Definition: Job.php:45
static rollbackPrimaryChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Roll back any open database transactions and log the stack trace of the throwable.
Object to access the $_FILES array.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
Represents a title within MediaWiki.
Definition: Title.php:76
static importScopedSession(array $params)
Import a client IP address, HTTP headers, user ID, and session ID.
static getMain()
Get the RequestContext object associated with the main request.
static makeWarningsSerializable( $warnings)
Convert the warnings array returned by checkWarnings() to something that can be serialized.
Definition: UploadBase.php:720
static setSessionStatus(UserIdentity $user, $statusKey, $value)
Set the current status of a chunked upload (used for polling).
Implements uploading from chunks.