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
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.
33 class UploadFromUrlJob
extends Job
{
34 const SESSION_KEYNAME
= 'wsUploadFromUrlJobData';
46 public function __construct( $title, $params, $id = 0 ) {
47 parent
::__construct( 'uploadFromUrl', $title, $params, $id );
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(),
59 $this->user
= User
::newFromName( $this->params
['userName'] );
63 if ( $wgCopyUploadAsyncTimeout ) {
64 $opts['timeout'] = $wgCopyUploadAsyncTimeout;
66 $status = $this->upload
->fetchFile( $opts );
67 if ( !$status->isOk() ) {
68 $this->leaveMessage( $status );
74 $result = $this->upload
->verifyUpload();
75 if ( $result['status'] != UploadBase
::OK
) {
76 $status = $this->upload
->convertVerifyErrorToStatus( $result );
77 $this->leaveMessage( $status );
83 if ( !$this->params
['ignoreWarnings'] ) {
84 $warnings = $this->upload
->checkWarnings();
88 $key = $this->upload
->stashFile();
90 if ( $this->params
['leaveMessage'] ) {
91 $this->user
->leaveUserMessage(
92 wfMessage( 'upload-warning-subj' )->text(),
93 wfMessage( 'upload-warning-msg',
95 $this->params
['url'] )->text()
98 wfSetupSession( $this->params
['sessionId'] );
99 $this->storeResultInSession( 'Warning',
100 'warnings', $warnings );
101 session_write_close();
109 $status = $this->upload
->performUpload(
110 $this->params
['comment'],
111 $this->params
['pageText'],
112 $this->params
['watch'],
115 $this->leaveMessage( $status );
121 * Leave a message on the user talk page or in the session according to
122 * $params['leaveMessage'].
124 * @param $status Status
126 protected function leaveMessage( $status ) {
127 if ( $this->params
['leaveMessage'] ) {
128 if ( $status->isGood() ) {
129 $this->user
->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(),
130 wfMessage( 'upload-success-msg',
131 $this->upload
->getTitle()->getText(),
135 $this->user
->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(),
136 wfMessage( 'upload-failure-msg',
137 $status->getWikiText(),
142 wfSetupSession( $this->params
['sessionId'] );
143 if ( $status->isOk() ) {
144 $this->storeResultInSession( 'Success',
145 'filename', $this->upload
->getLocalFile()->getName() );
147 $this->storeResultInSession( 'Failure',
148 'errors', $status->getErrorsArray() );
150 session_write_close();
155 * Store a result in the session data. Note that the caller is responsible
156 * for appropriate session_start and session_write_close calls.
158 * @param string $result the result (Success|Warning|Failure)
159 * @param string $dataKey the key of the extra data
160 * @param $dataValue Mixed: the extra data itself
162 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
163 $session =& self
::getSessionData( $this->params
['sessionKey'] );
164 $session['result'] = $result;
165 $session[$dataKey] = $dataValue;
169 * Initialize the session data. Sets the intial result to queued.
171 public function initializeSessionData() {
172 $session =& self
::getSessionData( $this->params
['sessionKey'] );
173 $
$session['result'] = 'Queued';
180 public static function &getSessionData( $key ) {
181 if ( !isset( $_SESSION[self
::SESSION_KEYNAME
][$key] ) ) {
182 $_SESSION[self
::SESSION_KEYNAME
][$key] = array();
185 return $_SESSION[self
::SESSION_KEYNAME
][$key];