Index: trunk/phase3/includes/upload/UploadStash.php |
— | — | @@ -7,7 +7,7 @@ |
8 | 8 | * Mostly all of them are the same except for storing some custom fields, which we subsume into the data array. |
9 | 9 | * - enable applications to find said files later, as long as the db table or temp files haven't been purged. |
10 | 10 | * - 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 |
12 | 12 | * implements a web interface to some files stored this way. |
13 | 13 | * |
14 | 14 | * UploadStash represents the entire stash of temporary files. |
— | — | @@ -18,7 +18,7 @@ |
19 | 19 | |
20 | 20 | // Format of the key for files -- has to be suitable as a filename itself (e.g. ab12cd34ef.jpg) |
21 | 21 | const KEY_FORMAT_REGEX = '/^[\w-]+\.\w*$/'; |
22 | | - |
| 22 | + |
23 | 23 | // When a given stashed file can't be loaded, wait for the slaves to catch up. If they're more than MAX_LAG |
24 | 24 | // behind, throw an exception instead. (at what point is broken better than slow?) |
25 | 25 | const MAX_LAG = 30; |
— | — | @@ -36,13 +36,13 @@ |
37 | 37 | |
38 | 38 | // array of initialized repo objects |
39 | 39 | protected $files = array(); |
40 | | - |
| 40 | + |
41 | 41 | // cache of the file metadata that's stored in the database |
42 | 42 | protected $fileMetadata = array(); |
43 | | - |
| 43 | + |
44 | 44 | // fileprops cache |
45 | 45 | protected $fileProps = array(); |
46 | | - |
| 46 | + |
47 | 47 | // current user |
48 | 48 | protected $user, $userId, $isLoggedIn; |
49 | 49 | |
— | — | @@ -55,17 +55,17 @@ |
56 | 56 | public function __construct( $repo, $user = null ) { |
57 | 57 | // this might change based on wiki's configuration. |
58 | 58 | $this->repo = $repo; |
59 | | - |
| 59 | + |
60 | 60 | // if a user was passed, use it. otherwise, attempt to use the global. |
61 | 61 | // this keeps FileRepo from breaking when it creates an UploadStash object |
62 | | - if( $user ) { |
| 62 | + if ( $user ) { |
63 | 63 | $this->user = $user; |
64 | 64 | } else { |
65 | 65 | global $wgUser; |
66 | 66 | $this->user = $wgUser; |
67 | 67 | } |
68 | | - |
69 | | - if( is_object($this->user) ) { |
| 68 | + |
| 69 | + if ( is_object( $this->user ) ) { |
70 | 70 | $this->userId = $this->user->getId(); |
71 | 71 | $this->isLoggedIn = $this->user->isLoggedIn(); |
72 | 72 | } |
— | — | @@ -83,39 +83,39 @@ |
84 | 84 | * @return UploadStashFile |
85 | 85 | */ |
86 | 86 | public function getFile( $key, $noAuth = false ) { |
87 | | - |
| 87 | + |
88 | 88 | if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) { |
89 | 89 | throw new UploadStashBadPathException( "key '$key' is not in a proper format" ); |
90 | 90 | } |
91 | | - |
92 | | - if( !$noAuth ) { |
93 | | - if( !$this->isLoggedIn ) { |
| 91 | + |
| 92 | + if ( !$noAuth ) { |
| 93 | + if ( !$this->isLoggedIn ) { |
94 | 94 | throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' ); |
95 | 95 | } |
96 | 96 | } |
97 | | - |
| 97 | + |
98 | 98 | $dbr = $this->repo->getSlaveDb(); |
99 | | - |
| 99 | + |
100 | 100 | if ( !isset( $this->fileMetadata[$key] ) ) { |
101 | 101 | // 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 |
103 | 103 | // (ie. validating image ownership) when replag is high |
104 | | - if( !$this->fetchFileMetadata($key) ) { |
| 104 | + if ( !$this->fetchFileMetadata( $key ) ) { |
105 | 105 | $lag = $dbr->getLag(); |
106 | | - if( $lag > 0 && $lag <= self::MAX_LAG ) { |
| 106 | + if ( $lag > 0 && $lag <= self::MAX_LAG ) { |
107 | 107 | // if there's not too much replication lag, just wait for the slave to catch up to our last insert. |
108 | 108 | sleep( ceil( $lag ) ); |
109 | | - } elseif($lag > self::MAX_LAG ) { |
| 109 | + } elseif ( $lag > self::MAX_LAG ) { |
110 | 110 | // that's a lot of lag to introduce into the middle of the UI. |
111 | 111 | throw new UploadStashMaxLagExceededException( |
112 | 112 | 'Couldn\'t load stashed file metadata, and replication lag is above threshold: (MAX_LAG=' . self::MAX_LAG . ')' |
113 | 113 | ); |
114 | 114 | } |
115 | | - |
| 115 | + |
116 | 116 | // now that the waiting has happened, try again |
117 | | - $this->fetchFileMetadata($key); |
| 117 | + $this->fetchFileMetadata( $key ); |
118 | 118 | } |
119 | | - |
| 119 | + |
120 | 120 | if ( !isset( $this->fileMetadata[$key] ) ) { |
121 | 121 | throw new UploadStashFileNotFoundException( "key '$key' not found in stash" ); |
122 | 122 | } |
— | — | @@ -130,18 +130,18 @@ |
131 | 131 | } |
132 | 132 | $this->fileProps[$key] = File::getPropsFromPath( $path ); |
133 | 133 | } |
134 | | - |
| 134 | + |
135 | 135 | if ( ! $this->files[$key]->exists() ) { |
136 | 136 | wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" ); |
137 | 137 | throw new UploadStashBadPathException( "path doesn't exist" ); |
138 | 138 | } |
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 ) { |
142 | 142 | throw new UploadStashWrongOwnerException( "This file ($key) doesn't belong to the current user." ); |
143 | 143 | } |
144 | 144 | } |
145 | | - |
| 145 | + |
146 | 146 | return $this->files[$key]; |
147 | 147 | } |
148 | 148 | |
— | — | @@ -153,7 +153,7 @@ |
154 | 154 | */ |
155 | 155 | public function getMetadata ( $key ) { |
156 | 156 | $this->getFile( $key ); |
157 | | - return $this->fileMetadata[$key]; |
| 157 | + return $this->fileMetadata[$key]; |
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
— | — | @@ -164,7 +164,7 @@ |
165 | 165 | */ |
166 | 166 | public function getFileProps ( $key ) { |
167 | 167 | $this->getFile( $key ); |
168 | | - return $this->fileProps[$key]; |
| 168 | + return $this->fileProps[$key]; |
169 | 169 | } |
170 | 170 | |
171 | 171 | /** |
— | — | @@ -205,17 +205,17 @@ |
206 | 206 | } |
207 | 207 | |
208 | 208 | $this->fileProps[$key] = $fileProps; |
209 | | - |
| 209 | + |
210 | 210 | if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) { |
211 | 211 | throw new UploadStashBadPathException( "key '$key' is not in a proper format" ); |
212 | 212 | } |
213 | | - |
| 213 | + |
214 | 214 | wfDebug( __METHOD__ . " key for '$path': $key\n" ); |
215 | 215 | |
216 | 216 | // if not already in a temporary area, put it there |
217 | 217 | $storeResult = $this->repo->storeTemp( basename( $path ), $path ); |
218 | 218 | |
219 | | - if( ! $storeResult->isOK() ) { |
| 219 | + if ( ! $storeResult->isOK() ) { |
220 | 220 | // It is a convention in MediaWiki to only return one error per API exception, even if multiple errors |
221 | 221 | // are available. We use reset() to pick the "first" thing that was wrong, preferring errors to warnings. |
222 | 222 | // 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 @@ |
234 | 234 | throw new UploadStashFileException( "error storing file in '$path': " . implode( '; ', $error ) ); |
235 | 235 | } |
236 | 236 | $stashPath = $storeResult->value; |
237 | | - |
| 237 | + |
238 | 238 | // fetch the current user ID |
239 | | - if( !$this->isLoggedIn ) { |
| 239 | + if ( !$this->isLoggedIn ) { |
240 | 240 | wfDebugCallstack(); |
241 | 241 | throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' ); |
242 | 242 | } |
— | — | @@ -252,22 +252,22 @@ |
253 | 253 | $row = $dbw->selectRow( |
254 | 254 | 'uploadstash', |
255 | 255 | 'us_user, us_timestamp', |
256 | | - array('us_key' => $key), |
| 256 | + array( 'us_key' => $key ), |
257 | 257 | __METHOD__ |
258 | 258 | ); |
259 | | - |
| 259 | + |
260 | 260 | // The current user can't have this key if: |
261 | 261 | // - the key is owned by someone else and |
262 | 262 | // - 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 && |
265 | 265 | $row->wfTimestamp( TS_UNIX, $row->us_timestamp ) > time() - UploadStash::REPO_AGE * 3600 |
266 | 266 | ) { |
267 | 267 | $dbw->rollback(); |
268 | 268 | throw new UploadStashWrongOwnerException( "Attempting to upload a duplicate of a file that someone else has stashed" ); |
269 | 269 | } |
270 | 270 | } |
271 | | - |
| 271 | + |
272 | 272 | $this->fileMetadata[$key] = array( |
273 | 273 | 'us_user' => $this->userId, |
274 | 274 | 'us_key' => $key, |
— | — | @@ -284,13 +284,13 @@ |
285 | 285 | 'us_timestamp' => $dbw->timestamp(), |
286 | 286 | 'us_status' => 'finished' |
287 | 287 | ); |
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. |
290 | 290 | $dbw->replace( |
291 | 291 | 'uploadstash', |
292 | 292 | 'us_key', |
293 | 293 | $this->fileMetadata[$key], |
294 | | - __METHOD__ |
| 294 | + __METHOD__ |
295 | 295 | ); |
296 | 296 | $dbw->commit(); |
297 | 297 | |
— | — | @@ -299,7 +299,7 @@ |
300 | 300 | |
301 | 301 | # create the UploadStashFile object for this file. |
302 | 302 | $this->initFile( $key ); |
303 | | - |
| 303 | + |
304 | 304 | return $this->getFile( $key ); |
305 | 305 | } |
306 | 306 | |
— | — | @@ -311,22 +311,22 @@ |
312 | 312 | * @return boolean: success |
313 | 313 | */ |
314 | 314 | public function clear() { |
315 | | - if( !$this->isLoggedIn ) { |
| 315 | + if ( !$this->isLoggedIn ) { |
316 | 316 | throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' ); |
317 | 317 | } |
318 | | - |
| 318 | + |
319 | 319 | wfDebug( __METHOD__ . " clearing all rows for user $userId\n" ); |
320 | 320 | $dbw = $this->repo->getMasterDb(); |
321 | 321 | $dbw->delete( |
322 | 322 | 'uploadstash', |
323 | 323 | array( 'us_user' => $this->userId ), |
324 | | - __METHOD__ |
| 324 | + __METHOD__ |
325 | 325 | ); |
326 | 326 | |
327 | 327 | # destroy objects. |
328 | 328 | $this->files = array(); |
329 | 329 | $this->fileMetadata = array(); |
330 | | - |
| 330 | + |
331 | 331 | return true; |
332 | 332 | } |
333 | 333 | |
— | — | @@ -337,26 +337,26 @@ |
338 | 338 | * @throws UploadStashWrongOwnerException |
339 | 339 | * @return boolean: success |
340 | 340 | */ |
341 | | - public function removeFile( $key ){ |
342 | | - if( !$this->isLoggedIn ) { |
| 341 | + public function removeFile( $key ) { |
| 342 | + if ( !$this->isLoggedIn ) { |
343 | 343 | throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' ); |
344 | 344 | } |
345 | | - |
| 345 | + |
346 | 346 | $dbw = $this->repo->getMasterDb(); |
347 | | - |
| 347 | + |
348 | 348 | // this is a cheap query. it runs on the master so that this function still works when there's lag. |
349 | 349 | // it won't be called all that often. |
350 | 350 | $row = $dbw->selectRow( |
351 | 351 | 'uploadstash', |
352 | 352 | 'us_user', |
353 | | - array('us_key' => $key), |
| 353 | + array( 'us_key' => $key ), |
354 | 354 | __METHOD__ |
355 | 355 | ); |
356 | | - |
357 | | - if( $row->us_user != $this->userId ) { |
| 356 | + |
| 357 | + if ( $row->us_user != $this->userId ) { |
358 | 358 | throw new UploadStashWrongOwnerException( "Can't delete: the file ($key) doesn't belong to this user." ); |
359 | 359 | } |
360 | | - |
| 360 | + |
361 | 361 | return $this->removeFileNoAuth( $key ); |
362 | 362 | } |
363 | 363 | |
— | — | @@ -370,23 +370,23 @@ |
371 | 371 | wfDebug( __METHOD__ . " clearing row $key\n" ); |
372 | 372 | |
373 | 373 | $dbw = $this->repo->getMasterDb(); |
374 | | - |
| 374 | + |
375 | 375 | // this gets its own transaction since it's called serially by the cleanupUploadStash maintenance script |
376 | 376 | $dbw->begin(); |
377 | 377 | $dbw->delete( |
378 | 378 | 'uploadstash', |
379 | | - array( 'us_key' => $key), |
380 | | - __METHOD__ |
| 379 | + array( 'us_key' => $key ), |
| 380 | + __METHOD__ |
381 | 381 | ); |
382 | 382 | $dbw->commit(); |
383 | | - |
| 383 | + |
384 | 384 | // TODO: look into UnregisteredLocalFile and find out why the rv here is sometimes wrong (false when file was removed) |
385 | 385 | // for now, ignore. |
386 | 386 | $this->files[$key]->remove(); |
387 | | - |
| 387 | + |
388 | 388 | unset( $this->files[$key] ); |
389 | 389 | unset( $this->fileMetadata[$key] ); |
390 | | - |
| 390 | + |
391 | 391 | return true; |
392 | 392 | } |
393 | 393 | |
— | — | @@ -397,26 +397,26 @@ |
398 | 398 | * @return Array |
399 | 399 | */ |
400 | 400 | public function listFiles() { |
401 | | - if( !$this->isLoggedIn ) { |
| 401 | + if ( !$this->isLoggedIn ) { |
402 | 402 | throw new UploadStashNotLoggedInException( __METHOD__ . ' No user is logged in, files must belong to users' ); |
403 | 403 | } |
404 | | - |
| 404 | + |
405 | 405 | $dbr = $this->repo->getSlaveDb(); |
406 | 406 | $res = $dbr->select( |
407 | 407 | 'uploadstash', |
408 | 408 | 'us_key', |
409 | | - array('us_key' => $key), |
| 409 | + array( 'us_key' => $key ), |
410 | 410 | __METHOD__ |
411 | 411 | ); |
412 | 412 | |
413 | | - if( !is_object( $res ) || $res->numRows() == 0 ) { |
| 413 | + if ( !is_object( $res ) || $res->numRows() == 0 ) { |
414 | 414 | // nothing to do. |
415 | 415 | return false; |
416 | 416 | } |
417 | 417 | |
418 | 418 | // finish the read before starting writes. |
419 | 419 | $keys = array(); |
420 | | - foreach($res as $row) { |
| 420 | + foreach ( $res as $row ) { |
421 | 421 | array_push( $keys, $row->us_key ); |
422 | 422 | } |
423 | 423 | |
— | — | @@ -465,15 +465,15 @@ |
466 | 466 | $row = $dbr->selectRow( |
467 | 467 | 'uploadstash', |
468 | 468 | '*', |
469 | | - array('us_key' => $key), |
| 469 | + array( 'us_key' => $key ), |
470 | 470 | __METHOD__ |
471 | 471 | ); |
472 | | - |
473 | | - if( !is_object( $row ) ) { |
| 472 | + |
| 473 | + if ( !is_object( $row ) ) { |
474 | 474 | // key wasn't present in the database. this will happen sometimes. |
475 | 475 | return false; |
476 | 476 | } |
477 | | - |
| 477 | + |
478 | 478 | $this->fileMetadata[$key] = array( |
479 | 479 | 'us_user' => $row->us_user, |
480 | 480 | 'us_key' => $row->us_key, |
— | — | @@ -490,10 +490,10 @@ |
491 | 491 | 'us_timestamp' => $row->us_timestamp, |
492 | 492 | 'us_status' => $row->us_status |
493 | 493 | ); |
494 | | - |
| 494 | + |
495 | 495 | return true; |
496 | 496 | } |
497 | | - |
| 497 | + |
498 | 498 | /** |
499 | 499 | * Helper function: Initialize the UploadStashFile for a given file. |
500 | 500 | * |
— | — | @@ -668,11 +668,11 @@ |
669 | 669 | * @return Status: success |
670 | 670 | */ |
671 | 671 | public function remove() { |
672 | | - if( !$this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ) ) { |
| 672 | + if ( !$this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ) ) { |
673 | 673 | // Maybe the file's already been removed? This could totally happen in UploadBase. |
674 | 674 | return true; |
675 | 675 | } |
676 | | - |
| 676 | + |
677 | 677 | return $this->repo->freeTemp( $this->path ); |
678 | 678 | } |
679 | 679 | |