94 $this->user =
$user ?? RequestContext::getMain()->getUser();
110 public function getFile( $key, $noAuth =
false ) {
111 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
113 wfMessage(
'uploadstash-bad-path-bad-format', $key )
117 if ( !$noAuth && !$this->user->isRegistered() ) {
123 if ( !isset( $this->fileMetadata[$key] ) ) {
130 if ( !isset( $this->fileMetadata[$key] ) ) {
132 wfMessage(
'uploadstash-file-not-found', $key )
141 isset( $this->fileMetadata[$key][
'us_props'] ) && strlen( $this->fileMetadata[$key][
'us_props'] )
143 $this->fileProps[$key] =
unserialize( $this->fileMetadata[$key][
'us_props'] );
145 wfDebug( __METHOD__ .
" fetched props for $key from file" );
146 $path = $this->fileMetadata[$key][
'us_path'];
147 $this->fileProps[$key] = $this->repo->getFileProps(
$path );
151 if ( !$this->files[$key]->exists() ) {
152 wfDebug( __METHOD__ .
" tried to get file at $key, but it doesn't exist" );
159 if ( !$noAuth && $this->fileMetadata[$key][
'us_user'] != $this->user->getId() ) {
161 wfMessage(
'uploadstash-wrong-owner', $key )
165 return $this->files[$key];
177 return $this->fileMetadata[$key];
189 return $this->fileProps[$key];
205 if ( !is_file(
$path ) ) {
206 wfDebug( __METHOD__ .
" tried to stash file at '$path', but it doesn't exist" );
214 wfDebug( __METHOD__ .
" stashing file at '$path'" );
219 if ( !preg_match(
"/\\.\\Q$extension\\E$/",
$path ) ) {
220 $pathWithGoodExtension =
"$path.$extension";
222 $pathWithGoodExtension =
$path;
230 list( $usec, $sec ) = explode(
' ', microtime() );
231 $usec = substr( $usec, 2 );
232 $key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) .
'.' .
233 Wikimedia\base_convert( mt_rand(), 10, 36 ) .
'.' .
234 $this->user->getId() .
'.' .
239 if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
241 wfMessage(
'uploadstash-bad-path-bad-format', $key )
245 wfDebug( __METHOD__ .
" key for '$path': $key" );
248 $storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ),
$path );
250 if ( !$storeStatus->isOK() ) {
259 $error = $storeStatus->getErrorsArray();
260 $error = reset( $error );
261 if ( !count( $error ) ) {
262 $error = $storeStatus->getWarningsArray();
263 $error = reset( $error );
264 if ( !count( $error ) ) {
265 $error = [
'unknown',
'no error recorded' ];
270 $errorMsg = array_shift( $error );
273 $stashPath = $storeStatus->value;
276 if ( !$this->user->isRegistered() ) {
283 wfDebug( __METHOD__ .
" inserting $stashPath under $key" );
284 $dbw = $this->repo->getPrimaryDB();
287 if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
296 'us_user' => $this->user->getId(),
298 'us_orig_path' =>
$path,
299 'us_path' => $stashPath,
300 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
308 'us_source_type' => $sourceType,
309 'us_timestamp' => $dbw->timestamp(),
310 'us_status' =>
'finished'
321 $insertRow[
'us_id'] = $dbw->insertId();
323 $this->fileMetadata[$key] = $insertRow;
325 # create the UploadStashFile object for this file.
339 if ( !$this->user->isRegistered() ) {
345 wfDebug( __METHOD__ .
' clearing all rows for user ' . $this->user->getId() );
346 $dbw = $this->repo->getPrimaryDB();
349 [
'us_user' => $this->user->getId() ],
355 $this->fileMetadata = [];
369 if ( !$this->user->isRegistered() ) {
375 $dbw = $this->repo->getPrimaryDB();
379 $row = $dbw->selectRow(
382 [
'us_key' => $key ],
388 wfMessage(
'uploadstash-no-such-key', $key )
392 if ( $row->us_user != $this->user->getId() ) {
394 wfMessage(
'uploadstash-wrong-owner', $key )
408 wfDebug( __METHOD__ .
" clearing row $key" );
413 $dbw = $this->repo->getPrimaryDB();
417 [
'us_key' => $key ],
424 $this->files[$key]->remove();
426 unset( $this->files[$key] );
427 unset( $this->fileMetadata[$key] );
439 if ( !$this->user->isRegistered() ) {
445 $dbr = $this->repo->getReplicaDB();
449 [
'us_user' => $this->user->getId() ],
453 if ( !is_object(
$res ) ||
$res->numRows() == 0 ) {
460 foreach (
$res as $row ) {
461 array_push(
$keys, $row->us_key );
479 $n = strrpos(
$path,
'.' );
481 if ( $n !==
false ) {
482 $extension = $n ? substr(
$path, $n + 1 ) :
'';
485 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
486 $mimeType = $magic->guessMimeType(
$path );
487 $extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType );
490 $extension = File::normalizeExtension( $extension );
514 $dbr = $this->repo->getPrimaryDB();
516 $dbr = $this->repo->getReplicaDB();
519 $row =
$dbr->selectRow(
522 'us_user',
'us_key',
'us_orig_path',
'us_path',
'us_props',
523 'us_size',
'us_sha1',
'us_mime',
'us_media_type',
524 'us_image_width',
'us_image_height',
'us_image_bits',
525 'us_source_type',
'us_timestamp',
'us_status',
527 [
'us_key' => $key ],
531 if ( !is_object( $row ) ) {
536 $this->fileMetadata[$key] = (array)$row;
537 $this->fileMetadata[$key][
'us_props'] =
$dbr->decodeBlob( $row->us_props );
551 if (
$file->getSize() === 0 ) {
556 $this->files[$key] =
$file;
unserialize( $serialized)
$wgProhibitedFileExtensions
Files with these extensions will never be allowed as uploads.
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.
A repository that stores files in the local filesystem and registers them in the wiki's own database.
MimeMagic helper wrapper.
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.