Merge "mw.Upload.BookletLayout: Don't explode when the API call fails with 'exception'"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / UploadFromUrlJob.php
1 <?php
2 /**
3 * Job for asynchronous upload-by-url.
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 JobQueue
22 */
23
24 /**
25 * Job for asynchronous upload-by-url.
26 *
27 * This job is in fact an interface to UploadFromUrl, which is designed such
28 * that it does not require any globals. If it does, fix it elsewhere, do not
29 * add globals in here.
30 *
31 * @ingroup JobQueue
32 */
33 class UploadFromUrlJob extends Job {
34 const SESSION_KEYNAME = 'wsUploadFromUrlJobData';
35
36 /** @var UploadFromUrl */
37 public $upload;
38
39 /** @var User */
40 protected $user;
41
42 public function __construct( Title $title, array $params ) {
43 parent::__construct( 'uploadFromUrl', $title, $params );
44 }
45
46 public function run() {
47 global $wgCopyUploadAsyncTimeout;
48 # Initialize this object and the upload object
49 $this->upload = new UploadFromUrl();
50 $this->upload->initialize(
51 $this->title->getText(),
52 $this->params['url'],
53 false
54 );
55 $this->user = User::newFromName( $this->params['userName'] );
56
57 # Fetch the file
58 $opts = array();
59 if ( $wgCopyUploadAsyncTimeout ) {
60 $opts['timeout'] = $wgCopyUploadAsyncTimeout;
61 }
62 $status = $this->upload->fetchFile( $opts );
63 if ( !$status->isOk() ) {
64 $this->leaveMessage( $status );
65
66 return true;
67 }
68
69 # Verify upload
70 $result = $this->upload->verifyUpload();
71 if ( $result['status'] != UploadBase::OK ) {
72 $status = $this->upload->convertVerifyErrorToStatus( $result );
73 $this->leaveMessage( $status );
74
75 return true;
76 }
77
78 # Check warnings
79 if ( !$this->params['ignoreWarnings'] ) {
80 $warnings = $this->upload->checkWarnings();
81 if ( $warnings ) {
82
83 # Stash the upload
84 $key = $this->upload->stashFile( $this->user );
85
86 // @todo FIXME: This has been broken for a while.
87 // User::leaveUserMessage() does not exist.
88 if ( $this->params['leaveMessage'] ) {
89 $this->user->leaveUserMessage(
90 wfMessage( 'upload-warning-subj' )->text(),
91 wfMessage( 'upload-warning-msg',
92 $key,
93 $this->params['url'] )->text()
94 );
95 } else {
96 $session = MediaWiki\Session\SessionManager::singleton()
97 ->getSessionById( $this->params['sessionId'] );
98 $this->storeResultInSession( $session, 'Warning',
99 'warnings', $warnings );
100 }
101
102 return true;
103 }
104 }
105
106 # Perform the upload
107 $status = $this->upload->performUpload(
108 $this->params['comment'],
109 $this->params['pageText'],
110 $this->params['watch'],
111 $this->user
112 );
113 $this->leaveMessage( $status );
114
115 return true;
116 }
117
118 /**
119 * Leave a message on the user talk page or in the session according to
120 * $params['leaveMessage'].
121 *
122 * @param Status $status
123 */
124 protected function leaveMessage( $status ) {
125 if ( $this->params['leaveMessage'] ) {
126 if ( $status->isGood() ) {
127 // @todo FIXME: user->leaveUserMessage does not exist.
128 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(),
129 wfMessage( 'upload-success-msg',
130 $this->upload->getTitle()->getText(),
131 $this->params['url']
132 )->text() );
133 } else {
134 // @todo FIXME: user->leaveUserMessage does not exist.
135 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(),
136 wfMessage( 'upload-failure-msg',
137 $status->getWikiText(),
138 $this->params['url']
139 )->text() );
140 }
141 } else {
142 $session = MediaWiki\Session\SessionManager::singleton()
143 ->getSessionById( $this->params['sessionId'] );
144 if ( $status->isOk() ) {
145 $this->storeResultInSession( $session, 'Success',
146 'filename', $this->upload->getLocalFile()->getName() );
147 } else {
148 $this->storeResultInSession( $session, 'Failure',
149 'errors', $status->getErrorsArray() );
150 }
151 }
152 }
153
154 /**
155 * Store a result in the session data. Note that the caller is responsible
156 * for appropriate session_start and session_write_close calls.
157 *
158 * @param MediaWiki\\Session\\Session|null $session Session to store result into
159 * @param string $result The result (Success|Warning|Failure)
160 * @param string $dataKey The key of the extra data
161 * @param mixed $dataValue The extra data itself
162 */
163 protected function storeResultInSession(
164 MediaWiki\Session\Session $session = null, $result, $dataKey, $dataValue
165 ) {
166 if ( $session ) {
167 $data = self::getSessionData( $session, $this->params['sessionKey'] );
168 $data['result'] = $result;
169 $data[$dataKey] = $dataValue;
170 self::setSessionData( $session, $this->params['sessionKey'], $data );
171 } else {
172 wfDebug( __METHOD__ . ': Cannot store result in session, session does not exist' );
173 }
174 }
175
176 /**
177 * Initialize the session data. Sets the initial result to queued.
178 */
179 public function initializeSessionData() {
180 $session = MediaWiki\Session\SessionManager::getGlobalSession();
181 $data = self::getSessionData( $session, $this->params['sessionKey'] );
182 $data['result'] = 'Queued';
183 self::setSessionData( $session, $this->params['sessionKey'], $data );
184 }
185
186 /**
187 * @param MediaWiki\\Session\\Session $session
188 * @param string $key
189 * @return mixed
190 */
191 public static function getSessionData( MediaWiki\Session\Session $session, $key ) {
192 $data = $session->get( self::SESSION_KEYNAME );
193 if ( !is_array( $data ) || !isset( $data[$key] ) ) {
194 self::setSessionData( $session, $key, array() );
195 return array();
196 }
197 return $data[$key];
198 }
199
200 /**
201 * @param MediaWiki\\Session\\Session $session
202 * @param string $key
203 * @param mixed $value
204 */
205 public static function setSessionData( MediaWiki\Session\Session $session, $key, $value ) {
206 $data = $session->get( self::SESSION_KEYNAME, array() );
207 if ( !is_array( $data ) ) {
208 $data = array();
209 }
210 $data[$key] = $value;
211 $session->set( self::SESSION_KEYNAME, $data );
212 }
213 }