MediaWiki  master
AssembleUploadChunksJob.php
Go to the documentation of this file.
1 <?php
23 use 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 
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() ) {
68  $user,
69  $this->params['filekey'],
70  [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
71  );
72 
73  // the chunks did not get assembled, but this should not be considered a job
74  // failure - they simply didn't pass verification for some reason, and that
75  // reason is stored in above session to inform the clients
76  return true;
77  }
78 
79  // We can only get warnings like 'duplicate' after concatenating the chunks
80  $status = Status::newGood();
81  $status->value = [
83  $upload->checkWarnings( $user )
84  )
85  ];
86 
87  // We have a new filekey for the fully concatenated file
88  $newFileKey = $upload->getStashFile()->getFileKey();
89 
90  // Remove the old stash file row and first chunk file
91  $upload->stash->removeFileNoAuth( $this->params['filekey'] );
92 
93  // Build the image info array while we have the local reference handy
94  $apiMain = new ApiMain(); // dummy object (XXX)
95  $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
96 
97  // Cleanup any temporary local file
98  $upload->cleanupTempFile();
99 
100  // Cache the info so the user doesn't have to wait forever to get the final info
102  $user,
103  $this->params['filekey'],
104  [
105  'result' => 'Success',
106  'stage' => 'assembling',
107  'filekey' => $newFileKey,
108  'imageinfo' => $imageInfo,
109  'status' => $status
110  ]
111  );
112  } catch ( Exception $e ) {
114  $user,
115  $this->params['filekey'],
116  [
117  'result' => 'Failure',
118  'stage' => 'assembling',
119  'status' => Status::newFatal( 'api-error-stashfailed' )
120  ]
121  );
122  $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
123  // To be extra robust.
125 
126  return false;
127  }
128 
129  return true;
130  }
131 
132  public function getDeduplicationInfo() {
133  $info = parent::getDeduplicationInfo();
134  if ( is_array( $info['params'] ) ) {
135  $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
136  }
137 
138  return $info;
139  }
140 
141  public function allowRetries() {
142  return false;
143  }
144 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:48
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
UploadBase\makeWarningsSerializable
static makeWarningsSerializable( $warnings)
Convert the warnings array returned by checkWarnings() to something that can be serialized.
Definition: UploadBase.php:750
AssembleUploadChunksJob\run
run()
Run the job.
Definition: AssembleUploadChunksJob.php:36
AssembleUploadChunksJob\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: AssembleUploadChunksJob.php:132
Job\$title
Title $title
Definition: Job.php:43
UploadBase\setSessionStatus
static setSessionStatus(User $user, $statusKey, $value)
Set the current status of a chunked upload (used for polling)
Definition: UploadBase.php:2286
Job\addTeardownCallback
addTeardownCallback( $callback)
Definition: Job.php:387
Job\$params
array $params
Array of job parameters.
Definition: Job.php:37
Job\setLastError
setLastError( $error)
Definition: Job.php:461
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:32
AssembleUploadChunksJob\allowRetries
allowRetries()
bool Whether this job can be retried on failure by job runners 1.21 Stable to override Stable to over...
Definition: AssembleUploadChunksJob.php:141
WebRequestUpload
Object to access the $_FILES array.
Definition: WebRequestUpload.php:30
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
AssembleUploadChunksJob\__construct
__construct(Title $title, array $params)
Definition: AssembleUploadChunksJob.php:31
AssembleUploadChunksJob
Assemble the segments of a chunked upload.
Definition: AssembleUploadChunksJob.php:30
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:454
RequestContext\importScopedSession
static importScopedSession(array $params)
Import an client IP address, HTTP headers, user ID, and session ID.
Definition: RequestContext.php:526
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MWExceptionHandler\rollbackMasterChangesAndLog
static rollbackMasterChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Roll back any open database transactions and log the stack trace of the throwable.
Definition: MWExceptionHandler.php:125
UploadFromChunks
Implements uploading from chunks.
Definition: UploadFromChunks.php:33