MediaWiki
REL1_24
|
00001 <?php 00033 class UploadFromUrlJob extends Job { 00034 const SESSION_KEYNAME = 'wsUploadFromUrlJobData'; 00035 00037 public $upload; 00038 00040 protected $user; 00041 00042 public function __construct( $title, $params ) { 00043 parent::__construct( 'uploadFromUrl', $title, $params ); 00044 } 00045 00046 public function run() { 00047 global $wgCopyUploadAsyncTimeout; 00048 # Initialize this object and the upload object 00049 $this->upload = new UploadFromUrl(); 00050 $this->upload->initialize( 00051 $this->title->getText(), 00052 $this->params['url'], 00053 false 00054 ); 00055 $this->user = User::newFromName( $this->params['userName'] ); 00056 00057 # Fetch the file 00058 $opts = array(); 00059 if ( $wgCopyUploadAsyncTimeout ) { 00060 $opts['timeout'] = $wgCopyUploadAsyncTimeout; 00061 } 00062 $status = $this->upload->fetchFile( $opts ); 00063 if ( !$status->isOk() ) { 00064 $this->leaveMessage( $status ); 00065 00066 return true; 00067 } 00068 00069 # Verify upload 00070 $result = $this->upload->verifyUpload(); 00071 if ( $result['status'] != UploadBase::OK ) { 00072 $status = $this->upload->convertVerifyErrorToStatus( $result ); 00073 $this->leaveMessage( $status ); 00074 00075 return true; 00076 } 00077 00078 # Check warnings 00079 if ( !$this->params['ignoreWarnings'] ) { 00080 $warnings = $this->upload->checkWarnings(); 00081 if ( $warnings ) { 00082 00083 # Stash the upload 00084 $key = $this->upload->stashFile(); 00085 00086 // @todo FIXME: This has been broken for a while. 00087 // User::leaveUserMessage() does not exist. 00088 if ( $this->params['leaveMessage'] ) { 00089 $this->user->leaveUserMessage( 00090 wfMessage( 'upload-warning-subj' )->text(), 00091 wfMessage( 'upload-warning-msg', 00092 $key, 00093 $this->params['url'] )->text() 00094 ); 00095 } else { 00096 wfSetupSession( $this->params['sessionId'] ); 00097 $this->storeResultInSession( 'Warning', 00098 'warnings', $warnings ); 00099 session_write_close(); 00100 } 00101 00102 return true; 00103 } 00104 } 00105 00106 # Perform the upload 00107 $status = $this->upload->performUpload( 00108 $this->params['comment'], 00109 $this->params['pageText'], 00110 $this->params['watch'], 00111 $this->user 00112 ); 00113 $this->leaveMessage( $status ); 00114 00115 return true; 00116 } 00117 00124 protected function leaveMessage( $status ) { 00125 if ( $this->params['leaveMessage'] ) { 00126 if ( $status->isGood() ) { 00127 // @todo FIXME: user->leaveUserMessage does not exist. 00128 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(), 00129 wfMessage( 'upload-success-msg', 00130 $this->upload->getTitle()->getText(), 00131 $this->params['url'] 00132 )->text() ); 00133 } else { 00134 // @todo FIXME: user->leaveUserMessage does not exist. 00135 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(), 00136 wfMessage( 'upload-failure-msg', 00137 $status->getWikiText(), 00138 $this->params['url'] 00139 )->text() ); 00140 } 00141 } else { 00142 wfSetupSession( $this->params['sessionId'] ); 00143 if ( $status->isOk() ) { 00144 $this->storeResultInSession( 'Success', 00145 'filename', $this->upload->getLocalFile()->getName() ); 00146 } else { 00147 $this->storeResultInSession( 'Failure', 00148 'errors', $status->getErrorsArray() ); 00149 } 00150 session_write_close(); 00151 } 00152 } 00153 00162 protected function storeResultInSession( $result, $dataKey, $dataValue ) { 00163 $session =& self::getSessionData( $this->params['sessionKey'] ); 00164 $session['result'] = $result; 00165 $session[$dataKey] = $dataValue; 00166 } 00167 00171 public function initializeSessionData() { 00172 $session =& self::getSessionData( $this->params['sessionKey'] ); 00173 $$session['result'] = 'Queued'; 00174 } 00175 00180 public static function &getSessionData( $key ) { 00181 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) { 00182 $_SESSION[self::SESSION_KEYNAME][$key] = array(); 00183 } 00184 00185 return $_SESSION[self::SESSION_KEYNAME][$key]; 00186 } 00187 }