r61388 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61387‎ | r61388 | r61389 >
Date:20:14, 22 January 2010
Author:btongminh
Status:deferred
Tags:
Comment:
Initial commit to move the temporary upload metadata from the session to the database

Added class UploadStash. An instance is obtained via LocalRepo::UploadStash. Class has methods to store an upload temporary and obtain information about stored upload.

Added class TemporaryUpload extends UnregisteredLocalFile. Represents a stashed upload.

UploadBase::stashSession: use UploadStash methods instead of storing information in $_SESSION. UploadStash::saveFile will call all filerepo and do all db magic.

UploadFromStash: Initialize from the stash instead of the session.
Modified paths:
  • /branches/staged-upload/includes/AutoLoader.php (modified) (history)
  • /branches/staged-upload/includes/filerepo/LocalRepo.php (modified) (history)
  • /branches/staged-upload/includes/upload/UploadBase.php (modified) (history)
  • /branches/staged-upload/includes/upload/UploadFromStash.php (modified) (history)
  • /branches/staged-upload/maintenance/tables.sql (modified) (history)
  • /branches/staged-upload/maintenance/updaters.inc (modified) (history)

Diff [purge]

Index: branches/staged-upload/maintenance/updaters.inc
@@ -169,6 +169,7 @@
170170 // A field changed name mid-release cycle, so fix it for anyone using
171171 // trunk
172172 array( 'rename_eu_wiki_id' ),
 173+ array( 'add_table', 'upload_stash', 'patch-upload_stash.sql' ),
173174 ),
174175
175176 'sqlite' => array(
@@ -197,6 +198,8 @@
198199
199200 // version-independent searchindex setup, added in 1.16
200201 array( 'sqlite_setup_searchindex' ),
 202+
 203+ array( 'add_table', 'upload_stash', 'patch-upload_stash.sql' ),
201204 ),
202205 );
203206
Index: branches/staged-upload/maintenance/tables.sql
@@ -1350,4 +1350,19 @@
13511351 ) /*$wgDBTableOptions*/;
13521352 CREATE INDEX /*i*/lc_lang_key ON /*_*/l10n_cache (lc_lang, lc_key);
13531353
 1354+CREATE TABLE /*_*/upload_stash (
 1355+ -- Numeric id
 1356+ us_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
 1357+ -- User this upload belongs to
 1358+ us_user int NOT NULL,
 1359+ -- Timestamp this was uploaded
 1360+ us_timestamp binary(14) NOT NULL default '19700101000000',
 1361+ -- Timestamp of expiry
 1362+ us_expiry binary(14) NOT NULL default '19700101000000',
 1363+ -- Virtual path of the file
 1364+ us_path varbinary(255) NOT NULL,
 1365+ -- Other data stored as serialized PHP blob
 1366+ us_data mediumblob NOT NULL
 1367+) /*$wgDBTableOptions*/;
 1368+
13541369 -- vim: sw=2 sts=2 et
Index: branches/staged-upload/includes/upload/UploadFromStash.php
@@ -9,40 +9,25 @@
1010 */
1111
1212 class UploadFromStash extends UploadBase {
13 - public static function isValidSessionKey( $key, $sessionData ) {
14 - return !empty( $key ) &&
15 - is_array( $sessionData ) &&
16 - isset( $sessionData[$key] ) &&
17 - isset( $sessionData[$key]['version'] ) &&
18 - $sessionData[$key]['version'] == self::SESSION_VERSION;
19 - }
20 -
2113 public static function isValidRequest( $request ) {
22 - $sessionData = $request->getSessionData( 'wsUploadData' );
23 - return self::isValidSessionKey(
24 - $request->getInt( 'wpSessionKey' ),
25 - $sessionData
26 - );
 14+ $stash = RepoGroup::singleton()->getLocalRepo()->getStash();
 15+ return (bool)$stash->getUpload( $request->getInt( 'wpSessionKey' ) );
2716 }
28 - /*
 17+ /**
2918 * some $na vars for uploadBase method compatibility.
3019 */
31 - public function initialize( $name, $sessionData, $na=false, $na2=false ) {
32 - /**
33 - * Confirming a temporarily stashed upload.
34 - * We don't want path names to be forged, so we keep
35 - * them in the session on the server and just give
36 - * an opaque key to the user agent.
37 - */
 20+ public function initialize( $name, $id, $na=false, $na2=false ) {
 21+ $this->mStash = RepoGroup::singleton()->getLocalRepo()->getStash();
 22+ $this->mTemporaryUpload = $this->mStash->getUpload( $id );
 23+ $data = $this->mTemporaryUpload->getData();
3824
39 - parent::initialize( $name,
40 - $this->getRealPath ( $sessionData['mTempPath'] ),
41 - $sessionData['mFileSize'],
42 - false
43 - );
 25+ parent::initialize( $name,
 26+ $this->mTemporaryUpload->getRealPath(),
 27+ $data['size'],
 28+ /* $removeTempFile */ false
 29+ );
4430
45 - $this->mVirtualTempPath = $sessionData['mTempPath'];
46 - $this->mFileProps = $sessionData['mFileProps'];
 31+ $this->mFileProps = $data['props'];
4732 }
4833
4934 public function initializeFromRequest( &$request ) {
@@ -52,7 +37,7 @@
5338 $desiredDestName = $request->getText( 'wpDestFile' );
5439 if( !$desiredDestName )
5540 $desiredDestName = $request->getText( 'wpUploadFile' );
56 - return $this->initialize( $desiredDestName, $sessionData[$this->mSessionKey], false );
 41+ return $this->initialize( $desiredDestName, $this->mSessionKey, false );
5742 }
5843
5944 /**
@@ -77,8 +62,7 @@
7863 * @return success
7964 */
8065 public function unsaveUploadedFile() {
81 - $repo = RepoGroup::singleton()->getLocalRepo();
82 - $success = $repo->freeTemp( $this->mVirtualTempPath );
 66+ $success = $this->mStash->freeUpload( $this->mTemporaryUpload );
8367 return $success;
8468 }
8569
Index: branches/staged-upload/includes/upload/UploadBase.php
@@ -452,24 +452,6 @@
453453 }
454454
455455 /**
456 - * Stash a file in a temporary directory for later processing
457 - * after the user has confirmed it.
458 - *
459 - * If the user doesn't explicitly cancel or accept, these files
460 - * can accumulate in the temp directory.
461 - *
462 - * @param string $saveName - the destination filename
463 - * @param string $tempSrc - the source temporary file to save
464 - * @return string - full path the stashed file, or false on failure
465 - * @access private
466 - */
467 - protected function saveTempUploadedFile( $saveName, $tempSrc ) {
468 - $repo = RepoGroup::singleton()->getLocalRepo();
469 - $status = $repo->storeTemp( $saveName, $tempSrc );
470 - return $status;
471 - }
472 -
473 - /**
474456 * Stash a file in a temporary directory for later processing,
475457 * and save the necessary descriptive info into the session.
476458 * Returns a key value which will be passed through a form
@@ -478,33 +460,23 @@
479461 * @return int Session key
480462 */
481463 public function stashSession() {
482 - $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
483 - if( !$status->isOK() ) {
484 - # Couldn't save the file.
 464+ $stash = RepoGroup::singleton()->getLocalRepo()->getStash();
 465+
 466+ global $wgUser, $wgTemporaryUploadExpiry;
 467+
 468+ $upload = $stash->saveFile( $wgUser, time() + $wgTemporaryUploadExpiry,
 469+ $this->mDestName, $this->mTempPath, array(
 470+ 'size' => $this->mFileSize,
 471+ 'props' => $this->mFileProps,
 472+ ) );
 473+
 474+ if ( !$upload )
485475 return false;
486 - }
487 - if(!isset($_SESSION))
488 - session_start(); // start up the session (might have been previously closed to prevent php session locking)
489 - $key = $this->getSessionKey();
490 - $_SESSION['wsUploadData'][$key] = array(
491 - 'mTempPath' => $status->value,
492 - 'mFileSize' => $this->mFileSize,
493 - 'mFileProps' => $this->mFileProps,
494 - 'version' => self::SESSION_VERSION,
495 - );
496 - return $key;
 476+
 477+ return $upload->getId();
497478 }
498479
499 - /**
500 - * Generate a random session key from stash in cases where we want to start an upload without much information
501 - */
502 - protected function getSessionKey(){
503 - $key = mt_rand( 0, 0x7fffffff );
504 - $_SESSION['wsUploadData'][$key] = array();
505 - return $key;
506 - }
507480
508 -
509481 /**
510482 * If we've modified the upload file we need to manually remove it
511483 * on exit to clean up.
Index: branches/staged-upload/includes/filerepo/LocalRepo.php
@@ -198,5 +198,15 @@
199199 $wgMemc->delete( $memcKey );
200200 }
201201 }
 202+
 203+ /**
 204+ * Return a place to temporary store files
 205+ *
 206+ * @return UploadStash
 207+ */
 208+ public function getStash() {
 209+ return new UploadStash( $this );
 210+ }
 211+
202212 }
203213
Index: branches/staged-upload/includes/AutoLoader.php
@@ -414,7 +414,9 @@
415415 'LocalRepo' => 'includes/filerepo/LocalRepo.php',
416416 'OldLocalFile' => 'includes/filerepo/OldLocalFile.php',
417417 'RepoGroup' => 'includes/filerepo/RepoGroup.php',
 418+ 'TemporaryUpload' => 'includes/filerepo/TemporaryUpload.php',
418419 'UnregisteredLocalFile' => 'includes/filerepo/UnregisteredLocalFile.php',
 420+ 'UploadStash' => 'includes/filerepo/UploadStash.php',
419421
420422 # includes/media
421423 'BitmapHandler' => 'includes/media/Bitmap.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r61392Followup to r61388: Forgot to svn add filesbtongminh20:41, 22 January 2010

Status & tagging log