Merge "Fix separated login link so that create account and login are always next...
[lhc/web/wiklou.git] / includes / job / 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 # Initialize this object and the upload object
52 $this->upload = new UploadFromUrl();
53 $this->upload->initialize(
54 $this->title->getText(),
55 $this->params['url'],
56 false
57 );
58 $this->user = User::newFromName( $this->params['userName'] );
59
60 # Fetch the file
61 $status = $this->upload->fetchFile();
62 if ( !$status->isOk() ) {
63 $this->leaveMessage( $status );
64 return true;
65 }
66
67 # Verify upload
68 $result = $this->upload->verifyUpload();
69 if ( $result['status'] != UploadBase::OK ) {
70 $status = $this->upload->convertVerifyErrorToStatus( $result );
71 $this->leaveMessage( $status );
72 return true;
73 }
74
75 # Check warnings
76 if ( !$this->params['ignoreWarnings'] ) {
77 $warnings = $this->upload->checkWarnings();
78 if ( $warnings ) {
79
80 # Stash the upload
81 $key = $this->upload->stashFile();
82
83 if ( $this->params['leaveMessage'] ) {
84 $this->user->leaveUserMessage(
85 wfMessage( 'upload-warning-subj' )->text(),
86 wfMessage( 'upload-warning-msg',
87 $key,
88 $this->params['url'] )->text()
89 );
90 } else {
91 wfSetupSession( $this->params['sessionId'] );
92 $this->storeResultInSession( 'Warning',
93 'warnings', $warnings );
94 session_write_close();
95 }
96
97 return true;
98 }
99 }
100
101 # Perform the upload
102 $status = $this->upload->performUpload(
103 $this->params['comment'],
104 $this->params['pageText'],
105 $this->params['watch'],
106 $this->user
107 );
108 $this->leaveMessage( $status );
109 return true;
110
111 }
112
113 /**
114 * Leave a message on the user talk page or in the session according to
115 * $params['leaveMessage'].
116 *
117 * @param $status Status
118 */
119 protected function leaveMessage( $status ) {
120 if ( $this->params['leaveMessage'] ) {
121 if ( $status->isGood() ) {
122 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(),
123 wfMessage( 'upload-success-msg',
124 $this->upload->getTitle()->getText(),
125 $this->params['url']
126 )->text() );
127 } else {
128 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(),
129 wfMessage( 'upload-failure-msg',
130 $status->getWikiText(),
131 $this->params['url']
132 )->text() );
133 }
134 } else {
135 wfSetupSession( $this->params['sessionId'] );
136 if ( $status->isOk() ) {
137 $this->storeResultInSession( 'Success',
138 'filename', $this->upload->getLocalFile()->getName() );
139 } else {
140 $this->storeResultInSession( 'Failure',
141 'errors', $status->getErrorsArray() );
142 }
143 session_write_close();
144 }
145 }
146
147 /**
148 * Store a result in the session data. Note that the caller is responsible
149 * for appropriate session_start and session_write_close calls.
150 *
151 * @param $result String: the result (Success|Warning|Failure)
152 * @param $dataKey String: the key of the extra data
153 * @param $dataValue Mixed: the extra data itself
154 */
155 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
156 $session =& self::getSessionData( $this->params['sessionKey'] );
157 $session['result'] = $result;
158 $session[$dataKey] = $dataValue;
159 }
160
161 /**
162 * Initialize the session data. Sets the intial result to queued.
163 */
164 public function initializeSessionData() {
165 $session =& self::getSessionData( $this->params['sessionKey'] );
166 $$session['result'] = 'Queued';
167 }
168
169 /**
170 * @param $key
171 * @return mixed
172 */
173 public static function &getSessionData( $key ) {
174 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
175 $_SESSION[self::SESSION_KEYNAME][$key] = array();
176 }
177 return $_SESSION[self::SESSION_KEYNAME][$key];
178 }
179 }