Index: branches/new-upload/phase3/maintenance/http_session_download.php |
— | — | @@ -24,7 +24,20 @@ |
25 | 25 | |
26 | 26 | exit(); |
27 | 27 | } |
| 28 | +wfProfileIn('http_session_download.php'); |
| 29 | + |
28 | 30 | //run the download: |
29 | 31 | Http::doSessionIdDownload( $options['sid'], $options['usk'] ); |
30 | 32 | |
| 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'); |
31 | 44 | ?> |
\ No newline at end of file |
Index: branches/new-upload/phase3/includes/HttpFunctions.php |
— | — | @@ -33,14 +33,15 @@ |
34 | 34 | $head = get_headers($url, 1); |
35 | 35 | if(isset($head['Content-Length']) && $head['Content-Length'] > $wgMaxUploadSize){ |
36 | 36 | return Status::newFatal('requested file length ' . $head['Content-Length'] . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize); |
37 | | - } |
38 | | - |
| 37 | + } |
39 | 38 | //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"); |
41 | 41 | //setup session and shell call: |
42 | 42 | return self::initBackgroundDownload( $url, $target_file_path ); |
43 | 43 | }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 |
45 | 46 | $opts['method']='GET'; |
46 | 47 | $opts['target_file_path'] = $target_file_path; |
47 | 48 | $req = new HttpRequest($url, $opts ); |
— | — | @@ -90,8 +91,9 @@ |
91 | 92 | * @param string $upload_session_key //the key of the given upload session |
92 | 93 | * (a given client could have started a few http uploads at once) |
93 | 94 | */ |
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"); |
96 | 98 | //set session to the provided key: |
97 | 99 | session_id($session_id); |
98 | 100 | //start the session |
— | — | @@ -99,6 +101,14 @@ |
100 | 102 | wfDebug( __METHOD__ . ' could not start session'); |
101 | 103 | } |
102 | 104 | //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: |
103 | 113 | $sessionData = $_SESSION[ 'wsDownload' ][$upload_session_key]; |
104 | 114 | //close down the session so we can other http queries can get session updates: |
105 | 115 | session_write_close(); |
— | — | @@ -107,17 +117,38 @@ |
108 | 118 | 'target_file_path' => $sessionData['target_file_path'], |
109 | 119 | 'upload_session_key' => $upload_session_key |
110 | 120 | ) ); |
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: |
114 | 131 | $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 | + |
118 | 149 | |
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(); |
122 | 153 | } |
123 | 154 | } |
124 | 155 | |
— | — | @@ -191,8 +222,10 @@ |
192 | 223 | private function doCurlReq(){ |
193 | 224 | global $wgHTTPFileTimeout, $wgHTTPProxy, $wgTitle; |
194 | 225 | |
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 ); |
197 | 230 | |
198 | 231 | //proxy setup: |
199 | 232 | if ( Http::isLocalURL( $this->url ) ) { |
— | — | @@ -224,7 +257,11 @@ |
225 | 258 | |
226 | 259 | //set the write back function (if we are writing to a file) |
227 | 260 | 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 | + } |
229 | 266 | curl_setopt( $c, CURLOPT_WRITEFUNCTION, array($cwrite, 'callbackWriteBody') ); |
230 | 267 | } |
231 | 268 | |
— | — | @@ -235,12 +272,14 @@ |
236 | 273 | //run the actual curl_exec: |
237 | 274 | try { |
238 | 275 | 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); |
241 | 279 | } |
242 | 280 | } catch (Exception $e) { |
243 | 281 | //do something with curl exec error? |
244 | 282 | } |
| 283 | + wfDebug("\nDONE WITH curl_exec \n"); |
245 | 284 | //if direct request output the results to the stats value: |
246 | 285 | if( !$this->target_file_path && $status->isOK() ){ |
247 | 286 | $status->value = ob_get_contents(); |
— | — | @@ -303,7 +342,7 @@ |
304 | 343 | var $target_file_path; |
305 | 344 | var $status = null; |
306 | 345 | 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 |
308 | 347 | |
309 | 348 | function simpleFileWriter($target_file_path, $session_id=false){ |
310 | 349 | $this->target_file_path = $target_file_path; |
— | — | @@ -317,26 +356,30 @@ |
318 | 357 | $this->prevTime = time(); |
319 | 358 | } |
320 | 359 | public function callbackWriteBody($ch, $data_packet){ |
321 | | - global $wgMaxUploadSize; |
| 360 | + global $wgMaxUploadSize; |
| 361 | + wfDebug("\ncallbackWriteBody::" . strlen($data_packet) . "\n"); |
322 | 362 | //check file size: |
323 | 363 | clearstatcache(); |
324 | 364 | $this->current_fsize = filesize( $this->target_file_path); |
325 | 365 | |
326 | 366 | if( $this->current_fsize > $wgMaxUploadSize){ |
327 | | - wfDebug( __METHOD__ . ' http download too large'); |
| 367 | + wfDebug( __METHOD__ . " ::http download too large\n"); |
328 | 368 | $this->status = Status::newFatal('HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' . |
329 | 369 | Language::formatSize($wgMaxUploadSize) . ' '); |
330 | 370 | return 0; |
331 | 371 | } |
332 | | - |
| 372 | + wfDebug("passed fsize check\n"); |
333 | 373 | //write out the content |
334 | 374 | if( fwrite($this->fp, $data_packet) === false){ |
| 375 | + wfDebug(__METHOD__ ." ::could-not-write-to-file\n"); |
335 | 376 | $this->status = Status::newFatal('HTTP::could-not-write-to-file'); |
336 | 377 | return 0; |
337 | 378 | } |
| 379 | + wfDebug("did fwrite oky\n"); |
338 | 380 | |
| 381 | + wfDebug("\n" .'check if we should update: ' . time() . ' - ' .$this->prevTime . ' > '. $this->session_update_interval . "\n"); |
339 | 382 | //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 ) { |
341 | 384 | $this->prevTime = time(); |
342 | 385 | $session_status = $this->update_session_progress(); |
343 | 386 | if( !$session_status->isOK() ){ |
Index: branches/new-upload/phase3/includes/api/ApiUpload.php |
— | — | @@ -32,7 +32,8 @@ |
33 | 33 | * @ingroup API |
34 | 34 | */ |
35 | 35 | class ApiUpload extends ApiBase { |
36 | | - |
| 36 | + var $mUpload = null; |
| 37 | + |
37 | 38 | public function __construct($main, $action) { |
38 | 39 | parent :: __construct($main, $action); |
39 | 40 | } |
— | — | @@ -63,7 +64,7 @@ |
64 | 65 | if( isset( $this->mUpload->status[ 'error' ] ) ) |
65 | 66 | $this->dieUsageMsg( $this->mUpload->status[ 'error' ] ); |
66 | 67 | |
67 | | - } else if( $this->mParams['sessionkey'] ) { |
| 68 | + }else if( $this->mParams['sessionkey'] ) { |
68 | 69 | // Stashed upload |
69 | 70 | $this->mUpload = new UploadFromStash(); |
70 | 71 | $this->mUpload->initialize( $this->mParams['sessionkey'] ); |
— | — | @@ -90,7 +91,7 @@ |
91 | 92 | if( !$status->isOK() ){ |
92 | 93 | $this->dieUsage( 'fetchfilerror', $status->getWikiText()); |
93 | 94 | } |
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) |
95 | 96 | if( $this->mUpload->isAsync() ){ |
96 | 97 | $upload_session_key = $status->value; |
97 | 98 | //update the session with anything with the params we will need to finish up the upload later on: |
— | — | @@ -99,10 +100,9 @@ |
100 | 101 | |
101 | 102 | $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
102 | 103 | |
103 | | - //do a wholesale copy of mParams |
104 | | - $sd['mParams'] = $this->mParams; |
| 104 | + //copy mParams for finishing up after: |
| 105 | + $sd['mParams'] = $this->mParams; |
105 | 106 | |
106 | | - |
107 | 107 | return $this->getResult()->addValue( null, $this->getModuleName(), |
108 | 108 | array( 'upload_session_key' => $upload_session_key |
109 | 109 | )); |
— | — | @@ -112,10 +112,32 @@ |
113 | 113 | } |
114 | 114 | |
115 | 115 | 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(); |
117 | 128 | |
| 129 | + $fileSize = filesize($tempPath); |
118 | 130 | |
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 |
120 | 142 | $permission = $this->mUpload->isAllowed( $wgUser ); |
121 | 143 | |
122 | 144 | |
— | — | @@ -133,7 +155,6 @@ |
134 | 156 | |
135 | 157 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
136 | 158 | } |
137 | | - |
138 | 159 | private function performUpload() { |
139 | 160 | global $wgUser; |
140 | 161 | $result = array(); |
— | — | @@ -248,7 +269,7 @@ |
249 | 270 | 'ignorewarnings' => false, |
250 | 271 | 'done' => false, |
251 | 272 | 'sessionkey' => null, |
252 | | - 'chunksessionkey'=> null, |
| 273 | + 'chunksessionkey'=> null |
253 | 274 | ); |
254 | 275 | } |
255 | 276 | |