MediaWiki REL1_37
PublishStashedFileJob.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
25
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( 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
53 UploadBase::setSessionStatus(
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 ];
71 UploadBase::setSessionStatus(
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() ) {
91 UploadBase::setSessionStatus(
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
109 UploadBase::setSessionStatus(
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 ) {
121 UploadBase::setSessionStatus(
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.
133 MWExceptionHandler::rollbackPrimaryChangesAndLog( $e );
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}
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
Class to both describe a background job and handle jobs.
Definition Job.php:37
Title $title
Definition Job.php:48
setLastError( $error)
Definition Job.php:466
addTeardownCallback( $callback)
Definition Job.php:392
array $params
Array of job parameters.
Definition Job.php:42
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.21to override
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
__construct(Title $title, array $params)
Represents a title within MediaWiki.
Definition Title.php:48
Implements uploading from previously stored file.