MediaWiki  1.27.1
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 = 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 );
108  $this->initializePathInfo( $name,
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 ) ) {
358  throw new UploadChunkVerificationException( $res[0] );
359  }
360  }
361 }
362 
364 }
365 
367 }
368 
370 }
static factory($prefix, $extension= '')
Make a new temporary file on the file system.
Definition: TempFSFile.php:54
getVerificationErrorCode($error)
Definition: UploadBase.php:77
__construct($user=null, $stash=false, $repo=false)
Setup local pointers to stash, repo and user (similar to UploadFromStash)
continueChunks($name, $key, $webRequestUpload)
Continue chunk uploading.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
getChunkIndex()
Get the current Chunk index.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
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:1798
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 newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
static extensionFromPath($path, $case= 'lowercase')
Get the final extension from a storage or FS path.
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:242
verifyPartialFile()
A verification routine suitable for partial files.
Definition: UploadBase.php:488
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.
$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
stashFile(User $user=null)
Calls the parent stashFile and updates the uploadsession table to handle "chunks".
Implements uploading from chunks.
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)
concatenateChunks()
Append the final chunk and ready file for parent::performUpload()
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:1004
const OK
Definition: UploadBase.php:60
getChunkStatus()
Get the chunk db state and populate update relevant local values.
outputChunk($chunkPath)
Output the chunk to disk.
getRealPath($srcPath)
Definition: UploadBase.php:291
initializePathInfo($name, $tempPath, $fileSize, $removeTempFile=false)
Initialize the path information.
Definition: UploadBase.php:221
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310