MediaWiki  REL1_20
UploadFromStash.php
Go to the documentation of this file.
00001 <?php
00030 class UploadFromStash extends UploadBase {
00031         protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
00032 
00033         // an instance of UploadStash
00034         private $stash;
00035 
00036         //LocalFile repo
00037         private $repo;
00038 
00044         public function __construct( $user = false, $stash = false, $repo = false ) {
00045                 // user object. sometimes this won't exist, as when running from cron.
00046                 $this->user = $user;
00047 
00048                 if( $repo ) {
00049                         $this->repo = $repo;
00050                 } else {
00051                         $this->repo = RepoGroup::singleton()->getLocalRepo();
00052                 }
00053 
00054                 if( $stash ) {
00055                         $this->stash = $stash;
00056                 } else {
00057                         if( $user ) {
00058                                 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
00059                         } else {
00060                                 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
00061                         }
00062 
00063                         $this->stash = new UploadStash( $this->repo, $this->user );
00064                 }
00065         }
00066 
00071         public static function isValidKey( $key ) {
00072                 // this is checked in more detail in UploadStash
00073                 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
00074         }
00075 
00081         public static function isValidRequest( $request ) {
00082                 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
00083                 // wpSessionKey has no default which guarantees failure if both are missing
00084                 // (though that should have been caught earlier)
00085                 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
00086         }
00087 
00092         public function initialize( $key, $name = 'upload_file' ) {
00099                 $metadata = $this->stash->getMetadata( $key );
00100                 $this->initializePathInfo( $name,
00101                         $this->getRealPath ( $metadata['us_path'] ),
00102                         $metadata['us_size'],
00103                         false
00104                 );
00105 
00106                 $this->mFileKey = $key;
00107                 $this->mVirtualTempPath = $metadata['us_path'];
00108                 $this->mFileProps = $this->stash->getFileProps( $key );
00109                 $this->mSourceType = $metadata['us_source_type'];
00110         }
00111 
00115         public function initializeFromRequest( &$request ) {
00116                 // sends wpSessionKey as a default when wpFileKey is missing
00117                 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
00118 
00119                 // chooses one of wpDestFile, wpUploadFile, filename in that order.
00120                 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
00121 
00122                 $this->initialize( $fileKey, $desiredDestName );
00123         }
00124 
00128         public function getSourceType() {
00129                 return $this->mSourceType;
00130         }
00131 
00132         /*
00133          * protected function verifyFile() inherited
00134          */
00135 
00141         public function stashFile() {
00142                 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
00143                 // that are useful for stashed files.
00144                 $this->mLocalFile = parent::stashFile();
00145                 return $this->mLocalFile;
00146         }
00147 
00152         public function stashSession() {
00153                 return $this->stashFile()->getFileKey();
00154         }
00155 
00160         public function unsaveUploadedFile() {
00161                 return $this->stash->removeFile( $this->mFileKey );
00162         }
00163 
00172         public function performUpload( $comment, $pageText, $watch, $user ) {
00173                 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
00174                 $this->unsaveUploadedFile();
00175                 return $rv;
00176         }
00177 }