r50113 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50112‎ | r50113 | r50114 >
Date:23:21, 1 May 2009
Author:dale
Status:deferred
Tags:
Comment:
upload (debug) commit tracking session for upload status
Modified paths:
  • /branches/new-upload/phase3/includes/HttpFunctions.php (modified) (history)
  • /branches/new-upload/phase3/includes/api/ApiUpload.php (modified) (history)
  • /branches/new-upload/phase3/maintenance/http_session_download.php (modified) (history)

Diff [purge]

Index: branches/new-upload/phase3/maintenance/http_session_download.php
@@ -24,7 +24,20 @@
2525
2626 exit();
2727 }
 28+wfProfileIn('http_session_download.php');
 29+
2830 //run the download:
2931 Http::doSessionIdDownload( $options['sid'], $options['usk'] );
3032
 33+//close up shop:
 34+// Execute any deferred updates
 35+wfDoUpdates();
 36+
 37+// Log what the user did, for book-keeping purposes.
 38+wfLogProfilingData();
 39+
 40+// Shut down the database before exit
 41+wfGetLBFactory()->shutdown();
 42+
 43+wfProfileOut('http_session_download.php');
3144 ?>
\ No newline at end of file
Index: branches/new-upload/phase3/includes/HttpFunctions.php
@@ -33,14 +33,15 @@
3434 $head = get_headers($url, 1);
3535 if(isset($head['Content-Length']) && $head['Content-Length'] > $wgMaxUploadSize){
3636 return Status::newFatal('requested file length ' . $head['Content-Length'] . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize);
37 - }
38 -
 37+ }
3938 //check if we can find phpCliPath (for doing a background shell request to php to do the download:
40 - if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD){
 39+ if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD){
 40+ wfDebug("\ASYNC_DOWNLOAD\n");
4141 //setup session and shell call:
4242 return self::initBackgroundDownload( $url, $target_file_path );
4343 }else if( $dl_mode== self::SYNC_DOWNLOAD ){
44 - //else just download as much as we can in the time we have left:
 44+ wfDebug("\nSYNC_DOWNLOAD\n");
 45+ //SYNC_DOWNLOAD download as much as we can in the time we have to execute
4546 $opts['method']='GET';
4647 $opts['target_file_path'] = $target_file_path;
4748 $req = new HttpRequest($url, $opts );
@@ -90,8 +91,9 @@
9192 * @param string $upload_session_key //the key of the given upload session
9293 * (a given client could have started a few http uploads at once)
9394 */
94 - public static function doSessionIdDownload( $session_id, $upload_session_key ){
95 -
 95+ public static function doSessionIdDownload( $session_id, $upload_session_key ){
 96+ global $wgUser;
 97+ wfDebug("\n\ndoSessionIdDownload\n\n");
9698 //set session to the provided key:
9799 session_id($session_id);
98100 //start the session
@@ -99,6 +101,14 @@
100102 wfDebug( __METHOD__ . ' could not start session');
101103 }
102104 //get all the vars we need from session_id
 105+ if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){
 106+ wfDebug( __METHOD__ .' Error:could not find upload session');
 107+ exit();
 108+ }
 109+ //setup the global user from the session key we just inherited
 110+ $wgUser = User::newFromSession();
 111+
 112+ //grab the session data to setup the request:
103113 $sessionData = $_SESSION[ 'wsDownload' ][$upload_session_key];
104114 //close down the session so we can other http queries can get session updates:
105115 session_write_close();
@@ -107,17 +117,38 @@
108118 'target_file_path' => $sessionData['target_file_path'],
109119 'upload_session_key' => $upload_session_key
110120 ) );
111 - $status = $req->doRequest();
112 - if( $status->isOK() ){
113 - //regrab the updated session data:
 121+ //run the actual request .. (this can take some time)
 122+ wfDebug("do Request: " . $sessionData['url'] . ' tf: ' .$sessionData['target_file_path'] );
 123+ $status = $req->doRequest();
 124+
 125+ if( $status->isOK() ){
 126+ //start up the session again:
 127+ if( session_start() === false){
 128+ wfDebug( __METHOD__ . ' ERROR:: Could not start session');
 129+ }
 130+ //re-grab the updated session data:
114131 $sessionData = $_SESSION[ 'wsDownload' ][$upload_session_key];
115 - //done with 'download' now to inject and display warnings
116 - wfDebug("\nDONE with session download now to inject:\n");
117 - wfDebug( print_r($_SESSION, true));
 132+ $reqData = $sessionData['mParam'];
 133+
 134+ $reqData['action'] = 'upload';
 135+
 136+ wfDebug('running FauxRequest: ' . print_r($reqData, true) );
 137+
 138+ $faxReq = new FauxRequest($reqData, true);
 139+ $processor = new ApiMain($faxReq, $wgEnableWriteAPI);
 140+
 141+ //init the mUpload var for the $processor
 142+ $processor->execFromSession($sessionData['target_file_path']);
 143+
 144+ ob_start();
 145+ $processor->doExecUpload();
 146+ $apiUploadResult = ob_get_contents();
 147+ ob_get_clean();
 148+
118149
119 -
120 - //$faxReq = new FauxRequest($sessionData['mParam'], true);
121 - //print_r($faxReq);
 150+ //the status updates runner will grab the result form the session:
 151+ $_SESSION[ 'wsDownload' ][$upload_session_key]['apiUploadResult'] = $apiUploadResult;
 152+ session_write_close();
122153 }
123154 }
124155
@@ -191,8 +222,10 @@
192223 private function doCurlReq(){
193224 global $wgHTTPFileTimeout, $wgHTTPProxy, $wgTitle;
194225
195 - $status = Status::newGood();
196 - $c = curl_init( $this->url );
 226+ $status = Status::newGood();
 227+ wfDebug("\ncurReq: $this->url (sleep 1 sec)\n");
 228+ sleep(1);
 229+ $c = curl_init( $this->url );
197230
198231 //proxy setup:
199232 if ( Http::isLocalURL( $this->url ) ) {
@@ -224,7 +257,11 @@
225258
226259 //set the write back function (if we are writing to a file)
227260 if( $this->target_file_path ){
228 - $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key );
 261+ $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key );
 262+ if(!$cwrite->status->isOK()){
 263+ wfDebug("ERROR in setting up simpleFileWriter\n");
 264+ $status = $cwrite->status;
 265+ }
229266 curl_setopt( $c, CURLOPT_WRITEFUNCTION, array($cwrite, 'callbackWriteBody') );
230267 }
231268
@@ -235,12 +272,14 @@
236273 //run the actual curl_exec:
237274 try {
238275 if (false === curl_exec($c)) {
239 - $status = Status::newFatal( 'Error sending request: #' . curl_errno($c) .
240 - ' '. curl_error($c) );
 276+ $error_txt ='Error sending request: #' . curl_errno($c) .' '. curl_error($c);
 277+ wfDebug($error_txt . "\n");
 278+ $status = Status::newFatal( $error_txt);
241279 }
242280 } catch (Exception $e) {
243281 //do something with curl exec error?
244282 }
 283+ wfDebug("\nDONE WITH curl_exec \n");
245284 //if direct request output the results to the stats value:
246285 if( !$this->target_file_path && $status->isOK() ){
247286 $status->value = ob_get_contents();
@@ -303,7 +342,7 @@
304343 var $target_file_path;
305344 var $status = null;
306345 var $session_id = null;
307 - static $session_update_interval = 2; //how offten to update the session while downloading
 346+ var $session_update_interval = 0; //how offten to update the session while downloading
308347
309348 function simpleFileWriter($target_file_path, $session_id=false){
310349 $this->target_file_path = $target_file_path;
@@ -317,26 +356,30 @@
318357 $this->prevTime = time();
319358 }
320359 public function callbackWriteBody($ch, $data_packet){
321 - global $wgMaxUploadSize;
 360+ global $wgMaxUploadSize;
 361+ wfDebug("\ncallbackWriteBody::" . strlen($data_packet) . "\n");
322362 //check file size:
323363 clearstatcache();
324364 $this->current_fsize = filesize( $this->target_file_path);
325365
326366 if( $this->current_fsize > $wgMaxUploadSize){
327 - wfDebug( __METHOD__ . ' http download too large');
 367+ wfDebug( __METHOD__ . " ::http download too large\n");
328368 $this->status = Status::newFatal('HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' .
329369 Language::formatSize($wgMaxUploadSize) . ' ');
330370 return 0;
331371 }
332 -
 372+ wfDebug("passed fsize check\n");
333373 //write out the content
334374 if( fwrite($this->fp, $data_packet) === false){
 375+ wfDebug(__METHOD__ ." ::could-not-write-to-file\n");
335376 $this->status = Status::newFatal('HTTP::could-not-write-to-file');
336377 return 0;
337378 }
 379+ wfDebug("did fwrite oky\n");
338380
 381+ wfDebug("\n" .'check if we should update: ' . time() . ' - ' .$this->prevTime . ' > '. $this->session_update_interval . "\n");
339382 //if more than 2 second have passed update_session_progress
340 - if($this->upload_session_key && (time() - $this->prevTime) > self::session_update_interval) {
 383+ if($this->upload_session_key && (time() - $this->prevTime) > $this->session_update_interval ) {
341384 $this->prevTime = time();
342385 $session_status = $this->update_session_progress();
343386 if( !$session_status->isOK() ){
Index: branches/new-upload/phase3/includes/api/ApiUpload.php
@@ -32,7 +32,8 @@
3333 * @ingroup API
3434 */
3535 class ApiUpload extends ApiBase {
36 -
 36+ var $mUpload = null;
 37+
3738 public function __construct($main, $action) {
3839 parent :: __construct($main, $action);
3940 }
@@ -63,7 +64,7 @@
6465 if( isset( $this->mUpload->status[ 'error' ] ) )
6566 $this->dieUsageMsg( $this->mUpload->status[ 'error' ] );
6667
67 - } else if( $this->mParams['sessionkey'] ) {
 68+ }else if( $this->mParams['sessionkey'] ) {
6869 // Stashed upload
6970 $this->mUpload = new UploadFromStash();
7071 $this->mUpload->initialize( $this->mParams['sessionkey'] );
@@ -90,7 +91,7 @@
9192 if( !$status->isOK() ){
9293 $this->dieUsage( 'fetchfilerror', $status->getWikiText());
9394 }
94 - //check if we doing a async request (return session info)
 95+ //check if we doing a async request set session info and return the upload_session_key)
9596 if( $this->mUpload->isAsync() ){
9697 $upload_session_key = $status->value;
9798 //update the session with anything with the params we will need to finish up the upload later on:
@@ -99,10 +100,9 @@
100101
101102 $sd =& $_SESSION['wsDownload'][$upload_session_key];
102103
103 - //do a wholesale copy of mParams
104 - $sd['mParams'] = $this->mParams;
 104+ //copy mParams for finishing up after:
 105+ $sd['mParams'] = $this->mParams;
105106
106 -
107107 return $this->getResult()->addValue( null, $this->getModuleName(),
108108 array( 'upload_session_key' => $upload_session_key
109109 ));
@@ -112,10 +112,32 @@
113113 }
114114
115115 if( !isset( $this->mUpload ) )
116 - $this->dieUsage( 'No upload module set', 'nomodule' );
 116+ $this->dieUsage( 'No upload module set', 'nomodule' );
 117+
 118+ //finish up the exec command:
 119+ $this->doExecUpload();
 120+ }
 121+ /**
 122+ * alternate entry point
 123+ */
 124+ function execFromSession($tempPath){
 125+ wfDebug("execFromSession: $tempPath");
 126+ //get the params from the init session:
 127+ $this->mParams = $this->extractRequestParams();
117128
 129+ $fileSize = filesize($tempPath);
118130
119 - // Check whether the user has the appropriate permissions to upload anyway
 131+ $this->mUpload = new UploadFromUpload();
 132+ $this->mUpload->initialize( $this->mParams['filename'], $tempPath, $fileSize);
 133+ if( !isset( $this->mUpload ) )
 134+ $this->dieUsage( 'No upload module set', 'nomodule' );
 135+
 136+ //finish up the exec command as a normal request:
 137+ $this->doExecUpload();
 138+ }
 139+ function doExecUpload(){
 140+ global $wgUser;
 141+ //Check whether the user has the appropriate permissions to upload anyway
120142 $permission = $this->mUpload->isAllowed( $wgUser );
121143
122144
@@ -133,7 +155,6 @@
134156
135157 $this->getResult()->addValue( null, $this->getModuleName(), $result );
136158 }
137 -
138159 private function performUpload() {
139160 global $wgUser;
140161 $result = array();
@@ -248,7 +269,7 @@
249270 'ignorewarnings' => false,
250271 'done' => false,
251272 'sessionkey' => null,
252 - 'chunksessionkey'=> null,
 273+ 'chunksessionkey'=> null
253274 );
254275 }
255276

Status & tagging log