http://www.mediawiki.org/wiki/Special:Code/MediaWiki/84520
Revision: 84520
Author: neilk
Date: 2011-03-22 06:31:14 +0000 (Tue, 22 Mar 2011)
Log Message:
-----------
first stab at database-backed uploadstash -- still non-functional, but getting
close
Modified Paths:
--------------
branches/uploadstash-db-backend/includes/filerepo/FileRepo.php
branches/uploadstash-db-backend/includes/upload/UploadBase.php
branches/uploadstash-db-backend/includes/upload/UploadStash.php
branches/uploadstash-db-backend/maintenance/tables.sql
Modified: branches/uploadstash-db-backend/includes/filerepo/FileRepo.php
===================================================================
--- branches/uploadstash-db-backend/includes/filerepo/FileRepo.php
2011-03-22 06:23:28 UTC (rev 84519)
+++ branches/uploadstash-db-backend/includes/filerepo/FileRepo.php
2011-03-22 06:31:14 UTC (rev 84520)
@@ -692,9 +692,10 @@
/**
* Get an UploadStash associated with this repo.
*
+ * @param $user User
* @return UploadStash
*/
- function getUploadStash() {
- return new UploadStash( $this );
+ function getUploadStash( $user ) {
+ return new UploadStash( $user, $this );
}
}
Modified: branches/uploadstash-db-backend/includes/upload/UploadBase.php
===================================================================
--- branches/uploadstash-db-backend/includes/upload/UploadBase.php
2011-03-22 06:23:28 UTC (rev 84519)
+++ branches/uploadstash-db-backend/includes/upload/UploadBase.php
2011-03-22 06:31:14 UTC (rev 84520)
@@ -11,6 +11,7 @@
* @author Brion Vibber
* @author Bryan Tong Minh
* @author Michael Dale
+ * @author Neil Kandalgaonkar
*/
abstract class UploadBase {
@@ -707,13 +708,13 @@
* API request to find this stashed file again.
*
* @param $key String: (optional) the session key used to find the file
info again. If not supplied, a key will be autogenerated.
- * @return UploadStashFile stashed file
+ * @return UploadStashFile: stashed file
*/
- public function stashSessionFile( $key = null ) {
- $stash =
RepoGroup::singleton()->getLocalRepo()->getUploadStash();
+ public function stashFile( $key = null ) {
+ $stash =
RepoGroup::singleton()->getLocalRepo()->getUploadStash( $wgUser );
$data = array(
- 'mFileProps' => $this->mFileProps,
- 'mSourceType' => $this->getSourceType(),
+ 'props' => $this->mFileProps,
+ 'sourcetype' => $this->getSourceType(),
);
$file = $stash->stashFile( $this->mTempPath, $data, $key );
$this->mLocalFile = $file;
Modified: branches/uploadstash-db-backend/includes/upload/UploadStash.php
===================================================================
--- branches/uploadstash-db-backend/includes/upload/UploadStash.php
2011-03-22 06:23:28 UTC (rev 84519)
+++ branches/uploadstash-db-backend/includes/upload/UploadStash.php
2011-03-22 06:31:14 UTC (rev 84520)
@@ -20,39 +20,56 @@
// public because we sometimes need to get a LocalFile within the same
repo.
public $repo;
+ // the user who has uploaded or otherwise owns all these files
+ public $user;
+
// array of initialized objects obtained from session (lazily
initialized upon getFile())
private $files = array();
// TODO: Once UploadBase starts using this, switch to use these
constants rather than UploadBase::SESSION*
// const SESSION_VERSION = 2;
// const SESSION_KEYNAME = 'wsUploadData';
+ public $dbfields = array(
+ 'si_key',
+ 'si_user',
+ 'si_name',
+ 'si_sourcetype',
+ 'si_size',
+ 'si_cursize',
+ 'si_width',
+ 'si_height',
+ 'si_metadata',
+ 'si_bits',
+ 'si_media_type',
+ 'si_major_mime',
+ 'si_minor_mime',
+ 'si_description',
+ 'si_timestamp',
+ 'si_sha1'
+ );
+
/**
* Represents the session which contains temporarily stored files.
* Designed to be compatible with the session stashing code in
UploadBase (should replace it eventually)
+ * @param $user User
+ * @param $repo FileRepo
*/
- public function __construct( $repo ) {
+ public function __construct( $user, $repo ) {
+ // the user for whom the files belong to.
+ $this->user = $user;
+
// this might change based on wiki's configuration.
$this->repo = $repo;
-
- if ( ! isset( $_SESSION ) ) {
- throw new UploadStashNotAvailableException( 'no session
variable' );
- }
-
- if ( !isset( $_SESSION[UploadBase::SESSION_KEYNAME] ) ) {
- $_SESSION[UploadBase::SESSION_KEYNAME] = array();
- }
-
}
/**
* 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.
+ * May throw exception if key not found.
*
* @param $key Integer: key
* @throws UploadStashFileNotFoundException
- * @throws UploadStashBadVersionException
* @return UploadStashFile
*/
public function getFile( $key ) {
@@ -61,37 +78,37 @@
}
if ( !isset( $this->files[$key] ) ) {
- if ( !isset(
$_SESSION[UploadBase::SESSION_KEYNAME][$key] ) ) {
+ $dbr = wfGetDB( DB_SLAVE );
+ $conditions = array(
+ 'si_key' => $key,
+ 'si_user' => $this->user->mId
+ );
+ $data = $dbr->selectRow( 'stashedimage',
UploadStash::$fields, $conditions, __METHOD__ );
+
+ if ( !$data ) {
throw new UploadStashFileNotFoundException(
"key '$key' not found in stash" );
}
- $data = $_SESSION[UploadBase::SESSION_KEYNAME][$key];
- // guards against PHP class changing while session data
doesn't
- if ($data['version'] !== UploadBase::SESSION_VERSION ) {
- throw new UploadStashBadVersionException(
$data['version'] . " does not match current version " .
UploadBase::SESSION_VERSION );
- }
-
// separate the stashData into the path, and then the
rest of the data
- $path = $data['mTempPath'];
- unset( $data['mTempPath'] );
+ // XXX how to represent data props?? unserialize(
$data->props );
+ $path = $data->path;
$file = new UploadStashFile( $this, $this->repo, $path,
$key, $data );
if ( $file->getSize() === 0 ) {
throw new UploadStashZeroLengthFileException(
"File is zero length" );
}
+
$this->files[$key] = $file;
}
return $this->files[$key];
}
- /**
- * Stash a file in a temp directory and record that we did this in the
session, along with other metadata.
- * 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.
+ /**
+ * Stash a file in a temp directory and record that we did this in the
database, along with other metadata.
*
* @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 $data Array: optional, other data you want associated with
the file. Do not use 'path', 'props', 'size'
* @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
@@ -145,22 +162,26 @@
}
$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.
+ // required info we always store.
$requiredData = array(
- 'mTempPath' => $stashPath,
- 'mFileSize' => $fileProps['size'],
- 'mFileProps' => $fileProps,
- 'version' => UploadBase::SESSION_VERSION
+ 'si_key' => $key,
+ 'si_user' => $this->user->mId,
+ 'si_timestamp' => $dbw->timestamp( time() ),
+ 'si_sha1' => $fileProps['sha1'],
+ 'si_path' => $stashPath,
+ 'si_size' => $fileProps['size'],
+ 'si_props' => serialize( $fileProps )
);
// 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.)
+ $merged_data = array_merge( $data, $requiredData );
wfDebug( __METHOD__ . " storing under $key\n" );
- $_SESSION[UploadBase::SESSION_KEYNAME][$key] = array_merge(
$data, $requiredData );
- return $this->getFile( $key );
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->replace( 'stashedimage', array( 'si_key' ), $merged_datam
__METHOD__ );
+
+ return $this->getFile( $user, $key );
}
/**
Modified: branches/uploadstash-db-backend/maintenance/tables.sql
===================================================================
--- branches/uploadstash-db-backend/maintenance/tables.sql 2011-03-22
06:23:28 UTC (rev 84519)
+++ branches/uploadstash-db-backend/maintenance/tables.sql 2011-03-22
06:31:14 UTC (rev 84520)
@@ -869,7 +869,70 @@
CREATE INDEX /*i*/oi_name_archive_name ON /*_*/oldimage
(oi_name,oi_archive_name(14));
CREATE INDEX /*i*/oi_sha1 ON /*_*/oldimage (oi_sha1);
+--
+-- Images and other files in the UploadStash.
+--
+CREATE TABLE /*_*/stashedimage (
+ -- Temporary path under which this is stored. Unlike regular images, this
path is arbitrary and is
+ -- supposed to be unguessable from other filename characteristics. Doubles
as primary key.
+ si_key varchar(255) binary NOT NULL PRIMARY KEY,
+
+ -- Filename.
+ -- This is also the title of the associated description page,
+ -- which will be in namespace 6 (NS_FILE).
+ si_name varchar(255) binary NOT NULL default '',
+
+ -- Source type -- what sort of upload was this from, 'file', 'url', etc.
+ si_sourcetype varchar(255) binary NOT NULL default '',
+
+ -- Expected file size in bytes.
+ si_size int unsigned NOT NULL default 0,
+
+ -- Current image size in bytes.
+ si_cursize int unsigned NOT NULL default 0,
+
+ -- For images, size in pixels.
+ si_width int NOT NULL default 0,
+ si_height int NOT NULL default 0,
+
+ -- Extracted EXIF metadata stored as a serialized PHP array.
+ si_metadata mediumblob NOT NULL,
+
+ -- For images, bits per pixel if known.
+ si_bits int NOT NULL default 0,
+
+ -- Media type as defined by the MEDIATYPE_xxx constants
+ si_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO",
"MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
+
+ -- major part of a MIME media type as defined by IANA
+ -- see http://www.iana.org/assignments/media-types/
+ si_major_mime ENUM("unknown", "application", "audio", "image", "text",
"video", "message", "model", "multipart") NOT NULL default "unknown",
+
+ -- minor part of a MIME media type as defined by IANA
+ -- the minor parts are not required to adher to any standard
+ -- but should be consistent throughout the database
+ -- see http://www.iana.org/assignments/media-types/
+ si_minor_mime varbinary(100) NOT NULL default "unknown",
+
+ -- Description field as entered by the uploader.
+ -- This is displayed in image upload history and logs.
+ si_description tinyblob NOT NULL,
+
+ -- user_id and user_name of uploader.
+ si_user int unsigned NOT NULL default 0,
+ si_user_text varchar(255) binary NOT NULL,
+
+ -- Time of the upload.
+ si_timestamp varbinary(14) NOT NULL default '',
+
+ -- SHA-1 content hash in base-36
+ si_sha1 varbinary(32) NOT NULL default ''
+
+) /*$wgDBTableOptions*/;
+
+
+
--
-- Record of deleted file data
--
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs