2010-11-03 04:32:41 +00:00
|
|
|
<?php
|
2012-06-05 22:58:54 +00:00
|
|
|
/**
|
|
|
|
|
* Temporary storage for uploaded files.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2011-03-08 18:12:17 +00:00
|
|
|
/**
|
2010-11-03 04:32:41 +00:00
|
|
|
* UploadStash is intended to accomplish a few things:
|
2014-05-09 17:56:05 +00:00
|
|
|
* - Enable applications to temporarily stash files without publishing them to
|
|
|
|
|
* the wiki.
|
|
|
|
|
* - Several parts of MediaWiki do this in similar ways: UploadBase,
|
|
|
|
|
* UploadWizard, and FirefoggChunkedExtension.
|
|
|
|
|
* And there are several that reimplement stashing from scratch, in
|
|
|
|
|
* idiosyncratic ways. The idea is to unify them all here.
|
|
|
|
|
* Mostly all of them are the same except for storing some custom fields,
|
|
|
|
|
* which we subsume into the data array.
|
|
|
|
|
* - Enable applications to find said files later, as long as the db table or
|
|
|
|
|
* temp files haven't been purged.
|
|
|
|
|
* - Enable the uploading user (and *ONLY* the uploading user) to access said
|
|
|
|
|
* files, and thumbnails of said files, via a URL. We accomplish this using
|
|
|
|
|
* a database table, with ownership checking as you might expect. See
|
|
|
|
|
* SpecialUploadStash, which implements a web interface to some files stored
|
|
|
|
|
* this way.
|
2010-12-06 21:01:26 +00:00
|
|
|
*
|
2014-05-09 17:56:05 +00:00
|
|
|
* UploadStash right now is *mostly* intended to show you one user's slice of
|
|
|
|
|
* the entire stash. The user parameter is only optional because there are few
|
|
|
|
|
* cases where we clean out the stash from an automated script. In the future we
|
|
|
|
|
* might refactor this.
|
2011-08-02 20:17:13 +00:00
|
|
|
*
|
2011-07-12 21:11:43 +00:00
|
|
|
* UploadStash represents the entire stash of temporary files.
|
|
|
|
|
* UploadStashFile is a filestore for the actual physical disk files.
|
2014-05-09 17:56:05 +00:00
|
|
|
* UploadFromStash extends UploadBase, and represents a single stashed file as
|
|
|
|
|
* it is moved from the stash to the regular file repository
|
2012-02-08 17:03:43 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Upload
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
|
|
|
|
class UploadStash {
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
// Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
|
2018-06-18 22:53:52 +00:00
|
|
|
const KEY_FORMAT_REGEX = '/^[\w\-\.]+\.\w*$/';
|
2015-10-24 18:15:26 +00:00
|
|
|
const MAX_US_PROPS_SIZE = 65535;
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-03-25 11:21:53 +00:00
|
|
|
/**
|
|
|
|
|
* repository that this uses to store temp files
|
|
|
|
|
* public because we sometimes need to get a LocalFile within the same repo.
|
|
|
|
|
*
|
|
|
|
|
* @var LocalRepo
|
|
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public $repo;
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// array of initialized repo objects
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $files = [];
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// cache of the file metadata that's stored in the database
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $fileMetadata = [];
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// fileprops cache
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $fileProps = [];
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2016-09-10 15:27:49 +00:00
|
|
|
// current user
|
|
|
|
|
protected $user, $userId, $isLoggedIn;
|
2010-11-03 04:32:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-07-12 21:11:43 +00:00
|
|
|
* Represents a temporary filestore, with metadata in the database.
|
2012-07-10 14:58:52 +00:00
|
|
|
* Designed to be compatible with the session stashing code in UploadBase
|
|
|
|
|
* (should replace it eventually).
|
2011-05-29 14:25:20 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param FileRepo $repo
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param User|null $user
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2016-09-10 15:27:49 +00:00
|
|
|
public function __construct( FileRepo $repo, $user = null ) {
|
2010-12-15 01:21:11 +00:00
|
|
|
// this might change based on wiki's configuration.
|
2011-01-25 21:26:53 +00:00
|
|
|
$this->repo = $repo;
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2016-09-10 15:27:49 +00:00
|
|
|
// if a user was passed, use it. otherwise, attempt to use the global.
|
|
|
|
|
// this keeps FileRepo from breaking when it creates an UploadStash object
|
|
|
|
|
if ( $user ) {
|
|
|
|
|
$this->user = $user;
|
|
|
|
|
} else {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$this->user = $wgUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_object( $this->user ) ) {
|
|
|
|
|
$this->userId = $this->user->getId();
|
|
|
|
|
$this->isLoggedIn = $this->user->isLoggedIn();
|
|
|
|
|
}
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
2010-11-12 16:42:46 +00:00
|
|
|
/**
|
2010-11-03 04:32:41 +00:00
|
|
|
* Get a file and its metadata from the stash.
|
2014-05-09 17:56:05 +00:00
|
|
|
* The noAuth param is a bit janky but is required for automated scripts
|
|
|
|
|
* which clean out the stash.
|
2010-11-12 16:42:46 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $key Key under which file information is stored
|
|
|
|
|
* @param bool $noAuth (optional) Don't check authentication. Used by maintenance scripts.
|
2010-11-03 04:32:41 +00:00
|
|
|
* @throws UploadStashFileNotFoundException
|
2011-07-12 21:11:43 +00:00
|
|
|
* @throws UploadStashNotLoggedInException
|
|
|
|
|
* @throws UploadStashWrongOwnerException
|
|
|
|
|
* @throws UploadStashBadPathException
|
2010-11-15 23:49:16 +00:00
|
|
|
* @return UploadStashFile
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-07-13 00:11:02 +00:00
|
|
|
public function getFile( $key, $noAuth = false ) {
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashBadPathException(
|
|
|
|
|
wfMessage( 'uploadstash-bad-path-bad-format', $key )
|
|
|
|
|
);
|
2011-03-08 18:12:17 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2012-11-19 08:07:50 +00:00
|
|
|
if ( !$noAuth && !$this->isLoggedIn ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNotLoggedInException(
|
|
|
|
|
wfMessage( 'uploadstash-not-logged-in' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
if ( !isset( $this->fileMetadata[$key] ) ) {
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->fetchFileMetadata( $key ) ) {
|
2014-05-09 17:56:05 +00:00
|
|
|
// If nothing was received, it's likely due to replication lag.
|
|
|
|
|
// Check the master to see if the record is there.
|
2011-08-16 17:57:32 +00:00
|
|
|
$this->fetchFileMetadata( $key, DB_MASTER );
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
if ( !isset( $this->fileMetadata[$key] ) ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashFileNotFoundException(
|
|
|
|
|
wfMessage( 'uploadstash-file-not-found', $key )
|
|
|
|
|
);
|
2010-12-06 21:01:26 +00:00
|
|
|
}
|
2011-03-08 18:12:17 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// create $this->files[$key]
|
|
|
|
|
$this->initFile( $key );
|
2010-11-03 04:32:41 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// fetch fileprops
|
2012-11-21 21:58:50 +00:00
|
|
|
if ( strlen( $this->fileMetadata[$key]['us_props'] ) ) {
|
|
|
|
|
$this->fileProps[$key] = unserialize( $this->fileMetadata[$key]['us_props'] );
|
|
|
|
|
} else { // b/c for rows with no us_props
|
|
|
|
|
wfDebug( __METHOD__ . " fetched props for $key from file\n" );
|
|
|
|
|
$path = $this->fileMetadata[$key]['us_path'];
|
|
|
|
|
$this->fileProps[$key] = $this->repo->getFileProps( $path );
|
|
|
|
|
}
|
2010-12-06 20:57:42 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !$this->files[$key]->exists() ) {
|
2011-07-14 23:01:00 +00:00
|
|
|
wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" );
|
2014-10-24 19:33:05 +00:00
|
|
|
// @todo Is this not an UploadStashFileNotFoundException case?
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashBadPathException(
|
|
|
|
|
wfMessage( 'uploadstash-bad-path' )
|
|
|
|
|
);
|
2011-07-14 23:01:00 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
|
|
|
|
if ( !$noAuth ) {
|
|
|
|
|
if ( $this->fileMetadata[$key]['us_user'] != $this->userId ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashWrongOwnerException(
|
|
|
|
|
wfMessage( 'uploadstash-wrong-owner', $key )
|
|
|
|
|
);
|
2011-07-13 00:11:02 +00:00
|
|
|
}
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-12-06 21:01:26 +00:00
|
|
|
return $this->files[$key];
|
2010-12-06 20:57:42 +00:00
|
|
|
}
|
2010-11-03 04:32:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-07-12 21:11:43 +00:00
|
|
|
* Getter for file metadata.
|
|
|
|
|
*
|
2014-07-24 17:43:44 +00:00
|
|
|
* @param string $key Key under which file information is stored
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return array
|
2011-07-12 21:11:43 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
public function getMetadata( $key ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->getFile( $key );
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-07-15 15:54:14 +00:00
|
|
|
return $this->fileMetadata[$key];
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Getter for fileProps
|
|
|
|
|
*
|
2014-07-24 17:43:44 +00:00
|
|
|
* @param string $key Key under which file information is stored
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return array
|
2011-07-12 21:11:43 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
public function getFileProps( $key ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->getFile( $key );
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-07-15 15:54:14 +00:00
|
|
|
return $this->fileProps[$key];
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:56:05 +00:00
|
|
|
* Stash a file in a temp directory and record that we did this in the
|
|
|
|
|
* database, along with other metadata.
|
2010-11-03 04:32:41 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $path Path to file you want stashed
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $sourceType The type of upload that generated this file
|
2014-05-09 17:56:05 +00:00
|
|
|
* (currently, I believe, 'file' or null)
|
2010-11-03 04:32:41 +00:00
|
|
|
* @throws UploadStashBadPathException
|
|
|
|
|
* @throws UploadStashFileException
|
2011-07-12 21:11:43 +00:00
|
|
|
* @throws UploadStashNotLoggedInException
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return UploadStashFile|null File, or null on failure
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-08-15 23:58:40 +00:00
|
|
|
public function stashFile( $path, $sourceType = null ) {
|
2012-11-18 10:32:50 +00:00
|
|
|
if ( !is_file( $path ) ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashBadPathException(
|
|
|
|
|
wfMessage( 'uploadstash-bad-path' )
|
|
|
|
|
);
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
2016-09-19 01:39:59 +00:00
|
|
|
|
2017-11-27 01:33:57 +00:00
|
|
|
$mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
|
2016-09-19 01:39:59 +00:00
|
|
|
$fileProps = $mwProps->getPropsFromPath( $path, true );
|
2011-07-12 21:11:43 +00:00
|
|
|
wfDebug( __METHOD__ . " stashing file at '$path'\n" );
|
|
|
|
|
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
// we will be initializing from some tmpnam files that don't have extensions.
|
|
|
|
|
// most of MediaWiki assumes all uploaded files have good extensions. So, we fix this.
|
|
|
|
|
$extension = self::getExtensionForPath( $path );
|
2012-11-18 10:32:50 +00:00
|
|
|
if ( !preg_match( "/\\.\\Q$extension\\E$/", $path ) ) {
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
$pathWithGoodExtension = "$path.$extension";
|
2012-11-18 10:32:50 +00:00
|
|
|
} else {
|
|
|
|
|
$pathWithGoodExtension = $path;
|
2011-03-08 18:12:17 +00:00
|
|
|
}
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
|
2014-05-09 17:56:05 +00:00
|
|
|
// If no key was supplied, make one. a mysql insertid would be totally
|
|
|
|
|
// reasonable here, except that for historical reasons, the key is this
|
|
|
|
|
// random thing instead. At least it's not guessable.
|
|
|
|
|
// Some things that when combined will make a suitably unique key.
|
2011-08-15 23:58:40 +00:00
|
|
|
// see: http://www.jwz.org/doc/mid.html
|
2013-02-03 18:47:42 +00:00
|
|
|
list( $usec, $sec ) = explode( ' ', microtime() );
|
|
|
|
|
$usec = substr( $usec, 2 );
|
2015-11-24 22:51:42 +00:00
|
|
|
$key = Wikimedia\base_convert( $sec . $usec, 10, 36 ) . '.' .
|
|
|
|
|
Wikimedia\base_convert( mt_rand(), 10, 36 ) . '.' .
|
2011-10-29 01:17:26 +00:00
|
|
|
$this->userId . '.' .
|
2011-08-15 23:58:40 +00:00
|
|
|
$extension;
|
2010-11-03 04:32:41 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->fileProps[$key] = $fileProps;
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashBadPathException(
|
|
|
|
|
wfMessage( 'uploadstash-bad-path-bad-format', $key )
|
|
|
|
|
);
|
2011-03-08 18:12:17 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
wfDebug( __METHOD__ . " key for '$path': $key\n" );
|
2010-11-03 04:32:41 +00:00
|
|
|
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
// if not already in a temporary area, put it there
|
2012-11-18 10:32:50 +00:00
|
|
|
$storeStatus = $this->repo->storeTemp( basename( $pathWithGoodExtension ), $path );
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !$storeStatus->isOK() ) {
|
2014-05-09 17:56:05 +00:00
|
|
|
// It is a convention in MediaWiki to only return one error per API
|
|
|
|
|
// exception, even if multiple errors are available. We use reset()
|
|
|
|
|
// to pick the "first" thing that was wrong, preferring errors to
|
|
|
|
|
// warnings. This is a bit lame, as we may have more info in the
|
|
|
|
|
// $storeStatus and we're throwing it away, but to fix it means
|
2010-11-03 04:32:41 +00:00
|
|
|
// redesigning API errors significantly.
|
2014-05-09 17:56:05 +00:00
|
|
|
// $storeStatus->value just contains the virtual URL (if anything)
|
|
|
|
|
// which is probably useless to the caller.
|
2011-07-25 16:55:19 +00:00
|
|
|
$error = $storeStatus->getErrorsArray();
|
2011-06-21 15:13:03 +00:00
|
|
|
$error = reset( $error );
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !count( $error ) ) {
|
2011-07-25 16:55:19 +00:00
|
|
|
$error = $storeStatus->getWarningsArray();
|
2011-06-21 15:13:03 +00:00
|
|
|
$error = reset( $error );
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !count( $error ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$error = [ 'unknown', 'no error recorded' ];
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 17:56:05 +00:00
|
|
|
// At this point, $error should contain the single "most important"
|
|
|
|
|
// error, plus any parameters.
|
2012-08-10 19:14:01 +00:00
|
|
|
$errorMsg = array_shift( $error );
|
2017-10-09 14:26:46 +00:00
|
|
|
throw new UploadStashFileException( wfMessage( $errorMsg, $error ) );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
2011-07-25 16:55:19 +00:00
|
|
|
$stashPath = $storeStatus->value;
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// fetch the current user ID
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->isLoggedIn ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNotLoggedInException(
|
|
|
|
|
wfMessage( 'uploadstash-not-logged-in' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-13 19:08:51 +00:00
|
|
|
// insert the file metadata into the db.
|
|
|
|
|
wfDebug( __METHOD__ . " inserting $stashPath under $key\n" );
|
2016-03-19 00:08:06 +00:00
|
|
|
$dbw = $this->repo->getMasterDB();
|
2011-07-14 21:33:09 +00:00
|
|
|
|
2015-10-24 18:15:26 +00:00
|
|
|
$serializedFileProps = serialize( $fileProps );
|
|
|
|
|
if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
|
|
|
|
|
// Database is going to truncate this and make the field invalid.
|
|
|
|
|
// Prioritize important metadata over file handler metadata.
|
|
|
|
|
// File handler should be prepared to regenerate invalid metadata if needed.
|
|
|
|
|
$fileProps['metadata'] = false;
|
|
|
|
|
$serializedFileProps = serialize( $fileProps );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->fileMetadata[$key] = [
|
2011-07-13 19:08:51 +00:00
|
|
|
'us_user' => $this->userId,
|
2011-07-12 21:11:43 +00:00
|
|
|
'us_key' => $key,
|
|
|
|
|
'us_orig_path' => $path,
|
2011-12-20 03:52:06 +00:00
|
|
|
'us_path' => $stashPath, // virtual URL
|
2015-10-24 18:15:26 +00:00
|
|
|
'us_props' => $dbw->encodeBlob( $serializedFileProps ),
|
2011-07-12 21:11:43 +00:00
|
|
|
'us_size' => $fileProps['size'],
|
|
|
|
|
'us_sha1' => $fileProps['sha1'],
|
|
|
|
|
'us_mime' => $fileProps['mime'],
|
|
|
|
|
'us_media_type' => $fileProps['media_type'],
|
|
|
|
|
'us_image_width' => $fileProps['width'],
|
|
|
|
|
'us_image_height' => $fileProps['height'],
|
|
|
|
|
'us_image_bits' => $fileProps['bits'],
|
|
|
|
|
'us_source_type' => $sourceType,
|
2011-07-13 19:08:51 +00:00
|
|
|
'us_timestamp' => $dbw->timestamp(),
|
|
|
|
|
'us_status' => 'finished'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-08-15 23:58:40 +00:00
|
|
|
$dbw->insert(
|
2011-07-12 21:11:43 +00:00
|
|
|
'uploadstash',
|
|
|
|
|
$this->fileMetadata[$key],
|
2011-07-15 15:54:14 +00:00
|
|
|
__METHOD__
|
2010-11-03 04:32:41 +00:00
|
|
|
);
|
|
|
|
|
|
2014-05-09 17:56:05 +00:00
|
|
|
// store the insertid in the class variable so immediate retrieval
|
2018-08-15 23:33:12 +00:00
|
|
|
// (possibly laggy) isn't necessary.
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->fileMetadata[$key]['us_id'] = $dbw->insertId();
|
2011-03-08 18:12:17 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
# create the UploadStashFile object for this file.
|
|
|
|
|
$this->initFile( $key );
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-12-06 21:01:26 +00:00
|
|
|
return $this->getFile( $key );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-15 01:21:11 +00:00
|
|
|
/**
|
|
|
|
|
* Remove all files from the stash.
|
|
|
|
|
* Does not clean up files in the repo, just the record of them.
|
2011-07-12 21:11:43 +00:00
|
|
|
*
|
|
|
|
|
* @throws UploadStashNotLoggedInException
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return bool Success
|
2010-12-15 01:21:11 +00:00
|
|
|
*/
|
|
|
|
|
public function clear() {
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->isLoggedIn ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNotLoggedInException(
|
|
|
|
|
wfMessage( 'uploadstash-not-logged-in' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-09-14 17:23:09 +00:00
|
|
|
wfDebug( __METHOD__ . ' clearing all rows for user ' . $this->userId . "\n" );
|
2016-03-19 00:08:06 +00:00
|
|
|
$dbw = $this->repo->getMasterDB();
|
2011-07-12 21:11:43 +00:00
|
|
|
$dbw->delete(
|
|
|
|
|
'uploadstash',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'us_user' => $this->userId ],
|
2011-07-15 15:54:14 +00:00
|
|
|
__METHOD__
|
2011-07-12 21:11:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# destroy objects.
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->files = [];
|
|
|
|
|
$this->fileMetadata = [];
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-12-15 01:21:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-07-12 21:11:43 +00:00
|
|
|
* Remove a particular file from the stash. Also removes it from the repo.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $key
|
2014-05-09 17:56:05 +00:00
|
|
|
* @throws UploadStashNoSuchKeyException|UploadStashNotLoggedInException
|
|
|
|
|
* @throws UploadStashWrongOwnerException
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return bool Success
|
2010-12-15 01:21:11 +00:00
|
|
|
*/
|
2011-07-15 15:54:14 +00:00
|
|
|
public function removeFile( $key ) {
|
|
|
|
|
if ( !$this->isLoggedIn ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNotLoggedInException(
|
|
|
|
|
wfMessage( 'uploadstash-not-logged-in' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2016-03-19 00:08:06 +00:00
|
|
|
$dbw = $this->repo->getMasterDB();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2014-05-09 17:56:05 +00:00
|
|
|
// this is a cheap query. it runs on the master so that this function
|
|
|
|
|
// still works when there's lag. It won't be called all that often.
|
2011-07-12 21:11:43 +00:00
|
|
|
$row = $dbw->selectRow(
|
|
|
|
|
'uploadstash',
|
|
|
|
|
'us_user',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'us_key' => $key ],
|
2011-07-12 21:11:43 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( !$row ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNoSuchKeyException(
|
|
|
|
|
wfMessage( 'uploadstash-no-such-key', $key )
|
|
|
|
|
);
|
2011-07-15 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( $row->us_user != $this->userId ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashWrongOwnerException(
|
|
|
|
|
wfMessage( 'uploadstash-wrong-owner', $key )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-13 00:11:02 +00:00
|
|
|
return $this->removeFileNoAuth( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a file (see removeFile), but doesn't check ownership first.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @return bool Success
|
2011-07-13 00:11:02 +00:00
|
|
|
*/
|
|
|
|
|
public function removeFileNoAuth( $key ) {
|
|
|
|
|
wfDebug( __METHOD__ . " clearing row $key\n" );
|
|
|
|
|
|
2013-02-26 23:15:17 +00:00
|
|
|
// Ensure we have the UploadStashFile loaded for this key
|
2013-10-29 15:56:35 +00:00
|
|
|
$this->getFile( $key, true );
|
2013-02-26 23:15:17 +00:00
|
|
|
|
2016-03-19 00:08:06 +00:00
|
|
|
$dbw = $this->repo->getMasterDB();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
$dbw->delete(
|
|
|
|
|
'uploadstash',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'us_key' => $key ],
|
2011-07-15 15:54:14 +00:00
|
|
|
__METHOD__
|
2011-07-12 21:11:43 +00:00
|
|
|
);
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2014-05-09 17:56:05 +00:00
|
|
|
/** @todo Look into UnregisteredLocalFile and find out why the rv here is
|
|
|
|
|
* sometimes wrong (false when file was removed). For now, ignore.
|
|
|
|
|
*/
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->files[$key]->remove();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
unset( $this->files[$key] );
|
|
|
|
|
unset( $this->fileMetadata[$key] );
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-12-15 01:21:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List all files in the stash.
|
2011-07-12 21:11:43 +00:00
|
|
|
*
|
|
|
|
|
* @throws UploadStashNotLoggedInException
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return array
|
2010-12-15 01:21:11 +00:00
|
|
|
*/
|
|
|
|
|
public function listFiles() {
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->isLoggedIn ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashNotLoggedInException(
|
|
|
|
|
wfMessage( 'uploadstash-not-logged-in' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2016-11-18 15:42:39 +00:00
|
|
|
$dbr = $this->repo->getReplicaDB();
|
2011-07-12 21:11:43 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'uploadstash',
|
|
|
|
|
'us_key',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'us_user' => $this->userId ],
|
2011-07-12 21:11:43 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !is_object( $res ) || $res->numRows() == 0 ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
// nothing to do.
|
2011-07-12 21:11:43 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 19:08:51 +00:00
|
|
|
// finish the read before starting writes.
|
2016-02-17 09:09:32 +00:00
|
|
|
$keys = [];
|
2011-07-15 15:54:14 +00:00
|
|
|
foreach ( $res as $row ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
array_push( $keys, $row->us_key );
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $keys;
|
2010-12-15 01:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
/**
|
2014-07-24 14:04:48 +00:00
|
|
|
* Find or guess extension -- ensuring that our extension matches our MIME type.
|
2011-03-08 18:12:17 +00:00
|
|
|
* Since these files are constructed from php tempnames they may not start off
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
* with an extension.
|
2011-03-08 18:12:17 +00:00
|
|
|
* XXX this is somewhat redundant with the checks that ApiUpload.php does with incoming
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
* uploads versus the desired filename. Maybe we can get that passed to us...
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $path
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws UploadStashFileException
|
2012-02-09 21:36:14 +00:00
|
|
|
* @return string
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public static function getExtensionForPath( $path ) {
|
2013-05-16 22:09:44 +00:00
|
|
|
global $wgFileBlacklist;
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
// Does this have an extension?
|
|
|
|
|
$n = strrpos( $path, '.' );
|
|
|
|
|
$extension = null;
|
|
|
|
|
if ( $n !== false ) {
|
|
|
|
|
$extension = $n ? substr( $path, $n + 1 ) : '';
|
|
|
|
|
} else {
|
2014-07-24 14:04:48 +00:00
|
|
|
// If not, assume that it should be related to the MIME type of the original file.
|
2017-11-27 01:33:57 +00:00
|
|
|
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
$mimeType = $magic->guessMimeType( $path );
|
2017-11-27 01:33:57 +00:00
|
|
|
$extensions = explode( ' ', $magic->getExtensionsForType( $mimeType ) );
|
2011-03-08 18:12:17 +00:00
|
|
|
if ( count( $extensions ) ) {
|
|
|
|
|
$extension = $extensions[0];
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_null( $extension ) ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashFileException(
|
|
|
|
|
wfMessage( 'uploadstash-no-extension' )
|
|
|
|
|
);
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 22:09:44 +00:00
|
|
|
$extension = File::normalizeExtension( $extension );
|
|
|
|
|
if ( in_array( $extension, $wgFileBlacklist ) ) {
|
|
|
|
|
// The file should already be checked for being evil.
|
|
|
|
|
// However, if somehow we got here, we definitely
|
|
|
|
|
// don't want to give it an extension of .php and
|
|
|
|
|
// put it in a web accesible directory.
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2013-05-16 22:09:44 +00:00
|
|
|
return $extension;
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function: do the actual database query to fetch file metadata.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $key
|
2016-09-05 19:55:19 +00:00
|
|
|
* @param int $readFromDB Constant (default: DB_REPLICA)
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return bool
|
2011-07-12 21:11:43 +00:00
|
|
|
*/
|
2016-09-05 19:55:19 +00:00
|
|
|
protected function fetchFileMetadata( $key, $readFromDB = DB_REPLICA ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
// populate $fileMetadata[$key]
|
2011-08-15 23:40:57 +00:00
|
|
|
$dbr = null;
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( $readFromDB === DB_MASTER ) {
|
2011-08-15 23:40:57 +00:00
|
|
|
// sometimes reading from the master is necessary, if there's replication lag.
|
2016-03-19 00:08:06 +00:00
|
|
|
$dbr = $this->repo->getMasterDB();
|
2011-08-15 23:40:57 +00:00
|
|
|
} else {
|
2016-11-18 15:42:39 +00:00
|
|
|
$dbr = $this->repo->getReplicaDB();
|
2011-08-15 23:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
$row = $dbr->selectRow(
|
|
|
|
|
'uploadstash',
|
2017-10-12 18:34:15 +00:00
|
|
|
[
|
|
|
|
|
'us_user', 'us_key', 'us_orig_path', 'us_path', 'us_props',
|
|
|
|
|
'us_size', 'us_sha1', 'us_mime', 'us_media_type',
|
|
|
|
|
'us_image_width', 'us_image_height', 'us_image_bits',
|
|
|
|
|
'us_source_type', 'us_timestamp', 'us_status',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'us_key' => $key ],
|
2011-07-12 21:11:43 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2011-07-15 15:54:14 +00:00
|
|
|
|
|
|
|
|
if ( !is_object( $row ) ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
// key wasn't present in the database. this will happen sometimes.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-08-15 18:17:51 +00:00
|
|
|
$this->fileMetadata[$key] = (array)$row;
|
2014-01-29 18:18:53 +00:00
|
|
|
$this->fileMetadata[$key]['us_props'] = $dbr->decodeBlob( $row->us_props );
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function: Initialize the UploadStashFile for a given file.
|
|
|
|
|
*
|
2014-07-24 17:43:44 +00:00
|
|
|
* @param string $key Key under which to store the object
|
2011-07-12 21:11:43 +00:00
|
|
|
* @throws UploadStashZeroLengthFileException
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function initFile( $key ) {
|
|
|
|
|
$file = new UploadStashFile( $this->repo, $this->fileMetadata[$key]['us_path'], $key );
|
|
|
|
|
if ( $file->getSize() === 0 ) {
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashZeroLengthFileException(
|
|
|
|
|
wfMessage( 'uploadstash-zero-length' )
|
|
|
|
|
);
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
$this->files[$key] = $file;
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2010-11-03 04:32:41 +00:00
|
|
|
class UploadStashFile extends UnregisteredLocalFile {
|
2011-07-12 21:11:43 +00:00
|
|
|
private $fileKey;
|
2010-11-03 04:32:41 +00:00
|
|
|
private $urlName;
|
2011-02-20 13:33:42 +00:00
|
|
|
protected $url;
|
2010-11-03 04:32:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:56:05 +00:00
|
|
|
* A LocalFile wrapper around a file that has been temporarily stashed,
|
|
|
|
|
* so we can do things like create thumbnails for it. Arguably
|
|
|
|
|
* UnregisteredLocalFile should be handling its own file repo but that
|
|
|
|
|
* class is a bit retarded currently.
|
2010-11-12 16:42:46 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param FileRepo $repo Repository where we should find the path
|
|
|
|
|
* @param string $path Path to file
|
|
|
|
|
* @param string $key Key to store the path and any stashed data under
|
2010-11-03 04:32:41 +00:00
|
|
|
* @throws UploadStashBadPathException
|
|
|
|
|
* @throws UploadStashFileNotFoundException
|
|
|
|
|
*/
|
2011-07-12 21:11:43 +00:00
|
|
|
public function __construct( $repo, $path, $key ) {
|
|
|
|
|
$this->fileKey = $key;
|
2010-11-03 04:32:41 +00:00
|
|
|
|
|
|
|
|
// resolve mwrepo:// urls
|
|
|
|
|
if ( $repo->isVirtualUrl( $path ) ) {
|
2011-03-08 18:12:17 +00:00
|
|
|
$path = $repo->resolveVirtualUrl( $path );
|
2011-06-28 22:00:21 +00:00
|
|
|
} else {
|
2014-05-09 17:56:05 +00:00
|
|
|
// check if path appears to be sane, no parent traversals,
|
|
|
|
|
// and is in this repo's temp zone.
|
2011-06-28 22:00:21 +00:00
|
|
|
$repoTempPath = $repo->getZonePath( 'temp' );
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( ( !$repo->validateFilename( $path ) ) ||
|
|
|
|
|
( strpos( $path, $repoTempPath ) !== 0 )
|
|
|
|
|
) {
|
2014-05-09 17:56:05 +00:00
|
|
|
wfDebug( "UploadStash: tried to construct an UploadStashFile "
|
|
|
|
|
. "from a file that should already exist at '$path', but path is not valid\n" );
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashBadPathException(
|
|
|
|
|
wfMessage( 'uploadstash-bad-path-invalid' )
|
|
|
|
|
);
|
2011-06-28 22:00:21 +00:00
|
|
|
}
|
2010-11-03 04:32:41 +00:00
|
|
|
|
2011-06-28 22:00:21 +00:00
|
|
|
// check if path exists! and is a plain file.
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !$repo->fileExists( $path ) ) {
|
2014-05-09 17:56:05 +00:00
|
|
|
wfDebug( "UploadStash: tried to construct an UploadStashFile from "
|
|
|
|
|
. "a file that should already exist at '$path', but path is not found\n" );
|
2017-10-16 15:29:37 +00:00
|
|
|
throw new UploadStashFileNotFoundException(
|
|
|
|
|
wfMessage( 'uploadstash-file-not-found-not-exists' )
|
|
|
|
|
);
|
2011-06-28 22:00:21 +00:00
|
|
|
}
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent::__construct( false, $repo, $path, false );
|
|
|
|
|
|
|
|
|
|
$this->name = basename( $this->path );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A method needed by the file transforming and scaling routines in File.php
|
|
|
|
|
* We do not necessarily care about doing the description at this point
|
2014-05-09 17:56:05 +00:00
|
|
|
* However, we also can't return the empty string, as the rest of MediaWiki
|
|
|
|
|
* demands this (and calls to imagemagick convert require it to be there)
|
2010-11-12 16:42:46 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string Dummy value
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
|
|
|
|
public function getDescriptionUrl() {
|
|
|
|
|
return $this->getUrl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the path for the thumbnail (actually any transformation of this file)
|
2011-03-08 18:12:17 +00:00
|
|
|
* The actual argument is the result of thumbName although we seem to have
|
2010-11-03 04:32:41 +00:00
|
|
|
* buggy code elsewhere that expects a boolean 'suffix'
|
|
|
|
|
*
|
2014-05-09 17:56:05 +00:00
|
|
|
* @param string $thumbName Name of thumbnail (e.g. "120px-123456.jpg" ),
|
|
|
|
|
* or false to just get the path
|
|
|
|
|
* @return string Path thumbnail should take on filesystem, or containing
|
|
|
|
|
* directory if thumbname is false
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public function getThumbPath( $thumbName = false ) {
|
2010-11-03 04:32:41 +00:00
|
|
|
$path = dirname( $this->path );
|
|
|
|
|
if ( $thumbName !== false ) {
|
|
|
|
|
$path .= "/$thumbName";
|
|
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
return $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-08 18:12:17 +00:00
|
|
|
* Return the file/url base name of a thumbnail with the specified parameters.
|
|
|
|
|
* We override this because we want to use the pretty url name instead of the
|
2011-02-11 19:10:31 +00:00
|
|
|
* ugly file name.
|
2010-11-03 04:32:41 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param array $params Handler-specific parameters
|
|
|
|
|
* @param int $flags Bitfield that supports THUMB_* constants
|
2015-10-05 15:53:13 +00:00
|
|
|
* @return string|null Base name for URL, like '120px-12345.jpg', or null if there is no handler
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2012-09-16 17:18:27 +00:00
|
|
|
function thumbName( $params, $flags = 0 ) {
|
2011-02-11 19:10:31 +00:00
|
|
|
return $this->generateThumbName( $this->getUrlName(), $params );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
2010-11-16 01:15:58 +00:00
|
|
|
/**
|
2014-05-09 17:56:05 +00:00
|
|
|
* Helper function -- given a 'subpage', return the local URL,
|
|
|
|
|
* e.g. /wiki/Special:UploadStash/subpage
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $subPage
|
|
|
|
|
* @return string Local URL for this subpage in the Special:UploadStash space.
|
2010-11-16 01:15:58 +00:00
|
|
|
*/
|
|
|
|
|
private function getSpecialUrl( $subPage ) {
|
|
|
|
|
return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL();
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-08 18:12:17 +00:00
|
|
|
/**
|
|
|
|
|
* Get a URL to access the thumbnail
|
|
|
|
|
* This is required because the model of how files work requires that
|
2014-05-09 17:56:05 +00:00
|
|
|
* the thumbnail urls be predictable. However, in our model the URL is
|
|
|
|
|
* not based on the filename (that's hidden in the db)
|
2010-11-03 04:32:41 +00:00
|
|
|
*
|
2014-05-09 17:56:05 +00:00
|
|
|
* @param string $thumbName Basename of thumbnail file -- however, we don't
|
|
|
|
|
* want to use the file exactly
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string URL to access thumbnail, or URL with partial path
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public function getThumbUrl( $thumbName = false ) {
|
Dual strategy thumbnailing -- locally for development and simpler wikis, or in the cluster for setups like the WMF's
When we did our test deploy, we found that we could not scale thumbnails locally in the cluster (and this was undesirable anyway).
So, we moved UploadStash thumbnailing to occur on URL invocation, which is more like the usual MediaWiki model anyway. So the customized transform()
moved from UploadStash to just being a special case of how SpecialUploadStash does things.
Note that the Special:UploadStash url masks how the thumbnail is obtained. Unlike the regular Commons wiki, the user never sees the cluster's URL for the thumbnail.
A web request, or an imageMagick call, is performed right there and then, and the results are catted out to the client.
For consistency, we did not use wfStreamfile since we were in some cases streaming from contents obtained over the MWhttpRequest, not just local files.
2010-11-30 02:45:10 +00:00
|
|
|
wfDebug( __METHOD__ . " getting for $thumbName \n" );
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2011-01-30 16:57:19 +00:00
|
|
|
return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-08 18:12:17 +00:00
|
|
|
/**
|
2010-11-03 04:32:41 +00:00
|
|
|
* The basename for the URL, which we want to not be related to the filename.
|
|
|
|
|
* Will also be used as the lookup key for a thumbnail file.
|
2010-11-12 16:42:46 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string Base url name, like '120px-123456.jpg'
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public function getUrlName() {
|
2014-05-09 14:53:19 +00:00
|
|
|
if ( !$this->urlName ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->urlName = $this->fileKey;
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
return $this->urlName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the URL of the file, if for some reason we wanted to download it
|
2010-11-12 16:42:46 +00:00
|
|
|
* We tend not to do this for the original file, but we do want thumb icons
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string Url
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
|
|
|
|
public function getUrl() {
|
|
|
|
|
if ( !isset( $this->url ) ) {
|
2011-01-30 16:57:19 +00:00
|
|
|
$this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
return $this->url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:56:05 +00:00
|
|
|
* Parent classes use this method, for no obvious reason, to return the path
|
|
|
|
|
* (relative to wiki root, I assume). But with this class, the URL is
|
|
|
|
|
* unrelated to the path.
|
2010-11-03 04:32:41 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string Url
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-03-08 18:12:17 +00:00
|
|
|
public function getFullUrl() {
|
2010-11-03 04:32:41 +00:00
|
|
|
return $this->getUrl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-09 17:56:05 +00:00
|
|
|
* Getter for file key (the unique id by which this file's location &
|
|
|
|
|
* metadata is stored in the db)
|
2010-11-12 16:42:46 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string File key
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-07-12 21:11:43 +00:00
|
|
|
public function getFileKey() {
|
|
|
|
|
return $this->fileKey;
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the associated temporary file
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return status Success
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
|
|
|
|
public function remove() {
|
2012-04-05 04:10:50 +00:00
|
|
|
if ( !$this->repo->fileExists( $this->path ) ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
// Maybe the file's already been removed? This could totally happen in UploadBase.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
return $this->repo->freeTemp( $this->path );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-14 23:01:00 +00:00
|
|
|
public function exists() {
|
2012-04-05 04:10:50 +00:00
|
|
|
return $this->repo->fileExists( $this->path );
|
2011-07-14 23:01:00 +00:00
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2017-10-09 14:26:46 +00:00
|
|
|
class UploadStashException extends MWException implements ILocalizedException {
|
|
|
|
|
/** @var string|array|MessageSpecifier */
|
|
|
|
|
protected $messageSpec;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|array|MessageSpecifier $messageSpec See Message::newFromSpecifier
|
|
|
|
|
* @param int $code Exception code
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param Exception|Throwable|null $previous The previous exception used for the exception
|
|
|
|
|
* chaining.
|
2017-10-09 14:26:46 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( $messageSpec, $code = 0, $previous = null ) {
|
|
|
|
|
$this->messageSpec = $messageSpec;
|
|
|
|
|
|
|
|
|
|
$msg = $this->getMessageObject()->text();
|
|
|
|
|
$msg = preg_replace( '!</?(var|kbd|samp|code)>!', '"', $msg );
|
|
|
|
|
$msg = Sanitizer::stripAllTags( $msg );
|
|
|
|
|
parent::__construct( $msg, $code, $previous );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getMessageObject() {
|
|
|
|
|
return Message::newFromSpecifier( $this->messageSpec );
|
|
|
|
|
}
|
2014-05-09 14:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashFileNotFoundException extends UploadStashException {
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashBadPathException extends UploadStashException {
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashFileException extends UploadStashException {
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashZeroLengthFileException extends UploadStashException {
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashNotLoggedInException extends UploadStashException {
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashWrongOwnerException extends UploadStashException {
|
|
|
|
|
}
|
2011-07-14 23:01:00 +00:00
|
|
|
|
2017-12-01 10:02:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
2014-05-09 14:53:19 +00:00
|
|
|
class UploadStashNoSuchKeyException extends UploadStashException {
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|