Revert "Adding sanity check to Title::isRedirect()."
[lhc/web/wiklou.git] / includes / upload / UploadFromChunks.php
1 <?php
2 /**
3 * Implements uploading from chunks
4 *
5 * @ingroup Upload
6 * @author Michael Dale
7 */
8 class UploadFromChunks extends UploadFromFile {
9 protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath;
10
11 /**
12 * Setup local pointers to stash, repo and user ( similar to UploadFromStash )
13 *
14 * @param $user User
15 * @param $stash UploadStash
16 * @param $repo FileRepo
17 */
18 public function __construct( $user = false, $stash = false, $repo = false ) {
19 // user object. sometimes this won't exist, as when running from cron.
20 $this->user = $user;
21
22 if( $repo ) {
23 $this->repo = $repo;
24 } else {
25 $this->repo = RepoGroup::singleton()->getLocalRepo();
26 }
27
28 if( $stash ) {
29 $this->stash = $stash;
30 } else {
31 if( $user ) {
32 wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
33 } else {
34 wfDebug( __METHOD__ . " creating new UploadFromChunks instance with no user\n" );
35 }
36 $this->stash = new UploadStash( $this->repo, $this->user );
37 }
38
39 return true;
40 }
41 /**
42 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
43 *
44 * @return UploadStashFile stashed file
45 */
46 public function stashFile() {
47 // Stash file is the called on creating a new chunk session:
48 $this->mChunkIndex = 0;
49 $this->mOffset = 0;
50 // Create a local stash target
51 $this->mLocalFile = parent::stashFile();
52 // Update the initial file offset ( based on file size )
53 $this->mOffset = $this->mLocalFile->getSize();
54 $this->mFileKey = $this->mLocalFile->getFileKey();
55
56 // Output a copy of this first to chunk 0 location:
57 $status = $this->outputChunk( $this->mLocalFile->getPath() );
58
59 // Update db table to reflect initial "chunk" state
60 $this->updateChunkStatus();
61 return $this->mLocalFile;
62 }
63
64 /**
65 * Continue chunk uploading
66 */
67 public function continueChunks( $name, $key, $webRequestUpload ) {
68 $this->mFileKey = $key;
69 $this->mUpload = $webRequestUpload;
70 // Get the chunk status form the db:
71 $this->getChunkStatus();
72
73 $metadata = $this->stash->getMetadata( $key );
74 $this->initializePathInfo( $name,
75 $this->getRealPath( $metadata['us_path'] ),
76 $metadata['us_size'],
77 false
78 );
79 }
80
81 /**
82 * Append the final chunk and ready file for parent::performUpload()
83 * @return FileRepoStatus
84 */
85 public function concatenateChunks() {
86 wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" .
87 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
88
89 // Concatenate all the chunks to mVirtualTempPath
90 $fileList = Array();
91 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
92 for( $i = 0; $i <= $this->getChunkIndex(); $i++ ){
93 $fileList[] = $this->getVirtualChunkLocation( $i );
94 }
95
96 // Get the file extension from the last chunk
97 $ext = FileBackend::extensionFromPath( $this->mVirtualTempPath );
98 // Get a 0-byte temp file to perform the concatenation at
99 $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext );
100 $tmpPath = $tmpFile
101 ? $tmpFile->getPath()
102 : false; // fail in concatenate()
103 // Concatenate the chunks at the temp file
104 $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE );
105 if( !$status->isOk() ){
106 return $status;
107 }
108 // Update the mTempPath and mLocalFile
109 // ( for FileUpload or normal Stash to take over )
110 $this->mTempPath = $tmpPath; // file system path
111 $this->mLocalFile = parent::stashFile();
112
113 return $status;
114 }
115
116 /**
117 * Perform the upload, then remove the temp copy afterward
118 * @param $comment string
119 * @param $pageText string
120 * @param $watch bool
121 * @param $user User
122 * @return Status
123 */
124 public function performUpload( $comment, $pageText, $watch, $user ) {
125 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
126 return $rv;
127 }
128
129 /**
130 * Returns the virtual chunk location:
131 * @param $index
132 * @return string
133 */
134 function getVirtualChunkLocation( $index ){
135 return $this->repo->getVirtualUrl( 'temp' ) .
136 '/' .
137 $this->repo->getHashPath(
138 $this->getChunkFileKey( $index )
139 ) .
140 $this->getChunkFileKey( $index );
141 }
142
143 /**
144 * Add a chunk to the temporary directory
145 *
146 * @param $chunkPath string path to temporary chunk file
147 * @param $chunkSize int size of the current chunk
148 * @param $offset int offset of current chunk ( mutch match database chunk offset )
149 * @return Status
150 */
151 public function addChunk( $chunkPath, $chunkSize, $offset ) {
152 // Get the offset before we add the chunk to the file system
153 $preAppendOffset = $this->getOffset();
154
155 if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize()) {
156 $status = Status::newFatal( 'file-too-large' );
157 } else {
158 // Make sure the client is uploading the correct chunk with a matching offset.
159 if ( $preAppendOffset == $offset ) {
160 // Update local chunk index for the current chunk
161 $this->mChunkIndex++;
162 $status = $this->outputChunk( $chunkPath );
163 if( $status->isGood() ){
164 // Update local offset:
165 $this->mOffset = $preAppendOffset + $chunkSize;
166 // Update chunk table status db
167 $this->updateChunkStatus();
168 }
169 } else {
170 $status = Status::newFatal( 'invalid-chunk-offset' );
171 }
172 }
173 return $status;
174 }
175
176 /**
177 * Update the chunk db table with the current status:
178 */
179 private function updateChunkStatus(){
180 wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
181 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
182
183 $dbw = $this->repo->getMasterDb();
184 $dbw->update(
185 'uploadstash',
186 array(
187 'us_status' => 'chunks',
188 'us_chunk_inx' => $this->getChunkIndex(),
189 'us_size' => $this->getOffset()
190 ),
191 array( 'us_key' => $this->mFileKey ),
192 __METHOD__
193 );
194 }
195 /**
196 * Get the chunk db state and populate update relevant local values
197 */
198 private function getChunkStatus(){
199 // get Master db to avoid race conditions.
200 // Otherwise, if chunk upload time < replag there will be spurious errors
201 $dbw = $this->repo->getMasterDb();
202 $row = $dbw->selectRow(
203 'uploadstash',
204 array(
205 'us_chunk_inx',
206 'us_size',
207 'us_path',
208 ),
209 array( 'us_key' => $this->mFileKey ),
210 __METHOD__
211 );
212 // Handle result:
213 if ( $row ) {
214 $this->mChunkIndex = $row->us_chunk_inx;
215 $this->mOffset = $row->us_size;
216 $this->mVirtualTempPath = $row->us_path;
217 }
218 }
219 /**
220 * Get the current Chunk index
221 * @return Integer index of the current chunk
222 */
223 private function getChunkIndex(){
224 if( $this->mChunkIndex !== null ){
225 return $this->mChunkIndex;
226 }
227 return 0;
228 }
229
230 /**
231 * Gets the current offset in fromt the stashedupload table
232 * @return Integer current byte offset of the chunk file set
233 */
234 private function getOffset(){
235 if ( $this->mOffset !== null ){
236 return $this->mOffset;
237 }
238 return 0;
239 }
240
241 /**
242 * Output the chunk to disk
243 *
244 * @param $chunkPath string
245 * @return FileRepoStatus
246 */
247 private function outputChunk( $chunkPath ){
248 // Key is fileKey + chunk index
249 $fileKey = $this->getChunkFileKey();
250
251 // Store the chunk per its indexed fileKey:
252 $hashPath = $this->repo->getHashPath( $fileKey );
253 $storeStatus = $this->repo->store( $chunkPath, 'temp', "$hashPath$fileKey" );
254
255 // Check for error in stashing the chunk:
256 if ( ! $storeStatus->isOK() ) {
257 $error = $storeStatus->getErrorsArray();
258 $error = reset( $error );
259 if ( ! count( $error ) ) {
260 $error = $storeStatus->getWarningsArray();
261 $error = reset( $error );
262 if ( ! count( $error ) ) {
263 $error = array( 'unknown', 'no error recorded' );
264 }
265 }
266 throw new UploadChunkFileException( "error storing file in '$chunkPath': " . implode( '; ', $error ) );
267 }
268 return $storeStatus;
269 }
270 private function getChunkFileKey( $index = null ){
271 if( $index === null ){
272 $index = $this->getChunkIndex();
273 }
274 return $this->mFileKey . '.' . $index ;
275 }
276 }
277
278 class UploadChunkZeroLengthFileException extends MWException {};
279 class UploadChunkFileException extends MWException {};