- * @param $session_id String: the session id to grab download details from
- * @param $upload_session_key String: the key of the given upload session
- * (a given client could have started a few http uploads at once)
- */
- public static function doSessionIdDownload( $session_id, $upload_session_key ){
- global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout, $wgServer,
- $wgSessionsInMemcached, $wgSessionHandler, $wgSessionStarted;
- wfDebug( __METHOD__ . "\n\n doSessionIdDownload :\n\n" );
- // set session to the provided key:
- session_id( $session_id );
- //fire up mediaWiki session system:
- wfSetupSession();
-
- // start the session
- if( session_start() === false ){
- wfDebug( __METHOD__ . ' could not start session' );
- }
- // get all the vars we need from session_id
- if( !isset( $_SESSION[ 'wsDownload' ][$upload_session_key] ) ){
- wfDebug( __METHOD__ . ' Error:could not find upload session');
- exit();
- }
- // setup the global user from the session key we just inherited
- $wgUser = User::newFromSession();
-
- // grab the session data to setup the request:
- $sd =& $_SESSION['wsDownload'][$upload_session_key];
-
- // update the wgServer var ( since cmd line thinks we are localhost when we are really orgServer)
- if( isset( $sd['orgServer'] ) && $sd['orgServer'] ){
- $wgServer = $sd['orgServer'];
- }
- // close down the session so we can other http queries can get session updates: (if not $wgSessionsInMemcached)
- if( !$wgSessionsInMemcached )
- session_write_close();
-
- $req = HttpRequest::newRequest( $sd['url'], array(
- 'target_file_path' => $sd['target_file_path'],
- 'upload_session_key'=> $upload_session_key,
- 'timeout' => $wgAsyncHTTPTimeout,
- 'do_close_session_update' => true
- ) );
- // run the actual request .. (this can take some time)
- wfDebug( __METHOD__ . 'do Session Download :: ' . $sd['url'] . ' tf: ' . $sd['target_file_path'] . "\n\n");
- $status = $req->doRequest();
- //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
-
- // start up the session again:
- if( session_start() === false ){
- wfDebug( __METHOD__ . ' ERROR:: Could not start session');
- }
- // grab the updated session data pointer
- $sd =& $_SESSION['wsDownload'][$upload_session_key];
- // if error update status:
- if( !$status->isOK() ){
- $sd['apiUploadResult'] = ApiFormatJson::getJsonEncode(
- array( 'error' => $status->getWikiText() )
- );
- }
- // if status okay process upload using fauxReq to api:
- if( $status->isOK() ){
- // setup the FauxRequest
- $fauxReqData = $sd['mParams'];
-
- // Fix boolean parameters
- foreach( $fauxReqData as $k => $v ) {
- if( $v === false )
- unset( $fauxReqData[$k] );
- }
-
- $fauxReqData['action'] = 'upload';
- $fauxReqData['format'] = 'json';
- $fauxReqData['internalhttpsession'] = $upload_session_key;
- // evil but no other clean way about it:
- $faxReq = new FauxRequest( $fauxReqData, true );
- $processor = new ApiMain( $faxReq, $wgEnableWriteAPI );
-
- //init the mUpload var for the $processor
- $processor->execute();
- $processor->getResult()->cleanUpUTF8();
- $printer = $processor->createPrinterByName( 'json' );
- $printer->initPrinter( false );
- ob_start();
- $printer->execute();
- $apiUploadResult = ob_get_clean();
-
- // the status updates runner will grab the result form the session:
- $sd['apiUploadResult'] = $apiUploadResult;
- }
- // close the session:
- session_write_close();