3 * Job for asynchronous upload-by-url.
10 * Job for asynchronous upload-by-url.
12 * This job is in fact an interface to UploadFromUrl, which is designed such
13 * that it does not require any globals. If it does, fix it elsewhere, do not
14 * add globals in here.
18 class UploadFromUrlJob
extends Job
{
19 const SESSION_KEYNAME
= 'wsUploadFromUrlJobData';
31 public function __construct( $title, $params, $id = 0 ) {
32 parent
::__construct( 'uploadFromUrl', $title, $params, $id );
35 public function run() {
36 # Initialize this object and the upload object
37 $this->upload
= new UploadFromUrl();
38 $this->upload
->initialize(
39 $this->title
->getText(),
43 $this->user
= User
::newFromName( $this->params
['userName'] );
46 $status = $this->upload
->fetchFile();
47 if ( !$status->isOk() ) {
48 $this->leaveMessage( $status );
53 $result = $this->upload
->verifyUpload();
54 if ( $result['status'] != UploadBase
::OK
) {
55 $status = $this->upload
->convertVerifyErrorToStatus( $result );
56 $this->leaveMessage( $status );
61 if ( !$this->params
['ignoreWarnings'] ) {
62 $warnings = $this->upload
->checkWarnings();
66 $key = $this->upload
->stashFile();
68 if ( $this->params
['leaveMessage'] ) {
69 $this->user
->leaveUserMessage(
70 wfMsg( 'upload-warning-subj' ),
71 wfMsg( 'upload-warning-msg',
73 $this->params
['url'] )
76 wfSetupSession( $this->params
['sessionId'] );
77 $this->storeResultInSession( 'Warning',
78 'warnings', $warnings );
79 session_write_close();
87 $status = $this->upload
->performUpload(
88 $this->params
['comment'],
89 $this->params
['pageText'],
90 $this->params
['watch'],
93 $this->leaveMessage( $status );
99 * Leave a message on the user talk page or in the session according to
100 * $params['leaveMessage'].
102 * @param $status Status
104 protected function leaveMessage( $status ) {
105 if ( $this->params
['leaveMessage'] ) {
106 if ( $status->isGood() ) {
107 $this->user
->leaveUserMessage( wfMsg( 'upload-success-subj' ),
108 wfMsg( 'upload-success-msg',
109 $this->upload
->getTitle()->getText(),
113 $this->user
->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
114 wfMsg( 'upload-failure-msg',
115 $status->getWikiText(),
120 wfSetupSession( $this->params
['sessionId'] );
121 if ( $status->isOk() ) {
122 $this->storeResultInSession( 'Success',
123 'filename', $this->upload
->getLocalFile()->getName() );
125 $this->storeResultInSession( 'Failure',
126 'errors', $status->getErrorsArray() );
128 session_write_close();
133 * Store a result in the session data. Note that the caller is responsible
134 * for appropriate session_start and session_write_close calls.
136 * @param $result String: the result (Success|Warning|Failure)
137 * @param $dataKey String: the key of the extra data
138 * @param $dataValue Mixed: the extra data itself
140 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
141 $session =& self
::getSessionData( $this->params
['sessionKey'] );
142 $session['result'] = $result;
143 $session[$dataKey] = $dataValue;
147 * Initialize the session data. Sets the intial result to queued.
149 public function initializeSessionData() {
150 $session =& self
::getSessionData( $this->params
['sessionKey'] );
151 $
$session['result'] = 'Queued';
158 public static function &getSessionData( $key ) {
159 if ( !isset( $_SESSION[self
::SESSION_KEYNAME
][$key] ) ) {
160 $_SESSION[self
::SESSION_KEYNAME
][$key] = array();
162 return $_SESSION[self
::SESSION_KEYNAME
][$key];