Merge "mediawiki.jqueryMsg: Implement `<nowiki>` support"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / PublishStashedFileJob.php
1 <?php
2 /**
3 * Upload a file from the upload stash into the local file repo.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Upload
22 * @ingroup JobQueue
23 */
24
25 /**
26 * Upload a file from the upload stash into the local file repo.
27 *
28 * @ingroup Upload
29 * @ingroup JobQueue
30 */
31 class PublishStashedFileJob extends Job {
32 public function __construct( Title $title, array $params ) {
33 parent::__construct( 'PublishStashedFile', $title, $params );
34 $this->removeDuplicates = true;
35 }
36
37 public function run() {
38 $scope = RequestContext::importScopedSession( $this->params['session'] );
39 $this->addTeardownCallback( function () use ( &$scope ) {
40 ScopedCallback::consume( $scope ); // T126450
41 } );
42
43 $context = RequestContext::getMain();
44 $user = $context->getUser();
45 try {
46 if ( !$user->isLoggedIn() ) {
47 $this->setLastError( "Could not load the author user from session." );
48
49 return false;
50 }
51
52 UploadBase::setSessionStatus(
53 $user,
54 $this->params['filekey'],
55 [ 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() ]
56 );
57
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'] );
64
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(
71 $user,
72 $this->params['filekey'],
73 [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
74 );
75 $this->setLastError( "Could not verify upload." );
76
77 return false;
78 }
79
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'],
85 $user,
86 isset( $this->params['tags'] ) ? $this->params['tags'] : []
87 );
88 if ( !$status->isGood() ) {
89 UploadBase::setSessionStatus(
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
107 UploadBase::setSessionStatus(
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 ) {
119 UploadBase::setSessionStatus(
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 bug 32551.
131 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
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 }