Simplified last_modified timestamp conversion in SwiftFileBackend to just use wfTimes...
[lhc/web/wiklou.git] / includes / filerepo / backend / SwiftFileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Russ Nelson
6 * @author Aaron Schulz
7 */
8
9 /**
10 * Class for an OpenStack Swift based file backend.
11 *
12 * This requires the SwiftCloudFiles MediaWiki extension, which includes
13 * the php-cloudfiles library (https://github.com/rackspace/php-cloudfiles).
14 * php-cloudfiles requires the curl, fileinfo, and mb_string PHP extensions.
15 *
16 * Status messages should avoid mentioning the Swift account name
17 * Likewise, error suppression should be used to avoid path disclosure.
18 *
19 * @ingroup FileBackend
20 * @since 1.19
21 */
22 class SwiftFileBackend extends FileBackend {
23 /** @var CF_Authentication */
24 protected $auth; // Swift authentication handler
25 protected $authTTL; // integer seconds
26 protected $swiftAnonUser; // string; username to handle unauthenticated requests
27 protected $maxContCacheSize = 20; // integer; max containers with entries
28
29 /** @var CF_Connection */
30 protected $conn; // Swift connection handle
31 protected $connStarted = 0; // integer UNIX timestamp
32 protected $connContainers = array(); // container object cache
33
34 /**
35 * @see FileBackend::__construct()
36 * Additional $config params include:
37 * swiftAuthUrl : Swift authentication server URL
38 * swiftUser : Swift user used by MediaWiki (account:username)
39 * swiftKey : Swift authentication key for the above user
40 * swiftAuthTTL : Swift authentication TTL (seconds)
41 * swiftAnonUser : Swift user used for end-user requests (account:username)
42 * shardViaHashLevels : Map of container names to the number of hash levels
43 */
44 public function __construct( array $config ) {
45 parent::__construct( $config );
46 // Required settings
47 $this->auth = new CF_Authentication(
48 $config['swiftUser'],
49 $config['swiftKey'],
50 null, // account; unused
51 $config['swiftAuthUrl']
52 );
53 // Optional settings
54 $this->authTTL = isset( $config['swiftAuthTTL'] )
55 ? $config['swiftAuthTTL']
56 : 120; // some sane number
57 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
58 ? $config['swiftAnonUser']
59 : '';
60 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
61 ? $config['shardViaHashLevels']
62 : '';
63 }
64
65 /**
66 * @see FileBackend::resolveContainerPath()
67 */
68 protected function resolveContainerPath( $container, $relStoragePath ) {
69 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
70 return null; // too long for Swift
71 }
72 return $relStoragePath;
73 }
74
75 /**
76 * @see FileBackend::isPathUsableInternal()
77 */
78 public function isPathUsableInternal( $storagePath ) {
79 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
80 if ( $rel === null ) {
81 return false; // invalid
82 }
83
84 try {
85 $this->getContainer( $container );
86 return true; // container exists
87 } catch ( NoSuchContainerException $e ) {
88 } catch ( InvalidResponseException $e ) {
89 } catch ( Exception $e ) { // some other exception?
90 $this->logException( $e, __METHOD__, array( 'path' => $storagePath ) );
91 }
92
93 return false;
94 }
95
96 /**
97 * @see FileBackend::doCopyInternal()
98 */
99 protected function doCreateInternal( array $params ) {
100 $status = Status::newGood();
101
102 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
103 if ( $dstRel === null ) {
104 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
105 return $status;
106 }
107
108 // (a) Check the destination container and object
109 try {
110 $dContObj = $this->getContainer( $dstCont );
111 if ( empty( $params['overwrite'] ) ) {
112 $destObj = $dContObj->create_object( $dstRel );
113 // Check if the object already exists (fields populated)
114 if ( $destObj->last_modified ) {
115 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
116 return $status;
117 }
118 }
119 } catch ( NoSuchContainerException $e ) {
120 $status->fatal( 'backend-fail-create', $params['dst'] );
121 return $status;
122 } catch ( InvalidResponseException $e ) {
123 $status->fatal( 'backend-fail-connect', $this->name );
124 return $status;
125 } catch ( Exception $e ) { // some other exception?
126 $status->fatal( 'backend-fail-internal', $this->name );
127 $this->logException( $e, __METHOD__, $params );
128 return $status;
129 }
130
131 // (b) Get a SHA-1 hash of the object
132 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
133
134 // (c) Actually create the object
135 try {
136 // Create a fresh CF_Object with no fields preloaded.
137 // We don't want to preserve headers, metadata, and such.
138 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
139 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
140 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
141 $obj->write( $params['content'] );
142 } catch ( BadContentTypeException $e ) {
143 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
144 } catch ( InvalidResponseException $e ) {
145 $status->fatal( 'backend-fail-connect', $this->name );
146 } catch ( Exception $e ) { // some other exception?
147 $status->fatal( 'backend-fail-internal', $this->name );
148 $this->logException( $e, __METHOD__, $params );
149 }
150
151 return $status;
152 }
153
154 /**
155 * @see FileBackend::doStoreInternal()
156 */
157 protected function doStoreInternal( array $params ) {
158 $status = Status::newGood();
159
160 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
161 if ( $dstRel === null ) {
162 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
163 return $status;
164 }
165
166 // (a) Check the destination container and object
167 try {
168 $dContObj = $this->getContainer( $dstCont );
169 if ( empty( $params['overwrite'] ) ) {
170 $destObj = $dContObj->create_object( $dstRel );
171 // Check if the object already exists (fields populated)
172 if ( $destObj->last_modified ) {
173 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
174 return $status;
175 }
176 }
177 } catch ( NoSuchContainerException $e ) {
178 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
179 return $status;
180 } catch ( InvalidResponseException $e ) {
181 $status->fatal( 'backend-fail-connect', $this->name );
182 return $status;
183 } catch ( Exception $e ) { // some other exception?
184 $status->fatal( 'backend-fail-internal', $this->name );
185 $this->logException( $e, __METHOD__, $params );
186 return $status;
187 }
188
189 // (b) Get a SHA-1 hash of the object
190 $sha1Hash = sha1_file( $params['src'] );
191 if ( $sha1Hash === false ) { // source doesn't exist?
192 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
193 return $status;
194 }
195 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
196
197 // (c) Actually store the object
198 try {
199 // Create a fresh CF_Object with no fields preloaded.
200 // We don't want to preserve headers, metadata, and such.
201 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
202 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
203 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
204 $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
205 } catch ( BadContentTypeException $e ) {
206 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
207 } catch ( IOException $e ) {
208 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
209 } catch ( InvalidResponseException $e ) {
210 $status->fatal( 'backend-fail-connect', $this->name );
211 } catch ( Exception $e ) { // some other exception?
212 $status->fatal( 'backend-fail-internal', $this->name );
213 $this->logException( $e, __METHOD__, $params );
214 }
215
216 return $status;
217 }
218
219 /**
220 * @see FileBackend::doCopyInternal()
221 */
222 protected function doCopyInternal( array $params ) {
223 $status = Status::newGood();
224
225 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
226 if ( $srcRel === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
228 return $status;
229 }
230
231 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
232 if ( $dstRel === null ) {
233 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
234 return $status;
235 }
236
237 // (a) Check the source/destination containers and destination object
238 try {
239 $sContObj = $this->getContainer( $srcCont );
240 $dContObj = $this->getContainer( $dstCont );
241 if ( empty( $params['overwrite'] ) ) {
242 $destObj = $dContObj->create_object( $dstRel );
243 // Check if the object already exists (fields populated)
244 if ( $destObj->last_modified ) {
245 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
246 return $status;
247 }
248 }
249 } catch ( NoSuchContainerException $e ) {
250 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
251 return $status;
252 } catch ( InvalidResponseException $e ) {
253 $status->fatal( 'backend-fail-connect', $this->name );
254 return $status;
255 } catch ( Exception $e ) { // some other exception?
256 $status->fatal( 'backend-fail-internal', $this->name );
257 $this->logException( $e, __METHOD__, $params );
258 return $status;
259 }
260
261 // (b) Actually copy the file to the destination
262 try {
263 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
264 } catch ( NoSuchObjectException $e ) { // source object does not exist
265 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
266 } catch ( InvalidResponseException $e ) {
267 $status->fatal( 'backend-fail-connect', $this->name );
268 } catch ( Exception $e ) { // some other exception?
269 $status->fatal( 'backend-fail-internal', $this->name );
270 $this->logException( $e, __METHOD__, $params );
271 }
272
273 return $status;
274 }
275
276 /**
277 * @see FileBackend::doDeleteInternal()
278 */
279 protected function doDeleteInternal( array $params ) {
280 $status = Status::newGood();
281
282 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
283 if ( $srcRel === null ) {
284 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
285 return $status;
286 }
287
288 try {
289 $sContObj = $this->getContainer( $srcCont );
290 $sContObj->delete_object( $srcRel );
291 } catch ( NoSuchContainerException $e ) {
292 $status->fatal( 'backend-fail-delete', $params['src'] );
293 } catch ( NoSuchObjectException $e ) {
294 if ( empty( $params['ignoreMissingSource'] ) ) {
295 $status->fatal( 'backend-fail-delete', $params['src'] );
296 }
297 } catch ( InvalidResponseException $e ) {
298 $status->fatal( 'backend-fail-connect', $this->name );
299 } catch ( Exception $e ) { // some other exception?
300 $status->fatal( 'backend-fail-internal', $this->name );
301 $this->logException( $e, __METHOD__, $params );
302 }
303
304 return $status;
305 }
306
307 /**
308 * @see FileBackend::doPrepareInternal()
309 */
310 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
311 $status = Status::newGood();
312
313 // (a) Check if container already exists
314 try {
315 $contObj = $this->getContainer( $fullCont );
316 // NoSuchContainerException not thrown: container must exist
317 return $status; // already exists
318 } catch ( NoSuchContainerException $e ) {
319 // NoSuchContainerException thrown: container does not exist
320 } catch ( InvalidResponseException $e ) {
321 $status->fatal( 'backend-fail-connect', $this->name );
322 return $status;
323 } catch ( Exception $e ) { // some other exception?
324 $status->fatal( 'backend-fail-internal', $this->name );
325 $this->logException( $e, __METHOD__, $params );
326 return $status;
327 }
328
329 // (b) Create container as needed
330 try {
331 $contObj = $this->createContainer( $fullCont );
332 if ( $this->swiftAnonUser != '' ) {
333 // Make container public to end-users...
334 $status->merge( $this->setContainerAccess(
335 $contObj,
336 array( $this->auth->username, $this->swiftAnonUser ), // read
337 array( $this->auth->username ) // write
338 ) );
339 }
340 } catch ( InvalidResponseException $e ) {
341 $status->fatal( 'backend-fail-connect', $this->name );
342 return $status;
343 } catch ( Exception $e ) { // some other exception?
344 $status->fatal( 'backend-fail-internal', $this->name );
345 $this->logException( $e, __METHOD__, $params );
346 return $status;
347 }
348
349 return $status;
350 }
351
352 /**
353 * @see FileBackend::doSecureInternal()
354 */
355 protected function doSecureInternal( $fullCont, $dir, array $params ) {
356 $status = Status::newGood();
357
358 if ( $this->swiftAnonUser != '' ) {
359 // Restrict container from end-users...
360 try {
361 // doPrepareInternal() should have been called,
362 // so the Swift container should already exist...
363 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
364 // NoSuchContainerException not thrown: container must exist
365 if ( !isset( $contObj->mw_wasSecured ) ) {
366 $status->merge( $this->setContainerAccess(
367 $contObj,
368 array( $this->auth->username ), // read
369 array( $this->auth->username ) // write
370 ) );
371 // @TODO: when php-cloudfiles supports container
372 // metadata, we can make use of that to avoid RTTs
373 $contObj->mw_wasSecured = true; // avoid useless RTTs
374 }
375 } catch ( InvalidResponseException $e ) {
376 $status->fatal( 'backend-fail-connect', $this->name );
377 } catch ( Exception $e ) { // some other exception?
378 $status->fatal( 'backend-fail-internal', $this->name );
379 $this->logException( $e, __METHOD__, $params );
380 }
381 }
382
383 return $status;
384 }
385
386 /**
387 * @see FileBackend::doCleanInternal()
388 */
389 protected function doCleanInternal( $fullCont, $dir, array $params ) {
390 $status = Status::newGood();
391
392 // Only containers themselves can be removed, all else is virtual
393 if ( $dir != '' ) {
394 return $status; // nothing to do
395 }
396
397 // (a) Check the container
398 try {
399 $contObj = $this->getContainer( $fullCont, true );
400 } catch ( NoSuchContainerException $e ) {
401 return $status; // ok, nothing to do
402 } catch ( InvalidResponseException $e ) {
403 $status->fatal( 'backend-fail-connect', $this->name );
404 return $status;
405 } catch ( Exception $e ) { // some other exception?
406 $status->fatal( 'backend-fail-internal', $this->name );
407 $this->logException( $e, __METHOD__, $params );
408 return $status;
409 }
410
411 // (b) Delete the container if empty
412 if ( $contObj->object_count == 0 ) {
413 try {
414 $this->deleteContainer( $fullCont );
415 } catch ( NoSuchContainerException $e ) {
416 return $status; // race?
417 } catch ( InvalidResponseException $e ) {
418 $status->fatal( 'backend-fail-connect', $this->name );
419 return $status;
420 } catch ( Exception $e ) { // some other exception?
421 $status->fatal( 'backend-fail-internal', $this->name );
422 $this->logException( $e, __METHOD__, $params );
423 return $status;
424 }
425 }
426
427 return $status;
428 }
429
430 /**
431 * @see FileBackend::doFileExists()
432 */
433 protected function doGetFileStat( array $params ) {
434 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
435 if ( $srcRel === null ) {
436 return false; // invalid storage path
437 }
438
439 $stat = false;
440 try {
441 $contObj = $this->getContainer( $srcCont );
442 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
443 $stat = array(
444 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
445 'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
446 'size' => $srcObj->content_length,
447 'sha1' => $srcObj->metadata['Sha1base36']
448 );
449 } catch ( NoSuchContainerException $e ) {
450 } catch ( NoSuchObjectException $e ) {
451 } catch ( InvalidResponseException $e ) {
452 $stat = null;
453 } catch ( Exception $e ) { // some other exception?
454 $stat = null;
455 $this->logException( $e, __METHOD__, $params );
456 }
457
458 return $stat;
459 }
460
461 /**
462 * @see FileBackendBase::getFileContents()
463 */
464 public function getFileContents( array $params ) {
465 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
466 if ( $srcRel === null ) {
467 return false; // invalid storage path
468 }
469
470 $data = false;
471 try {
472 $container = $this->getContainer( $srcCont );
473 $obj = $container->get_object( $srcRel );
474 $data = $obj->read( $this->headersFromParams( $params ) );
475 } catch ( NoSuchContainerException $e ) {
476 } catch ( NoSuchObjectException $e ) {
477 } catch ( InvalidResponseException $e ) {
478 } catch ( Exception $e ) { // some other exception?
479 $this->logException( $e, __METHOD__, $params );
480 }
481
482 return $data;
483 }
484
485 /**
486 * @see FileBackend::getFileListInternal()
487 */
488 public function getFileListInternal( $fullCont, $dir, array $params ) {
489 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
490 }
491
492 /**
493 * Do not call this function outside of SwiftFileBackendFileList
494 *
495 * @param $fullCont string Resolved container name
496 * @param $dir string Resolved storage directory with no trailing slash
497 * @param $after string Storage path of file to list items after
498 * @param $limit integer Max number of items to list
499 * @return Array
500 */
501 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
502 $files = array();
503
504 try {
505 $container = $this->getContainer( $fullCont );
506 $prefix = ( $dir == '' ) ? null : "{$dir}/";
507 $files = $container->list_objects( $limit, $after, $prefix );
508 } catch ( NoSuchContainerException $e ) {
509 } catch ( NoSuchObjectException $e ) {
510 } catch ( InvalidResponseException $e ) {
511 } catch ( Exception $e ) { // some other exception?
512 $this->logException( $e, __METHOD__, array( 'cont' => $fullCont, 'dir' => $dir ) );
513 }
514
515 return $files;
516 }
517
518 /**
519 * @see FileBackend::doGetFileSha1base36()
520 */
521 public function doGetFileSha1base36( array $params ) {
522 $stat = $this->getFileStat( $params );
523 if ( $stat ) {
524 return $stat['sha1'];
525 } else {
526 return false;
527 }
528 }
529
530 /**
531 * @see FileBackend::doStreamFile()
532 */
533 protected function doStreamFile( array $params ) {
534 $status = Status::newGood();
535
536 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
537 if ( $srcRel === null ) {
538 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
539 }
540
541 try {
542 $cont = $this->getContainer( $srcCont );
543 } catch ( NoSuchContainerException $e ) {
544 $status->fatal( 'backend-fail-stream', $params['src'] );
545 return $status;
546 } catch ( InvalidResponseException $e ) {
547 $status->fatal( 'backend-fail-connect', $this->name );
548 return $status;
549 } catch ( Exception $e ) { // some other exception?
550 $status->fatal( 'backend-fail-stream', $params['src'] );
551 $this->logException( $e, __METHOD__, $params );
552 return $status;
553 }
554
555 try {
556 $output = fopen( 'php://output', 'w' );
557 // FileBackend::streamFile() already checks existence
558 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
559 $obj->stream( $output, $this->headersFromParams( $params ) );
560 } catch ( InvalidResponseException $e ) { // 404? connection problem?
561 $status->fatal( 'backend-fail-stream', $params['src'] );
562 } catch ( Exception $e ) { // some other exception?
563 $status->fatal( 'backend-fail-stream', $params['src'] );
564 $this->logException( $e, __METHOD__, $params );
565 }
566
567 return $status;
568 }
569
570 /**
571 * @see FileBackend::getLocalCopy()
572 */
573 public function getLocalCopy( array $params ) {
574 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
575 if ( $srcRel === null ) {
576 return null;
577 }
578
579 $tmpFile = null;
580 try {
581 $cont = $this->getContainer( $srcCont );
582 $obj = $cont->get_object( $srcRel );
583 // Get source file extension
584 $ext = FileBackend::extensionFromPath( $srcRel );
585 // Create a new temporary file...
586 $tmpFile = TempFSFile::factory( wfBaseName( $srcRel ) . '_', $ext );
587 if ( $tmpFile ) {
588 $handle = fopen( $tmpFile->getPath(), 'w' );
589 if ( $handle ) {
590 $obj->stream( $handle, $this->headersFromParams( $params ) );
591 fclose( $handle );
592 } else {
593 $tmpFile = null; // couldn't open temp file
594 }
595 }
596 } catch ( NoSuchContainerException $e ) {
597 $tmpFile = null;
598 } catch ( NoSuchObjectException $e ) {
599 $tmpFile = null;
600 } catch ( InvalidResponseException $e ) {
601 $tmpFile = null;
602 } catch ( Exception $e ) { // some other exception?
603 $tmpFile = null;
604 $this->logException( $e, __METHOD__, $params );
605 }
606
607 return $tmpFile;
608 }
609
610 /**
611 * Get headers to send to Swift when reading a file based
612 * on a FileBackend params array, e.g. that of getLocalCopy().
613 * $params is currently only checked for a 'latest' flag.
614 *
615 * @param $params Array
616 * @return Array
617 */
618 protected function headersFromParams( array $params ) {
619 $hdrs = array();
620 if ( !empty( $params['latest'] ) ) {
621 $hdrs[] = 'X-Newest: true';
622 }
623 return $hdrs;
624 }
625
626 /**
627 * Set read/write permissions for a Swift container
628 *
629 * @param $contObj CF_Container Swift container
630 * @param $readGrps Array Swift users who can read (account:user)
631 * @param $writeGrps Array Swift users who can write (account:user)
632 * @return Status
633 */
634 protected function setContainerAccess(
635 CF_Container $contObj, array $readGrps, array $writeGrps
636 ) {
637 $creds = $contObj->cfs_auth->export_credentials();
638
639 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
640
641 // Note: 10 second timeout consistent with php-cloudfiles
642 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
643 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
644 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
645 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
646
647 return $req->execute(); // should return 204
648 }
649
650 /**
651 * Get a connection to the Swift proxy
652 *
653 * @return CF_Connection|false
654 * @throws InvalidResponseException
655 */
656 protected function getConnection() {
657 if ( $this->conn === false ) {
658 return false; // failed last attempt
659 }
660 // Session keys expire after a while, so we renew them periodically
661 if ( $this->conn && ( time() - $this->connStarted ) > $this->authTTL ) {
662 $this->conn->close(); // close active cURL connections
663 $this->conn = null;
664 }
665 // Authenticate with proxy and get a session key...
666 if ( $this->conn === null ) {
667 $this->connContainers = array();
668 try {
669 $this->auth->authenticate();
670 $this->conn = new CF_Connection( $this->auth );
671 $this->connStarted = time();
672 } catch ( AuthenticationException $e ) {
673 $this->conn = false; // don't keep re-trying
674 } catch ( InvalidResponseException $e ) {
675 $this->conn = false; // don't keep re-trying
676 }
677 }
678 if ( !$this->conn ) {
679 throw new InvalidResponseException; // auth/connection problem
680 }
681 return $this->conn;
682 }
683
684 /**
685 * @see FileBackend::doClearCache()
686 */
687 protected function doClearCache( array $paths = null ) {
688 $this->connContainers = array(); // clear container object cache
689 }
690
691 /**
692 * Get a Swift container object, possibly from process cache.
693 * Use $reCache if the file count or byte count is needed.
694 *
695 * @param $container string Container name
696 * @param $reCache bool Refresh the process cache
697 * @return CF_Container
698 */
699 protected function getContainer( $container, $reCache = false ) {
700 $conn = $this->getConnection(); // Swift proxy connection
701 if ( $reCache ) {
702 unset( $this->connContainers[$container] ); // purge cache
703 }
704 if ( !isset( $this->connContainers[$container] ) ) {
705 $contObj = $conn->get_container( $container );
706 // NoSuchContainerException not thrown: container must exist
707 if ( count( $this->connContainers ) >= $this->maxContCacheSize ) { // trim cache?
708 reset( $this->connContainers );
709 $key = key( $this->connContainers );
710 unset( $this->connContainers[$key] );
711 }
712 $this->connContainers[$container] = $contObj; // cache it
713 }
714 return $this->connContainers[$container];
715 }
716
717 /**
718 * Create a Swift container
719 *
720 * @param $container string Container name
721 * @return CF_Container
722 */
723 protected function createContainer( $container ) {
724 $conn = $this->getConnection(); // Swift proxy connection
725 $contObj = $conn->create_container( $container );
726 $this->connContainers[$container] = $contObj; // cache it
727 return $contObj;
728 }
729
730 /**
731 * Delete a Swift container
732 *
733 * @param $container string Container name
734 * @return void
735 */
736 protected function deleteContainer( $container ) {
737 $conn = $this->getConnection(); // Swift proxy connection
738 $conn->delete_container( $container );
739 unset( $this->connContainers[$container] ); // purge cache
740 }
741
742 /**
743 * Log an unexpected exception for this backend
744 *
745 * @param $e Exception
746 * @param $func string
747 * @param $params Array
748 * @return void
749 */
750 protected function logException( Exception $e, $func, array $params ) {
751 wfDebugLog( 'SwiftBackend',
752 get_class( $e ) . " in '{$this->name}': '{$func}' with " . serialize( $params )
753 );
754 }
755 }
756
757 /**
758 * SwiftFileBackend helper class to page through object listings.
759 * Swift also has a listing limit of 10,000 objects for sanity.
760 * Do not use this class from places outside SwiftFileBackend.
761 *
762 * @ingroup FileBackend
763 */
764 class SwiftFileBackendFileList implements Iterator {
765 /** @var Array */
766 protected $bufferIter = array();
767 protected $bufferAfter = null; // string; list items *after* this path
768 protected $pos = 0; // integer
769
770 /** @var SwiftFileBackend */
771 protected $backend;
772 protected $container; //
773 protected $dir; // string storage directory
774 protected $suffixStart; // integer
775
776 const PAGE_SIZE = 5000; // file listing buffer size
777
778 /**
779 * @param $backend SwiftFileBackend
780 * @param $fullCont string Resolved container name
781 * @param $dir string Resolved directory relative to container
782 */
783 public function __construct( SwiftFileBackend $backend, $fullCont, $dir ) {
784 $this->backend = $backend;
785 $this->container = $fullCont;
786 $this->dir = $dir;
787 if ( substr( $this->dir, -1 ) === '/' ) {
788 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
789 }
790 if ( $this->dir == '' ) { // whole container
791 $this->suffixStart = 0;
792 } else { // dir within container
793 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
794 }
795 }
796
797 public function current() {
798 return substr( current( $this->bufferIter ), $this->suffixStart );
799 }
800
801 public function key() {
802 return $this->pos;
803 }
804
805 public function next() {
806 // Advance to the next file in the page
807 next( $this->bufferIter );
808 ++$this->pos;
809 // Check if there are no files left in this page and
810 // advance to the next page if this page was not empty.
811 if ( !$this->valid() && count( $this->bufferIter ) ) {
812 $this->bufferAfter = end( $this->bufferIter );
813 $this->bufferIter = $this->backend->getFileListPageInternal(
814 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE
815 );
816 }
817 }
818
819 public function rewind() {
820 $this->pos = 0;
821 $this->bufferAfter = null;
822 $this->bufferIter = $this->backend->getFileListPageInternal(
823 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE
824 );
825 }
826
827 public function valid() {
828 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
829 }
830 }