MediaWiki  REL1_19
UploadFromUrlJob.php
Go to the documentation of this file.
00001 <?php
00018 class UploadFromUrlJob extends Job {
00019         const SESSION_KEYNAME = 'wsUploadFromUrlJobData';
00020 
00024         public $upload;
00025 
00029         protected $user;
00030 
00031         public function __construct( $title, $params, $id = 0 ) {
00032                 parent::__construct( 'uploadFromUrl', $title, $params, $id );
00033         }
00034 
00035         public function run() {
00036                 # Initialize this object and the upload object
00037                 $this->upload = new UploadFromUrl();
00038                 $this->upload->initialize(
00039                         $this->title->getText(),
00040                         $this->params['url'],
00041                         false
00042                 );
00043                 $this->user = User::newFromName( $this->params['userName'] );
00044 
00045                 # Fetch the file
00046                 $status = $this->upload->fetchFile();
00047                 if ( !$status->isOk() ) {
00048                         $this->leaveMessage( $status );
00049                         return true;
00050                 }
00051 
00052                 # Verify upload
00053                 $result = $this->upload->verifyUpload();
00054                 if ( $result['status'] != UploadBase::OK ) {
00055                         $status = $this->upload->convertVerifyErrorToStatus( $result );
00056                         $this->leaveMessage( $status );
00057                         return true;
00058                 }
00059 
00060                 # Check warnings
00061                 if ( !$this->params['ignoreWarnings'] ) {
00062                         $warnings = $this->upload->checkWarnings();
00063                         if ( $warnings ) {
00064 
00065                                 # Stash the upload
00066                                 $key = $this->upload->stashFile();
00067 
00068                                 if ( $this->params['leaveMessage'] ) {
00069                                         $this->user->leaveUserMessage(
00070                                                 wfMsg( 'upload-warning-subj' ),
00071                                                 wfMsg( 'upload-warning-msg',
00072                                                         $key,
00073                                                         $this->params['url'] )
00074                                         );
00075                                 } else {
00076                                         wfSetupSession( $this->params['sessionId'] );
00077                                         $this->storeResultInSession( 'Warning',
00078                                                 'warnings', $warnings );
00079                                         session_write_close();
00080                                 }
00081 
00082                                 return true;
00083                         }
00084                 }
00085 
00086                 # Perform the upload
00087                 $status = $this->upload->performUpload(
00088                         $this->params['comment'],
00089                         $this->params['pageText'],
00090                         $this->params['watch'],
00091                         $this->user
00092                 );
00093                 $this->leaveMessage( $status );
00094                 return true;
00095 
00096         }
00097 
00104         protected function leaveMessage( $status ) {
00105                 if ( $this->params['leaveMessage'] ) {
00106                         if ( $status->isGood() ) {
00107                                 $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ),
00108                                         wfMsg( 'upload-success-msg',
00109                                                 $this->upload->getTitle()->getText(),
00110                                                 $this->params['url']
00111                                         ) );
00112                         } else {
00113                                 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
00114                                         wfMsg( 'upload-failure-msg',
00115                                                 $status->getWikiText(),
00116                                                 $this->params['url']
00117                                         ) );
00118                         }
00119                 } else {
00120                         wfSetupSession( $this->params['sessionId'] );
00121                         if ( $status->isOk() ) {
00122                                 $this->storeResultInSession( 'Success',
00123                                         'filename', $this->upload->getLocalFile()->getName() );
00124                         } else {
00125                                 $this->storeResultInSession( 'Failure',
00126                                         'errors', $status->getErrorsArray() );
00127                         }
00128                         session_write_close();
00129                 }
00130         }
00131 
00140         protected function storeResultInSession( $result, $dataKey, $dataValue ) {
00141                 $session =& self::getSessionData( $this->params['sessionKey'] );
00142                 $session['result'] = $result;
00143                 $session[$dataKey] = $dataValue;
00144         }
00145 
00149         public function initializeSessionData() {
00150                 $session =& self::getSessionData( $this->params['sessionKey'] );
00151                 $$session['result'] = 'Queued';
00152         }
00153 
00158         public static function &getSessionData( $key ) {
00159                 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
00160                         $_SESSION[self::SESSION_KEYNAME][$key] = array();
00161                 }
00162                 return $_SESSION[self::SESSION_KEYNAME][$key];
00163         }
00164 }