*
*/
class UploadStash {
- // Format of the key for files -- has to be suitable as a filename itself in some cases.
- // This should encompass a sha1 content hash in hex (new style), or an integer (old style),
- // and also thumbnails with prepended strings like "120px-".
- // The file extension should not be part of the key.
- const KEY_FORMAT_REGEX = '/^[\w-]+$/';
+
+ // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
+ const KEY_FORMAT_REGEX = '/^[\w-]+\.\w+$/';
// repository that this uses to store temp files
- protected $repo;
+ // public because we sometimes need to get a LocalFile within the same repo.
+ public $repo;
// array of initialized objects obtained from session (lazily initialized upon getFile())
private $files = array();
- // the base URL for files in the stash
- private $baseUrl;
-
// TODO: Once UploadBase starts using this, switch to use these constants rather than UploadBase::SESSION*
// const SESSION_VERSION = 2;
// const SESSION_KEYNAME = 'wsUploadData';
/**
* Represents the session which contains temporarily stored files.
* Designed to be compatible with the session stashing code in UploadBase (should replace it eventually)
- * @param {FileRepo} $repo: optional -- repo in which to store files. Will choose LocalRepo if not supplied.
*/
- public function __construct( $repo = null ) {
-
- if ( is_null( $repo ) ) {
- $repo = RepoGroup::singleton()->getLocalRepo();
- }
+ public function __construct() {
- $this->repo = $repo;
+ // this might change based on wiki's configuration.
+ $this->repo = RepoGroup::singleton()->getLocalRepo();
if ( ! isset( $_SESSION ) ) {
throw new UploadStashNotAvailableException( 'no session variable' );
$_SESSION[UploadBase::SESSION_KEYNAME] = array();
}
- $this->baseUrl = SpecialPage::getTitleFor( 'UploadStash' )->getLocalURL();
}
- /**
- * Get the base of URLs by which one can access the files
- * @return {String} url
- */
- public function getBaseUrl() {
- return $this->baseUrl;
- }
- /**
+ /**
* Get a file and its metadata from the stash.
* May throw exception if session data cannot be parsed due to schema change, or key not found.
- * @param {Integer} $key: key
+ *
+ * @param $key Integer: key
* @throws UploadStashFileNotFoundException
* @throws UploadStashBadVersionException
- * @return {UploadStashItem} null if no such item or item out of date, or the item
+ * @return UploadStashFile
*/
public function getFile( $key ) {
if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
if ( !isset( $this->files[$key] ) ) {
if ( !isset( $_SESSION[UploadBase::SESSION_KEYNAME][$key] ) ) {
- throw new UploadStashFileNotFoundException( "key '$key' not found in session" );
+ throw new UploadStashFileNotFoundException( "key '$key' not found in stash" );
}
$data = $_SESSION[UploadBase::SESSION_KEYNAME][$key];
unset( $data['mTempPath'] );
$file = new UploadStashFile( $this, $this->repo, $path, $key, $data );
-
+ if ( $file->getSize() === 0 ) {
+ throw new UploadStashZeroLengthFileException( "File is zero length" );
+ }
$this->files[$key] = $file;
}
* We store data in a flat key-val namespace because that's how UploadBase did it. This also means we have to
* ensure that the key-val pairs in $data do not overwrite other required fields.
*
- * @param {String} $path: path to file you want stashed
- * @param {Array} $data: optional, other data you want associated with the file. Do not use 'mTempPath', 'mFileProps', 'mFileSize', or 'version' as keys here
- * @param {String} $key: optional, unique key for this file in this session. Used for directory hashing when storing, otherwise not important
+ * @param $path String: path to file you want stashed
+ * @param $data Array: optional, other data you want associated with the file. Do not use 'mTempPath', 'mFileProps', 'mFileSize', or 'version' as keys here
+ * @param $key String: optional, unique key for this file in this session. Used for directory hashing when storing, otherwise not important
* @throws UploadStashBadPathException
* @throws UploadStashFileException
- * @return {null|UploadStashFile} file, or null on failure
+ * @return UploadStashFile: file, or null on failure
*/
public function stashFile( $path, $data = array(), $key = null ) {
if ( ! file_exists( $path ) ) {
- throw new UploadStashBadPathException( "path '$path' doesn't exist" );
+ wfDebug( "UploadStash: tried to stash file at '$path', but it doesn't exist\n" );
+ throw new UploadStashBadPathException( "path doesn't exist" );
}
$fileProps = File::getPropsFromPath( $path );
+ // 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;
+ }
+
// If no key was supplied, use content hash. Also has the nice property of collapsing multiple identical files
// uploaded this session, which could happen if uploads had failed.
if ( is_null( $key ) ) {
- $key = $fileProps['sha1'];
+ $key = $fileProps['sha1'] . "." . $extension;
}
if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
}
- // if not already in a temporary area, put it there
+
+ // if not already in a temporary area, put it there
$status = $this->repo->storeTemp( basename( $path ), $path );
+
if( ! $status->isOK() ) {
// 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.
throw new UploadStashFileException( "error storing file in '$path': " . implode( '; ', $error ) );
}
$stashPath = $status->value;
-
+
// required info we always store. Must trump any other application info in $data
// 'mTempPath', 'mFileSize', and 'mFileProps' are arbitrary names
// chosen for compatibility with UploadBase's way of doing this.
// now, merge required info and extra data into the session. (The extra data changes from application to application.
// UploadWizard wants different things than say FirefoggChunkedUpload.)
+ wfDebug( __METHOD__ . " storing under $key\n" );
$_SESSION[UploadBase::SESSION_KEYNAME][$key] = array_merge( $data, $requiredData );
return $this->getFile( $key );
}
+ /**
+ * Remove all files from the stash.
+ * Does not clean up files in the repo, just the record of them.
+ * @return boolean: success
+ */
+ public function clear() {
+ $_SESSION[UploadBase::SESSION_KEYNAME] = array();
+ return true;
+ }
+
+
+ /**
+ * Remove a particular file from the stash.
+ * Does not clean up file in the repo, just the record of it.
+ * @return boolean: success
+ */
+ public function removeFile( $key ) {
+ unset ( $_SESSION[UploadBase::SESSION_KEYNAME][$key] );
+ return true;
+ }
+
+
+ /**
+ * List all files in the stash.
+ */
+ public function listFiles() {
+ return array_keys( $_SESSION[UploadBase::SESSION_KEYNAME] );
+ }
+
+
+ /**
+ * Find or guess extension -- ensuring that our extension matches our mime type.
+ * Since these files are constructed from php tempnames they may not start off
+ * with an extension.
+ * XXX this is somewhat redundant with the checks that ApiUpload.php does with incoming
+ * uploads versus the desired filename. Maybe we can get that passed to us...
+ */
+ public static function getExtensionForPath( $path ) {
+ // 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 ) );
+ if ( count( $extensions ) ) {
+ $extension = $extensions[0];
+ }
+ }
+
+ if ( is_null( $extension ) ) {
+ throw new UploadStashFileException( "extension is null" );
+ }
+
+ return File::normalizeExtension( $extension );
+ }
+
}
class UploadStashFile extends UnregisteredLocalFile {
/**
* 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
- * @param {UploadStash} $stash: UploadStash, useful for obtaining config, stashing transformed files
- * @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
- * @param {String} $data: any other data we want stored with this file
+ *
+ * @param $stash UploadStash: useful for obtaining config, stashing transformed files
+ * @param $repo FileRepo: repository where we should find the path
+ * @param $path String: path to file
+ * @param $key String: key to store the path and any stashed data under
+ * @param $data String: any other data we want stored with this file
* @throws UploadStashBadPathException
* @throws UploadStashFileNotFoundException
*/
$repoTempPath = $repo->getZonePath( 'temp' );
if ( ( ! $repo->validateFilename( $path ) ) ||
( strpos( $path, $repoTempPath ) !== 0 ) ) {
- throw new UploadStashBadPathException( "path '$path' is not valid or is not in repo temp area: '$repoTempPath'" );
+ 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' );
}
// check if path exists! and is a plain file.
if ( ! $repo->fileExists( $path, FileRepo::FILES_ONLY ) ) {
- throw new UploadStashFileNotFoundException( "cannot find path '$path'" );
+ 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' );
}
+
+
parent::__construct( false, $repo, $path, false );
- // 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.
$this->name = basename( $this->path );
- $this->setExtension();
-
}
/**
* 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)
- * @return {String} dummy value
+ *
+ * @return String: dummy value
*/
public function getDescriptionUrl() {
return $this->getUrl();
}
- /**
- * Find or guess extension -- ensuring that our extension matches our mime type.
- * Since these files are constructed from php tempnames they may not start off
- * with an extension.
- * This does not override getExtension() because things like getMimeType() already call getExtension(),
- * and that results in infinite recursion. So, we preemptively *set* the extension so getExtension() can find it.
- * For obvious reasons this should be called as early as possible, as part of initialization
- */
- public function setExtension() {
- // Does this have an extension?
- $n = strrpos( $this->path, '.' );
- $extension = null;
- if ( $n !== false ) {
- $extension = $n ? substr( $this->path, $n + 1 ) : '';
- } else {
- // If not, assume that it should be related to the mime type of the original file.
- //
- // This entire thing is backwards -- we *should* just create an extension based on
- // the mime type of the transformed file, *after* transformation. But File.php demands
- // to know the name of the transformed file before creating it.
- $mimeType = $this->getMimeType();
- $extensions = explode( ' ', MimeMagic::singleton()->getExtensionsForType( $mimeType ) );
- if ( count( $extensions ) ) {
- $extension = $extensions[0];
- }
- }
-
- if ( is_null( $extension ) ) {
- throw new UploadStashFileException( "extension '$extension' is null" );
- }
-
- $this->extension = parent::normalizeExtension( $extension );
- }
-
/**
* Get the path for the thumbnail (actually any transformation of this file)
* The actual argument is the result of thumbName although we seem to have
* buggy code elsewhere that expects a boolean 'suffix'
*
- * @param {String|false} $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
+ * @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
*/
public function getThumbPath( $thumbName = false ) {
$path = dirname( $this->path );
/**
* Return the file/url base name of a thumbnail with the specified parameters
*
- * @param {Array} $params: handler-specific parameters
- * @return {String|null} base name for URL, like '120px-12345.jpg', or null if there is no handler
+ * @param $params Array: handler-specific parameters
+ * @return String: base name for URL, like '120px-12345.jpg', or null if there is no handler
*/
function thumbName( $params ) {
+ return $this->getParamThumbName( $this->getUrlName(), $params );
+ }
+
+
+ /**
+ * Given the name of the original, i.e. Foo.jpg, and scaling parameters, returns filename with appropriate extension
+ * This is abstracted from getThumbName because we also use it to calculate the thumbname the file should have on
+ * remote image scalers
+ *
+ * @param String $urlName: A filename, like MyMovie.ogx
+ * @param Array $parameters: scaling parameters, like array( 'width' => '120' );
+ * @return String|null parameterized thumb name, like 120px-MyMovie.ogx.jpg, or null if no handler found
+ */
+ function getParamThumbName( $urlName, $params ) {
if ( !$this->getHandler() ) {
return null;
}
$extension = $this->getExtension();
- list( $thumbExt, $thumbMime ) = $this->handler->getThumbType( $extension, $this->getMimeType(), $params );
- $thumbName = $this->getHandler()->makeParamString( $params ) . '-' . $this->getUrlName();
+ list( $thumbExt, ) = $this->handler->getThumbType( $extension, $this->getMimeType(), $params );
+ $thumbName = $this->getHandler()->makeParamString( $params ) . '-' . $urlName;
if ( $thumbExt != $extension ) {
$thumbName .= ".$thumbExt";
}
return $thumbName;
}
+ /**
+ * Helper function -- given a 'subpage', return the local URL e.g. /wiki/Special:UploadStash/subpage
+ * @param {String} $subPage
+ * @return {String} local URL for this subpage in the Special:UploadStash space.
+ */
+ private function getSpecialUrl( $subPage ) {
+ return SpecialPage::getTitleFor( 'UploadStash', $subPage )->getLocalURL();
+ }
+
+
/**
* Get a URL to access the thumbnail
* This is required because the model of how files work requires that
* the thumbnail urls be predictable. However, in our model the URL is not based on the filename
* (that's hidden in the session)
*
- * @param {String} $thumbName: 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
+ * @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
*/
public function getThumbUrl( $thumbName = false ) {
- $path = $this->sessionStash->getBaseUrl();
- if ( $thumbName !== false ) {
- $path .= '/' . rawurlencode( $thumbName );
- }
- return $path;
+ wfDebug( __METHOD__ . " getting for $thumbName \n" );
+ return $this->getSpecialUrl( $thumbName );
}
/**
* 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.
- * @return {String} base url name, like '120px-123456.jpg'
+ *
+ * @return String: base url name, like '120px-123456.jpg'
*/
public function getUrlName() {
if ( ! $this->urlName ) {
- $this->urlName = $this->sessionKey . '.' . $this->getExtension();
+ $this->urlName = $this->sessionKey;
}
return $this->urlName;
}
/**
* Return the URL of the file, if for some reason we wanted to download it
- * We tend not to do this for the original file, but we do want thumb icons
- * @return {String} url
+ * We tend not to do this for the original file, but we do want thumb icons
+ *
+ * @return String: url
*/
public function getUrl() {
if ( !isset( $this->url ) ) {
- $this->url = $this->sessionStash->getBaseUrl() . '/' . $this->getUrlName();
+ $this->url = $this->getSpecialUrl( $this->getUrlName() );
}
return $this->url;
}
* 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.
*
- * @return {String} url
+ * @return String: url
*/
public function getFullUrl() {
return $this->getUrl();
/**
* Getter for session key (the session-unique id by which this file's location & metadata is stored in the session)
- * @return {String} session key
+ *
+ * @return String: session key
*/
public function getSessionKey() {
return $this->sessionKey;
}
- /**
- * Typically, transform() returns a ThumbnailImage, which you can think of as being the exact
- * equivalent of an HTML thumbnail on Wikipedia. So its URL is the full-size file, not the thumbnail's URL.
- *
- * Here we override transform() to stash the thumbnail file, and then
- * provide a way to get at the stashed thumbnail file to extract properties such as its URL
- *
- * @param {Array} $params: parameters suitable for File::transform()
- * @param {Bitmask} $flags: flags suitable for File::transform()
- * @return {ThumbnailImage} with additional File thumbnailFile property
- */
- public function transform( $params, $flags = 0 ) {
-
- // force it to get a thumbnail right away
- $flags |= self::RENDER_NOW;
-
- // returns a ThumbnailImage object containing the url and path. Note. NOT A FILE OBJECT.
- $thumb = parent::transform( $params, $flags );
- $key = $this->thumbName($params);
-
- // remove extension, so it's stored in the session under '120px-123456'
- // this makes it uniform with the other session key for the original, '123456'
- $n = strrpos( $key, '.' );
- if ( $n !== false ) {
- $key = substr( $key, 0, $n );
- }
-
- // stash the thumbnail File, and provide our caller with a way to get at its properties
- $stashedThumbFile = $this->sessionStash->stashFile( $thumb->path, array(), $key );
- $thumb->thumbnailFile = $stashedThumbFile;
-
- return $thumb;
-
- }
-
/**
* Remove the associated temporary file
- * @return {Status} success
+ * @return Status: success
*/
public function remove() {
return $this->repo->freeTemp( $this->path );
class UploadStashBadPathException extends MWException {};
class UploadStashBadVersionException extends MWException {};
class UploadStashFileException extends MWException {};
+class UploadStashZeroLengthFileException extends MWException {};