MediaWiki  1.28.0
UploadFromChunks.php
Go to the documentation of this file.
1 <?php
31  protected $mOffset;
32  protected $mChunkIndex;
33  protected $mFileKey;
34  protected $mVirtualTempPath;
36  private $repo;
37 
45  public function __construct( User $user, $stash = false, $repo = false ) {
46  $this->user = $user;
47 
48  if ( $repo ) {
49  $this->repo = $repo;
50  } else {
51  $this->repo = RepoGroup::singleton()->getLocalRepo();
52  }
53 
54  if ( $stash ) {
55  $this->stash = $stash;
56  } else {
57  if ( $user ) {
58  wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
59  } else {
60  wfDebug( __METHOD__ . " creating new UploadFromChunks instance with no user\n" );
61  }
62  $this->stash = new UploadStash( $this->repo, $this->user );
63  }
64  }
65 
72  protected function doStashFile( User $user = null ) {
73  // Stash file is the called on creating a new chunk session:
74  $this->mChunkIndex = 0;
75  $this->mOffset = 0;
76 
77  $this->verifyChunk();
78  // Create a local stash target
79  $this->mStashFile = parent::doStashFile( $user );
80  // Update the initial file offset (based on file size)
81  $this->mOffset = $this->mStashFile->getSize();
82  $this->mFileKey = $this->mStashFile->getFileKey();
83 
84  // Output a copy of this first to chunk 0 location:
85  $this->outputChunk( $this->mStashFile->getPath() );
86 
87  // Update db table to reflect initial "chunk" state
88  $this->updateChunkStatus();
89 
90  return $this->mStashFile;
91  }
92 
100  public function continueChunks( $name, $key, $webRequestUpload ) {
101  $this->mFileKey = $key;
102  $this->mUpload = $webRequestUpload;
103  // Get the chunk status form the db:
104  $this->getChunkStatus();
105 
106  $metadata = $this->stash->getMetadata( $key );
107  $this->initializePathInfo( $name,
108  $this->getRealPath( $metadata['us_path'] ),
109  $metadata['us_size'],
110  false
111  );
112  }
113 
118  public function concatenateChunks() {
119  $chunkIndex = $this->getChunkIndex();
120  wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" .
121  $this->getOffset() . ' inx:' . $chunkIndex . "\n" );
122 
123  // Concatenate all the chunks to mVirtualTempPath
124  $fileList = [];
125  // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
126  for ( $i = 0; $i <= $chunkIndex; $i++ ) {
127  $fileList[] = $this->getVirtualChunkLocation( $i );
128  }
129 
130  // Get the file extension from the last chunk
131  $ext = FileBackend::extensionFromPath( $this->mVirtualTempPath );
132  // Get a 0-byte temp file to perform the concatenation at
133  $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext, wfTempDir() );
134  $tmpPath = false; // fail in concatenate()
135  if ( $tmpFile ) {
136  // keep alive with $this
137  $tmpPath = $tmpFile->bind( $this )->getPath();
138  }
139 
140  // Concatenate the chunks at the temp file
141  $tStart = microtime( true );
142  $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE );
143  $tAmount = microtime( true ) - $tStart;
144  if ( !$status->isOK() ) {
145  return $status;
146  }
147 
148  wfDebugLog( 'fileconcatenate', "Combined $i chunks in $tAmount seconds." );
149 
150  // File system path of the actual full temp file
151  $this->setTempFile( $tmpPath );
152 
153  $ret = $this->verifyUpload();
154  if ( $ret['status'] !== UploadBase::OK ) {
155  wfDebugLog( 'fileconcatenate', "Verification failed for chunked upload" );
156  $status->fatal( $this->getVerificationErrorCode( $ret['status'] ) );
157 
158  return $status;
159  }
160 
161  // Update the mTempPath and mStashFile
162  // (for FileUpload or normal Stash to take over)
163  $tStart = microtime( true );
164  // This is a re-implementation of UploadBase::tryStashFile(), we can't call it because we
165  // override doStashFile() with completely different functionality in this class...
166  $error = $this->runUploadStashFileHook( $this->user );
167  if ( $error ) {
168  call_user_func_array( [ $status, 'fatal' ], $error );
169  return $status;
170  }
171  try {
172  $this->mStashFile = parent::doStashFile( $this->user );
173  } catch ( UploadStashException $e ) {
174  $status->fatal( 'uploadstash-exception', get_class( $e ), $e->getMessage() );
175  return $status;
176  }
177 
178  $tAmount = microtime( true ) - $tStart;
179  $this->mStashFile->setLocalReference( $tmpFile ); // reuse (e.g. for getImageInfo())
180  wfDebugLog( 'fileconcatenate', "Stashed combined file ($i chunks) in $tAmount seconds." );
181 
182  return $status;
183  }
184 
190  function getVirtualChunkLocation( $index ) {
191  return $this->repo->getVirtualUrl( 'temp' ) .
192  '/' .
193  $this->repo->getHashPath(
194  $this->getChunkFileKey( $index )
195  ) .
196  $this->getChunkFileKey( $index );
197  }
198 
207  public function addChunk( $chunkPath, $chunkSize, $offset ) {
208  // Get the offset before we add the chunk to the file system
209  $preAppendOffset = $this->getOffset();
210 
211  if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize() ) {
212  $status = Status::newFatal( 'file-too-large' );
213  } else {
214  // Make sure the client is uploading the correct chunk with a matching offset.
215  if ( $preAppendOffset == $offset ) {
216  // Update local chunk index for the current chunk
217  $this->mChunkIndex++;
218  try {
219  # For some reason mTempPath is set to first part
220  $oldTemp = $this->mTempPath;
221  $this->mTempPath = $chunkPath;
222  $this->verifyChunk();
223  $this->mTempPath = $oldTemp;
225  return Status::newFatal( $e->getMessage() );
226  }
227  $status = $this->outputChunk( $chunkPath );
228  if ( $status->isGood() ) {
229  // Update local offset:
230  $this->mOffset = $preAppendOffset + $chunkSize;
231  // Update chunk table status db
232  $this->updateChunkStatus();
233  }
234  } else {
235  $status = Status::newFatal( 'invalid-chunk-offset' );
236  }
237  }
238 
239  return $status;
240  }
241 
245  private function updateChunkStatus() {
246  wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
247  $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
248 
249  $dbw = $this->repo->getMasterDB();
250  $dbw->update(
251  'uploadstash',
252  [
253  'us_status' => 'chunks',
254  'us_chunk_inx' => $this->getChunkIndex(),
255  'us_size' => $this->getOffset()
256  ],
257  [ 'us_key' => $this->mFileKey ],
258  __METHOD__
259  );
260  }
261 
265  private function getChunkStatus() {
266  // get Master db to avoid race conditions.
267  // Otherwise, if chunk upload time < replag there will be spurious errors
268  $dbw = $this->repo->getMasterDB();
269  $row = $dbw->selectRow(
270  'uploadstash',
271  [
272  'us_chunk_inx',
273  'us_size',
274  'us_path',
275  ],
276  [ 'us_key' => $this->mFileKey ],
277  __METHOD__
278  );
279  // Handle result:
280  if ( $row ) {
281  $this->mChunkIndex = $row->us_chunk_inx;
282  $this->mOffset = $row->us_size;
283  $this->mVirtualTempPath = $row->us_path;
284  }
285  }
286 
291  private function getChunkIndex() {
292  if ( $this->mChunkIndex !== null ) {
293  return $this->mChunkIndex;
294  }
295 
296  return 0;
297  }
298 
303  public function getOffset() {
304  if ( $this->mOffset !== null ) {
305  return $this->mOffset;
306  }
307 
308  return 0;
309  }
310 
318  private function outputChunk( $chunkPath ) {
319  // Key is fileKey + chunk index
320  $fileKey = $this->getChunkFileKey();
321 
322  // Store the chunk per its indexed fileKey:
323  $hashPath = $this->repo->getHashPath( $fileKey );
324  $storeStatus = $this->repo->quickImport( $chunkPath,
325  $this->repo->getZonePath( 'temp' ) . "/{$hashPath}{$fileKey}" );
326 
327  // Check for error in stashing the chunk:
328  if ( !$storeStatus->isOK() ) {
329  $error = $storeStatus->getErrorsArray();
330  $error = reset( $error );
331  if ( !count( $error ) ) {
332  $error = $storeStatus->getWarningsArray();
333  $error = reset( $error );
334  if ( !count( $error ) ) {
335  $error = [ 'unknown', 'no error recorded' ];
336  }
337  }
338  throw new UploadChunkFileException( "Error storing file in '$chunkPath': " .
339  implode( '; ', $error ) );
340  }
341 
342  return $storeStatus;
343  }
344 
345  private function getChunkFileKey( $index = null ) {
346  if ( $index === null ) {
347  $index = $this->getChunkIndex();
348  }
349 
350  return $this->mFileKey . '.' . $index;
351  }
352 
358  private function verifyChunk() {
359  // Rest mDesiredDestName here so we verify the name as if it were mFileKey
360  $oldDesiredDestName = $this->mDesiredDestName;
361  $this->mDesiredDestName = $this->mFileKey;
362  $this->mTitle = false;
363  $res = $this->verifyPartialFile();
364  $this->mDesiredDestName = $oldDesiredDestName;
365  $this->mTitle = false;
366  if ( is_array( $res ) ) {
367  throw new UploadChunkVerificationException( $res[0] );
368  }
369  }
370 }
371 
373 }
374 
376 }
377 
379 }
getVerificationErrorCode($error)
Definition: UploadBase.php:86
continueChunks($name, $key, $webRequestUpload)
Continue chunk uploading.
getChunkIndex()
Get the current Chunk index.
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:1936
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
const DELETE_SOURCE
Definition: FileRepo.php:38
string $mTempPath
Local file system path to the file to upload (or a local copy)
Definition: UploadBase.php:40
updateChunkStatus()
Update the chunk db table with the current status:
static extensionFromPath($path, $case= 'lowercase')
Get the final extension from a storage or FS path.
static factory($prefix, $extension= '', $tmpDirectory=null)
Make a new temporary file on the file system.
Definition: TempFSFile.php:55
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
getOffset()
Get the offset at which the next uploaded chunk will be appended to.
static getMaxUploadSize($forType=null)
Get the MediaWiki maximum uploaded file size for given type of upload, based on $wgMaxUploadSize.
setTempFile($tempPath, $fileSize=null)
Definition: UploadBase.php:251
verifyPartialFile()
A verification routine suitable for partial files.
Definition: UploadBase.php:502
addChunk($chunkPath, $chunkSize, $offset)
Add a chunk to the temporary directory.
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
Implements regular file uploads.
wfTempDir()
Tries to get the system directory for temporary files.
$res
Definition: database.txt:21
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
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.
Definition: distributors.txt:9
Implements uploading from chunks.
__construct(User $user, $stash=false, $repo=false)
Setup local pointers to stash, repo and user (similar to UploadFromStash)
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:242
verifyChunk()
Verify that the chunk isn't really an evil html file.
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:35
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:54
getChunkFileKey($index=null)
getId()
Get the user's ID.
Definition: User.php:2083
concatenateChunks()
Append the final chunk and ready file for parent::performUpload()
doStashFile(User $user=null)
Calls the parent doStashFile and updates the uploadsession table to handle "chunks".
getVirtualChunkLocation($index)
Returns the virtual chunk location:
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:1046
const OK
Definition: UploadBase.php:69
getChunkStatus()
Get the chunk db state and populate update relevant local values.
outputChunk($chunkPath)
Output the chunk to disk.
getRealPath($srcPath)
Definition: UploadBase.php:300
initializePathInfo($name, $tempPath, $fileSize, $removeTempFile=false)
Initialize the path information.
Definition: UploadBase.php:230
runUploadStashFileHook(User $user)
Definition: UploadBase.php:978
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300