3 * Upload a file from the upload stash into the local file repo.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 * Upload a file from the upload stash into the local file repo.
31 class PublishStashedFileJob
extends Job
{
32 public function __construct( Title
$title, array $params ) {
33 parent
::__construct( 'PublishStashedFile', $title, $params );
34 $this->removeDuplicates
= true;
37 public function run() {
38 $scope = RequestContext
::importScopedSession( $this->params
['session'] );
39 $this->addTeardownCallback( function () use ( &$scope ) {
40 ScopedCallback
::consume( $scope ); // T126450
43 $context = RequestContext
::getMain();
44 $user = $context->getUser();
46 if ( !$user->isLoggedIn() ) {
47 $this->setLastError( "Could not load the author user from session." );
52 UploadBase
::setSessionStatus(
54 $this->params
['filekey'],
55 [ 'result' => 'Poll', 'stage' => 'publish', 'status' => Status
::newGood() ]
58 $upload = new UploadFromStash( $user );
59 // @todo initialize() causes a GET, ideally we could frontload the antivirus
60 // checks and anything else to the stash stage (which includes concatenation and
61 // the local file is thus already there). That way, instead of GET+PUT, there could
62 // just be a COPY operation from the stash to the public zone.
63 $upload->initialize( $this->params
['filekey'], $this->params
['filename'] );
65 // Check if the local file checks out (this is generally a no-op)
66 $verification = $upload->verifyUpload();
67 if ( $verification['status'] !== UploadBase
::OK
) {
68 $status = Status
::newFatal( 'verification-error' );
69 $status->value
= [ 'verification' => $verification ];
70 UploadBase
::setSessionStatus(
72 $this->params
['filekey'],
73 [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
75 $this->setLastError( "Could not verify upload." );
80 // Upload the stashed file to a permanent location
81 $status = $upload->performUpload(
82 $this->params
['comment'],
83 $this->params
['text'],
84 $this->params
['watch'],
86 isset( $this->params
['tags'] ) ?
$this->params
['tags'] : []
88 if ( !$status->isGood() ) {
89 UploadBase
::setSessionStatus(
91 $this->params
['filekey'],
92 [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
94 $this->setLastError( $status->getWikiText( false, false, 'en' ) );
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() );
103 // Cleanup any temporary local file
104 $upload->cleanupTempFile();
106 // Cache the info so the user doesn't have to wait forever to get the final info
107 UploadBase
::setSessionStatus(
109 $this->params
['filekey'],
111 'result' => 'Success',
112 'stage' => 'publish',
113 'filename' => $upload->getLocalFile()->getName(),
114 'imageinfo' => $imageInfo,
115 'status' => Status
::newGood()
118 } catch ( Exception
$e ) {
119 UploadBase
::setSessionStatus(
121 $this->params
['filekey'],
123 'result' => 'Failure',
124 'stage' => 'publish',
125 'status' => Status
::newFatal( 'api-error-publishfailed' )
128 $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
129 // To prevent potential database referential integrity issues.
131 MWExceptionHandler
::rollbackMasterChangesAndLog( $e );
139 public function getDeduplicationInfo() {
140 $info = parent
::getDeduplicationInfo();
141 if ( is_array( $info['params'] ) ) {
142 $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
148 public function allowRetries() {