MediaWiki
REL1_19
|
00001 <?php 00008 class UploadFromStash extends UploadBase { 00009 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType; 00010 00011 // an instance of UploadStash 00012 private $stash; 00013 00014 //LocalFile repo 00015 private $repo; 00016 00022 public function __construct( $user = false, $stash = false, $repo = false ) { 00023 // user object. sometimes this won't exist, as when running from cron. 00024 $this->user = $user; 00025 00026 if( $repo ) { 00027 $this->repo = $repo; 00028 } else { 00029 $this->repo = RepoGroup::singleton()->getLocalRepo(); 00030 } 00031 00032 if( $stash ) { 00033 $this->stash = $stash; 00034 } else { 00035 if( $user ) { 00036 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" ); 00037 } else { 00038 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" ); 00039 } 00040 00041 $this->stash = new UploadStash( $this->repo, $this->user ); 00042 } 00043 00044 return true; 00045 } 00046 00051 public static function isValidKey( $key ) { 00052 // this is checked in more detail in UploadStash 00053 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key ); 00054 } 00055 00061 public static function isValidRequest( $request ) { 00062 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set. 00063 // wpSessionKey has no default which guarantees failure if both are missing 00064 // (though that should have been caught earlier) 00065 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) ); 00066 } 00067 00072 public function initialize( $key, $name = 'upload_file' ) { 00079 $metadata = $this->stash->getMetadata( $key ); 00080 $this->initializePathInfo( $name, 00081 $this->getRealPath ( $metadata['us_path'] ), 00082 $metadata['us_size'], 00083 false 00084 ); 00085 00086 $this->mFileKey = $key; 00087 $this->mVirtualTempPath = $metadata['us_path']; 00088 $this->mFileProps = $this->stash->getFileProps( $key ); 00089 $this->mSourceType = $metadata['us_source_type']; 00090 } 00091 00095 public function initializeFromRequest( &$request ) { 00096 // sends wpSessionKey as a default when wpFileKey is missing 00097 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ); 00098 00099 // chooses one of wpDestFile, wpUploadFile, filename in that order. 00100 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) ); 00101 00102 return $this->initialize( $fileKey, $desiredDestName ); 00103 } 00104 00108 public function getSourceType() { 00109 return $this->mSourceType; 00110 } 00111 00112 /* 00113 * protected function verifyFile() inherited 00114 */ 00115 00121 public function stashFile() { 00122 // replace mLocalFile with an instance of UploadStashFile, which adds some methods 00123 // that are useful for stashed files. 00124 $this->mLocalFile = parent::stashFile(); 00125 return $this->mLocalFile; 00126 } 00127 00132 public function stashSession() { 00133 return $this->stashFile()->getFileKey(); 00134 } 00135 00140 public function unsaveUploadedFile() { 00141 return $this->stash->removeFile( $this->mFileKey ); 00142 } 00143 00152 public function performUpload( $comment, $pageText, $watch, $user ) { 00153 $rv = parent::performUpload( $comment, $pageText, $watch, $user ); 00154 $this->unsaveUploadedFile(); 00155 return $rv; 00156 } 00157 }