r61392 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61391‎ | r61392 | r61393 >
Date:20:41, 22 January 2010
Author:btongminh
Status:deferred
Tags:
Comment:
Followup to r61388: Forgot to svn add files
Modified paths:
  • /branches/staged-upload/includes/filerepo/TemporaryUpload.php (added) (history)
  • /branches/staged-upload/includes/filerepo/UploadStash.php (added) (history)

Diff [purge]

Index: branches/staged-upload/includes/filerepo/TemporaryUpload.php
@@ -0,0 +1,134 @@
 2+<?php
 3+
 4+class TemporaryUpload extends UnregisteredLocalFile {
 5+ public static function newFromRow( $repo, $row ) {
 6+ return TemporaryUpload::newFromArray( $repo, array(
 7+ 'us_id' => $row->us_id,
 8+ 'us_user' => $row->us_user,
 9+ 'us_timestamp' => $row->us_timestamp,
 10+ 'us_expiry' => $row->us_expiry,
 11+ 'us_path' => $row->us_path,
 12+ 'us_data' => $row->us_data,
 13+ ) );
 14+ }
 15+ public static function newFromArray( $repo, $row ) {
 16+ $path = $repo->resolveVirtualUrl( $row['us_path'] );
 17+
 18+ $file = new TemporaryUpload( $repo, $path );
 19+ $file->loadFromArray( $row );
 20+
 21+ return $file;
 22+ }
 23+
 24+ public function __construct( $repo, $path ) {
 25+ parent::__construct( false, $repo, $path );
 26+ }
 27+
 28+ public function loadFromArray( $row ) {
 29+ $this->id = $row['us_id'];
 30+ $this->userId = $row['us_user'];
 31+ $this->user = null;
 32+ $this->timestamp = wfTimestamp( TS_MW, $row['us_timestamp'] );
 33+ $this->expiry = wfTimestamp( TS_MW, $row['us_expiry'] );
 34+ $this->virtualPath = $row['us_path'];
 35+ $this->data = unserialize( $row['us_data'] );
 36+ }
 37+
 38+ /**
 39+ * Returns the id of the upload
 40+ *
 41+ * @return int
 42+ */
 43+ public function getId() {
 44+ return $this->id;
 45+ }
 46+ /**
 47+ * Returns a the owner of this file. Overrides parent
 48+ *
 49+ * @return mixed
 50+ */
 51+ public function getUser( $type = 'text' ) {
 52+ if ( is_null( $this->user ) )
 53+ $this->user = User::newFromId( $this->getUserId() );
 54+
 55+ if ( $type == 'id' )
 56+ return $this->getUserId();
 57+ elseif ( $type == 'text' )
 58+ return $this->user->getName();
 59+
 60+ return null;
 61+ }
 62+ /**
 63+ * Returns the id of the owner of this file
 64+ *
 65+ * @return int
 66+ */
 67+ public function getUserId() {
 68+ return $this->userId;
 69+ }
 70+ /**
 71+ * Returns the time of upload in TS_MW format
 72+ *
 73+ * @return string
 74+ */
 75+ public function getTimestamp() {
 76+ return $this->timestamp;
 77+ }
 78+ /**
 79+ * Returns the expiry time of the file in TS_MW format
 80+ *
 81+ * @return string
 82+ */
 83+ public function getExpiry() {
 84+ return $this->expiry;
 85+ }
 86+ /**
 87+ * Returns whether the file is expired
 88+ *
 89+ * @return bool
 90+ */
 91+ public function isExpired() {
 92+ return (wfTimestamp( TS_MW ) > $this->getExpiry());
 93+ }
 94+ /**
 95+ * Returns the virtual path of the file
 96+ *
 97+ * @param $suffix string
 98+ * @return string
 99+ */
 100+ public function getVirtualUrl( $suffix = false ) {
 101+ $path = $this->virtualPath;
 102+ if ( $suffix !== false ) {
 103+ $path .= '/' . rawurlencode( $suffix );
 104+ }
 105+ return $path;
 106+ }
 107+ /**
 108+ * Returns the path on the file system
 109+ *
 110+ * @return string
 111+ */
 112+ public function getRealPath() {
 113+ # Set by parent constructor
 114+ return $this->path;
 115+ }
 116+ /**
 117+ * Returns any associated data
 118+ *
 119+ * @return array
 120+ */
 121+ public function getData() {
 122+ return $this->data;
 123+ }
 124+
 125+ /**
 126+ * Returns the public URL. Overrides parent.
 127+ *
 128+ * @return string
 129+ */
 130+ public function getURL() {
 131+ # Not really necessary yet, but once we have thumbnailing for temp
 132+ # files this is needed.
 133+ return false;
 134+ }
 135+}
Property changes on: branches/staged-upload/includes/filerepo/TemporaryUpload.php
___________________________________________________________________
Name: svn:eol-style
1136 + native
Index: branches/staged-upload/includes/filerepo/UploadStash.php
@@ -0,0 +1,118 @@
 2+<?php
 3+/**
 4+ * Store for uncompleted uploads.
 5+ *
 6+ *
 7+ */
 8+
 9+class UploadStash {
 10+ protected $repo;
 11+ protected static $selectFields = array(
 12+ 'us_id', 'us_user', 'us_timestamp',
 13+ 'us_expiry', 'us_path', 'us_data'
 14+ );
 15+
 16+ public function __construct( $repo ) {
 17+ $this->repo = $repo;
 18+ }
 19+
 20+ /**
 21+ * Stash a file in a temporary directory for later processing
 22+ * and record it into the database
 23+ *
 24+ * @param $user User
 25+ * @param $expiry Timestamp
 26+ * @param string $saveName - the destination filename
 27+ * @param string $tempSrc - the source temporary file to save
 28+ * @param $data array Additional data that needs to be stored
 29+ * @return TemporaryUpload
 30+ */
 31+ public function saveFile( $user, $expiry, $saveName, $tempSrc, $data ) {
 32+ $status = $this->repo->storeTemp( $saveName, $tempSrc );
 33+ if ( !$status->isOk() )
 34+ return false;
 35+
 36+ $upload = $this->recordUpload( $user, $expiry, $status->value, $data );
 37+ return $upload;
 38+ }
 39+
 40+ /**
 41+ * Record a temporary upload into the database
 42+ *
 43+ * @param $user User
 44+ * @param $expiry Timestamp
 45+ * @param $path string The mwrepo:// virtual url
 46+ * @param $data array Additional data that needs to be stored
 47+ * @return TemporaryUpload
 48+ */
 49+ public function recordUpload( $user, $expiry, $path, $data ) {
 50+ $dbw = $this->repo->getMasterDB();
 51+ $insert = array(
 52+ 'us_id' => $dbw->nextSequenceValue( 'upload_stash_us_id_seq' ),
 53+ 'us_user' => $user->getId(),
 54+ 'us_timestamp' => $dbw->timestamp(),
 55+ 'us_expiry' => $dbw->timestamp( $expiry ),
 56+ 'us_path' => $path,
 57+ 'us_data' => serialize( $data ),
 58+ );
 59+ $dbw->insert( 'upload_stash', $insert, __METHOD__ );
 60+
 61+ $insert['us_id'] = $dbw->insertId();
 62+ return TemporaryUpload::newFromArray( $this->repo, $insert );
 63+ }
 64+
 65+ /**
 66+ * Get the information for a specific upload
 67+ *
 68+ * @param $id int
 69+ * @return TemporaryUpload
 70+ */
 71+ public function getUpload( $id ) {
 72+ $dbr = $this->repo->getSlaveDB();
 73+ $row = $dbr->selectRow( 'upload_stash',
 74+ self::$selectFields,
 75+ array( 'us_id' => $id ),
 76+ __METHOD__
 77+ );
 78+ if ( !$row )
 79+ return null;
 80+
 81+ return TemporaryUpload::newFromRow( $this->repo, $row );
 82+
 83+ }
 84+ /**
 85+ * List the uploads from a certain user
 86+ *
 87+ * @param $user User
 88+ * @return array Array of items
 89+ */
 90+ public function listUploads( $user ) {
 91+ $dbr = $this->repo->getSlaveDB();
 92+ $result = $dbr->select( 'upload_stash',
 93+ self::$selectFields,
 94+ array( 'us_user' => $user->getId() ),
 95+ __METHOD__
 96+ );
 97+
 98+ $uploads = array();
 99+ foreach ( $result as $row ) {
 100+ $uploads[] = TemporaryUpload::newFromRow( $this->repo, $row );
 101+ }
 102+ return $uploads;
 103+ }
 104+
 105+ /**
 106+ * Delete the temporary file and remove the record from the database
 107+ *
 108+ * FIXME
 109+ */
 110+ public function freeUpload( $upload ) {
 111+ $dbw = $this->repo->getMasterDB();
 112+ $dbw->delete( 'upload_stash', array( 'us_id' => $upload->getId() ), __METHOD__ );
 113+
 114+ return $this->repo->freeTemp( $upload->getVirtualUrl() );
 115+ }
 116+
 117+}
 118+
 119+
Property changes on: branches/staged-upload/includes/filerepo/UploadStash.php
___________________________________________________________________
Name: svn:eol-style
1120 + native

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r61388Initial commit to move the temporary upload metadata from the session to the ...btongminh20:14, 22 January 2010

Status & tagging log