Merge "Add page_restrictions to readlock in lockSearchindex"
[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, $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 $context = RequestContext::getMain();
40 $user = $context->getUser();
41 try {
42 if ( !$user->isLoggedIn() ) {
43 $this->setLastError( "Could not load the author user from session." );
44
45 return false;
46 }
47
48 UploadBase::setSessionStatus(
49 $user,
50 $this->params['filekey'],
51 array( 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() )
52 );
53
54 $upload = new UploadFromStash( $user );
55 // @todo initialize() causes a GET, ideally we could frontload the antivirus
56 // checks and anything else to the stash stage (which includes concatenation and
57 // the local file is thus already there). That way, instead of GET+PUT, there could
58 // just be a COPY operation from the stash to the public zone.
59 $upload->initialize( $this->params['filekey'], $this->params['filename'] );
60
61 // Check if the local file checks out (this is generally a no-op)
62 $verification = $upload->verifyUpload();
63 if ( $verification['status'] !== UploadBase::OK ) {
64 $status = Status::newFatal( 'verification-error' );
65 $status->value = array( 'verification' => $verification );
66 UploadBase::setSessionStatus(
67 $user,
68 $this->params['filekey'],
69 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
70 );
71 $this->setLastError( "Could not verify upload." );
72
73 return false;
74 }
75
76 // Upload the stashed file to a permanent location
77 $status = $upload->performUpload(
78 $this->params['comment'],
79 $this->params['text'],
80 $this->params['watch'],
81 $user
82 );
83 if ( !$status->isGood() ) {
84 UploadBase::setSessionStatus(
85 $user,
86 $this->params['filekey'],
87 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
88 );
89 $this->setLastError( $status->getWikiText() );
90
91 return false;
92 }
93
94 // Build the image info array while we have the local reference handy
95 $apiMain = new ApiMain(); // dummy object (XXX)
96 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
97
98 // Cleanup any temporary local file
99 $upload->cleanupTempFile();
100
101 // Cache the info so the user doesn't have to wait forever to get the final info
102 UploadBase::setSessionStatus(
103 $user,
104 $this->params['filekey'],
105 array(
106 'result' => 'Success',
107 'stage' => 'publish',
108 'filename' => $upload->getLocalFile()->getName(),
109 'imageinfo' => $imageInfo,
110 'status' => Status::newGood()
111 )
112 );
113 } catch ( Exception $e ) {
114 UploadBase::setSessionStatus(
115 $user,
116 $this->params['filekey'],
117 array(
118 'result' => 'Failure',
119 'stage' => 'publish',
120 'status' => Status::newFatal( 'api-error-publishfailed' )
121 )
122 );
123 $this->setLastError( get_class( $e ) . ": " . $e->getText() );
124 // To prevent potential database referential integrity issues.
125 // See bug 32551.
126 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
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 }