MediaWiki  master
PublishStashedFileJob.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\ScopedCallback;
25 
32 class PublishStashedFileJob extends Job {
33  public function __construct( Title $title, array $params ) {
34  parent::__construct( 'PublishStashedFile', $title, $params );
35  $this->removeDuplicates = true;
36  }
37 
38  public function run() {
39  $scope = RequestContext::importScopedSession( $this->params['session'] );
40  $this->addTeardownCallback( function () use ( &$scope ) {
41  ScopedCallback::consume( $scope ); // T126450
42  } );
43 
44  $context = RequestContext::getMain();
45  $user = $context->getUser();
46  try {
47  if ( !$user->isLoggedIn() ) {
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' => 'publish', 'status' => Status::newGood() ]
57  );
58 
59  $upload = new UploadFromStash( $user );
60  // @todo initialize() causes a GET, ideally we could frontload the antivirus
61  // checks and anything else to the stash stage (which includes concatenation and
62  // the local file is thus already there). That way, instead of GET+PUT, there could
63  // just be a COPY operation from the stash to the public zone.
64  $upload->initialize( $this->params['filekey'], $this->params['filename'] );
65 
66  // Check if the local file checks out (this is generally a no-op)
67  $verification = $upload->verifyUpload();
68  if ( $verification['status'] !== UploadBase::OK ) {
69  $status = Status::newFatal( 'verification-error' );
70  $status->value = [ 'verification' => $verification ];
72  $user,
73  $this->params['filekey'],
74  [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
75  );
76  $this->setLastError( "Could not verify upload." );
77 
78  return false;
79  }
80 
81  // Upload the stashed file to a permanent location
82  $status = $upload->performUpload(
83  $this->params['comment'],
84  $this->params['text'],
85  $this->params['watch'],
86  $user,
87  $this->params['tags'] ?? [],
88  $this->params['watchlistexpiry'] ?? null
89  );
90  if ( !$status->isGood() ) {
92  $user,
93  $this->params['filekey'],
94  [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
95  );
96  $this->setLastError( $status->getWikiText( false, false, 'en' ) );
97 
98  return false;
99  }
100 
101  // Build the image info array while we have the local reference handy
102  $apiMain = new ApiMain(); // dummy object (XXX)
103  $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
104 
105  // Cleanup any temporary local file
106  $upload->cleanupTempFile();
107 
108  // Cache the info so the user doesn't have to wait forever to get the final info
110  $user,
111  $this->params['filekey'],
112  [
113  'result' => 'Success',
114  'stage' => 'publish',
115  'filename' => $upload->getLocalFile()->getName(),
116  'imageinfo' => $imageInfo,
117  'status' => Status::newGood()
118  ]
119  );
120  } catch ( Exception $e ) {
122  $user,
123  $this->params['filekey'],
124  [
125  'result' => 'Failure',
126  'stage' => 'publish',
127  'status' => Status::newFatal( 'api-error-publishfailed' )
128  ]
129  );
130  $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
131  // To prevent potential database referential integrity issues.
132  // See T34551.
134 
135  return false;
136  }
137 
138  return true;
139  }
140 
141  public function getDeduplicationInfo() {
142  $info = parent::getDeduplicationInfo();
143  if ( is_array( $info['params'] ) ) {
144  $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
145  }
146 
147  return $info;
148  }
149 
150  public function allowRetries() {
151  return false;
152  }
153 }
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
PublishStashedFileJob\__construct
__construct(Title $title, array $params)
Definition: PublishStashedFileJob.php:33
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
UploadFromStash
Implements uploading from previously stored file.
Definition: UploadFromStash.php:32
UploadBase\OK
const OK
Definition: UploadBase.php:101
Job\$params
array $params
Array of job parameters.
Definition: Job.php:37
Job\setLastError
setLastError( $error)
Definition: Job.php:461
PublishStashedFileJob
Upload a file from the upload stash into the local file repo.
Definition: PublishStashedFileJob.php:32
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:32
PublishStashedFileJob\allowRetries
allowRetries()
bool Whether this job can be retried on failure by job runners 1.21 Stable to override Stable to over...
Definition: PublishStashedFileJob.php:150
PublishStashedFileJob\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: PublishStashedFileJob.php:141
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
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
PublishStashedFileJob\run
run()
Run the job.
Definition: PublishStashedFileJob.php:38