MediaWiki REL1_27
UploadFromChunks.php
Go to the documentation of this file.
1<?php
31 protected $mOffset;
32 protected $mChunkIndex;
33 protected $mFileKey;
36 private $repo;
37
45 public function __construct( $user = null, $stash = false, $repo = false ) {
46 // user object. sometimes this won't exist, as when running from cron.
47 $this->user = $user;
48
49 if ( $repo ) {
50 $this->repo = $repo;
51 } else {
52 $this->repo = RepoGroup::singleton()->getLocalRepo();
53 }
54
55 if ( $stash ) {
56 $this->stash = $stash;
57 } else {
58 if ( $user ) {
59 wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
60 } else {
61 wfDebug( __METHOD__ . " creating new UploadFromChunks instance with no user\n" );
62 }
63 $this->stash = new UploadStash( $this->repo, $this->user );
64 }
65 }
66
73 public function stashFile( User $user = null ) {
74 // Stash file is the called on creating a new chunk session:
75 $this->mChunkIndex = 0;
76 $this->mOffset = 0;
77
78 $this->verifyChunk();
79 // Create a local stash target
80 $this->mLocalFile = parent::stashFile( $user );
81 // Update the initial file offset (based on file size)
82 $this->mOffset = $this->mLocalFile->getSize();
83 $this->mFileKey = $this->mLocalFile->getFileKey();
84
85 // Output a copy of this first to chunk 0 location:
86 $this->outputChunk( $this->mLocalFile->getPath() );
87
88 // Update db table to reflect initial "chunk" state
89 $this->updateChunkStatus();
90
91 return $this->mLocalFile;
92 }
93
101 public function continueChunks( $name, $key, $webRequestUpload ) {
102 $this->mFileKey = $key;
103 $this->mUpload = $webRequestUpload;
104 // Get the chunk status form the db:
105 $this->getChunkStatus();
106
107 $metadata = $this->stash->getMetadata( $key );
109 $this->getRealPath( $metadata['us_path'] ),
110 $metadata['us_size'],
111 false
112 );
113 }
114
119 public function concatenateChunks() {
120 $chunkIndex = $this->getChunkIndex();
121 wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" .
122 $this->getOffset() . ' inx:' . $chunkIndex . "\n" );
123
124 // Concatenate all the chunks to mVirtualTempPath
125 $fileList = [];
126 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
127 for ( $i = 0; $i <= $chunkIndex; $i++ ) {
128 $fileList[] = $this->getVirtualChunkLocation( $i );
129 }
130
131 // Get the file extension from the last chunk
132 $ext = FileBackend::extensionFromPath( $this->mVirtualTempPath );
133 // Get a 0-byte temp file to perform the concatenation at
134 $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext );
135 $tmpPath = false; // fail in concatenate()
136 if ( $tmpFile ) {
137 // keep alive with $this
138 $tmpPath = $tmpFile->bind( $this )->getPath();
139 }
140
141 // Concatenate the chunks at the temp file
142 $tStart = microtime( true );
143 $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE );
144 $tAmount = microtime( true ) - $tStart;
145 if ( !$status->isOK() ) {
146 return $status;
147 }
148
149 wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds." );
150
151 // File system path of the actual full temp file
152 $this->setTempFile( $tmpPath );
153
154 $ret = $this->verifyUpload();
155 if ( $ret['status'] !== UploadBase::OK ) {
156 wfDebugLog( 'fileconcatenate', "Verification failed for chunked upload" );
157 $status->fatal( $this->getVerificationErrorCode( $ret['status'] ) );
158
159 return $status;
160 }
161
162 // Update the mTempPath and mLocalFile
163 // (for FileUpload or normal Stash to take over)
164 $tStart = microtime( true );
165 $this->mLocalFile = parent::stashFile( $this->user );
166 $tAmount = microtime( true ) - $tStart;
167 $this->mLocalFile->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo())
168 wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds." );
169
170 return $status;
171 }
172
178 function getVirtualChunkLocation( $index ) {
179 return $this->repo->getVirtualUrl( 'temp' ) .
180 '/' .
181 $this->repo->getHashPath(
182 $this->getChunkFileKey( $index )
183 ) .
184 $this->getChunkFileKey( $index );
185 }
186
195 public function addChunk( $chunkPath, $chunkSize, $offset ) {
196 // Get the offset before we add the chunk to the file system
197 $preAppendOffset = $this->getOffset();
198
199 if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize() ) {
200 $status = Status::newFatal( 'file-too-large' );
201 } else {
202 // Make sure the client is uploading the correct chunk with a matching offset.
203 if ( $preAppendOffset == $offset ) {
204 // Update local chunk index for the current chunk
205 $this->mChunkIndex++;
206 try {
207 # For some reason mTempPath is set to first part
208 $oldTemp = $this->mTempPath;
209 $this->mTempPath = $chunkPath;
210 $this->verifyChunk();
211 $this->mTempPath = $oldTemp;
213 return Status::newFatal( $e->getMessage() );
214 }
215 $status = $this->outputChunk( $chunkPath );
216 if ( $status->isGood() ) {
217 // Update local offset:
218 $this->mOffset = $preAppendOffset + $chunkSize;
219 // Update chunk table status db
220 $this->updateChunkStatus();
221 }
222 } else {
223 $status = Status::newFatal( 'invalid-chunk-offset' );
224 }
225 }
226
227 return $status;
228 }
229
233 private function updateChunkStatus() {
234 wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
235 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
236
237 $dbw = $this->repo->getMasterDB();
238 // Use a quick transaction since we will upload the full temp file into shared
239 // storage, which takes time for large files. We don't want to hold locks then.
240 $dbw->update(
241 'uploadstash',
242 [
243 'us_status' => 'chunks',
244 'us_chunk_inx' => $this->getChunkIndex(),
245 'us_size' => $this->getOffset()
246 ],
247 [ 'us_key' => $this->mFileKey ],
248 __METHOD__
249 );
250 $dbw->commit( __METHOD__, 'flush' );
251 }
252
256 private function getChunkStatus() {
257 // get Master db to avoid race conditions.
258 // Otherwise, if chunk upload time < replag there will be spurious errors
259 $dbw = $this->repo->getMasterDB();
260 $row = $dbw->selectRow(
261 'uploadstash',
262 [
263 'us_chunk_inx',
264 'us_size',
265 'us_path',
266 ],
267 [ 'us_key' => $this->mFileKey ],
268 __METHOD__
269 );
270 // Handle result:
271 if ( $row ) {
272 $this->mChunkIndex = $row->us_chunk_inx;
273 $this->mOffset = $row->us_size;
274 $this->mVirtualTempPath = $row->us_path;
275 }
276 }
277
282 private function getChunkIndex() {
283 if ( $this->mChunkIndex !== null ) {
284 return $this->mChunkIndex;
285 }
286
287 return 0;
288 }
289
294 public function getOffset() {
295 if ( $this->mOffset !== null ) {
296 return $this->mOffset;
297 }
298
299 return 0;
300 }
301
309 private function outputChunk( $chunkPath ) {
310 // Key is fileKey + chunk index
311 $fileKey = $this->getChunkFileKey();
312
313 // Store the chunk per its indexed fileKey:
314 $hashPath = $this->repo->getHashPath( $fileKey );
315 $storeStatus = $this->repo->quickImport( $chunkPath,
316 $this->repo->getZonePath( 'temp' ) . "/{$hashPath}{$fileKey}" );
317
318 // Check for error in stashing the chunk:
319 if ( !$storeStatus->isOK() ) {
320 $error = $storeStatus->getErrorsArray();
321 $error = reset( $error );
322 if ( !count( $error ) ) {
323 $error = $storeStatus->getWarningsArray();
324 $error = reset( $error );
325 if ( !count( $error ) ) {
326 $error = [ 'unknown', 'no error recorded' ];
327 }
328 }
329 throw new UploadChunkFileException( "Error storing file in '$chunkPath': " .
330 implode( '; ', $error ) );
331 }
332
333 return $storeStatus;
334 }
335
336 private function getChunkFileKey( $index = null ) {
337 if ( $index === null ) {
338 $index = $this->getChunkIndex();
339 }
340
341 return $this->mFileKey . '.' . $index;
342 }
343
349 private function verifyChunk() {
350 // Rest mDesiredDestName here so we verify the name as if it were mFileKey
351 $oldDesiredDestName = $this->mDesiredDestName;
352 $this->mDesiredDestName = $this->mFileKey;
353 $this->mTitle = false;
354 $res = $this->verifyPartialFile();
355 $this->mDesiredDestName = $oldDesiredDestName;
356 $this->mTitle = false;
357 if ( is_array( $res ) ) {
359 }
360 }
361}
362
365
368
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
$i
Definition Parser.php:1694
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
const DELETE_SOURCE
Definition FileRepo.php:38
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:31
MediaWiki exception.
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
static factory( $prefix, $extension='')
Make a new temporary file on the file system.
string $mTempPath
Local file system path to the file to upload (or a local copy)
getRealPath( $srcPath)
verifyPartialFile()
A verification routine suitable for partial files.
getVerificationErrorCode( $error)
setTempFile( $tempPath, $fileSize=null)
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Initialize the path information.
static getMaxUploadSize( $forType=null)
Get the MediaWiki maximum uploaded file size for given type of upload, based on $wgMaxUploadSize.
Implements uploading from chunks.
addChunk( $chunkPath, $chunkSize, $offset)
Add a chunk to the temporary directory.
outputChunk( $chunkPath)
Output the chunk to disk.
getChunkFileKey( $index=null)
stashFile(User $user=null)
Calls the parent stashFile and updates the uploadsession table to handle "chunks".
continueChunks( $name, $key, $webRequestUpload)
Continue chunk uploading.
getVirtualChunkLocation( $index)
Returns the virtual chunk location:
getOffset()
Get the offset at which the next uploaded chunk will be appended to.
updateChunkStatus()
Update the chunk db table with the current status:
getChunkStatus()
Get the chunk db state and populate update relevant local values.
concatenateChunks()
Append the final chunk and ready file for parent::performUpload()
__construct( $user=null, $stash=false, $repo=false)
Setup local pointers to stash, repo and user (similar to UploadFromStash)
verifyChunk()
Verify that the chunk isn't really an evil html file.
getChunkIndex()
Get the current Chunk index.
Implements regular file uploads.
UploadStash is intended to accomplish a few things:
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1007
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:249
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1810
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:314
returning false will NOT prevent logging $e
Definition hooks.txt:1940
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37