MediaWiki  REL1_24
UploadFromStash.php
Go to the documentation of this file.
00001 <?php
00030 class UploadFromStash extends UploadBase {
00031     protected $mFileKey;
00032     protected $mVirtualTempPath;
00033     protected $mFileProps;
00034     protected $mSourceType;
00035 
00036     // an instance of UploadStash
00037     private $stash;
00038 
00039     //LocalFile repo
00040     private $repo;
00041 
00047     public function __construct( $user = false, $stash = false, $repo = false ) {
00048         // user object. sometimes this won't exist, as when running from cron.
00049         $this->user = $user;
00050 
00051         if ( $repo ) {
00052             $this->repo = $repo;
00053         } else {
00054             $this->repo = RepoGroup::singleton()->getLocalRepo();
00055         }
00056 
00057         if ( $stash ) {
00058             $this->stash = $stash;
00059         } else {
00060             if ( $user ) {
00061                 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
00062             } else {
00063                 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
00064             }
00065 
00066             $this->stash = new UploadStash( $this->repo, $this->user );
00067         }
00068     }
00069 
00074     public static function isValidKey( $key ) {
00075         // this is checked in more detail in UploadStash
00076         return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
00077     }
00078 
00083     public static function isValidRequest( $request ) {
00084         // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
00085         // wpSessionKey has no default which guarantees failure if both are missing
00086         // (though that should have been caught earlier)
00087         return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
00088     }
00089 
00095     public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
00102         $metadata = $this->stash->getMetadata( $key );
00103         $this->initializePathInfo( $name,
00104             $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
00105             $metadata['us_size'],
00106             false
00107         );
00108 
00109         $this->mFileKey = $key;
00110         $this->mVirtualTempPath = $metadata['us_path'];
00111         $this->mFileProps = $this->stash->getFileProps( $key );
00112         $this->mSourceType = $metadata['us_source_type'];
00113     }
00114 
00118     public function initializeFromRequest( &$request ) {
00119         // sends wpSessionKey as a default when wpFileKey is missing
00120         $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
00121 
00122         // chooses one of wpDestFile, wpUploadFile, filename in that order.
00123         $desiredDestName = $request->getText(
00124             'wpDestFile',
00125             $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
00126         );
00127 
00128         $this->initialize( $fileKey, $desiredDestName );
00129     }
00130 
00134     public function getSourceType() {
00135         return $this->mSourceType;
00136     }
00137 
00142     public function getTempFileSha1Base36() {
00143         return $this->mFileProps['sha1'];
00144     }
00145 
00146     /*
00147      * protected function verifyFile() inherited
00148      */
00149 
00156     public function stashFile( User $user = null ) {
00157         // replace mLocalFile with an instance of UploadStashFile, which adds some methods
00158         // that are useful for stashed files.
00159         $this->mLocalFile = parent::stashFile( $user );
00160 
00161         return $this->mLocalFile;
00162     }
00163 
00168     public function stashSession() {
00169         return $this->stashFile()->getFileKey();
00170     }
00171 
00176     public function unsaveUploadedFile() {
00177         return $this->stash->removeFile( $this->mFileKey );
00178     }
00179 
00188     public function performUpload( $comment, $pageText, $watch, $user ) {
00189         $rv = parent::performUpload( $comment, $pageText, $watch, $user );
00190         $this->unsaveUploadedFile();
00191 
00192         return $rv;
00193     }
00194 }