MediaWiki  master
PublishStashedFileJob.php
Go to the documentation of this file.
1 <?php
22 use Wikimedia\ScopedCallback;
23 
30 class PublishStashedFileJob extends Job {
31  public function __construct( Title $title, array $params ) {
32  parent::__construct( 'PublishStashedFile', $title, $params );
33  $this->removeDuplicates = true;
34  }
35 
36  public function run() {
37  $scope = RequestContext::importScopedSession( $this->params['session'] );
38  $this->addTeardownCallback( static function () use ( &$scope ) {
39  ScopedCallback::consume( $scope ); // T126450
40  } );
41 
42  $context = RequestContext::getMain();
43  $user = $context->getUser();
44  try {
45  if ( !$user->isRegistered() ) {
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' => 'publish', 'status' => Status::newGood() ]
55  );
56 
57  $upload = new UploadFromStash( $user );
58  // @todo initialize() causes a GET, ideally we could frontload the antivirus
59  // checks and anything else to the stash stage (which includes concatenation and
60  // the local file is thus already there). That way, instead of GET+PUT, there could
61  // just be a COPY operation from the stash to the public zone.
62  $upload->initialize( $this->params['filekey'], $this->params['filename'] );
63 
64  // Check if the local file checks out (this is generally a no-op)
65  $verification = $upload->verifyUpload();
66  if ( $verification['status'] !== UploadBase::OK ) {
67  $status = Status::newFatal( 'verification-error' );
68  $status->value = [ 'verification' => $verification ];
70  $user,
71  $this->params['filekey'],
72  [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
73  );
74  $this->setLastError( "Could not verify upload." );
75 
76  return false;
77  }
78 
79  // Upload the stashed file to a permanent location
80  $status = $upload->performUpload(
81  $this->params['comment'],
82  $this->params['text'],
83  $this->params['watch'],
84  $user,
85  $this->params['tags'] ?? [],
86  $this->params['watchlistexpiry'] ?? null
87  );
88  if ( !$status->isGood() ) {
90  $user,
91  $this->params['filekey'],
92  [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
93  );
94  $this->setLastError( $status->getWikiText( false, false, 'en' ) );
95 
96  return false;
97  }
98 
99  // Build the image info array while we have the local reference handy
100  $apiMain = new ApiMain(); // dummy object (XXX)
101  $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
102 
103  // Cleanup any temporary local file
104  $upload->cleanupTempFile();
105 
106  // Cache the info so the user doesn't have to wait forever to get the final info
108  $user,
109  $this->params['filekey'],
110  [
111  'result' => 'Success',
112  'stage' => 'publish',
113  'filename' => $upload->getLocalFile()->getName(),
114  'imageinfo' => $imageInfo,
115  'status' => Status::newGood()
116  ]
117  );
118  } catch ( Exception $e ) {
120  $user,
121  $this->params['filekey'],
122  [
123  'result' => 'Failure',
124  'stage' => 'publish',
125  'status' => Status::newFatal( 'api-error-publishfailed' )
126  ]
127  );
128  $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
129  // To prevent potential database referential integrity issues.
130  // See T34551.
132 
133  return false;
134  }
135 
136  return true;
137  }
138 
139  public function getDeduplicationInfo() {
140  $info = parent::getDeduplicationInfo();
141  if ( is_array( $info['params'] ) ) {
142  $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
143  }
144 
145  return $info;
146  }
147 
148  public function allowRetries() {
149  return false;
150  }
151 }
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:59
Class to both describe a background job and handle jobs.
Definition: Job.php:39
Title $title
Definition: Job.php:50
setLastError( $error)
Definition: Job.php:431
addTeardownCallback( $callback)
Definition: Job.php:353
array $params
Array of job parameters.
Definition: Job.php:44
static rollbackPrimaryChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Roll back any open database transactions and log the stack trace of the throwable.
Represents a title within MediaWiki.
Definition: Title.php:82
Upload a file from the upload stash into the local file repo.
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.
__construct(Title $title, array $params)
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 newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85
static setSessionStatus(UserIdentity $user, $statusKey, $value)
Set the current status of a chunked upload (used for polling)
Implements uploading from previously stored file.