60 private const MAX_US_PROPS_SIZE = 65535;
96 $this->user = $user ?? RequestContext::getMain()->getUser();
112 public function getFile( $key, $noAuth =
false ) {
113 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
115 wfMessage(
'uploadstash-bad-path-bad-format', $key )
119 if ( !$noAuth && !$this->user->isRegistered() ) {
125 if ( !isset( $this->fileMetadata[$key] ) ) {
132 if ( !isset( $this->fileMetadata[$key] ) ) {
134 wfMessage(
'uploadstash-file-not-found', $key )
143 isset( $this->fileMetadata[$key][
'us_props'] ) && strlen( $this->fileMetadata[$key][
'us_props'] )
145 $this->fileProps[$key] =
unserialize( $this->fileMetadata[$key][
'us_props'] );
147 wfDebug( __METHOD__ .
" fetched props for $key from file" );
148 $path = $this->fileMetadata[$key][
'us_path'];
149 $this->fileProps[$key] = $this->repo->getFileProps(
$path );
153 if ( !$this->files[$key]->exists() ) {
154 wfDebug( __METHOD__ .
" tried to get file at $key, but it doesn't exist" );
161 if ( !$noAuth && $this->fileMetadata[$key][
'us_user'] != $this->user->getId() ) {
163 wfMessage(
'uploadstash-wrong-owner', $key )
167 return $this->files[$key];
179 return $this->fileMetadata[$key];
191 return $this->fileProps[$key];
207 if ( !is_file(
$path ) ) {
208 wfDebug( __METHOD__ .
" tried to stash file at '$path', but it doesn't exist" );
214 $mwProps =
new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() );
216 wfDebug( __METHOD__ .
" stashing file at '$path'" );
221 if ( !preg_match(
"/\\.\\Q$extension\\E$/",
$path ) ) {
222 $pathWithGoodExtension =
"$path.$extension";
224 $pathWithGoodExtension =
$path;
232 list( $usec, $sec ) = explode(
' ', microtime() );
233 $usec = substr( $usec, 2 );
234 $key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) .
'.' .
235 Wikimedia\base_convert( (
string)mt_rand(), 10, 36 ) .
'.' .
236 $this->user->getId() .
'.' .
241 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
243 wfMessage(
'uploadstash-bad-path-bad-format', $key )
247 wfDebug( __METHOD__ .
" key for '$path': $key" );
250 $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ),
$path );
252 if ( !$storeStatus->isOK() ) {
261 $error = $storeStatus->getErrorsArray();
262 $error = reset( $error );
263 if ( !count( $error ) ) {
264 $error = $storeStatus->getWarningsArray();
265 $error = reset( $error );
266 if ( !count( $error ) ) {
267 $error = [
'unknown',
'no error recorded' ];
272 $errorMsg = array_shift( $error );
275 $stashPath = $storeStatus->value;
278 if ( !$this->user->isRegistered() ) {
285 wfDebug( __METHOD__ .
" inserting $stashPath under $key" );
286 $dbw = $this->repo->getPrimaryDB();
289 if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
298 'us_user' => $this->user->getId(),
300 'us_orig_path' =>
$path,
301 'us_path' => $stashPath,
302 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
310 'us_source_type' => $sourceType,
311 'us_timestamp' => $dbw->timestamp(),
312 'us_status' =>
'finished'
323 $insertRow[
'us_id'] = $dbw->insertId();
325 $this->fileMetadata[$key] = $insertRow;
327 # create the UploadStashFile object for this file.
341 if ( !$this->user->isRegistered() ) {
347 wfDebug( __METHOD__ .
' clearing all rows for user ' . $this->user->getId() );
348 $dbw = $this->repo->getPrimaryDB();
351 [
'us_user' => $this->user->getId() ],
357 $this->fileMetadata = [];
371 if ( !$this->user->isRegistered() ) {
377 $dbw = $this->repo->getPrimaryDB();
381 $row = $dbw->selectRow(
384 [
'us_key' => $key ],
390 wfMessage(
'uploadstash-no-such-key', $key )
394 if ( $row->us_user != $this->user->getId() ) {
396 wfMessage(
'uploadstash-wrong-owner', $key )
410 wfDebug( __METHOD__ .
" clearing row $key" );
415 $dbw = $this->repo->getPrimaryDB();
419 [
'us_key' => $key ],
426 $this->files[$key]->remove();
428 unset( $this->files[$key] );
429 unset( $this->fileMetadata[$key] );
441 if ( !$this->user->isRegistered() ) {
447 $dbr = $this->repo->getReplicaDB();
451 [
'us_user' => $this->user->getId() ],
455 if ( !is_object(
$res ) ||
$res->numRows() == 0 ) {
462 foreach (
$res as $row ) {
463 array_push(
$keys, $row->us_key );
479 $prohibitedFileExtensions = MediaWikiServices::getInstance()
480 ->getMainConfig()->get( MainConfigNames::ProhibitedFileExtensions );
482 $n = strrpos(
$path,
'.' );
484 if ( $n !==
false ) {
485 $extension = $n ? substr(
$path, $n + 1 ) :
'';
488 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
489 $mimeType = $magic->guessMimeType(
$path );
490 $extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType ) ??
'';
493 $extension = File::normalizeExtension( $extension );
494 if ( in_array( $extension, $prohibitedFileExtensions ) ) {
516 $dbr = $this->repo->getPrimaryDB();
518 $dbr = $this->repo->getReplicaDB();
521 $row =
$dbr->selectRow(
524 'us_user',
'us_key',
'us_orig_path',
'us_path',
'us_props',
525 'us_size',
'us_sha1',
'us_mime',
'us_media_type',
526 'us_image_width',
'us_image_height',
'us_image_bits',
527 'us_source_type',
'us_timestamp',
'us_status',
529 [
'us_key' => $key ],
533 if ( !is_object( $row ) ) {
538 $this->fileMetadata[$key] = (array)$row;
539 $this->fileMetadata[$key][
'us_props'] =
$dbr->decodeBlob( $row->us_props );
553 if (
$file->getSize() === 0 ) {
558 $this->files[$key] =
$file;
unserialize( $serialized)
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.
getFile()
Get the file for this page, if one exists.
Base class for file repositories.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
MimeMagic helper wrapper.
A class containing constants representing the names of configuration variables.
UploadStash is intended to accomplish a few things:
static getExtensionForPath( $path)
Find or guess extension – ensuring that our extension matches our MIME type.
removeFile( $key)
Remove a particular file from the stash.
fetchFileMetadata( $key, $readFromDB=DB_REPLICA)
Helper function: do the actual database query to fetch file metadata.
getFileProps( $key)
Getter for fileProps.
stashFile( $path, $sourceType=null)
Stash a file in a temp directory and record that we did this in the database, along with other metada...
clear()
Remove all files from the stash.
array $fileMetadata
cache of the file metadata that's stored in the database
array $fileProps
fileprops cache
listFiles()
List all files in the stash.
__construct(FileRepo $repo, UserIdentity $user=null)
Represents a temporary filestore, with metadata in the database.
getMetadata( $key)
Getter for file metadata.
removeFileNoAuth( $key)
Remove a file (see removeFile), but doesn't check ownership first.
initFile( $key)
Helper function: Initialize the UploadStashFile for a given file.
getFile( $key, $noAuth=false)
Get a file and its metadata from the stash.
LocalRepo $repo
repository that this uses to store temp files public because we sometimes need to get a LocalFile wit...
array $files
array of initialized repo objects
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.