Merge "Typo fix"
[mediawiki.git] / includes / job / jobs / UploadFromUrlJob.php
blob875491405c7caafe6d1086cb96dc3daa971eb277
1 <?php
2 /**
3 * Job for asynchronous upload-by-url.
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.
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.
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
20 * @file
21 * @ingroup JobQueue
24 /**
25 * Job for asynchronous upload-by-url.
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.
31 * @ingroup JobQueue
33 class UploadFromUrlJob extends Job {
34 const SESSION_KEYNAME = 'wsUploadFromUrlJobData';
36 /**
37 * @var UploadFromUrl
39 public $upload;
41 /**
42 * @var User
44 protected $user;
46 public function __construct( $title, $params, $id = 0 ) {
47 parent::__construct( 'uploadFromUrl', $title, $params, $id );
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
58 $this->user = User::newFromName( $this->params['userName'] );
60 # Fetch the file
61 $status = $this->upload->fetchFile();
62 if ( !$status->isOk() ) {
63 $this->leaveMessage( $status );
64 return true;
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;
75 # Check warnings
76 if ( !$this->params['ignoreWarnings'] ) {
77 $warnings = $this->upload->checkWarnings();
78 if ( $warnings ) {
80 # Stash the upload
81 $key = $this->upload->stashFile();
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()
90 } else {
91 wfSetupSession( $this->params['sessionId'] );
92 $this->storeResultInSession( 'Warning',
93 'warnings', $warnings );
94 session_write_close();
97 return true;
101 # Perform the upload
102 $status = $this->upload->performUpload(
103 $this->params['comment'],
104 $this->params['pageText'],
105 $this->params['watch'],
106 $this->user
108 $this->leaveMessage( $status );
109 return true;
114 * Leave a message on the user talk page or in the session according to
115 * $params['leaveMessage'].
117 * @param $status Status
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() );
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() );
143 session_write_close();
148 * Store a result in the session data. Note that the caller is responsible
149 * for appropriate session_start and session_write_close calls.
151 * @param string $result the result (Success|Warning|Failure)
152 * @param string $dataKey the key of the extra data
153 * @param $dataValue Mixed: the extra data itself
155 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
156 $session =& self::getSessionData( $this->params['sessionKey'] );
157 $session['result'] = $result;
158 $session[$dataKey] = $dataValue;
162 * Initialize the session data. Sets the intial result to queued.
164 public function initializeSessionData() {
165 $session =& self::getSessionData( $this->params['sessionKey'] );
166 $$session['result'] = 'Queued';
170 * @param $key
171 * @return mixed
173 public static function &getSessionData( $key ) {
174 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
175 $_SESSION[self::SESSION_KEYNAME][$key] = array();
177 return $_SESSION[self::SESSION_KEYNAME][$key];