r108481 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108480‎ | r108481 | r108482 >
Date:00:24, 10 January 2012
Author:aaron
Status:deferred
Tags:
Comment:
In SwiftFileBackend:
* Updated backend to match /trunk changes (r108353).
* Optimized doGetFileSha1Base36() and doStreamFile(),
* Moved doCreateInternal() code up.
Modified paths:
  • /branches/FileBackend/phase3/includes/filerepo/backend/SwiftFileBackend.php (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/includes/filerepo/backend/SwiftFileBackend.php
@@ -67,9 +67,9 @@
6868 }
6969
7070 /**
71 - * @see FileBackend::doStoreInternal()
 71+ * @see FileBackend::doCopyInternal()
7272 */
73 - protected function doStoreInternal( array $params ) {
 73+ protected function doCreateInternal( array $params ) {
7474 $status = Status::newGood();
7575
7676 list( $dstCont, $destRel ) = $this->resolveStoragePathReal( $params['dst'] );
@@ -89,7 +89,7 @@
9090 try {
9191 $dContObj = $conn->get_container( $dstCont );
9292 } catch ( NoSuchContainerException $e ) {
93 - $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
 93+ $status->fatal( 'backend-fail-create', $params['dst'] );
9494 return $status;
9595 } catch ( InvalidResponseException $e ) {
9696 $status->fatal( 'backend-fail-connect' );
@@ -119,14 +119,17 @@
120120 return $status;
121121 }
122122
123 - // (d) Actually store the object
 123+ // (d) Get a SHA-1 hash of the object
 124+ $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
 125+
 126+ // (e) Actually create the object
124127 try {
125128 $obj = $dContObj->create_object( $destRel );
126 - $obj->load_from_filename( $params['src'], True );
 129+ // Note: metadata keys stored as [Upper case char][[Lower case char]...]
 130+ $obj->metadata = array( 'Sha1base36' => $sha1Hash );
 131+ $obj->write( $params['content'] );
127132 } catch ( BadContentTypeException $e ) {
128133 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
129 - } catch ( IOException $e ) {
130 - $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
131134 } catch ( InvalidResponseException $e ) {
132135 $status->fatal( 'backend-fail-connect' );
133136 } catch ( Exception $e ) { // some other exception?
@@ -138,17 +141,11 @@
139142 }
140143
141144 /**
142 - * @see FileBackend::doCopyInternal()
 145+ * @see FileBackend::doStoreInternal()
143146 */
144 - protected function doCopyInternal( array $params ) {
 147+ protected function doStoreInternal( array $params ) {
145148 $status = Status::newGood();
146149
147 - list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
148 - if ( $srcRel === null ) {
149 - $status->fatal( 'backend-fail-invalidpath', $params['src'] );
150 - return $status;
151 - }
152 -
153150 list( $dstCont, $destRel ) = $this->resolveStoragePathReal( $params['dst'] );
154151 if ( $destRel === null ) {
155152 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
@@ -162,9 +159,8 @@
163160 return $status;
164161 }
165162
166 - // (b) Check the source and destination containers
 163+ // (b) Check the destination container
167164 try {
168 - $sContObj = $conn->get_container( $srcCont );
169165 $dContObj = $conn->get_container( $dstCont );
170166 } catch ( NoSuchContainerException $e ) {
171167 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
@@ -197,10 +193,23 @@
198194 return $status;
199195 }
200196
201 - // (d) Actually copy the file to the destination
 197+ // (d) Get a SHA-1 hash of the object
 198+ $sha1Hash = sha1_file( $params['src'] );
 199+ if ( $sha1Hash === false ) { // source doesn't exist?
 200+ $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
 201+ return $status;
 202+ }
 203+ $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
 204+
 205+ // (e) Actually store the object
202206 try {
203 - $sContObj->copy_object_to( $srcRel, $dContObj, $destRel );
204 - } catch ( NoSuchObjectException $e ) { // source object does not exist
 207+ $obj = $dContObj->create_object( $destRel );
 208+ // Note: metadata keys stored as [Upper case char][[Lower case char]...]
 209+ $obj->metadata = array( 'Sha1base36' => $sha1Hash );
 210+ $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
 211+ } catch ( BadContentTypeException $e ) {
 212+ $status->fatal( 'backend-fail-contenttype', $params['dst'] );
 213+ } catch ( IOException $e ) {
205214 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
206215 } catch ( InvalidResponseException $e ) {
207216 $status->fatal( 'backend-fail-connect' );
@@ -213,9 +222,9 @@
214223 }
215224
216225 /**
217 - * @see FileBackend::doDeleteInternal()
 226+ * @see FileBackend::doCopyInternal()
218227 */
219 - protected function doDeleteInternal( array $params ) {
 228+ protected function doCopyInternal( array $params ) {
220229 $status = Status::newGood();
221230
222231 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
@@ -224,6 +233,12 @@
225234 return $status;
226235 }
227236
 237+ list( $dstCont, $destRel ) = $this->resolveStoragePathReal( $params['dst'] );
 238+ if ( $destRel === null ) {
 239+ $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
 240+ return $status;
 241+ }
 242+
228243 // (a) Get a swift proxy connection
229244 $conn = $this->getConnection();
230245 if ( !$conn ) {
@@ -231,11 +246,12 @@
232247 return $status;
233248 }
234249
235 - // (b) Check the source container
 250+ // (b) Check the source and destination containers
236251 try {
237252 $sContObj = $conn->get_container( $srcCont );
 253+ $dContObj = $conn->get_container( $dstCont );
238254 } catch ( NoSuchContainerException $e ) {
239 - $status->fatal( 'backend-fail-delete', $params['src'] );
 255+ $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
240256 return $status;
241257 } catch ( InvalidResponseException $e ) {
242258 $status->fatal( 'backend-fail-connect' );
@@ -246,32 +262,49 @@
247263 return $status;
248264 }
249265
250 - // (c) Actually delete the object
 266+ // (c) Check if the destination object already exists
251267 try {
252 - $sContObj->delete_object( $srcRel );
 268+ $dContObj->get_object( $destRel ); // throws NoSuchObjectException
 269+ // NoSuchObjectException not thrown: file must exist
 270+ if ( empty( $params['overwriteDest'] ) ) {
 271+ $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
 272+ return $status;
 273+ }
253274 } catch ( NoSuchObjectException $e ) {
254 - if ( empty( $params['ignoreMissingSource'] ) ) {
255 - $status->fatal( 'backend-fail-delete', $params['src'] );
256 - }
 275+ // NoSuchObjectException thrown: file does not exist
257276 } catch ( InvalidResponseException $e ) {
258277 $status->fatal( 'backend-fail-connect' );
 278+ return $status;
259279 } catch ( Exception $e ) { // some other exception?
260280 $status->fatal( 'backend-fail-internal' );
261281 $this->logException( $e, __METHOD__, $params );
 282+ return $status;
262283 }
263284
 285+ // (d) Actually copy the file to the destination
 286+ try {
 287+ $sContObj->copy_object_to( $srcRel, $dContObj, $destRel );
 288+ } catch ( NoSuchObjectException $e ) { // source object does not exist
 289+ $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
 290+ } catch ( InvalidResponseException $e ) {
 291+ $status->fatal( 'backend-fail-connect' );
 292+ } catch ( Exception $e ) { // some other exception?
 293+ $status->fatal( 'backend-fail-internal' );
 294+ $this->logException( $e, __METHOD__, $params );
 295+ }
 296+
264297 return $status;
265298 }
266299
267300 /**
268 - * @see FileBackend::doCopyInternal()
 301+ * @see FileBackend::doDeleteInternal()
269302 */
270 - protected function doCreateInternal( array $params ) {
 303+ protected function doDeleteInternal( array $params ) {
271304 $status = Status::newGood();
272305
273 - list( $dstCont, $destRel ) = $this->resolveStoragePathReal( $params['dst'] );
274 - if ( $destRel === null ) {
275 - $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
 306+ list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
 307+ if ( $srcRel === null ) {
 308+ $status->fatal( 'backend-fail-invalidpath', $params['src'] );
276309 return $status;
277310 }
278311
@@ -282,11 +315,11 @@
283316 return $status;
284317 }
285318
286 - // (b) Check the destination container
 319+ // (b) Check the source container
287320 try {
288 - $dContObj = $conn->get_container( $dstCont );
 321+ $sContObj = $conn->get_container( $srcCont );
289322 } catch ( NoSuchContainerException $e ) {
290 - $status->fatal( 'backend-fail-create', $params['dst'] );
 323+ $status->fatal( 'backend-fail-delete', $params['src'] );
291324 return $status;
292325 } catch ( InvalidResponseException $e ) {
293326 $status->fatal( 'backend-fail-connect' );
@@ -297,45 +330,27 @@
298331 return $status;
299332 }
300333
301 - // (c) Check if the destination object already exists
 334+ // (c) Actually delete the object
302335 try {
303 - $dContObj->get_object( $destRel ); // throws NoSuchObjectException
304 - // NoSuchObjectException not thrown: file must exist
305 - if ( empty( $params['overwriteDest'] ) ) {
306 - $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
307 - return $status;
 336+ $sContObj->delete_object( $srcRel );
 337+ } catch ( NoSuchObjectException $e ) {
 338+ if ( empty( $params['ignoreMissingSource'] ) ) {
 339+ $status->fatal( 'backend-fail-delete', $params['src'] );
308340 }
309 - } catch ( NoSuchObjectException $e ) {
310 - // NoSuchObjectException thrown: file does not exist
311341 } catch ( InvalidResponseException $e ) {
312342 $status->fatal( 'backend-fail-connect' );
313 - return $status;
314343 } catch ( Exception $e ) { // some other exception?
315344 $status->fatal( 'backend-fail-internal' );
316345 $this->logException( $e, __METHOD__, $params );
317 - return $status;
318346 }
319347
320 - // (d) Actually create the object
321 - try {
322 - $obj = $dContObj->create_object( $destRel );
323 - $obj->write( $params['content'] );
324 - } catch ( BadContentTypeException $e ) {
325 - $status->fatal( 'backend-fail-contenttype', $params['dst'] );
326 - } catch ( InvalidResponseException $e ) {
327 - $status->fatal( 'backend-fail-connect' );
328 - } catch ( Exception $e ) { // some other exception?
329 - $status->fatal( 'backend-fail-internal' );
330 - $this->logException( $e, __METHOD__, $params );
331 - }
332 -
333348 return $status;
334349 }
335350
336351 /**
337 - * @see FileBackend::prepate()
 352+ * @see FileBackend::doPrepareInternal()
338353 */
339 - protected function doPrepare( $fullCont, $dir, array $params ) {
 354+ protected function doPrepareInternal( $fullCont, $dir, array $params ) {
340355 $status = Status::newGood();
341356
342357 // (a) Get a swift proxy connection
@@ -348,6 +363,8 @@
349364 // (b) Create the destination container
350365 try {
351366 $conn->create_container( $fullCont );
 367+ } catch ( InvalidResponseException $e ) {
 368+ $status->fatal( 'backend-fail-connect' );
352369 } catch ( Exception $e ) { // some other exception?
353370 $status->fatal( 'backend-fail-internal' );
354371 $this->logException( $e, __METHOD__, $params );
@@ -357,9 +374,9 @@
358375 }
359376
360377 /**
361 - * @see FileBackend::secure()
 378+ * @see FileBackend::doSecureInternal()
362379 */
363 - protected function doSecure( $fullCont, $dir, array $params ) {
 380+ protected function doSecureInternal( $fullCont, $dir, array $params ) {
364381 $status = Status::newGood();
365382 // @TODO: restrict container from $this->swiftProxyUser
366383 return $status; // badgers? We don't need no steenking badgers!
@@ -368,7 +385,7 @@
369386 /**
370387 * @see FileBackend::doFileExists()
371388 */
372 - protected function doFileExists( array $params ) {
 389+ protected function doGetFileStat( array $params ) {
373390 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
374391 if ( $srcRel === null ) {
375392 return false; // invalid storage path
@@ -376,59 +393,34 @@
377394
378395 $conn = $this->getConnection();
379396 if ( !$conn ) {
380 - return false; // fail vs not exists?
 397+ return null;
381398 }
382399
 400+ $stat = false;
383401 try {
384402 $container = $conn->get_container( $srcCont );
385 - $container->get_object( $srcRel );
386 - $exists = true;
387 - } catch ( NoSuchContainerException $e ) {
388 - $exists = false;
389 - } catch ( NoSuchObjectException $e ) {
390 - $exists = false;
391 - } catch ( Exception $e ) { // some other exception?
392 - $exists = false; // fail vs not exists?
393 - $this->logException( $e, __METHOD__, $params );
394 - }
395 -
396 - return $exists;
397 - }
398 -
399 - /**
400 - * @see FileBackend::doGetFileTimestamp()
401 - */
402 - protected function doGetFileTimestamp( array $params ) {
403 - list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
404 - if ( $srcRel === null ) {
405 - return false; // invalid storage path
406 - }
407 -
408 - $conn = $this->getConnection();
409 - if ( !$conn ) {
410 - $status->fatal( 'backend-fail-connect' );
411 - return $status;
412 - }
413 -
414 - try {
415 - $container = $conn->get_container( $srcCont);
416403 $obj = $container->get_object( $srcRel );
 404+ // Convert "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
 405+ $date = DateTime::createFromFormat( 'D, d F Y G:i:s e', $obj->last_modified );
 406+ if ( $date ) {
 407+ $stat = array(
 408+ 'mtime' => $date->format( 'YmdHis' ),
 409+ 'size' => $obj->content_length,
 410+ 'sha1' => $obj->metadata['Sha1base36']
 411+ );
 412+ } else { // exception will be caught below
 413+ throw new Exception( "Could not parse date for object {$srcRel}" );
 414+ }
417415 } catch ( NoSuchContainerException $e ) {
418 - $obj = null;
419416 } catch ( NoSuchObjectException $e ) {
420 - $obj = null;
 417+ } catch ( InvalidResponseException $e ) {
 418+ $stat = null;
421419 } catch ( Exception $e ) { // some other exception?
422 - $obj = null; // fail vs not exists?
 420+ $stat = null;
423421 $this->logException( $e, __METHOD__, $params );
424422 }
425423
426 - if ( $obj ) {
427 - // Convert "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
428 - $date = DateTime::createFromFormat( 'D, d F Y G:i:s e', $obj->last_modified );
429 - return $date ? $date->format( 'YmdHis' ) : false; // fail vs not exists?
430 - } else {
431 - return false; // file not found
432 - }
 424+ return $stat;
433425 }
434426
435427 /**
@@ -442,20 +434,18 @@
443435
444436 $conn = $this->getConnection();
445437 if ( !$conn ) {
446 - $status->fatal( 'backend-fail-connect' );
447 - return $status;
 438+ return false;
448439 }
449440
 441+ $data = false;
450442 try {
451 - $container = $conn->get_container( $srcCont);
 443+ $container = $conn->get_container( $srcCont );
452444 $obj = $container->get_object( $srcRel );
453445 $data = $obj->read();
454446 } catch ( NoSuchContainerException $e ) {
455 - $data = false;
456447 } catch ( NoSuchObjectException $e ) {
457 - $data = false;
 448+ } catch ( InvalidResponseException $e ) {
458449 } catch ( Exception $e ) { // some other exception?
459 - $data = false; // fail vs not exists?
460450 $this->logException( $e, __METHOD__, $params );
461451 }
462452
@@ -484,15 +474,14 @@
485475 return null;
486476 }
487477
 478+ $files = array();
488479 try {
489480 $container = $conn->get_container( $fullCont );
490481 $files = $container->list_objects( $limit, $after, $dir );
491482 } catch ( NoSuchContainerException $e ) {
492 - $files = array();
493483 } catch ( NoSuchObjectException $e ) {
494 - $files = array();
 484+ } catch ( InvalidResponseException $e ) {
495485 } catch ( Exception $e ) { // some other exception?
496 - $files = array();
497486 $this->logException( $e, __METHOD__, $params );
498487 }
499488
@@ -500,6 +489,65 @@
501490 }
502491
503492 /**
 493+ * @see FileBackend::doGetFileSha1base36()
 494+ */
 495+ public function doGetFileSha1base36( array $params ) {
 496+ $stat = $this->getFileStat( $params );
 497+ if ( $stat ) {
 498+ return $stat['sha1'];
 499+ } else {
 500+ return false;
 501+ }
 502+ }
 503+
 504+ /**
 505+ * @see FileBackend::doStreamFile()
 506+ */
 507+ protected function doStreamFile( array $params ) {
 508+ $status = Status::newGood();
 509+
 510+ list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
 511+ if ( $srcRel === null ) {
 512+ $status->fatal( 'backend-fail-invalidpath', $params['src'] );
 513+ }
 514+
 515+ $conn = $this->getConnection();
 516+ if ( !$conn ) {
 517+ $status->fatal( 'backend-fail-connect' );
 518+ }
 519+
 520+ try {
 521+ $cont = $conn->get_container( $srcCont );
 522+ $obj = $cont->get_object( $srcRel );
 523+ } catch ( NoSuchContainerException $e ) {
 524+ $status->fatal( 'backend-fail-stream', $params['src'] );
 525+ return $status;
 526+ } catch ( NoSuchObjectException $e ) {
 527+ $status->fatal( 'backend-fail-stream', $params['src'] );
 528+ return $status;
 529+ } catch ( IOException $e ) {
 530+ $status->fatal( 'backend-fail-stream', $params['src'] );
 531+ return $status;
 532+ } catch ( Exception $e ) { // some other exception?
 533+ $status->fatal( 'backend-fail-stream', $params['src'] );
 534+ $this->logException( $e, __METHOD__, $params );
 535+ return $status;
 536+ }
 537+
 538+ try {
 539+ $output = fopen("php://output", "w");
 540+ $obj->stream( $output );
 541+ } catch ( InvalidResponseException $e ) {
 542+ $status->fatal( 'backend-fail-connect' );
 543+ } catch ( Exception $e ) { // some other exception?
 544+ $status->fatal( 'backend-fail-stream', $params['src'] );
 545+ $this->logException( $e, __METHOD__, $params );
 546+ }
 547+
 548+ return $status;
 549+ }
 550+
 551+ /**
504552 * @see FileBackend::getLocalCopy()
505553 */
506554 public function getLocalCopy( array $params ) {
@@ -508,6 +556,11 @@
509557 return null;
510558 }
511559
 560+ $conn = $this->getConnection();
 561+ if ( !$conn ) {
 562+ return null;
 563+ }
 564+
512565 // Get source file extension
513566 $ext = FileBackend::extensionFromPath( $srcRel );
514567 // Create a new temporary file...
@@ -516,11 +569,6 @@
517570 return null;
518571 }
519572
520 - $conn = $this->getConnection();
521 - if ( !$conn ) {
522 - return null;
523 - }
524 -
525573 try {
526574 $cont = $conn->get_container( $srcCont );
527575 $obj = $cont->get_object( $srcRel );
@@ -531,6 +579,8 @@
532580 $tmpFile = null;
533581 } catch ( IOException $e ) {
534582 $tmpFile = null;
 583+ } catch ( InvalidResponseException $e ) {
 584+ $tmpFile = null;
535585 } catch ( Exception $e ) { // some other exception?
536586 $tmpFile = null;
537587 $this->logException( $e, __METHOD__, $params );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r108353In FileBackend:...aaron08:40, 8 January 2012

Status & tagging log