MediaWiki  REL1_22
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', $initTempFile = true ) {
00099         $metadata = $this->stash->getMetadata( $key );
00100         $this->initializePathInfo( $name,
00101             $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
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 
00136     public function getTempFileSha1Base36() {
00137         return $this->mFileProps['sha1'];
00138     }
00139 
00140     /*
00141      * protected function verifyFile() inherited
00142      */
00143 
00150     public function stashFile( User $user = null ) {
00151         // replace mLocalFile with an instance of UploadStashFile, which adds some methods
00152         // that are useful for stashed files.
00153         $this->mLocalFile = parent::stashFile( $user );
00154         return $this->mLocalFile;
00155     }
00156 
00161     public function stashSession() {
00162         return $this->stashFile()->getFileKey();
00163     }
00164 
00169     public function unsaveUploadedFile() {
00170         return $this->stash->removeFile( $this->mFileKey );
00171     }
00172 
00181     public function performUpload( $comment, $pageText, $watch, $user ) {
00182         $rv = parent::performUpload( $comment, $pageText, $watch, $user );
00183         $this->unsaveUploadedFile();
00184         return $rv;
00185     }
00186 }