Update formatting of job related files
[lhc/web/wiklou.git] / includes / job / 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 */
23
24 /**
25 * Upload a file from the upload stash into the local file repo.
26 *
27 * @ingroup Upload
28 */
29 class PublishStashedFileJob extends Job {
30 public function __construct( $title, $params, $id = 0 ) {
31 parent::__construct( 'PublishStashedFile', $title, $params, $id );
32 $this->removeDuplicates = true;
33 }
34
35 public function run() {
36 $scope = RequestContext::importScopedSession( $this->params['session'] );
37 $context = RequestContext::getMain();
38 try {
39 $user = $context->getUser();
40 if ( !$user->isLoggedIn() ) {
41 $this->setLastError( "Could not load the author user from session." );
42
43 return false;
44 }
45
46 if ( count( $_SESSION ) === 0 ) {
47 // Empty session probably indicates that we didn't associate
48 // with the session correctly. Note that being able to load
49 // the user does not necessarily mean the session was loaded.
50 // Most likely cause by suhosin.session.encrypt = On.
51 $this->setLastError( "Error associating with user session. Try setting suhosin.session.encrypt = Off" );
52
53 return false;
54 }
55
56 UploadBase::setSessionStatus(
57 $this->params['filekey'],
58 array( 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() )
59 );
60
61 $upload = new UploadFromStash( $user );
62 // @todo initialize() causes a GET, ideally we could frontload the antivirus
63 // checks and anything else to the stash stage (which includes concatenation and
64 // the local file is thus already there). That way, instead of GET+PUT, there could
65 // just be a COPY operation from the stash to the public zone.
66 $upload->initialize( $this->params['filekey'], $this->params['filename'] );
67
68 // Check if the local file checks out (this is generally a no-op)
69 $verification = $upload->verifyUpload();
70 if ( $verification['status'] !== UploadBase::OK ) {
71 $status = Status::newFatal( 'verification-error' );
72 $status->value = array( 'verification' => $verification );
73 UploadBase::setSessionStatus(
74 $this->params['filekey'],
75 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
76 );
77 $this->setLastError( "Could not verify upload." );
78
79 return false;
80 }
81
82 // Upload the stashed file to a permanent location
83 $status = $upload->performUpload(
84 $this->params['comment'],
85 $this->params['text'],
86 $this->params['watch'],
87 $user
88 );
89 if ( !$status->isGood() ) {
90 UploadBase::setSessionStatus(
91 $this->params['filekey'],
92 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
93 );
94 $this->setLastError( $status->getWikiText() );
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 $this->params['filekey'],
109 array(
110 'result' => 'Success',
111 'stage' => 'publish',
112 'filename' => $upload->getLocalFile()->getName(),
113 'imageinfo' => $imageInfo,
114 'status' => Status::newGood()
115 )
116 );
117 } catch ( MWException $e ) {
118 UploadBase::setSessionStatus(
119 $this->params['filekey'],
120 array(
121 'result' => 'Failure',
122 'stage' => 'publish',
123 'status' => Status::newFatal( 'api-error-publishfailed' )
124 )
125 );
126 $this->setLastError( get_class( $e ) . ": " . $e->getText() );
127
128 return false;
129 }
130
131 return true;
132 }
133
134 public function getDeduplicationInfo() {
135 $info = parent::getDeduplicationInfo();
136 if ( is_array( $info['params'] ) ) {
137 $info['params'] = array( 'filekey' => $info['params']['filekey'] );
138 }
139
140 return $info;
141 }
142
143 public function allowRetries() {
144 return false;
145 }
146 }