MediaWiki
REL1_22
|
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 global $wgCopyUploadAsyncTimeout; 00052 # Initialize this object and the upload object 00053 $this->upload = new UploadFromUrl(); 00054 $this->upload->initialize( 00055 $this->title->getText(), 00056 $this->params['url'], 00057 false 00058 ); 00059 $this->user = User::newFromName( $this->params['userName'] ); 00060 00061 # Fetch the file 00062 $opts = array(); 00063 if ( $wgCopyUploadAsyncTimeout ) { 00064 $opts['timeout'] = $wgCopyUploadAsyncTimeout; 00065 } 00066 $status = $this->upload->fetchFile( $opts ); 00067 if ( !$status->isOk() ) { 00068 $this->leaveMessage( $status ); 00069 return true; 00070 } 00071 00072 # Verify upload 00073 $result = $this->upload->verifyUpload(); 00074 if ( $result['status'] != UploadBase::OK ) { 00075 $status = $this->upload->convertVerifyErrorToStatus( $result ); 00076 $this->leaveMessage( $status ); 00077 return true; 00078 } 00079 00080 # Check warnings 00081 if ( !$this->params['ignoreWarnings'] ) { 00082 $warnings = $this->upload->checkWarnings(); 00083 if ( $warnings ) { 00084 00085 # Stash the upload 00086 $key = $this->upload->stashFile(); 00087 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 return true; 00115 00116 } 00117 00124 protected function leaveMessage( $status ) { 00125 if ( $this->params['leaveMessage'] ) { 00126 if ( $status->isGood() ) { 00127 $this->user->leaveUserMessage( wfMessage( 'upload-success-subj' )->text(), 00128 wfMessage( 'upload-success-msg', 00129 $this->upload->getTitle()->getText(), 00130 $this->params['url'] 00131 )->text() ); 00132 } else { 00133 $this->user->leaveUserMessage( wfMessage( 'upload-failure-subj' )->text(), 00134 wfMessage( 'upload-failure-msg', 00135 $status->getWikiText(), 00136 $this->params['url'] 00137 )->text() ); 00138 } 00139 } else { 00140 wfSetupSession( $this->params['sessionId'] ); 00141 if ( $status->isOk() ) { 00142 $this->storeResultInSession( 'Success', 00143 'filename', $this->upload->getLocalFile()->getName() ); 00144 } else { 00145 $this->storeResultInSession( 'Failure', 00146 'errors', $status->getErrorsArray() ); 00147 } 00148 session_write_close(); 00149 } 00150 } 00151 00160 protected function storeResultInSession( $result, $dataKey, $dataValue ) { 00161 $session =& self::getSessionData( $this->params['sessionKey'] ); 00162 $session['result'] = $result; 00163 $session[$dataKey] = $dataValue; 00164 } 00165 00169 public function initializeSessionData() { 00170 $session =& self::getSessionData( $this->params['sessionKey'] ); 00171 $$session['result'] = 'Queued'; 00172 } 00173 00178 public static function &getSessionData( $key ) { 00179 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) { 00180 $_SESSION[self::SESSION_KEYNAME][$key] = array(); 00181 } 00182 return $_SESSION[self::SESSION_KEYNAME][$key]; 00183 } 00184 }