MediaWiki  REL1_22
PublishStashedFileJob.php
Go to the documentation of this file.
00001 <?php
00029 class PublishStashedFileJob extends Job {
00030     public function __construct( $title, $params, $id = 0 ) {
00031         parent::__construct( 'PublishStashedFile', $title, $params, $id );
00032         $this->removeDuplicates = true;
00033     }
00034 
00035     public function run() {
00036         $scope = RequestContext::importScopedSession( $this->params['session'] );
00037         $context = RequestContext::getMain();
00038         try {
00039             $user = $context->getUser();
00040             if ( !$user->isLoggedIn() ) {
00041                 $this->setLastError( "Could not load the author user from session." );
00042                 return false;
00043             }
00044 
00045             if ( count( $_SESSION ) === 0 ) {
00046                 // Empty session probably indicates that we didn't associate
00047                 // with the session correctly. Note that being able to load
00048                 // the user does not necessarily mean the session was loaded.
00049                 // Most likely cause by suhosin.session.encrypt = On.
00050                 $this->setLastError( "Error associating with user session. Try setting suhosin.session.encrypt = Off" );
00051                 return false;
00052             }
00053 
00054 
00055             UploadBase::setSessionStatus(
00056                 $this->params['filekey'],
00057                 array( 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() )
00058             );
00059 
00060             $upload = new UploadFromStash( $user );
00061             // @todo initialize() causes a GET, ideally we could frontload the antivirus
00062             // checks and anything else to the stash stage (which includes concatenation and
00063             // the local file is thus already there). That way, instead of GET+PUT, there could
00064             // just be a COPY operation from the stash to the public zone.
00065             $upload->initialize( $this->params['filekey'], $this->params['filename'] );
00066 
00067             // Check if the local file checks out (this is generally a no-op)
00068             $verification = $upload->verifyUpload();
00069             if ( $verification['status'] !== UploadBase::OK ) {
00070                 $status = Status::newFatal( 'verification-error' );
00071                 $status->value = array( 'verification' => $verification );
00072                 UploadBase::setSessionStatus(
00073                     $this->params['filekey'],
00074                     array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
00075                 );
00076                 $this->setLastError( "Could not verify upload." );
00077                 return false;
00078             }
00079 
00080             // Upload the stashed file to a permanent location
00081             $status = $upload->performUpload(
00082                 $this->params['comment'],
00083                 $this->params['text'],
00084                 $this->params['watch'],
00085                 $user
00086             );
00087             if ( !$status->isGood() ) {
00088                 UploadBase::setSessionStatus(
00089                     $this->params['filekey'],
00090                     array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
00091                 );
00092                 $this->setLastError( $status->getWikiText() );
00093                 return false;
00094             }
00095 
00096             // Build the image info array while we have the local reference handy
00097             $apiMain = new ApiMain(); // dummy object (XXX)
00098             $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
00099 
00100             // Cleanup any temporary local file
00101             $upload->cleanupTempFile();
00102 
00103             // Cache the info so the user doesn't have to wait forever to get the final info
00104             UploadBase::setSessionStatus(
00105                 $this->params['filekey'],
00106                 array(
00107                     'result' => 'Success',
00108                     'stage' => 'publish',
00109                     'filename' => $upload->getLocalFile()->getName(),
00110                     'imageinfo' => $imageInfo,
00111                     'status' => Status::newGood()
00112                 )
00113             );
00114         } catch ( MWException $e ) {
00115             UploadBase::setSessionStatus(
00116                 $this->params['filekey'],
00117                 array(
00118                     'result' => 'Failure',
00119                     'stage' => 'publish',
00120                     'status' => Status::newFatal( 'api-error-publishfailed' )
00121                 )
00122             );
00123             $this->setLastError( get_class( $e ) . ": " . $e->getText() );
00124             return false;
00125         }
00126         return true;
00127     }
00128 
00129     public function getDeduplicationInfo() {
00130         $info = parent::getDeduplicationInfo();
00131         if ( is_array( $info['params'] ) ) {
00132             $info['params'] = array( 'filekey' => $info['params']['filekey'] );
00133         }
00134         return $info;
00135     }
00136 
00137     public function allowRetries() {
00138         return false;
00139     }
00140 }