Merge "jquery.tablesorter: buildCollationTable() on first sort, not on load"
[lhc/web/wiklou.git] / includes / job / 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 /**
37 * @var UploadFromUrl
38 */
39 public $upload;
40
41 /**
42 * @var User
43 */
44 protected $user;
45
46 public function __construct( $title, $params, $id = 0 ) {
47 parent::__construct( 'uploadFromUrl', $title, $params, $id );
48 }
49
50 public function run() {
51 global $wgCopyUploadAsyncTimeout;
52 # Initialize this object and the upload object
53 $this->upload = new UploadFromUrl();
54 $this->upload->initialize(
55 $this->title->getText(),
56 $this->params['url'],
57 false
58 );
59 $this->user = User::newFromName( $this->params['userName'] );
60
61 # Fetch the file
62 $opts = array();
63 if ( $wgCopyUploadAsyncTimeout ) {
64 $opts['timeout'] = $wgCopyUploadAsyncTimeout;
65 }
66 $status = $this->upload->fetchFile( $opts );
67 if ( !$status->isOk() ) {
68 $this->leaveMessage( $status );
69 return true;
70 }
71
72 # Verify upload
73 $result = $this->upload->verifyUpload();
74 if ( $result['status'] != UploadBase::OK ) {
75 $status = $this->upload->convertVerifyErrorToStatus( $result );
76 $this->leaveMessage( $status );
77 return true;
78 }
79
80 # Check warnings
81 if ( !$this->params['ignoreWarnings'] ) {
82 $warnings = $this->upload->checkWarnings();
83 if ( $warnings ) {
84
85 # Stash the upload
86 $key = $this->upload->stashFile();
87
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 wfSetupSession( $this->params['sessionId'] );
97 $this->storeResultInSession( 'Warning',
98 'warnings', $warnings );
99 session_write_close();
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 return true;
115
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 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(),
128 wfMessage( 'upload-success-msg',
129 $this->upload->getTitle()->getText(),
130 $this->params['url']
131 )->text() );
132 } else {
133 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(),
134 wfMessage( 'upload-failure-msg',
135 $status->getWikiText(),
136 $this->params['url']
137 )->text() );
138 }
139 } else {
140 wfSetupSession( $this->params['sessionId'] );
141 if ( $status->isOk() ) {
142 $this->storeResultInSession( 'Success',
143 'filename', $this->upload->getLocalFile()->getName() );
144 } else {
145 $this->storeResultInSession( 'Failure',
146 'errors', $status->getErrorsArray() );
147 }
148 session_write_close();
149 }
150 }
151
152 /**
153 * Store a result in the session data. Note that the caller is responsible
154 * for appropriate session_start and session_write_close calls.
155 *
156 * @param string $result the result (Success|Warning|Failure)
157 * @param string $dataKey the key of the extra data
158 * @param $dataValue Mixed: the extra data itself
159 */
160 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
161 $session =& self::getSessionData( $this->params['sessionKey'] );
162 $session['result'] = $result;
163 $session[$dataKey] = $dataValue;
164 }
165
166 /**
167 * Initialize the session data. Sets the intial result to queued.
168 */
169 public function initializeSessionData() {
170 $session =& self::getSessionData( $this->params['sessionKey'] );
171 $$session['result'] = 'Queued';
172 }
173
174 /**
175 * @param $key
176 * @return mixed
177 */
178 public static function &getSessionData( $key ) {
179 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
180 $_SESSION[self::SESSION_KEYNAME][$key] = array();
181 }
182 return $_SESSION[self::SESSION_KEYNAME][$key];
183 }
184 }