r92247 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92246‎ | r92247 | r92248 >
Date:15:54, 15 July 2011
Author:demon
Status:ok
Tags:
Comment:
Stylize
Modified paths:
  • /trunk/phase3/includes/upload/UploadStash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/upload/UploadStash.php
@@ -7,7 +7,7 @@
88 * Mostly all of them are the same except for storing some custom fields, which we subsume into the data array.
99 * - enable applications to find said files later, as long as the db table or temp files haven't been purged.
1010 * - enable the uploading user (and *ONLY* the uploading user) to access said files, and thumbnails of said files, via a URL.
11 - * We accomplish this using a database table, with ownership checking as you might expect. See SpecialUploadStash, which
 11+ * We accomplish this using a database table, with ownership checking as you might expect. See SpecialUploadStash, which
1212 * implements a web interface to some files stored this way.
1313 *
1414 * UploadStash represents the entire stash of temporary files.
@@ -18,7 +18,7 @@
1919
2020 // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg)
2121 const KEY_FORMAT_REGEX = '/^[\w-]+\.\w*$/';
22 -
 22+
2323 // When a given stashed file can't be loaded, wait for the slaves to catch up. If they're more than MAX_LAG
2424 // behind, throw an exception instead. (at what point is broken better than slow?)
2525 const MAX_LAG = 30;
@@ -36,13 +36,13 @@
3737
3838 // array of initialized repo objects
3939 protected $files = array();
40 -
 40+
4141 // cache of the file metadata that's stored in the database
4242 protected $fileMetadata = array();
43 -
 43+
4444 // fileprops cache
4545 protected $fileProps = array();
46 -
 46+
4747 // current user
4848 protected $user, $userId, $isLoggedIn;
4949
@@ -55,17 +55,17 @@
5656 public function __construct( $repo, $user = null ) {
5757 // this might change based on wiki's configuration.
5858 $this->repo = $repo;
59 -
 59+
6060 // if a user was passed, use it. otherwise, attempt to use the global.
6161 // this keeps FileRepo from breaking when it creates an UploadStash object
62 - if( $user ) {
 62+ if ( $user ) {
6363 $this->user = $user;
6464 } else {
6565 global $wgUser;
6666 $this->user = $wgUser;
6767 }
68 -
69 - if( is_object($this->user) ) {
 68+
 69+ if ( is_object( $this->user ) ) {
7070 $this->userId = $this->user->getId();
7171 $this->isLoggedIn = $this->user->isLoggedIn();
7272 }
@@ -83,39 +83,39 @@
8484 * @return UploadStashFile
8585 */
8686 public function getFile( $key, $noAuth = false ) {
87 -
 87+
8888 if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
8989 throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
9090 }
91 -
92 - if( !$noAuth ) {
93 - if( !$this->isLoggedIn ) {
 91+
 92+ if ( !$noAuth ) {
 93+ if ( !$this->isLoggedIn ) {
9494 throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
9595 }
9696 }
97 -
 97+
9898 $dbr = $this->repo->getSlaveDb();
99 -
 99+
100100 if ( !isset( $this->fileMetadata[$key] ) ) {
101101 // try this first. if it fails to find the row, check for lag, wait, try again. if its still missing, throw an exception.
102 - // this more complex solution keeps things moving for page loads with many requests
 102+ // this more complex solution keeps things moving for page loads with many requests
103103 // (ie. validating image ownership) when replag is high
104 - if( !$this->fetchFileMetadata($key) ) {
 104+ if ( !$this->fetchFileMetadata( $key ) ) {
105105 $lag = $dbr->getLag();
106 - if( $lag > 0 && $lag <= self::MAX_LAG ) {
 106+ if ( $lag > 0 && $lag <= self::MAX_LAG ) {
107107 // if there's not too much replication lag, just wait for the slave to catch up to our last insert.
108108 sleep( ceil( $lag ) );
109 - } elseif($lag > self::MAX_LAG ) {
 109+ } elseif ( $lag > self::MAX_LAG ) {
110110 // that's a lot of lag to introduce into the middle of the UI.
111111 throw new UploadStashMaxLagExceededException(
112112 'Couldn\'t load stashed file metadata, and replication lag is above threshold: (MAX_LAG=' . self::MAX_LAG . ')'
113113 );
114114 }
115 -
 115+
116116 // now that the waiting has happened, try again
117 - $this->fetchFileMetadata($key);
 117+ $this->fetchFileMetadata( $key );
118118 }
119 -
 119+
120120 if ( !isset( $this->fileMetadata[$key] ) ) {
121121 throw new UploadStashFileNotFoundException( "key '$key' not found in stash" );
122122 }
@@ -130,18 +130,18 @@
131131 }
132132 $this->fileProps[$key] = File::getPropsFromPath( $path );
133133 }
134 -
 134+
135135 if ( ! $this->files[$key]->exists() ) {
136136 wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" );
137137 throw new UploadStashBadPathException( "path doesn't exist" );
138138 }
139 -
140 - if( !$noAuth ) {
141 - if( $this->fileMetadata[$key]['us_user'] != $this->userId ) {
 139+
 140+ if ( !$noAuth ) {
 141+ if ( $this->fileMetadata[$key]['us_user'] != $this->userId ) {
142142 throw new UploadStashWrongOwnerException( "This file ($key) doesn't belong to the current user." );
143143 }
144144 }
145 -
 145+
146146 return $this->files[$key];
147147 }
148148
@@ -153,7 +153,7 @@
154154 */
155155 public function getMetadata ( $key ) {
156156 $this->getFile( $key );
157 - return $this->fileMetadata[$key];
 157+ return $this->fileMetadata[$key];
158158 }
159159
160160 /**
@@ -164,7 +164,7 @@
165165 */
166166 public function getFileProps ( $key ) {
167167 $this->getFile( $key );
168 - return $this->fileProps[$key];
 168+ return $this->fileProps[$key];
169169 }
170170
171171 /**
@@ -205,17 +205,17 @@
206206 }
207207
208208 $this->fileProps[$key] = $fileProps;
209 -
 209+
210210 if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
211211 throw new UploadStashBadPathException( "key '$key' is not in a proper format" );
212212 }
213 -
 213+
214214 wfDebug( __METHOD__ . " key for '$path': $key\n" );
215215
216216 // if not already in a temporary area, put it there
217217 $storeResult = $this->repo->storeTemp( basename( $path ), $path );
218218
219 - if( ! $storeResult->isOK() ) {
 219+ if ( ! $storeResult->isOK() ) {
220220 // It is a convention in MediaWiki to only return one error per API exception, even if multiple errors
221221 // are available. We use reset() to pick the "first" thing that was wrong, preferring errors to warnings.
222222 // This is a bit lame, as we may have more info in the $storeResult and we're throwing it away, but to fix it means
@@ -233,9 +233,9 @@
234234 throw new UploadStashFileException( "error storing file in '$path': " . implode( '; ', $error ) );
235235 }
236236 $stashPath = $storeResult->value;
237 -
 237+
238238 // fetch the current user ID
239 - if( !$this->isLoggedIn ) {
 239+ if ( !$this->isLoggedIn ) {
240240 wfDebugCallstack();
241241 throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
242242 }
@@ -252,22 +252,22 @@
253253 $row = $dbw->selectRow(
254254 'uploadstash',
255255 'us_user, us_timestamp',
256 - array('us_key' => $key),
 256+ array( 'us_key' => $key ),
257257 __METHOD__
258258 );
259 -
 259+
260260 // The current user can't have this key if:
261261 // - the key is owned by someone else and
262262 // - the age of the key is less than REPO_AGE
263 - if( is_object( $row ) ) {
264 - if( $row->us_user != $this->userId &&
 263+ if ( is_object( $row ) ) {
 264+ if ( $row->us_user != $this->userId &&
265265 $row->wfTimestamp( TS_UNIX, $row->us_timestamp ) > time() - UploadStash::REPO_AGE * 3600
266266 ) {
267267 $dbw->rollback();
268268 throw new UploadStashWrongOwnerException( "Attempting to upload a duplicate of a file that someone else has stashed" );
269269 }
270270 }
271 -
 271+
272272 $this->fileMetadata[$key] = array(
273273 'us_user' => $this->userId,
274274 'us_key' => $key,
@@ -284,13 +284,13 @@
285285 'us_timestamp' => $dbw->timestamp(),
286286 'us_status' => 'finished'
287287 );
288 -
289 - // if a row exists but previous checks on it passed, let the current user take over this key.
 288+
 289+ // if a row exists but previous checks on it passed, let the current user take over this key.
290290 $dbw->replace(
291291 'uploadstash',
292292 'us_key',
293293 $this->fileMetadata[$key],
294 - __METHOD__
 294+ __METHOD__
295295 );
296296 $dbw->commit();
297297
@@ -299,7 +299,7 @@
300300
301301 # create the UploadStashFile object for this file.
302302 $this->initFile( $key );
303 -
 303+
304304 return $this->getFile( $key );
305305 }
306306
@@ -311,22 +311,22 @@
312312 * @return boolean: success
313313 */
314314 public function clear() {
315 - if( !$this->isLoggedIn ) {
 315+ if ( !$this->isLoggedIn ) {
316316 throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
317317 }
318 -
 318+
319319 wfDebug( __METHOD__ . " clearing all rows for user $userId\n" );
320320 $dbw = $this->repo->getMasterDb();
321321 $dbw->delete(
322322 'uploadstash',
323323 array( 'us_user' => $this->userId ),
324 - __METHOD__
 324+ __METHOD__
325325 );
326326
327327 # destroy objects.
328328 $this->files = array();
329329 $this->fileMetadata = array();
330 -
 330+
331331 return true;
332332 }
333333
@@ -337,26 +337,26 @@
338338 * @throws UploadStashWrongOwnerException
339339 * @return boolean: success
340340 */
341 - public function removeFile( $key ){
342 - if( !$this->isLoggedIn ) {
 341+ public function removeFile( $key ) {
 342+ if ( !$this->isLoggedIn ) {
343343 throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
344344 }
345 -
 345+
346346 $dbw = $this->repo->getMasterDb();
347 -
 347+
348348 // this is a cheap query. it runs on the master so that this function still works when there's lag.
349349 // it won't be called all that often.
350350 $row = $dbw->selectRow(
351351 'uploadstash',
352352 'us_user',
353 - array('us_key' => $key),
 353+ array( 'us_key' => $key ),
354354 __METHOD__
355355 );
356 -
357 - if( $row->us_user != $this->userId ) {
 356+
 357+ if ( $row->us_user != $this->userId ) {
358358 throw new UploadStashWrongOwnerException( "Can't delete: the file ($key) doesn't belong to this user." );
359359 }
360 -
 360+
361361 return $this->removeFileNoAuth( $key );
362362 }
363363
@@ -370,23 +370,23 @@
371371 wfDebug( __METHOD__ . " clearing row $key\n" );
372372
373373 $dbw = $this->repo->getMasterDb();
374 -
 374+
375375 // this gets its own transaction since it's called serially by the cleanupUploadStash maintenance script
376376 $dbw->begin();
377377 $dbw->delete(
378378 'uploadstash',
379 - array( 'us_key' => $key),
380 - __METHOD__
 379+ array( 'us_key' => $key ),
 380+ __METHOD__
381381 );
382382 $dbw->commit();
383 -
 383+
384384 // TODO: look into UnregisteredLocalFile and find out why the rv here is sometimes wrong (false when file was removed)
385385 // for now, ignore.
386386 $this->files[$key]->remove();
387 -
 387+
388388 unset( $this->files[$key] );
389389 unset( $this->fileMetadata[$key] );
390 -
 390+
391391 return true;
392392 }
393393
@@ -397,26 +397,26 @@
398398 * @return Array
399399 */
400400 public function listFiles() {
401 - if( !$this->isLoggedIn ) {
 401+ if ( !$this->isLoggedIn ) {
402402 throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' );
403403 }
404 -
 404+
405405 $dbr = $this->repo->getSlaveDb();
406406 $res = $dbr->select(
407407 'uploadstash',
408408 'us_key',
409 - array('us_key' => $key),
 409+ array( 'us_key' => $key ),
410410 __METHOD__
411411 );
412412
413 - if( !is_object( $res ) || $res->numRows() == 0 ) {
 413+ if ( !is_object( $res ) || $res->numRows() == 0 ) {
414414 // nothing to do.
415415 return false;
416416 }
417417
418418 // finish the read before starting writes.
419419 $keys = array();
420 - foreach($res as $row) {
 420+ foreach ( $res as $row ) {
421421 array_push( $keys, $row->us_key );
422422 }
423423
@@ -465,15 +465,15 @@
466466 $row = $dbr->selectRow(
467467 'uploadstash',
468468 '*',
469 - array('us_key' => $key),
 469+ array( 'us_key' => $key ),
470470 __METHOD__
471471 );
472 -
473 - if( !is_object( $row ) ) {
 472+
 473+ if ( !is_object( $row ) ) {
474474 // key wasn't present in the database. this will happen sometimes.
475475 return false;
476476 }
477 -
 477+
478478 $this->fileMetadata[$key] = array(
479479 'us_user' => $row->us_user,
480480 'us_key' => $row->us_key,
@@ -490,10 +490,10 @@
491491 'us_timestamp' => $row->us_timestamp,
492492 'us_status' => $row->us_status
493493 );
494 -
 494+
495495 return true;
496496 }
497 -
 497+
498498 /**
499499 * Helper function: Initialize the UploadStashFile for a given file.
500500 *
@@ -668,11 +668,11 @@
669669 * @return Status: success
670670 */
671671 public function remove() {
672 - if( !$this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ) ) {
 672+ if ( !$this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ) ) {
673673 // Maybe the file's already been removed? This could totally happen in UploadBase.
674674 return true;
675675 }
676 -
 676+
677677 return $this->repo->freeTemp( $this->path );
678678 }
679679

Status & tagging log