Update some deprecated code
[lhc/web/wiklou.git] / includes / upload / UploadFromStash.php
1 <?php
2 /**
3 * Implements uploading from previously stored file.
4 *
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 */
9
10 class UploadFromStash extends UploadBase {
11 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
12
13 // an instance of UploadStash
14 private $stash;
15
16 //LocalFile repo
17 private $repo;
18
19 /**
20 * @param $user User
21 * @param $stash UploadStash
22 * @param $repo FileRepo
23 */
24 public function __construct( $user = false, $stash = false, $repo = false ) {
25 // user object. sometimes this won't exist, as when running from cron.
26 $this->user = $user;
27
28 if( $repo ) {
29 $this->repo = $repo;
30 } else {
31 $this->repo = RepoGroup::singleton()->getLocalRepo();
32 }
33
34 if( $stash ) {
35 $this->stash = $stash;
36 } else {
37 if( $user ) {
38 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
39 } else {
40 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
41 }
42
43 $this->stash = new UploadStash( $this->repo, $this->user );
44 }
45
46 return true;
47 }
48
49 /**
50 * @param $key string
51 * @return bool
52 */
53 public static function isValidKey( $key ) {
54 // this is checked in more detail in UploadStash
55 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
56 }
57
58 /**
59 * @param $request WebRequest
60 *
61 * @return Boolean
62 */
63 public static function isValidRequest( $request ) {
64 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
65 // wpSessionKey has no default which guarantees failure if both are missing
66 // (though that should have been caught earlier)
67 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
68 }
69
70 /**
71 * @param $key string
72 * @param $name string
73 */
74 public function initialize( $key, $name = 'upload_file' ) {
75 /**
76 * Confirming a temporarily stashed upload.
77 * We don't want path names to be forged, so we keep
78 * them in the session on the server and just give
79 * an opaque key to the user agent.
80 */
81 $metadata = $this->stash->getMetadata( $key );
82 $this->initializePathInfo( $name,
83 $this->getRealPath ( $metadata['us_path'] ),
84 $metadata['us_size'],
85 false
86 );
87
88 $this->mFileKey = $key;
89 $this->mVirtualTempPath = $metadata['us_path'];
90 $this->mFileProps = $this->stash->getFileProps( $key );
91 $this->mSourceType = $metadata['us_source_type'];
92 }
93
94 /**
95 * @param $request WebRequest
96 */
97 public function initializeFromRequest( &$request ) {
98 // sends wpSessionKey as a default when wpFileKey is missing
99 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
100
101 // chooses one of wpDestFile, wpUploadFile, filename in that order.
102 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
103
104 return $this->initialize( $fileKey, $desiredDestName );
105 }
106
107 /**
108 * @return string
109 */
110 public function getSourceType() {
111 return $this->mSourceType;
112 }
113
114 /**
115 * File has been previously verified so no need to do so again.
116 *
117 * @return bool
118 */
119 protected function verifyFile() {
120 return true;
121 }
122
123 /**
124 * Stash the file.
125 *
126 * @return UploadStashFile
127 */
128 public function stashFile() {
129 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
130 // that are useful for stashed files.
131 $this->mLocalFile = parent::stashFile();
132 return $this->mLocalFile;
133 }
134
135 /**
136 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
137 */
138 public function stashSession() {
139 return $this->stashFile()->getFileKey();
140 }
141
142 /**
143 * Remove a temporarily kept file stashed by saveTempUploadedFile().
144 * @return success
145 */
146 public function unsaveUploadedFile() {
147 return $this->stash->removeFile( $this->mFileKey );
148 }
149
150 /**
151 * Perform the upload, then remove the database record afterward.
152 * @param $comment string
153 * @param $pageText string
154 * @param $watch bool
155 * @param $user User
156 * @return Status
157 */
158 public function performUpload( $comment, $pageText, $watch, $user ) {
159 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
160 $this->unsaveUploadedFile();
161 return $rv;
162 }
163
164 /**
165 * Append a chunk to the temporary file.
166 *
167 * @return Status
168 */
169 public function appendChunk($chunk, $chunkSize, $offset) {
170 //to use $this->getFileSize() here, db needs to be updated
171 //in appendToUploadFile for that
172 $fileSize = $this->stash->getFile( $this->mFileKey )->getSize();
173 if ( $fileSize + $chunkSize > $this->getMaxUploadSize()) {
174 $status = Status::newFatal( 'file-too-large' );
175 } else {
176 //append chunk
177 if ( $fileSize == $offset ) {
178 $status = $this->appendToUploadFile( $chunk,
179 $this->mVirtualTempPath );
180 } else {
181 $status = Status::newFatal( 'invalid-chunk-offset' );
182 }
183 }
184 return $status;
185 }
186
187 /**
188 * Append the final chunk and ready file for parent::performUpload()
189 * @return void
190 */
191 public function finalizeFile() {
192 $this->appendFinish ( $this->mVirtualTempPath );
193 $this->cleanupTempFile();
194 $this->mTempPath = $this->getRealPath( $this->mVirtualTempPath );
195 }
196 }