9namespace MediaWiki\Upload;
11use MediaWiki\Context\RequestContext;
12use MediaWiki\FileRepo\File\File;
13use MediaWiki\FileRepo\LocalRepo;
14use MediaWiki\MainConfigNames;
15use MediaWiki\MediaWikiServices;
16use MediaWiki\Upload\Exception\UploadStashBadPathException;
17use MediaWiki\Upload\Exception\UploadStashFileException;
18use MediaWiki\Upload\Exception\UploadStashFileNotFoundException;
19use MediaWiki\Upload\Exception\UploadStashNoSuchKeyException;
20use MediaWiki\Upload\Exception\UploadStashNotLoggedInException;
21use MediaWiki\Upload\Exception\UploadStashWrongOwnerException;
22use MediaWiki\Upload\Exception\UploadStashZeroLengthFileException;
23use MediaWiki\User\UserIdentity;
24use MediaWiki\Utils\MWFileProps;
39 private const MAX_US_PROPS_SIZE = 65535;
68 $this->user = $user ?? RequestContext::getMain()->getUser();
84 public function getFile( $key, $noAuth =
false ) {
85 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
87 wfMessage(
'uploadstash-bad-path-bad-format', $key )
91 if ( !$noAuth && !$this->user->isRegistered() ) {
97 if ( !isset( $this->fileMetadata[$key] ) ) {
104 if ( !isset( $this->fileMetadata[$key] ) ) {
106 wfMessage(
'uploadstash-file-not-found', $key )
115 isset( $this->fileMetadata[$key][
'us_props'] )
116 && strlen( $this->fileMetadata[$key][
'us_props'] )
118 $this->fileProps[$key] = unserialize( $this->fileMetadata[$key][
'us_props'] );
121 wfDebug( __METHOD__ .
" fetched props for $key from file" );
122 $path = $this->fileMetadata[$key][
'us_path'];
123 $this->fileProps[$key] = $this->repo->getFileProps(
$path );
127 if ( !$noAuth && $this->fileMetadata[$key][
'us_user'] != $this->user->getId() ) {
129 wfMessage(
'uploadstash-wrong-owner', $key )
133 return $this->files[$key];
145 return $this->fileMetadata[$key];
157 return $this->fileProps[$key];
174 if ( !$this->user->isRegistered() ) {
180 if ( !is_file(
$path ) ) {
181 wfDebug( __METHOD__ .
" tried to stash file at '$path', but it doesn't exist" );
199 wfDebug( __METHOD__ .
" stashing file at '$path'" );
204 if ( !str_ends_with(
$path,
".$extension" ) ) {
205 $pathWithGoodExtension =
"$path.$extension";
207 $pathWithGoodExtension =
$path;
211 [ $usec, $sec ] = explode(
' ', microtime() );
212 $usec = substr( $usec, 2 );
213 $key = \Wikimedia\base_convert( $sec . $usec, 10, 36 ) .
'.' .
214 \Wikimedia\base_convert( (
string)mt_rand(), 10, 36 ) .
'.' .
215 $this->user->getId() .
'.' .
220 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
222 wfMessage(
'uploadstash-bad-path-bad-format', $key )
226 wfDebug( __METHOD__ .
" key for '$path': $key" );
229 $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ),
$path );
231 if ( !$storeStatus->isOK() ) {
235 foreach ( $storeStatus->getMessages(
'error' ) as $msg ) {
238 foreach ( $storeStatus->getMessages(
'warning' ) as $msg ) {
244 $stashPath = $storeStatus->value;
247 wfDebug( __METHOD__ .
" inserting $stashPath under $key" );
248 $dbw = $this->repo->getPrimaryDB();
250 $serializedFileProps = serialize(
$fileProps );
251 if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
257 $serializedFileProps = serialize(
$fileProps );
261 'us_user' => $this->user->getId(),
263 'us_orig_path' =>
$path,
264 'us_path' => $stashPath,
265 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
273 'us_source_type' => $sourceType,
274 'us_timestamp' => $dbw->timestamp(),
275 'us_status' =>
'finished'
278 $dbw->newInsertQueryBuilder()
279 ->insertInto(
'uploadstash' )
281 ->caller( __METHOD__ )->execute();
283 $insertRow[
'us_id'] = $dbw->insertId();
284 $this->fileMetadata[$key] = $insertRow;
298 if ( !$this->user->isRegistered() ) {
304 wfDebug( __METHOD__ .
' clearing all rows for user ' . $this->user->getId() );
305 $dbw = $this->repo->getPrimaryDB();
306 $dbw->newDeleteQueryBuilder()
307 ->deleteFrom(
'uploadstash' )
308 ->where( [
'us_user' => $this->user->getId() ] )
309 ->caller( __METHOD__ )->execute();
313 $this->fileMetadata = [];
327 if ( !$this->user->isRegistered() ) {
333 $dbw = $this->repo->getPrimaryDB();
336 $row = $dbw->newSelectQueryBuilder()
337 ->select(
'us_user' )
338 ->from(
'uploadstash' )
339 ->where( [
'us_key' => $key ] )
340 ->caller( __METHOD__ )->fetchRow();
344 wfMessage(
'uploadstash-no-such-key', $key )
348 if ( $row->us_user != $this->user->getId() ) {
350 wfMessage(
'uploadstash-wrong-owner', $key )
364 wfDebug( __METHOD__ .
" clearing row $key" );
369 $dbw = $this->repo->getPrimaryDB();
371 $dbw->newDeleteQueryBuilder()
372 ->deleteFrom(
'uploadstash' )
373 ->where( [
'us_key' => $key ] )
374 ->caller( __METHOD__ )->execute();
376 $success = $this->files[$key]->remove();
378 unset( $this->files[$key] );
379 unset( $this->fileMetadata[$key] );
391 if ( !$this->user->isRegistered() ) {
397 return $this->repo->getReplicaDB()->newSelectQueryBuilder()
399 ->from(
'uploadstash' )
400 ->where( [
'us_user' => $this->user->getId() ] )
401 ->caller( __METHOD__ )
402 ->fetchFieldValues();
420 $n = strrpos(
$path,
'.' );
422 if ( $n !==
false ) {
423 $extension = $n ? substr(
$path, $n + 1 ) :
'';
427 $mimeType = $magic->guessMimeType(
$path );
428 $extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType ) ??
'';
431 $extension = File::normalizeExtension( $extension );
432 if ( in_array( $extension, $prohibitedFileExtensions ) ) {
451 $dbr = $this->repo->getPrimaryDB();
453 $dbr = $this->repo->getReplicaDB();
456 $row = $dbr->newSelectQueryBuilder()
458 'us_user',
'us_key',
'us_orig_path',
'us_path',
'us_props',
459 'us_size',
'us_sha1',
'us_mime',
'us_media_type',
460 'us_image_width',
'us_image_height',
'us_image_bits',
461 'us_source_type',
'us_timestamp',
'us_status',
463 ->from(
'uploadstash' )
464 ->where( [
'us_key' => $key ] )
465 ->caller( __METHOD__ )->fetchRow();
467 if ( !is_object( $row ) ) {
472 $this->fileMetadata[$key] = (array)$row;
473 $this->fileMetadata[$key][
'us_props'] = $dbr->decodeBlob( $row->us_props );
488 $this->fileMetadata[$key][
'us_path'],
490 $this->fileMetadata[$key][
'us_sha1'],
491 $this->fileMetadata[$key][
'us_mime'] ??
false
493 $this->files[$key] = $file;
500class_alias( UploadStash::class,
'UploadStash' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
const ProhibitedFileExtensions
Name constant for the ProhibitedFileExtensions setting, for use with Config\get().