3 * Implements uploading from previously stored file.
6 * @author Bryan Tong Minh
8 class UploadFromStash
extends UploadBase
{
9 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
11 // an instance of UploadStash
19 * @param $stash UploadStash
20 * @param $repo FileRepo
22 public function __construct( $user = false, $stash = false, $repo = false ) {
23 // user object. sometimes this won't exist, as when running from cron.
29 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
33 $this->stash
= $stash;
36 wfDebug( __METHOD__
. " creating new UploadStash instance for " . $user->getId() . "\n" );
38 wfDebug( __METHOD__
. " creating new UploadStash instance with no user\n" );
41 $this->stash
= new UploadStash( $this->repo
, $this->user
);
51 public static function isValidKey( $key ) {
52 // this is checked in more detail in UploadStash
53 return (bool)preg_match( UploadStash
::KEY_FORMAT_REGEX
, $key );
57 * @param $request WebRequest
61 public static function isValidRequest( $request ) {
62 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
63 // wpSessionKey has no default which guarantees failure if both are missing
64 // (though that should have been caught earlier)
65 return self
::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
72 public function initialize( $key, $name = 'upload_file' ) {
74 * Confirming a temporarily stashed upload.
75 * We don't want path names to be forged, so we keep
76 * them in the session on the server and just give
77 * an opaque key to the user agent.
79 $metadata = $this->stash
->getMetadata( $key );
80 $this->initializePathInfo( $name,
81 $this->getRealPath ( $metadata['us_path'] ),
86 $this->mFileKey
= $key;
87 $this->mVirtualTempPath
= $metadata['us_path'];
88 $this->mFileProps
= $this->stash
->getFileProps( $key );
89 $this->mSourceType
= $metadata['us_source_type'];
93 * @param $request WebRequest
95 public function initializeFromRequest( &$request ) {
96 // sends wpSessionKey as a default when wpFileKey is missing
97 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
99 // chooses one of wpDestFile, wpUploadFile, filename in that order.
100 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
102 return $this->initialize( $fileKey, $desiredDestName );
108 public function getSourceType() {
109 return $this->mSourceType
;
113 * File has been previously verified so no need to do so again.
117 protected function verifyFile() {
124 * @return UploadStashFile
126 public function stashFile() {
127 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
128 // that are useful for stashed files.
129 $this->mLocalFile
= parent
::stashFile();
130 return $this->mLocalFile
;
134 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
137 public function stashSession() {
138 return $this->stashFile()->getFileKey();
142 * Remove a temporarily kept file stashed by saveTempUploadedFile().
143 * @return bool success
145 public function unsaveUploadedFile() {
146 return $this->stash
->removeFile( $this->mFileKey
);
150 * Perform the upload, then remove the database record afterward.
151 * @param $comment string
152 * @param $pageText string
157 public function performUpload( $comment, $pageText, $watch, $user ) {
158 $rv = parent
::performUpload( $comment, $pageText, $watch, $user );
159 $this->unsaveUploadedFile();