10 * @brief Class for an OpenStack Swift based file backend.
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.
16 * Status messages should avoid mentioning the Swift account name.
17 * Likewise, error suppression should be used to avoid path disclosure.
19 * @ingroup FileBackend
22 class SwiftFileBackend
extends FileBackendStore
{
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 = 100; // integer; max containers with entries
29 /** @var CF_Connection */
30 protected $conn; // Swift connection handle
31 protected $connStarted = 0; // integer UNIX timestamp
32 protected $connContainers = array(); // container object cache
35 * @see FileBackendStore::__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 sharding config with:
43 * 'base' : base of hash characters, 16 or 36
44 * 'levels' : the number of hash levels (and digits)
45 * 'repeat' : hash subdirectories are prefixed with all the
46 * parent hash directory names (e.g. "a/ab/abc")
48 public function __construct( array $config ) {
49 parent
::__construct( $config );
51 $this->auth
= new CF_Authentication(
54 null, // account; unused
55 $config['swiftAuthUrl']
58 $this->authTTL
= isset( $config['swiftAuthTTL'] )
59 ?
$config['swiftAuthTTL']
60 : 120; // some sane number
61 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
62 ?
$config['swiftAnonUser']
64 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
65 ?
$config['shardViaHashLevels']
67 // Cache container info to mask latency
68 $this->memCache
= wfGetMainCache();
72 * @see FileBackendStore::resolveContainerPath()
75 protected function resolveContainerPath( $container, $relStoragePath ) {
76 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
77 return null; // too long for Swift
79 return $relStoragePath;
83 * @see FileBackendStore::isPathUsableInternal()
86 public function isPathUsableInternal( $storagePath ) {
87 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
88 if ( $rel === null ) {
89 return false; // invalid
93 $this->getContainer( $container );
94 return true; // container exists
95 } catch ( NoSuchContainerException
$e ) {
96 } catch ( InvalidResponseException
$e ) {
97 } catch ( Exception
$e ) { // some other exception?
98 $this->logException( $e, __METHOD__
, array( 'path' => $storagePath ) );
105 * @see FileBackendStore::doCreateInternal()
108 protected function doCreateInternal( array $params ) {
109 $status = Status
::newGood();
111 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
112 if ( $dstRel === null ) {
113 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
117 // (a) Check the destination container and object
119 $dContObj = $this->getContainer( $dstCont );
120 if ( empty( $params['overwrite'] ) &&
121 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
123 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
126 } catch ( NoSuchContainerException
$e ) {
127 $status->fatal( 'backend-fail-create', $params['dst'] );
129 } catch ( InvalidResponseException
$e ) {
130 $status->fatal( 'backend-fail-connect', $this->name
);
132 } catch ( Exception
$e ) { // some other exception?
133 $status->fatal( 'backend-fail-internal', $this->name
);
134 $this->logException( $e, __METHOD__
, $params );
138 // (b) Get a SHA-1 hash of the object
139 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
141 // (c) Actually create the object
143 // Create a fresh CF_Object with no fields preloaded.
144 // We don't want to preserve headers, metadata, and such.
145 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
146 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
147 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
148 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
149 // The MD5 here will be checked within Swift against its own MD5.
150 $obj->set_etag( md5( $params['content'] ) );
151 // Use the same content type as StreamFile for security
152 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
153 // Actually write the object in Swift
154 $obj->write( $params['content'] );
155 } catch ( BadContentTypeException
$e ) {
156 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
157 } catch ( InvalidResponseException
$e ) {
158 $status->fatal( 'backend-fail-connect', $this->name
);
159 } catch ( Exception
$e ) { // some other exception?
160 $status->fatal( 'backend-fail-internal', $this->name
);
161 $this->logException( $e, __METHOD__
, $params );
168 * @see FileBackendStore::doStoreInternal()
171 protected function doStoreInternal( array $params ) {
172 $status = Status
::newGood();
174 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
175 if ( $dstRel === null ) {
176 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
180 // (a) Check the destination container and object
182 $dContObj = $this->getContainer( $dstCont );
183 if ( empty( $params['overwrite'] ) &&
184 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
186 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
189 } catch ( NoSuchContainerException
$e ) {
190 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
192 } catch ( InvalidResponseException
$e ) {
193 $status->fatal( 'backend-fail-connect', $this->name
);
195 } catch ( Exception
$e ) { // some other exception?
196 $status->fatal( 'backend-fail-internal', $this->name
);
197 $this->logException( $e, __METHOD__
, $params );
201 // (b) Get a SHA-1 hash of the object
202 $sha1Hash = sha1_file( $params['src'] );
203 if ( $sha1Hash === false ) { // source doesn't exist?
204 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
207 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
209 // (c) Actually store the object
211 // Create a fresh CF_Object with no fields preloaded.
212 // We don't want to preserve headers, metadata, and such.
213 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
214 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
215 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
216 // The MD5 here will be checked within Swift against its own MD5.
217 $obj->set_etag( md5_file( $params['src'] ) );
218 // Use the same content type as StreamFile for security
219 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
220 // Actually write the object in Swift
221 $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
222 } catch ( BadContentTypeException
$e ) {
223 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
224 } catch ( IOException
$e ) {
225 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
226 } catch ( InvalidResponseException
$e ) {
227 $status->fatal( 'backend-fail-connect', $this->name
);
228 } catch ( Exception
$e ) { // some other exception?
229 $status->fatal( 'backend-fail-internal', $this->name
);
230 $this->logException( $e, __METHOD__
, $params );
237 * @see FileBackendStore::doCopyInternal()
240 protected function doCopyInternal( array $params ) {
241 $status = Status
::newGood();
243 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
244 if ( $srcRel === null ) {
245 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
249 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
250 if ( $dstRel === null ) {
251 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
255 // (a) Check the source/destination containers and destination object
257 $sContObj = $this->getContainer( $srcCont );
258 $dContObj = $this->getContainer( $dstCont );
259 if ( empty( $params['overwrite'] ) &&
260 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
262 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
265 } catch ( NoSuchContainerException
$e ) {
266 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
268 } catch ( InvalidResponseException
$e ) {
269 $status->fatal( 'backend-fail-connect', $this->name
);
271 } catch ( Exception
$e ) { // some other exception?
272 $status->fatal( 'backend-fail-internal', $this->name
);
273 $this->logException( $e, __METHOD__
, $params );
277 // (b) Actually copy the file to the destination
279 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
280 } catch ( NoSuchObjectException
$e ) { // source object does not exist
281 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
282 } catch ( InvalidResponseException
$e ) {
283 $status->fatal( 'backend-fail-connect', $this->name
);
284 } catch ( Exception
$e ) { // some other exception?
285 $status->fatal( 'backend-fail-internal', $this->name
);
286 $this->logException( $e, __METHOD__
, $params );
293 * @see FileBackendStore::doDeleteInternal()
296 protected function doDeleteInternal( array $params ) {
297 $status = Status
::newGood();
299 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
300 if ( $srcRel === null ) {
301 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
306 $sContObj = $this->getContainer( $srcCont );
307 $sContObj->delete_object( $srcRel );
308 } catch ( NoSuchContainerException
$e ) {
309 $status->fatal( 'backend-fail-delete', $params['src'] );
310 } catch ( NoSuchObjectException
$e ) {
311 if ( empty( $params['ignoreMissingSource'] ) ) {
312 $status->fatal( 'backend-fail-delete', $params['src'] );
314 } catch ( InvalidResponseException
$e ) {
315 $status->fatal( 'backend-fail-connect', $this->name
);
316 } catch ( Exception
$e ) { // some other exception?
317 $status->fatal( 'backend-fail-internal', $this->name
);
318 $this->logException( $e, __METHOD__
, $params );
325 * @see FileBackendStore::doPrepareInternal()
328 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
329 $status = Status
::newGood();
331 // (a) Check if container already exists
333 $contObj = $this->getContainer( $fullCont );
334 // NoSuchContainerException not thrown: container must exist
335 return $status; // already exists
336 } catch ( NoSuchContainerException
$e ) {
337 // NoSuchContainerException thrown: container does not exist
338 } catch ( InvalidResponseException
$e ) {
339 $status->fatal( 'backend-fail-connect', $this->name
);
341 } catch ( Exception
$e ) { // some other exception?
342 $status->fatal( 'backend-fail-internal', $this->name
);
343 $this->logException( $e, __METHOD__
, $params );
347 // (b) Create container as needed
349 $contObj = $this->createContainer( $fullCont );
350 if ( $this->swiftAnonUser
!= '' ) {
351 // Make container public to end-users...
352 $status->merge( $this->setContainerAccess(
354 array( $this->auth
->username
, $this->swiftAnonUser
), // read
355 array( $this->auth
->username
) // write
358 } catch ( InvalidResponseException
$e ) {
359 $status->fatal( 'backend-fail-connect', $this->name
);
361 } catch ( Exception
$e ) { // some other exception?
362 $status->fatal( 'backend-fail-internal', $this->name
);
363 $this->logException( $e, __METHOD__
, $params );
371 * @see FileBackendStore::doSecureInternal()
374 protected function doSecureInternal( $fullCont, $dir, array $params ) {
375 $status = Status
::newGood();
377 if ( $this->swiftAnonUser
!= '' ) {
378 // Restrict container from end-users...
380 // doPrepareInternal() should have been called,
381 // so the Swift container should already exist...
382 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
383 // NoSuchContainerException not thrown: container must exist
384 if ( !isset( $contObj->mw_wasSecured
) ) {
385 $status->merge( $this->setContainerAccess(
387 array( $this->auth
->username
), // read
388 array( $this->auth
->username
) // write
390 // @TODO: when php-cloudfiles supports container
391 // metadata, we can make use of that to avoid RTTs
392 $contObj->mw_wasSecured
= true; // avoid useless RTTs
394 } catch ( InvalidResponseException
$e ) {
395 $status->fatal( 'backend-fail-connect', $this->name
);
396 } catch ( Exception
$e ) { // some other exception?
397 $status->fatal( 'backend-fail-internal', $this->name
);
398 $this->logException( $e, __METHOD__
, $params );
406 * @see FileBackendStore::doCleanInternal()
409 protected function doCleanInternal( $fullCont, $dir, array $params ) {
410 $status = Status
::newGood();
412 // Only containers themselves can be removed, all else is virtual
414 return $status; // nothing to do
417 // (a) Check the container
419 $contObj = $this->getContainer( $fullCont, true );
420 } catch ( NoSuchContainerException
$e ) {
421 return $status; // ok, nothing to do
422 } catch ( InvalidResponseException
$e ) {
423 $status->fatal( 'backend-fail-connect', $this->name
);
425 } catch ( Exception
$e ) { // some other exception?
426 $status->fatal( 'backend-fail-internal', $this->name
);
427 $this->logException( $e, __METHOD__
, $params );
431 // (b) Delete the container if empty
432 if ( $contObj->object_count
== 0 ) {
434 $this->deleteContainer( $fullCont );
435 } catch ( NoSuchContainerException
$e ) {
436 return $status; // race?
437 } catch ( InvalidResponseException
$e ) {
438 $status->fatal( 'backend-fail-connect', $this->name
);
440 } catch ( Exception
$e ) { // some other exception?
441 $status->fatal( 'backend-fail-internal', $this->name
);
442 $this->logException( $e, __METHOD__
, $params );
451 * @see FileBackendStore::doFileExists()
452 * @return array|bool|null
454 protected function doGetFileStat( array $params ) {
455 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
456 if ( $srcRel === null ) {
457 return false; // invalid storage path
462 $contObj = $this->getContainer( $srcCont );
463 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
464 $this->addMissingMetadata( $srcObj, $params['src'] );
466 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
467 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
468 'size' => $srcObj->content_length
,
469 'sha1' => $srcObj->metadata
['Sha1base36']
471 } catch ( NoSuchContainerException
$e ) {
472 } catch ( NoSuchObjectException
$e ) {
473 } catch ( InvalidResponseException
$e ) {
475 } catch ( Exception
$e ) { // some other exception?
477 $this->logException( $e, __METHOD__
, $params );
484 * Fill in any missing object metadata and save it to Swift
486 * @param $obj CF_Object
487 * @param $path string Storage path to object
488 * @return bool Success
489 * @throws Exception cloudfiles exceptions
491 protected function addMissingMetadata( CF_Object
$obj, $path ) {
492 if ( isset( $obj->metadata
['Sha1base36'] ) ) {
493 return true; // nothing to do
495 $status = Status
::newGood();
496 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
497 if ( $status->isOK() ) {
498 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
500 $hash = $tmpFile->getSha1Base36();
501 if ( $hash !== false ) {
502 $obj->metadata
['Sha1base36'] = $hash;
503 $obj->sync_metadata(); // save to Swift
504 return true; // success
508 $obj->metadata
['Sha1base36'] = false;
509 return false; // failed
513 * @see FileBackend::getFileContents()
514 * @return bool|null|string
516 public function getFileContents( array $params ) {
517 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
518 if ( $srcRel === null ) {
519 return false; // invalid storage path
522 if ( !$this->fileExists( $params ) ) {
528 $sContObj = $this->getContainer( $srcCont );
529 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD request
530 $data = $obj->read( $this->headersFromParams( $params ) );
531 } catch ( NoSuchContainerException
$e ) {
532 } catch ( InvalidResponseException
$e ) {
533 } catch ( Exception
$e ) { // some other exception?
534 $this->logException( $e, __METHOD__
, $params );
541 * @see FileBackendStore::getFileListInternal()
542 * @return SwiftFileBackendFileList
544 public function getFileListInternal( $fullCont, $dir, array $params ) {
545 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
549 * Do not call this function outside of SwiftFileBackendFileList
551 * @param $fullCont string Resolved container name
552 * @param $dir string Resolved storage directory with no trailing slash
553 * @param $after string Storage path of file to list items after
554 * @param $limit integer Max number of items to list
557 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
561 $container = $this->getContainer( $fullCont );
562 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
563 $files = $container->list_objects( $limit, $after, $prefix );
564 } catch ( NoSuchContainerException
$e ) {
565 } catch ( InvalidResponseException
$e ) {
566 } catch ( Exception
$e ) { // some other exception?
567 $this->logException( $e, __METHOD__
, array( 'cont' => $fullCont, 'dir' => $dir ) );
574 * @see FileBackendStore::doGetFileSha1base36()
577 protected function doGetFileSha1base36( array $params ) {
578 $stat = $this->getFileStat( $params );
580 return $stat['sha1'];
587 * @see FileBackendStore::doStreamFile()
590 protected function doStreamFile( array $params ) {
591 $status = Status
::newGood();
593 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
594 if ( $srcRel === null ) {
595 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
599 $cont = $this->getContainer( $srcCont );
600 } catch ( NoSuchContainerException
$e ) {
601 $status->fatal( 'backend-fail-stream', $params['src'] );
603 } catch ( InvalidResponseException
$e ) {
604 $status->fatal( 'backend-fail-connect', $this->name
);
606 } catch ( Exception
$e ) { // some other exception?
607 $status->fatal( 'backend-fail-stream', $params['src'] );
608 $this->logException( $e, __METHOD__
, $params );
613 $output = fopen( 'php://output', 'wb' );
614 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
615 $obj->stream( $output, $this->headersFromParams( $params ) );
616 } catch ( InvalidResponseException
$e ) { // 404? connection problem?
617 $status->fatal( 'backend-fail-stream', $params['src'] );
618 } catch ( Exception
$e ) { // some other exception?
619 $status->fatal( 'backend-fail-stream', $params['src'] );
620 $this->logException( $e, __METHOD__
, $params );
627 * @see FileBackendStore::getLocalCopy()
628 * @return null|TempFSFile
630 public function getLocalCopy( array $params ) {
631 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
632 if ( $srcRel === null ) {
636 if ( !$this->fileExists( $params ) ) {
642 $sContObj = $this->getContainer( $srcCont );
643 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
644 // Get source file extension
645 $ext = FileBackend
::extensionFromPath( $srcRel );
646 // Create a new temporary file...
647 $tmpFile = TempFSFile
::factory( wfBaseName( $srcRel ) . '_', $ext );
649 $handle = fopen( $tmpFile->getPath(), 'wb' );
651 $obj->stream( $handle, $this->headersFromParams( $params ) );
654 $tmpFile = null; // couldn't open temp file
657 } catch ( NoSuchContainerException
$e ) {
659 } catch ( InvalidResponseException
$e ) {
661 } catch ( Exception
$e ) { // some other exception?
663 $this->logException( $e, __METHOD__
, $params );
670 * Get headers to send to Swift when reading a file based
671 * on a FileBackend params array, e.g. that of getLocalCopy().
672 * $params is currently only checked for a 'latest' flag.
674 * @param $params Array
677 protected function headersFromParams( array $params ) {
679 if ( !empty( $params['latest'] ) ) {
680 $hdrs[] = 'X-Newest: true';
686 * Set read/write permissions for a Swift container
688 * @param $contObj CF_Container Swift container
689 * @param $readGrps Array Swift users who can read (account:user)
690 * @param $writeGrps Array Swift users who can write (account:user)
693 protected function setContainerAccess(
694 CF_Container
$contObj, array $readGrps, array $writeGrps
696 $creds = $contObj->cfs_auth
->export_credentials();
698 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
700 // Note: 10 second timeout consistent with php-cloudfiles
701 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
702 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
703 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
704 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
706 return $req->execute(); // should return 204
710 * Get a connection to the Swift proxy
712 * @return CF_Connection|bool False on failure
713 * @throws InvalidResponseException
715 protected function getConnection() {
716 if ( $this->conn
=== false ) {
717 throw new InvalidResponseException
; // failed last attempt
719 // Session keys expire after a while, so we renew them periodically
720 if ( $this->conn
&& ( time() - $this->connStarted
) > $this->authTTL
) {
721 $this->conn
->close(); // close active cURL connections
724 // Authenticate with proxy and get a session key...
725 if ( $this->conn
=== null ) {
726 $this->connContainers
= array();
728 $this->auth
->authenticate();
729 $this->conn
= new CF_Connection( $this->auth
);
730 $this->connStarted
= time();
731 } catch ( AuthenticationException
$e ) {
732 $this->conn
= false; // don't keep re-trying
733 } catch ( InvalidResponseException
$e ) {
734 $this->conn
= false; // don't keep re-trying
737 if ( !$this->conn
) {
738 throw new InvalidResponseException
; // auth/connection problem
744 * @see FileBackendStore::doClearCache()
746 protected function doClearCache( array $paths = null ) {
747 $this->connContainers
= array(); // clear container object cache
751 * Get a Swift container object, possibly from process cache.
752 * Use $reCache if the file count or byte count is needed.
754 * @param $container string Container name
755 * @param $bypassCache bool Bypass all caches and load from Swift
756 * @return CF_Container
757 * @throws InvalidResponseException
759 protected function getContainer( $container, $bypassCache = false ) {
760 $conn = $this->getConnection(); // Swift proxy connection
761 if ( $bypassCache ) { // purge cache
762 unset( $this->connContainers
[$container] );
763 } elseif ( !isset( $this->connContainers
[$container] ) ) {
764 $this->primeContainerCache( array( $container ) ); // check persistent cache
766 if ( !isset( $this->connContainers
[$container] ) ) {
767 $contObj = $conn->get_container( $container );
768 // NoSuchContainerException not thrown: container must exist
769 if ( count( $this->connContainers
) >= $this->maxContCacheSize
) { // trim cache?
770 reset( $this->connContainers
);
771 unset( $this->connContainers
[key( $this->connContainers
)] );
773 $this->connContainers
[$container] = $contObj; // cache it
774 if ( !$bypassCache ) {
775 $this->setContainerCache( $container, // update persistent cache
776 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
780 return $this->connContainers
[$container];
784 * Create a Swift container
786 * @param $container string Container name
787 * @return CF_Container
788 * @throws InvalidResponseException
790 protected function createContainer( $container ) {
791 $conn = $this->getConnection(); // Swift proxy connection
792 $contObj = $conn->create_container( $container );
793 $this->connContainers
[$container] = $contObj; // cache it
798 * Delete a Swift container
800 * @param $container string Container name
802 * @throws InvalidResponseException
804 protected function deleteContainer( $container ) {
805 $conn = $this->getConnection(); // Swift proxy connection
806 $conn->delete_container( $container );
807 unset( $this->connContainers
[$container] ); // purge cache
811 * @see FileBackendStore::doPrimeContainerCache()
814 protected function doPrimeContainerCache( array $containerInfo ) {
816 $conn = $this->getConnection(); // Swift proxy connection
817 foreach ( $containerInfo as $container => $info ) {
818 $this->connContainers
[$container] = new CF_Container(
826 } catch ( InvalidResponseException
$e ) {
827 } catch ( Exception
$e ) { // some other exception?
828 $this->logException( $e, __METHOD__
, array() );
833 * Log an unexpected exception for this backend
835 * @param $e Exception
836 * @param $func string
837 * @param $params Array
840 protected function logException( Exception
$e, $func, array $params ) {
841 wfDebugLog( 'SwiftBackend',
842 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
843 ( $e instanceof InvalidResponseException
844 ?
": {$e->getMessage()}"
852 * SwiftFileBackend helper class to page through object listings.
853 * Swift also has a listing limit of 10,000 objects for sanity.
854 * Do not use this class from places outside SwiftFileBackend.
856 * @ingroup FileBackend
858 class SwiftFileBackendFileList
implements Iterator
{
860 protected $bufferIter = array();
861 protected $bufferAfter = null; // string; list items *after* this path
862 protected $pos = 0; // integer
864 /** @var SwiftFileBackend */
866 protected $container; //
867 protected $dir; // string storage directory
868 protected $suffixStart; // integer
870 const PAGE_SIZE
= 5000; // file listing buffer size
873 * @param $backend SwiftFileBackend
874 * @param $fullCont string Resolved container name
875 * @param $dir string Resolved directory relative to container
877 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir ) {
878 $this->backend
= $backend;
879 $this->container
= $fullCont;
881 if ( substr( $this->dir
, -1 ) === '/' ) {
882 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
884 if ( $this->dir
== '' ) { // whole container
885 $this->suffixStart
= 0;
886 } else { // dir within container
887 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
892 * @see Iterator::current()
893 * @return string|bool String or false
895 public function current() {
896 return substr( current( $this->bufferIter
), $this->suffixStart
);
900 * @see Iterator::key()
903 public function key() {
908 * @see Iterator::next()
911 public function next() {
912 // Advance to the next file in the page
913 next( $this->bufferIter
);
915 // Check if there are no files left in this page and
916 // advance to the next page if this page was not empty.
917 if ( !$this->valid() && count( $this->bufferIter
) ) {
918 $this->bufferAfter
= end( $this->bufferIter
);
919 $this->bufferIter
= $this->backend
->getFileListPageInternal(
920 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
926 * @see Iterator::rewind()
929 public function rewind() {
931 $this->bufferAfter
= null;
932 $this->bufferIter
= $this->backend
->getFileListPageInternal(
933 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
938 * @see Iterator::valid()
941 public function valid() {
942 return ( current( $this->bufferIter
) !== false ); // no paths can have this value