3 * Temporary storage for uploaded files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * UploadStash is intended to accomplish a few things:
25 * - Enable applications to temporarily stash files without publishing them to
27 * - Several parts of MediaWiki do this in similar ways: UploadBase,
28 * UploadWizard, and FirefoggChunkedExtension.
29 * And there are several that reimplement stashing from scratch, in
30 * idiosyncratic ways. The idea is to unify them all here.
31 * Mostly all of them are the same except for storing some custom fields,
32 * which we subsume into the data array.
33 * - Enable applications to find said files later, as long as the db table or
34 * temp files haven't been purged.
35 * - Enable the uploading user (and *ONLY* the uploading user) to access said
36 * files, and thumbnails of said files, via a URL. We accomplish this using
37 * a database table, with ownership checking as you might expect. See
38 * SpecialUploadStash, which implements a web interface to some files stored
41 * UploadStash right now is *mostly* intended to show you one user's slice of
42 * the entire stash. The user parameter is only optional because there are few
43 * cases where we clean out the stash from an automated script. In the future we
44 * might refactor this.
46 * UploadStash represents the entire stash of temporary files.
47 * UploadStashFile is a filestore for the actual physical disk files.
48 * UploadFromStash extends UploadBase, and represents a single stashed file as
49 * it is moved from the stash to the regular file repository
54 // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
55 const KEY_FORMAT_REGEX
= '/^[\w\-\.]+\.\w*$/';
56 const MAX_US_PROPS_SIZE
= 65535;
59 * repository that this uses to store temp files
60 * public because we sometimes need to get a LocalFile within the same repo.
66 // array of initialized repo objects
67 protected $files = [];
69 // cache of the file metadata that's stored in the database
70 protected $fileMetadata = [];
73 protected $fileProps = [];
76 protected $user, $userId, $isLoggedIn;
79 * Represents a temporary filestore, with metadata in the database.
80 * Designed to be compatible with the session stashing code in UploadBase
81 * (should replace it eventually).
83 * @param FileRepo $repo
84 * @param User|null $user
86 public function __construct( FileRepo
$repo, $user = null ) {
87 // this might change based on wiki's configuration.
90 // if a user was passed, use it. otherwise, attempt to use the global.
91 // this keeps FileRepo from breaking when it creates an UploadStash object
96 $this->user
= $wgUser;
99 if ( is_object( $this->user
) ) {
100 $this->userId
= $this->user
->getId();
101 $this->isLoggedIn
= $this->user
->isLoggedIn();
106 * Get a file and its metadata from the stash.
107 * The noAuth param is a bit janky but is required for automated scripts
108 * which clean out the stash.
110 * @param string $key Key under which file information is stored
111 * @param bool $noAuth (optional) Don't check authentication. Used by maintenance scripts.
112 * @throws UploadStashFileNotFoundException
113 * @throws UploadStashNotLoggedInException
114 * @throws UploadStashWrongOwnerException
115 * @throws UploadStashBadPathException
116 * @return UploadStashFile
118 public function getFile( $key, $noAuth = false ) {
119 if ( !preg_match( self
::KEY_FORMAT_REGEX
, $key ) ) {
120 throw new UploadStashBadPathException(
121 wfMessage( 'uploadstash-bad-path-bad-format', $key )
125 if ( !$noAuth && !$this->isLoggedIn
) {
126 throw new UploadStashNotLoggedInException(
127 wfMessage( 'uploadstash-not-logged-in' )
131 if ( !isset( $this->fileMetadata
[$key] ) ) {
132 if ( !$this->fetchFileMetadata( $key ) ) {
133 // If nothing was received, it's likely due to replication lag.
134 // Check the master to see if the record is there.
135 $this->fetchFileMetadata( $key, DB_MASTER
);
138 if ( !isset( $this->fileMetadata
[$key] ) ) {
139 throw new UploadStashFileNotFoundException(
140 wfMessage( 'uploadstash-file-not-found', $key )
144 // create $this->files[$key]
145 $this->initFile( $key );
148 if ( strlen( $this->fileMetadata
[$key]['us_props'] ) ) {
149 $this->fileProps
[$key] = unserialize( $this->fileMetadata
[$key]['us_props'] );
150 } else { // b/c for rows with no us_props
151 wfDebug( __METHOD__
. " fetched props for $key from file\n" );
152 $path = $this->fileMetadata
[$key]['us_path'];
153 $this->fileProps
[$key] = $this->repo
->getFileProps( $path );
157 if ( !$this->files
[$key]->exists() ) {
158 wfDebug( __METHOD__
. " tried to get file at $key, but it doesn't exist\n" );
159 // @todo Is this not an UploadStashFileNotFoundException case?
160 throw new UploadStashBadPathException(
161 wfMessage( 'uploadstash-bad-path' )
165 if ( !$noAuth && $this->fileMetadata
[$key]['us_user'] != $this->userId
) {
166 throw new UploadStashWrongOwnerException(
167 wfMessage( 'uploadstash-wrong-owner', $key )
171 return $this->files
[$key];
175 * Getter for file metadata.
177 * @param string $key Key under which file information is stored
180 public function getMetadata( $key ) {
181 $this->getFile( $key );
183 return $this->fileMetadata
[$key];
187 * Getter for fileProps
189 * @param string $key Key under which file information is stored
192 public function getFileProps( $key ) {
193 $this->getFile( $key );
195 return $this->fileProps
[$key];
199 * Stash a file in a temp directory and record that we did this in the
200 * database, along with other metadata.
202 * @param string $path Path to file you want stashed
203 * @param string|null $sourceType The type of upload that generated this file
204 * (currently, I believe, 'file' or null)
205 * @throws UploadStashBadPathException
206 * @throws UploadStashFileException
207 * @throws UploadStashNotLoggedInException
208 * @return UploadStashFile|null File, or null on failure
210 public function stashFile( $path, $sourceType = null ) {
211 if ( !is_file( $path ) ) {
212 wfDebug( __METHOD__
. " tried to stash file at '$path', but it doesn't exist\n" );
213 throw new UploadStashBadPathException(
214 wfMessage( 'uploadstash-bad-path' )
218 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer() );
219 $fileProps = $mwProps->getPropsFromPath( $path, true );
220 wfDebug( __METHOD__
. " stashing file at '$path'\n" );
222 // we will be initializing from some tmpnam files that don't have extensions.
223 // most of MediaWiki assumes all uploaded files have good extensions. So, we fix this.
224 $extension = self
::getExtensionForPath( $path );
225 if ( !preg_match( "/\\.\\Q$extension\\E$/", $path ) ) {
226 $pathWithGoodExtension = "$path.$extension";
228 $pathWithGoodExtension = $path;
231 // If no key was supplied, make one. a mysql insertid would be totally
232 // reasonable here, except that for historical reasons, the key is this
233 // random thing instead. At least it's not guessable.
234 // Some things that when combined will make a suitably unique key.
235 // see: http://www.jwz.org/doc/mid.html
236 list( $usec, $sec ) = explode( ' ', microtime() );
237 $usec = substr( $usec, 2 );
238 $key = Wikimedia\base_convert
( $sec . $usec, 10, 36 ) . '.' .
239 Wikimedia\base_convert
( mt_rand(), 10, 36 ) . '.' .
240 $this->userId
. '.' .
243 $this->fileProps
[$key] = $fileProps;
245 if ( !preg_match( self
::KEY_FORMAT_REGEX
, $key ) ) {
246 throw new UploadStashBadPathException(
247 wfMessage( 'uploadstash-bad-path-bad-format', $key )
251 wfDebug( __METHOD__
. " key for '$path': $key\n" );
253 // if not already in a temporary area, put it there
254 $storeStatus = $this->repo
->storeTemp( basename( $pathWithGoodExtension ), $path );
256 if ( !$storeStatus->isOK() ) {
257 // It is a convention in MediaWiki to only return one error per API
258 // exception, even if multiple errors are available. We use reset()
259 // to pick the "first" thing that was wrong, preferring errors to
260 // warnings. This is a bit lame, as we may have more info in the
261 // $storeStatus and we're throwing it away, but to fix it means
262 // redesigning API errors significantly.
263 // $storeStatus->value just contains the virtual URL (if anything)
264 // which is probably useless to the caller.
265 $error = $storeStatus->getErrorsArray();
266 $error = reset( $error );
267 if ( !count( $error ) ) {
268 $error = $storeStatus->getWarningsArray();
269 $error = reset( $error );
270 if ( !count( $error ) ) {
271 $error = [ 'unknown', 'no error recorded' ];
274 // At this point, $error should contain the single "most important"
275 // error, plus any parameters.
276 $errorMsg = array_shift( $error );
277 throw new UploadStashFileException( wfMessage( $errorMsg, $error ) );
279 $stashPath = $storeStatus->value
;
281 // fetch the current user ID
282 if ( !$this->isLoggedIn
) {
283 throw new UploadStashNotLoggedInException(
284 wfMessage( 'uploadstash-not-logged-in' )
288 // insert the file metadata into the db.
289 wfDebug( __METHOD__
. " inserting $stashPath under $key\n" );
290 $dbw = $this->repo
->getMasterDB();
292 $serializedFileProps = serialize( $fileProps );
293 if ( strlen( $serializedFileProps ) > self
::MAX_US_PROPS_SIZE
) {
294 // Database is going to truncate this and make the field invalid.
295 // Prioritize important metadata over file handler metadata.
296 // File handler should be prepared to regenerate invalid metadata if needed.
297 $fileProps['metadata'] = false;
298 $serializedFileProps = serialize( $fileProps );
301 $this->fileMetadata
[$key] = [
302 'us_user' => $this->userId
,
304 'us_orig_path' => $path,
305 'us_path' => $stashPath, // virtual URL
306 'us_props' => $dbw->encodeBlob( $serializedFileProps ),
307 'us_size' => $fileProps['size'],
308 'us_sha1' => $fileProps['sha1'],
309 'us_mime' => $fileProps['mime'],
310 'us_media_type' => $fileProps['media_type'],
311 'us_image_width' => $fileProps['width'],
312 'us_image_height' => $fileProps['height'],
313 'us_image_bits' => $fileProps['bits'],
314 'us_source_type' => $sourceType,
315 'us_timestamp' => $dbw->timestamp(),
316 'us_status' => 'finished'
321 $this->fileMetadata
[$key],
325 // store the insertid in the class variable so immediate retrieval
326 // (possibly laggy) isn't necessary.
327 $this->fileMetadata
[$key]['us_id'] = $dbw->insertId();
329 # create the UploadStashFile object for this file.
330 $this->initFile( $key );
332 return $this->getFile( $key );
336 * Remove all files from the stash.
337 * Does not clean up files in the repo, just the record of them.
339 * @throws UploadStashNotLoggedInException
340 * @return bool Success
342 public function clear() {
343 if ( !$this->isLoggedIn
) {
344 throw new UploadStashNotLoggedInException(
345 wfMessage( 'uploadstash-not-logged-in' )
349 wfDebug( __METHOD__
. ' clearing all rows for user ' . $this->userId
. "\n" );
350 $dbw = $this->repo
->getMasterDB();
353 [ 'us_user' => $this->userId
],
359 $this->fileMetadata
= [];
365 * Remove a particular file from the stash. Also removes it from the repo.
368 * @throws UploadStashNoSuchKeyException|UploadStashNotLoggedInException
369 * @throws UploadStashWrongOwnerException
370 * @return bool Success
372 public function removeFile( $key ) {
373 if ( !$this->isLoggedIn
) {
374 throw new UploadStashNotLoggedInException(
375 wfMessage( 'uploadstash-not-logged-in' )
379 $dbw = $this->repo
->getMasterDB();
381 // this is a cheap query. it runs on the master so that this function
382 // still works when there's lag. It won't be called all that often.
383 $row = $dbw->selectRow(
386 [ 'us_key' => $key ],
391 throw new UploadStashNoSuchKeyException(
392 wfMessage( 'uploadstash-no-such-key', $key )
396 if ( $row->us_user
!= $this->userId
) {
397 throw new UploadStashWrongOwnerException(
398 wfMessage( 'uploadstash-wrong-owner', $key )
402 return $this->removeFileNoAuth( $key );
406 * Remove a file (see removeFile), but doesn't check ownership first.
409 * @return bool Success
411 public function removeFileNoAuth( $key ) {
412 wfDebug( __METHOD__
. " clearing row $key\n" );
414 // Ensure we have the UploadStashFile loaded for this key
415 $this->getFile( $key, true );
417 $dbw = $this->repo
->getMasterDB();
421 [ 'us_key' => $key ],
425 /** @todo Look into UnregisteredLocalFile and find out why the rv here is
426 * sometimes wrong (false when file was removed). For now, ignore.
428 $this->files
[$key]->remove();
430 unset( $this->files
[$key] );
431 unset( $this->fileMetadata
[$key] );
437 * List all files in the stash.
439 * @throws UploadStashNotLoggedInException
442 public function listFiles() {
443 if ( !$this->isLoggedIn
) {
444 throw new UploadStashNotLoggedInException(
445 wfMessage( 'uploadstash-not-logged-in' )
449 $dbr = $this->repo
->getReplicaDB();
453 [ 'us_user' => $this->userId
],
457 if ( !is_object( $res ) ||
$res->numRows() == 0 ) {
462 // finish the read before starting writes.
464 foreach ( $res as $row ) {
465 array_push( $keys, $row->us_key
);
472 * Find or guess extension -- ensuring that our extension matches our MIME type.
473 * Since these files are constructed from php tempnames they may not start off
475 * XXX this is somewhat redundant with the checks that ApiUpload.php does with incoming
476 * uploads versus the desired filename. Maybe we can get that passed to us...
477 * @param string $path
478 * @throws UploadStashFileException
481 public static function getExtensionForPath( $path ) {
482 global $wgFileBlacklist;
483 // Does this have an extension?
484 $n = strrpos( $path, '.' );
486 if ( $n !== false ) {
487 $extension = $n ?
substr( $path, $n +
1 ) : '';
489 // If not, assume that it should be related to the MIME type of the original file.
490 $magic = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer();
491 $mimeType = $magic->guessMimeType( $path );
492 $extensions = explode( ' ', $magic->getExtensionsForType( $mimeType ) );
493 if ( count( $extensions ) ) {
494 $extension = $extensions[0];
498 if ( is_null( $extension ) ) {
499 throw new UploadStashFileException(
500 wfMessage( 'uploadstash-no-extension' )
504 $extension = File
::normalizeExtension( $extension );
505 if ( in_array( $extension, $wgFileBlacklist ) ) {
506 // The file should already be checked for being evil.
507 // However, if somehow we got here, we definitely
508 // don't want to give it an extension of .php and
509 // put it in a web accesible directory.
517 * Helper function: do the actual database query to fetch file metadata.
520 * @param int $readFromDB Constant (default: DB_REPLICA)
523 protected function fetchFileMetadata( $key, $readFromDB = DB_REPLICA
) {
524 // populate $fileMetadata[$key]
526 if ( $readFromDB === DB_MASTER
) {
527 // sometimes reading from the master is necessary, if there's replication lag.
528 $dbr = $this->repo
->getMasterDB();
530 $dbr = $this->repo
->getReplicaDB();
533 $row = $dbr->selectRow(
536 'us_user', 'us_key', 'us_orig_path', 'us_path', 'us_props',
537 'us_size', 'us_sha1', 'us_mime', 'us_media_type',
538 'us_image_width', 'us_image_height', 'us_image_bits',
539 'us_source_type', 'us_timestamp', 'us_status',
541 [ 'us_key' => $key ],
545 if ( !is_object( $row ) ) {
546 // key wasn't present in the database. this will happen sometimes.
550 $this->fileMetadata
[$key] = (array)$row;
551 $this->fileMetadata
[$key]['us_props'] = $dbr->decodeBlob( $row->us_props
);
557 * Helper function: Initialize the UploadStashFile for a given file.
559 * @param string $key Key under which to store the object
560 * @throws UploadStashZeroLengthFileException
563 protected function initFile( $key ) {
564 $file = new UploadStashFile( $this->repo
, $this->fileMetadata
[$key]['us_path'], $key );
565 if ( $file->getSize() === 0 ) {
566 throw new UploadStashZeroLengthFileException(
567 wfMessage( 'uploadstash-zero-length' )
570 $this->files
[$key] = $file;