MediaWiki
REL1_20
|
00001 <?php 00033 class UploadFromUrlJob extends Job { 00034 const SESSION_KEYNAME = 'wsUploadFromUrlJobData'; 00035 00039 public $upload; 00040 00044 protected $user; 00045 00046 public function __construct( $title, $params, $id = 0 ) { 00047 parent::__construct( 'uploadFromUrl', $title, $params, $id ); 00048 } 00049 00050 public function run() { 00051 # Initialize this object and the upload object 00052 $this->upload = new UploadFromUrl(); 00053 $this->upload->initialize( 00054 $this->title->getText(), 00055 $this->params['url'], 00056 false 00057 ); 00058 $this->user = User::newFromName( $this->params['userName'] ); 00059 00060 # Fetch the file 00061 $status = $this->upload->fetchFile(); 00062 if ( !$status->isOk() ) { 00063 $this->leaveMessage( $status ); 00064 return true; 00065 } 00066 00067 # Verify upload 00068 $result = $this->upload->verifyUpload(); 00069 if ( $result['status'] != UploadBase::OK ) { 00070 $status = $this->upload->convertVerifyErrorToStatus( $result ); 00071 $this->leaveMessage( $status ); 00072 return true; 00073 } 00074 00075 # Check warnings 00076 if ( !$this->params['ignoreWarnings'] ) { 00077 $warnings = $this->upload->checkWarnings(); 00078 if ( $warnings ) { 00079 00080 # Stash the upload 00081 $key = $this->upload->stashFile(); 00082 00083 if ( $this->params['leaveMessage'] ) { 00084 $this->user->leaveUserMessage( 00085 wfMessage( 'upload-warning-subj' )->text(), 00086 wfMessage( 'upload-warning-msg', 00087 $key, 00088 $this->params['url'] )->text() 00089 ); 00090 } else { 00091 wfSetupSession( $this->params['sessionId'] ); 00092 $this->storeResultInSession( 'Warning', 00093 'warnings', $warnings ); 00094 session_write_close(); 00095 } 00096 00097 return true; 00098 } 00099 } 00100 00101 # Perform the upload 00102 $status = $this->upload->performUpload( 00103 $this->params['comment'], 00104 $this->params['pageText'], 00105 $this->params['watch'], 00106 $this->user 00107 ); 00108 $this->leaveMessage( $status ); 00109 return true; 00110 00111 } 00112 00119 protected function leaveMessage( $status ) { 00120 if ( $this->params['leaveMessage'] ) { 00121 if ( $status->isGood() ) { 00122 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(), 00123 wfMessage( 'upload-success-msg', 00124 $this->upload->getTitle()->getText(), 00125 $this->params['url'] 00126 )->text() ); 00127 } else { 00128 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(), 00129 wfMessage( 'upload-failure-msg', 00130 $status->getWikiText(), 00131 $this->params['url'] 00132 )->text() ); 00133 } 00134 } else { 00135 wfSetupSession( $this->params['sessionId'] ); 00136 if ( $status->isOk() ) { 00137 $this->storeResultInSession( 'Success', 00138 'filename', $this->upload->getLocalFile()->getName() ); 00139 } else { 00140 $this->storeResultInSession( 'Failure', 00141 'errors', $status->getErrorsArray() ); 00142 } 00143 session_write_close(); 00144 } 00145 } 00146 00155 protected function storeResultInSession( $result, $dataKey, $dataValue ) { 00156 $session =& self::getSessionData( $this->params['sessionKey'] ); 00157 $session['result'] = $result; 00158 $session[$dataKey] = $dataValue; 00159 } 00160 00164 public function initializeSessionData() { 00165 $session =& self::getSessionData( $this->params['sessionKey'] ); 00166 $$session['result'] = 'Queued'; 00167 } 00168 00173 public static function &getSessionData( $key ) { 00174 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) { 00175 $_SESSION[self::SESSION_KEYNAME][$key] = array(); 00176 } 00177 return $_SESSION[self::SESSION_KEYNAME][$key]; 00178 } 00179 }