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
|
|
|
|
|
* @ingroup Upload
|
|
|
|
|
*/
|
|
|
|
|
|
2011-03-08 18:12:17 +00:00
|
|
|
/**
|
2010-11-03 04:32:41 +00:00
|
|
|
* UploadStash is intended to accomplish a few things:
|
|
|
|
|
* - 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.
|
2011-07-12 21:11:43 +00:00
|
|
|
* - enable applications to find said files later, as long as the db table or temp files haven't been purged.
|
2010-11-03 04:32:41 +00:00
|
|
|
* - enable the uploading user (and *ONLY* the uploading user) to access said files, and thumbnails of said files, via a URL.
|
2011-07-15 15:54:14 +00:00
|
|
|
* We accomplish this using a database table, with ownership checking as you might expect. See SpecialUploadStash, which
|
2011-07-12 21:11:43 +00:00
|
|
|
* implements a web interface to some files stored this way.
|
2010-12-06 21:01:26 +00:00
|
|
|
*
|
2011-08-02 20:17:13 +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-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.
|
|
|
|
|
* 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)
|
2011-07-15 18:33:31 +00:00
|
|
|
const KEY_FORMAT_REGEX = '/^[\w-\.]+\.\w*$/';
|
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
|
|
|
|
|
protected $files = array();
|
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
|
|
|
|
|
protected $fileMetadata = array();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// fileprops cache
|
|
|
|
|
protected $fileProps = array();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-13 19:08:51 +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.
|
2010-11-03 04:32:41 +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
|
|
|
*
|
|
|
|
|
* @param $repo FileRepo
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
2011-12-20 03:52:06 +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
|
|
|
|
2011-07-13 19:08:51 +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
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( $user ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
$this->user = $user;
|
|
|
|
|
} else {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$this->user = $wgUser;
|
|
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
|
|
|
|
if ( is_object( $this->user ) ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
$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.
|
2011-08-02 20:17:13 +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
|
|
|
*
|
2011-07-12 21:11:43 +00:00
|
|
|
* @param $key String: key under which file information is stored
|
2011-07-13 19:08:51 +00:00
|
|
|
* @param $noAuth Boolean (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 ) {
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
|
|
|
|
|
throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
|
2011-03-08 18:12:17 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
|
|
|
|
if ( !$noAuth ) {
|
|
|
|
|
if ( !$this->isLoggedIn ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
|
2011-07-13 00:11:02 +00:00
|
|
|
}
|
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 ) ) {
|
2011-08-15 23:40:57 +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] ) ) {
|
|
|
|
|
throw new UploadStashFileNotFoundException( "key '$key' not found in stash" );
|
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
|
|
|
|
|
$path = $this->fileMetadata[$key]['us_path'];
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->fileProps[$key] = $this->repo->getFileProps( $path );
|
2010-12-06 20:57:42 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-14 23:01:00 +00:00
|
|
|
if ( ! $this->files[$key]->exists() ) {
|
|
|
|
|
wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" );
|
|
|
|
|
throw new UploadStashBadPathException( "path doesn't exist" );
|
|
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
|
|
|
|
if ( !$noAuth ) {
|
|
|
|
|
if ( $this->fileMetadata[$key]['us_user'] != $this->userId ) {
|
2011-07-13 00:11:02 +00:00
|
|
|
throw new UploadStashWrongOwnerException( "This file ($key) doesn't belong to the current user." );
|
|
|
|
|
}
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param key String: key under which file information is stored
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function getMetadata ( $key ) {
|
|
|
|
|
$this->getFile( $key );
|
2011-07-15 15:54:14 +00:00
|
|
|
return $this->fileMetadata[$key];
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Getter for fileProps
|
|
|
|
|
*
|
|
|
|
|
* @param key String: key under which file information is stored
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function getFileProps ( $key ) {
|
|
|
|
|
$this->getFile( $key );
|
2011-07-15 15:54:14 +00:00
|
|
|
return $this->fileProps[$key];
|
2011-07-12 21:11:43 +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
|
|
|
*
|
2010-11-12 16:42:46 +00:00
|
|
|
* @param $path String: path to file you want stashed
|
2011-07-12 21:11:43 +00:00
|
|
|
* @param $sourceType String: the type of upload that generated this file (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
|
2010-11-12 16:42:46 +00:00
|
|
|
* @return UploadStashFile: 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 ) {
|
2010-11-03 04:32:41 +00:00
|
|
|
if ( ! file_exists( $path ) ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
wfDebug( __METHOD__ . " tried to stash file at '$path', but it doesn't exist\n" );
|
2010-11-15 23:49:16 +00:00
|
|
|
throw new UploadStashBadPathException( "path doesn't exist" );
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
$fileProps = FSFile::getPropsFromPath( $path );
|
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 );
|
|
|
|
|
if ( ! preg_match( "/\\.\\Q$extension\\E$/", $path ) ) {
|
|
|
|
|
$pathWithGoodExtension = "$path.$extension";
|
|
|
|
|
if ( ! rename( $path, $pathWithGoodExtension ) ) {
|
|
|
|
|
throw new UploadStashFileException( "couldn't rename $path to have a better extension at $pathWithGoodExtension" );
|
|
|
|
|
}
|
|
|
|
|
$path = $pathWithGoodExtension;
|
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
|
|
|
|
2011-07-15 18:33:31 +00:00
|
|
|
// If no key was supplied, make one. a mysql insertid would be totally reasonable here, except
|
2011-08-15 23:58:40 +00:00
|
|
|
// 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.
|
|
|
|
|
// see: http://www.jwz.org/doc/mid.html
|
|
|
|
|
list ($usec, $sec) = explode( ' ', microtime() );
|
|
|
|
|
$usec = substr($usec, 2);
|
|
|
|
|
$key = wfBaseConvert( $sec . $usec, 10, 36 ) . '.' .
|
|
|
|
|
wfBaseConvert( 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
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
|
|
|
|
|
throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
|
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
|
2011-07-25 16:55:19 +00:00
|
|
|
$storeStatus = $this->repo->storeTemp( basename( $path ), $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
|
|
|
|
2011-07-25 16:55:19 +00:00
|
|
|
if ( ! $storeStatus->isOK() ) {
|
2010-11-03 04:32:41 +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.
|
2011-07-25 16:55:19 +00:00
|
|
|
// 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.
|
2011-07-25 16:55:19 +00:00
|
|
|
// $storeStatus->value just contains the virtual URL (if anything) which is probably useless to the caller
|
|
|
|
|
$error = $storeStatus->getErrorsArray();
|
2011-06-21 15:13:03 +00:00
|
|
|
$error = reset( $error );
|
2010-11-03 04:32:41 +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 );
|
2010-11-03 04:32:41 +00:00
|
|
|
if ( ! count( $error ) ) {
|
|
|
|
|
$error = array( 'unknown', 'no error recorded' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-08 23:09:19 +00:00
|
|
|
// at this point, $error should contain the single "most important" error, plus any parameters.
|
2012-02-08 23:25:58 +00:00
|
|
|
throw new UploadStashFileException( "Error storing file in '$path': " . wfMessage( $error )->text() );
|
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 ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
|
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" );
|
|
|
|
|
$dbw = $this->repo->getMasterDb();
|
2011-07-14 21:33:09 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->fileMetadata[$key] = array(
|
2011-11-10 07:41:12 +00:00
|
|
|
'us_id' => $dbw->nextSequenceValue( 'uploadstash_us_id_seq' ),
|
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
|
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'
|
2011-07-12 21:11:43 +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
|
|
|
);
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// store the insertid in the class variable so immediate retrieval (possibly laggy) isn't necesary.
|
|
|
|
|
$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
|
2010-12-15 01:21:11 +00:00
|
|
|
* @return boolean: success
|
|
|
|
|
*/
|
|
|
|
|
public function clear() {
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->isLoggedIn ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
|
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" );
|
2011-07-13 19:08:51 +00:00
|
|
|
$dbw = $this->repo->getMasterDb();
|
2011-07-12 21:11:43 +00:00
|
|
|
$dbw->delete(
|
|
|
|
|
'uploadstash',
|
2011-07-13 19:08:51 +00:00
|
|
|
array( 'us_user' => $this->userId ),
|
2011-07-15 15:54:14 +00:00
|
|
|
__METHOD__
|
2011-07-12 21:11:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# destroy objects.
|
|
|
|
|
$this->files = array();
|
|
|
|
|
$this->fileMetadata = array();
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @throws UploadStashNotLoggedInException
|
2011-07-13 00:11:02 +00:00
|
|
|
* @throws UploadStashWrongOwnerException
|
2010-12-15 01:21:11 +00:00
|
|
|
* @return boolean: success
|
|
|
|
|
*/
|
2011-07-15 15:54:14 +00:00
|
|
|
public function removeFile( $key ) {
|
|
|
|
|
if ( !$this->isLoggedIn ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-13 19:08:51 +00:00
|
|
|
$dbw = $this->repo->getMasterDb();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +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.
|
|
|
|
|
$row = $dbw->selectRow(
|
|
|
|
|
'uploadstash',
|
|
|
|
|
'us_user',
|
2011-07-15 15:54:14 +00:00
|
|
|
array( 'us_key' => $key ),
|
2011-07-12 21:11:43 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-15 16:59:38 +00:00
|
|
|
if( !$row ) {
|
|
|
|
|
throw new UploadStashNoSuchKeyException( "No such key ($key), cannot remove" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( $row->us_user != $this->userId ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
throw new UploadStashWrongOwnerException( "Can't delete: the file ($key) doesn't belong to this user." );
|
|
|
|
|
}
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean: success
|
|
|
|
|
*/
|
|
|
|
|
public function removeFileNoAuth( $key ) {
|
|
|
|
|
wfDebug( __METHOD__ . " clearing row $key\n" );
|
|
|
|
|
|
2011-07-13 19:08:51 +00:00
|
|
|
$dbw = $this->repo->getMasterDb();
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-13 00:11:02 +00:00
|
|
|
// this gets its own transaction since it's called serially by the cleanupUploadStash maintenance script
|
2012-01-18 21:27:28 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2011-07-12 21:11:43 +00:00
|
|
|
$dbw->delete(
|
|
|
|
|
'uploadstash',
|
2011-07-15 15:54:14 +00:00
|
|
|
array( 'us_key' => $key ),
|
|
|
|
|
__METHOD__
|
2011-07-12 21:11:43 +00:00
|
|
|
);
|
2012-01-18 21:27:28 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
// TODO: look into UnregisteredLocalFile and find out why the rv here is sometimes wrong (false when file was removed)
|
|
|
|
|
// for now, ignore.
|
|
|
|
|
$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
|
|
|
|
|
* @return Array
|
2010-12-15 01:21:11 +00:00
|
|
|
*/
|
|
|
|
|
public function listFiles() {
|
2011-07-15 15:54:14 +00:00
|
|
|
if ( !$this->isLoggedIn ) {
|
2011-07-13 19:08:51 +00:00
|
|
|
throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
|
2011-07-12 21:11:43 +00:00
|
|
|
}
|
2011-07-15 15:54:14 +00:00
|
|
|
|
2011-07-13 19:08:51 +00:00
|
|
|
$dbr = $this->repo->getSlaveDb();
|
2011-07-12 21:11:43 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
|
'uploadstash',
|
|
|
|
|
'us_key',
|
2011-08-15 18:10:10 +00:00
|
|
|
array( '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.
|
2011-07-12 21:11:43 +00:00
|
|
|
$keys = array();
|
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
|
|
|
/**
|
|
|
|
|
* 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...
|
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 ) {
|
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 {
|
|
|
|
|
// If not, assume that it should be related to the mime type of the original file.
|
|
|
|
|
$magic = MimeMagic::singleton();
|
|
|
|
|
$mimeType = $magic->guessMimeType( $path );
|
|
|
|
|
$extensions = explode( ' ', MimeMagic::singleton()->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 ) ) {
|
|
|
|
|
throw new UploadStashFileException( "extension is null" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return File::normalizeExtension( $extension );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function: do the actual database query to fetch file metadata.
|
|
|
|
|
*
|
|
|
|
|
* @param $key String: key
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2011-08-16 17:57:32 +00:00
|
|
|
protected function fetchFileMetadata( $key, $readFromDB = DB_SLAVE ) {
|
2011-07-12 21:11:43 +00:00
|
|
|
// populate $fileMetadata[$key]
|
2011-08-15 23:40:57 +00:00
|
|
|
$dbr = null;
|
2011-08-16 17:57:32 +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.
|
|
|
|
|
$dbr = $this->repo->getMasterDb();
|
|
|
|
|
} else {
|
|
|
|
|
$dbr = $this->repo->getSlaveDb();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-12 21:11:43 +00:00
|
|
|
$row = $dbr->selectRow(
|
|
|
|
|
'uploadstash',
|
|
|
|
|
'*',
|
2011-07-15 15:54:14 +00:00
|
|
|
array( '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;
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param $path String: path to file
|
|
|
|
|
* @param $key String: key under which to store the object
|
|
|
|
|
* @throws UploadStashZeroLengthFileException
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function initFile( $key ) {
|
|
|
|
|
$file = new UploadStashFile( $this->repo, $this->fileMetadata[$key]['us_path'], $key );
|
|
|
|
|
if ( $file->getSize() === 0 ) {
|
|
|
|
|
throw new UploadStashZeroLengthFileException( "File is zero length" );
|
|
|
|
|
}
|
|
|
|
|
$this->files[$key] = $file;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*
|
2011-12-20 19:25:23 +00:00
|
|
|
* @param $repo FileRepo: repository where we should find the path
|
2010-11-12 16:42:46 +00:00
|
|
|
* @param $path String: path to file
|
|
|
|
|
* @param $key String: 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 {
|
2010-11-03 04:32:41 +00:00
|
|
|
|
2011-06-28 22:00:21 +00:00
|
|
|
// check if path appears to be sane, no parent traversals, and is in this repo's temp zone.
|
|
|
|
|
$repoTempPath = $repo->getZonePath( 'temp' );
|
|
|
|
|
if ( ( ! $repo->validateFilename( $path ) ) ||
|
|
|
|
|
( strpos( $path, $repoTempPath ) !== 0 ) ) {
|
|
|
|
|
wfDebug( "UploadStash: tried to construct an UploadStashFile from a file that should already exist at '$path', but path is not valid\n" );
|
|
|
|
|
throw new UploadStashBadPathException( 'path is not valid' );
|
|
|
|
|
}
|
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.
|
2012-04-05 04:10:50 +00:00
|
|
|
if ( ! $repo->fileExists( $path ) ) {
|
2011-06-28 22:00:21 +00:00
|
|
|
wfDebug( "UploadStash: tried to construct an UploadStashFile from a file that should already exist at '$path', but path is not found\n" );
|
|
|
|
|
throw new UploadStashFileNotFoundException( 'cannot find path, or not a plain file' );
|
|
|
|
|
}
|
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
|
|
|
|
|
* 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
|
|
|
*
|
|
|
|
|
* @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'
|
|
|
|
|
*
|
2010-11-12 16:42:46 +00:00
|
|
|
* @param $thumbName String: 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";
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2010-11-12 16:42:46 +00:00
|
|
|
* @param $params Array: handler-specific parameters
|
|
|
|
|
* @return String: base name for URL, like '120px-12345.jpg', or null if there is no handler
|
2010-11-03 04:32:41 +00:00
|
|
|
*/
|
|
|
|
|
function thumbName( $params ) {
|
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
|
|
|
/**
|
|
|
|
|
* Helper function -- given a 'subpage', return the local URL e.g. /wiki/Special:UploadStash/subpage
|
|
|
|
|
* @param {String} $subPage
|
2011-03-08 18:12:17 +00:00
|
|
|
* @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
|
2010-11-03 04:32:41 +00:00
|
|
|
* the thumbnail urls be predictable. However, in our model the URL is not based on the filename
|
2011-07-12 21:11:43 +00:00
|
|
|
* (that's hidden in the db)
|
2010-11-03 04:32:41 +00:00
|
|
|
*
|
2010-11-12 16:42:46 +00:00
|
|
|
* @param $thumbName String: basename of thumbnail file -- however, we don't want to use the file exactly
|
|
|
|
|
* @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" );
|
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
|
|
|
*
|
|
|
|
|
* @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() {
|
2010-11-03 04:32:41 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
return $this->url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-08 18:12:17 +00:00
|
|
|
* Parent classes use this method, for no obvious reason, to return the path (relative to wiki root, I assume).
|
2010-11-03 04:32:41 +00:00
|
|
|
* But with this class, the URL is unrelated to the path.
|
|
|
|
|
*
|
2010-11-12 16:42:46 +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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-07-12 21:11:43 +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
|
|
|
*
|
2011-07-12 21:11:43 +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
|
2010-11-12 16:42:46 +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
|
|
|
}
|
|
|
|
|
|
2010-11-03 04:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UploadStashNotAvailableException extends MWException {};
|
|
|
|
|
class UploadStashFileNotFoundException extends MWException {};
|
|
|
|
|
class UploadStashBadPathException extends MWException {};
|
|
|
|
|
class UploadStashFileException extends MWException {};
|
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
|
|
|
class UploadStashZeroLengthFileException extends MWException {};
|
2011-07-12 21:11:43 +00:00
|
|
|
class UploadStashNotLoggedInException extends MWException {};
|
|
|
|
|
class UploadStashWrongOwnerException extends MWException {};
|
2011-07-15 16:59:38 +00:00
|
|
|
class UploadStashNoSuchKeyException extends MWException {};
|