10 * 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 = 20; // 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']
70 * @see FileBackendStore::resolveContainerPath()
72 protected function resolveContainerPath( $container, $relStoragePath ) {
73 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
74 return null; // too long for Swift
76 return $relStoragePath;
80 * @see FileBackendStore::isPathUsableInternal()
82 public function isPathUsableInternal( $storagePath ) {
83 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
84 if ( $rel === null ) {
85 return false; // invalid
89 $this->getContainer( $container );
90 return true; // container exists
91 } catch ( NoSuchContainerException
$e ) {
92 } catch ( InvalidResponseException
$e ) {
93 } catch ( Exception
$e ) { // some other exception?
94 $this->logException( $e, __METHOD__
, array( 'path' => $storagePath ) );
101 * @see FileBackendStore::doCopyInternal()
103 protected function doCreateInternal( array $params ) {
104 $status = Status
::newGood();
106 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
107 if ( $dstRel === null ) {
108 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
112 // (a) Check the destination container and object
114 $dContObj = $this->getContainer( $dstCont );
115 if ( empty( $params['overwrite'] ) &&
116 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
118 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
121 } catch ( NoSuchContainerException
$e ) {
122 $status->fatal( 'backend-fail-create', $params['dst'] );
124 } catch ( InvalidResponseException
$e ) {
125 $status->fatal( 'backend-fail-connect', $this->name
);
127 } catch ( Exception
$e ) { // some other exception?
128 $status->fatal( 'backend-fail-internal', $this->name
);
129 $this->logException( $e, __METHOD__
, $params );
133 // (b) Get a SHA-1 hash of the object
134 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
136 // (c) Actually create the object
138 // Create a fresh CF_Object with no fields preloaded.
139 // We don't want to preserve headers, metadata, and such.
140 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
141 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
142 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
143 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
144 // The MD5 here will be checked within Swift against its own MD5.
145 $obj->set_etag( md5( $params['content'] ) );
146 // Use the same content type as StreamFile for security
147 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
148 // Actually write the object in Swift
149 $obj->write( $params['content'] );
150 } catch ( BadContentTypeException
$e ) {
151 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
152 } catch ( InvalidResponseException
$e ) {
153 $status->fatal( 'backend-fail-connect', $this->name
);
154 } catch ( Exception
$e ) { // some other exception?
155 $status->fatal( 'backend-fail-internal', $this->name
);
156 $this->logException( $e, __METHOD__
, $params );
163 * @see FileBackendStore::doStoreInternal()
165 protected function doStoreInternal( array $params ) {
166 $status = Status
::newGood();
168 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
169 if ( $dstRel === null ) {
170 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
174 // (a) Check the destination container and object
176 $dContObj = $this->getContainer( $dstCont );
177 if ( empty( $params['overwrite'] ) &&
178 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
180 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
183 } catch ( NoSuchContainerException
$e ) {
184 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
186 } catch ( InvalidResponseException
$e ) {
187 $status->fatal( 'backend-fail-connect', $this->name
);
189 } catch ( Exception
$e ) { // some other exception?
190 $status->fatal( 'backend-fail-internal', $this->name
);
191 $this->logException( $e, __METHOD__
, $params );
195 // (b) Get a SHA-1 hash of the object
196 $sha1Hash = sha1_file( $params['src'] );
197 if ( $sha1Hash === false ) { // source doesn't exist?
198 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
201 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
203 // (c) Actually store the object
205 // Create a fresh CF_Object with no fields preloaded.
206 // We don't want to preserve headers, metadata, and such.
207 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
208 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
209 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
210 // The MD5 here will be checked within Swift against its own MD5.
211 $obj->set_etag( md5_file( $params['src'] ) );
212 // Use the same content type as StreamFile for security
213 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
214 // Actually write the object in Swift
215 $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
216 } catch ( BadContentTypeException
$e ) {
217 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
218 } catch ( IOException
$e ) {
219 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
220 } catch ( InvalidResponseException
$e ) {
221 $status->fatal( 'backend-fail-connect', $this->name
);
222 } catch ( Exception
$e ) { // some other exception?
223 $status->fatal( 'backend-fail-internal', $this->name
);
224 $this->logException( $e, __METHOD__
, $params );
231 * @see FileBackendStore::doCopyInternal()
233 protected function doCopyInternal( array $params ) {
234 $status = Status
::newGood();
236 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
237 if ( $srcRel === null ) {
238 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
242 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
243 if ( $dstRel === null ) {
244 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
248 // (a) Check the source/destination containers and destination object
250 $sContObj = $this->getContainer( $srcCont );
251 $dContObj = $this->getContainer( $dstCont );
252 if ( empty( $params['overwrite'] ) &&
253 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
255 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
258 } catch ( NoSuchContainerException
$e ) {
259 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
261 } catch ( InvalidResponseException
$e ) {
262 $status->fatal( 'backend-fail-connect', $this->name
);
264 } catch ( Exception
$e ) { // some other exception?
265 $status->fatal( 'backend-fail-internal', $this->name
);
266 $this->logException( $e, __METHOD__
, $params );
270 // (b) Actually copy the file to the destination
272 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
273 } catch ( NoSuchObjectException
$e ) { // source object does not exist
274 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
275 } catch ( InvalidResponseException
$e ) {
276 $status->fatal( 'backend-fail-connect', $this->name
);
277 } catch ( Exception
$e ) { // some other exception?
278 $status->fatal( 'backend-fail-internal', $this->name
);
279 $this->logException( $e, __METHOD__
, $params );
286 * @see FileBackendStore::doDeleteInternal()
288 protected function doDeleteInternal( array $params ) {
289 $status = Status
::newGood();
291 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
292 if ( $srcRel === null ) {
293 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
298 $sContObj = $this->getContainer( $srcCont );
299 $sContObj->delete_object( $srcRel );
300 } catch ( NoSuchContainerException
$e ) {
301 $status->fatal( 'backend-fail-delete', $params['src'] );
302 } catch ( NoSuchObjectException
$e ) {
303 if ( empty( $params['ignoreMissingSource'] ) ) {
304 $status->fatal( 'backend-fail-delete', $params['src'] );
306 } catch ( InvalidResponseException
$e ) {
307 $status->fatal( 'backend-fail-connect', $this->name
);
308 } catch ( Exception
$e ) { // some other exception?
309 $status->fatal( 'backend-fail-internal', $this->name
);
310 $this->logException( $e, __METHOD__
, $params );
317 * @see FileBackendStore::doPrepareInternal()
319 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
320 $status = Status
::newGood();
322 // (a) Check if container already exists
324 $contObj = $this->getContainer( $fullCont );
325 // NoSuchContainerException not thrown: container must exist
326 return $status; // already exists
327 } catch ( NoSuchContainerException
$e ) {
328 // NoSuchContainerException thrown: container does not exist
329 } catch ( InvalidResponseException
$e ) {
330 $status->fatal( 'backend-fail-connect', $this->name
);
332 } catch ( Exception
$e ) { // some other exception?
333 $status->fatal( 'backend-fail-internal', $this->name
);
334 $this->logException( $e, __METHOD__
, $params );
338 // (b) Create container as needed
340 $contObj = $this->createContainer( $fullCont );
341 if ( $this->swiftAnonUser
!= '' ) {
342 // Make container public to end-users...
343 $status->merge( $this->setContainerAccess(
345 array( $this->auth
->username
, $this->swiftAnonUser
), // read
346 array( $this->auth
->username
) // write
349 } catch ( InvalidResponseException
$e ) {
350 $status->fatal( 'backend-fail-connect', $this->name
);
352 } catch ( Exception
$e ) { // some other exception?
353 $status->fatal( 'backend-fail-internal', $this->name
);
354 $this->logException( $e, __METHOD__
, $params );
362 * @see FileBackendStore::doSecureInternal()
364 protected function doSecureInternal( $fullCont, $dir, array $params ) {
365 $status = Status
::newGood();
367 if ( $this->swiftAnonUser
!= '' ) {
368 // Restrict container from end-users...
370 // doPrepareInternal() should have been called,
371 // so the Swift container should already exist...
372 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
373 // NoSuchContainerException not thrown: container must exist
374 if ( !isset( $contObj->mw_wasSecured
) ) {
375 $status->merge( $this->setContainerAccess(
377 array( $this->auth
->username
), // read
378 array( $this->auth
->username
) // write
380 // @TODO: when php-cloudfiles supports container
381 // metadata, we can make use of that to avoid RTTs
382 $contObj->mw_wasSecured
= true; // avoid useless RTTs
384 } catch ( InvalidResponseException
$e ) {
385 $status->fatal( 'backend-fail-connect', $this->name
);
386 } catch ( Exception
$e ) { // some other exception?
387 $status->fatal( 'backend-fail-internal', $this->name
);
388 $this->logException( $e, __METHOD__
, $params );
396 * @see FileBackendStore::doCleanInternal()
398 protected function doCleanInternal( $fullCont, $dir, array $params ) {
399 $status = Status
::newGood();
401 // Only containers themselves can be removed, all else is virtual
403 return $status; // nothing to do
406 // (a) Check the container
408 $contObj = $this->getContainer( $fullCont, true );
409 } catch ( NoSuchContainerException
$e ) {
410 return $status; // ok, nothing to do
411 } catch ( InvalidResponseException
$e ) {
412 $status->fatal( 'backend-fail-connect', $this->name
);
414 } catch ( Exception
$e ) { // some other exception?
415 $status->fatal( 'backend-fail-internal', $this->name
);
416 $this->logException( $e, __METHOD__
, $params );
420 // (b) Delete the container if empty
421 if ( $contObj->object_count
== 0 ) {
423 $this->deleteContainer( $fullCont );
424 } catch ( NoSuchContainerException
$e ) {
425 return $status; // race?
426 } catch ( InvalidResponseException
$e ) {
427 $status->fatal( 'backend-fail-connect', $this->name
);
429 } catch ( Exception
$e ) { // some other exception?
430 $status->fatal( 'backend-fail-internal', $this->name
);
431 $this->logException( $e, __METHOD__
, $params );
440 * @see FileBackendStore::doFileExists()
442 protected function doGetFileStat( array $params ) {
443 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
444 if ( $srcRel === null ) {
445 return false; // invalid storage path
450 $contObj = $this->getContainer( $srcCont );
451 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
452 $this->addMissingMetadata( $srcObj, $params['src'] );
454 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
455 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
456 'size' => $srcObj->content_length
,
457 'sha1' => $srcObj->metadata
['Sha1base36']
459 } catch ( NoSuchContainerException
$e ) {
460 } catch ( NoSuchObjectException
$e ) {
461 } catch ( InvalidResponseException
$e ) {
463 } catch ( Exception
$e ) { // some other exception?
465 $this->logException( $e, __METHOD__
, $params );
472 * Fill in any missing object metadata and save it to Swift
474 * @param $obj CF_Object
475 * @param $path string Storage path to object
476 * @return bool Success
477 * @throws Exception cloudfiles exceptions
479 protected function addMissingMetadata( CF_Object
$obj, $path ) {
480 if ( isset( $obj->metadata
['Sha1base36'] ) ) {
481 return true; // nothing to do
483 $status = Status
::newGood();
484 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
485 if ( $status->isOK() ) {
486 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
488 $hash = $tmpFile->getSha1Base36();
489 if ( $hash !== false ) {
490 $obj->metadata
['Sha1base36'] = $hash;
491 $obj->sync_metadata(); // save to Swift
492 return true; // success
496 $obj->metadata
['Sha1base36'] = false;
497 return false; // failed
501 * @see FileBackend::getFileContents()
503 public function getFileContents( array $params ) {
504 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
505 if ( $srcRel === null ) {
506 return false; // invalid storage path
509 if ( !$this->fileExists( $params ) ) {
515 $sContObj = $this->getContainer( $srcCont );
516 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD request
517 $data = $obj->read( $this->headersFromParams( $params ) );
518 } catch ( NoSuchContainerException
$e ) {
519 } catch ( InvalidResponseException
$e ) {
520 } catch ( Exception
$e ) { // some other exception?
521 $this->logException( $e, __METHOD__
, $params );
528 * @see FileBackendStore::getFileListInternal()
530 public function getFileListInternal( $fullCont, $dir, array $params ) {
531 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
535 * Do not call this function outside of SwiftFileBackendFileList
537 * @param $fullCont string Resolved container name
538 * @param $dir string Resolved storage directory with no trailing slash
539 * @param $after string Storage path of file to list items after
540 * @param $limit integer Max number of items to list
543 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
547 $container = $this->getContainer( $fullCont );
548 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
549 $files = $container->list_objects( $limit, $after, $prefix );
550 } catch ( NoSuchContainerException
$e ) {
551 } catch ( NoSuchObjectException
$e ) {
552 } catch ( InvalidResponseException
$e ) {
553 } catch ( Exception
$e ) { // some other exception?
554 $this->logException( $e, __METHOD__
, array( 'cont' => $fullCont, 'dir' => $dir ) );
561 * @see FileBackendStore::doGetFileSha1base36()
563 public function doGetFileSha1base36( array $params ) {
564 $stat = $this->getFileStat( $params );
566 return $stat['sha1'];
573 * @see FileBackendStore::doStreamFile()
575 protected function doStreamFile( array $params ) {
576 $status = Status
::newGood();
578 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
579 if ( $srcRel === null ) {
580 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
584 $cont = $this->getContainer( $srcCont );
585 } catch ( NoSuchContainerException
$e ) {
586 $status->fatal( 'backend-fail-stream', $params['src'] );
588 } catch ( InvalidResponseException
$e ) {
589 $status->fatal( 'backend-fail-connect', $this->name
);
591 } catch ( Exception
$e ) { // some other exception?
592 $status->fatal( 'backend-fail-stream', $params['src'] );
593 $this->logException( $e, __METHOD__
, $params );
598 $output = fopen( 'php://output', 'w' );
599 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
600 $obj->stream( $output, $this->headersFromParams( $params ) );
601 } catch ( InvalidResponseException
$e ) { // 404? connection problem?
602 $status->fatal( 'backend-fail-stream', $params['src'] );
603 } catch ( Exception
$e ) { // some other exception?
604 $status->fatal( 'backend-fail-stream', $params['src'] );
605 $this->logException( $e, __METHOD__
, $params );
612 * @see FileBackendStore::getLocalCopy()
614 public function getLocalCopy( array $params ) {
615 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
616 if ( $srcRel === null ) {
620 if ( !$this->fileExists( $params ) ) {
626 $sContObj = $this->getContainer( $srcCont );
627 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
628 // Get source file extension
629 $ext = FileBackend
::extensionFromPath( $srcRel );
630 // Create a new temporary file...
631 $tmpFile = TempFSFile
::factory( wfBaseName( $srcRel ) . '_', $ext );
633 $handle = fopen( $tmpFile->getPath(), 'wb' );
635 $obj->stream( $handle, $this->headersFromParams( $params ) );
638 $tmpFile = null; // couldn't open temp file
641 } catch ( NoSuchContainerException
$e ) {
643 } catch ( InvalidResponseException
$e ) {
645 } catch ( Exception
$e ) { // some other exception?
647 $this->logException( $e, __METHOD__
, $params );
654 * Get headers to send to Swift when reading a file based
655 * on a FileBackend params array, e.g. that of getLocalCopy().
656 * $params is currently only checked for a 'latest' flag.
658 * @param $params Array
661 protected function headersFromParams( array $params ) {
663 if ( !empty( $params['latest'] ) ) {
664 $hdrs[] = 'X-Newest: true';
670 * Set read/write permissions for a Swift container
672 * @param $contObj CF_Container Swift container
673 * @param $readGrps Array Swift users who can read (account:user)
674 * @param $writeGrps Array Swift users who can write (account:user)
677 protected function setContainerAccess(
678 CF_Container
$contObj, array $readGrps, array $writeGrps
680 $creds = $contObj->cfs_auth
->export_credentials();
682 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
684 // Note: 10 second timeout consistent with php-cloudfiles
685 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
686 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
687 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
688 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
690 return $req->execute(); // should return 204
694 * Get a connection to the Swift proxy
696 * @return CF_Connection|false
697 * @throws InvalidResponseException
699 protected function getConnection() {
700 if ( $this->conn
=== false ) {
701 throw new InvalidResponseException
; // failed last attempt
703 // Session keys expire after a while, so we renew them periodically
704 if ( $this->conn
&& ( time() - $this->connStarted
) > $this->authTTL
) {
705 $this->conn
->close(); // close active cURL connections
708 // Authenticate with proxy and get a session key...
709 if ( $this->conn
=== null ) {
710 $this->connContainers
= array();
712 $this->auth
->authenticate();
713 $this->conn
= new CF_Connection( $this->auth
);
714 $this->connStarted
= time();
715 } catch ( AuthenticationException
$e ) {
716 $this->conn
= false; // don't keep re-trying
717 } catch ( InvalidResponseException
$e ) {
718 $this->conn
= false; // don't keep re-trying
721 if ( !$this->conn
) {
722 throw new InvalidResponseException
; // auth/connection problem
728 * @see FileBackendStore::doClearCache()
730 protected function doClearCache( array $paths = null ) {
731 $this->connContainers
= array(); // clear container object cache
735 * Get a Swift container object, possibly from process cache.
736 * Use $reCache if the file count or byte count is needed.
738 * @param $container string Container name
739 * @param $reCache bool Refresh the process cache
740 * @return CF_Container
742 protected function getContainer( $container, $reCache = false ) {
743 $conn = $this->getConnection(); // Swift proxy connection
745 unset( $this->connContainers
[$container] ); // purge cache
747 if ( !isset( $this->connContainers
[$container] ) ) {
748 $contObj = $conn->get_container( $container );
749 // NoSuchContainerException not thrown: container must exist
750 if ( count( $this->connContainers
) >= $this->maxContCacheSize
) { // trim cache?
751 reset( $this->connContainers
);
752 $key = key( $this->connContainers
);
753 unset( $this->connContainers
[$key] );
755 $this->connContainers
[$container] = $contObj; // cache it
757 return $this->connContainers
[$container];
761 * Create a Swift container
763 * @param $container string Container name
764 * @return CF_Container
766 protected function createContainer( $container ) {
767 $conn = $this->getConnection(); // Swift proxy connection
768 $contObj = $conn->create_container( $container );
769 $this->connContainers
[$container] = $contObj; // cache it
774 * Delete a Swift container
776 * @param $container string Container name
779 protected function deleteContainer( $container ) {
780 $conn = $this->getConnection(); // Swift proxy connection
781 $conn->delete_container( $container );
782 unset( $this->connContainers
[$container] ); // purge cache
786 * Log an unexpected exception for this backend
788 * @param $e Exception
789 * @param $func string
790 * @param $params Array
793 protected function logException( Exception
$e, $func, array $params ) {
794 wfDebugLog( 'SwiftBackend',
795 get_class( $e ) . " in '{$this->name}': '{$func}' with " . serialize( $params )
801 * SwiftFileBackend helper class to page through object listings.
802 * Swift also has a listing limit of 10,000 objects for sanity.
803 * Do not use this class from places outside SwiftFileBackend.
805 * @ingroup FileBackend
807 class SwiftFileBackendFileList
implements Iterator
{
809 protected $bufferIter = array();
810 protected $bufferAfter = null; // string; list items *after* this path
811 protected $pos = 0; // integer
813 /** @var SwiftFileBackend */
815 protected $container; //
816 protected $dir; // string storage directory
817 protected $suffixStart; // integer
819 const PAGE_SIZE
= 5000; // file listing buffer size
822 * @param $backend SwiftFileBackend
823 * @param $fullCont string Resolved container name
824 * @param $dir string Resolved directory relative to container
826 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir ) {
827 $this->backend
= $backend;
828 $this->container
= $fullCont;
830 if ( substr( $this->dir
, -1 ) === '/' ) {
831 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
833 if ( $this->dir
== '' ) { // whole container
834 $this->suffixStart
= 0;
835 } else { // dir within container
836 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
840 public function current() {
841 return substr( current( $this->bufferIter
), $this->suffixStart
);
844 public function key() {
848 public function next() {
849 // Advance to the next file in the page
850 next( $this->bufferIter
);
852 // Check if there are no files left in this page and
853 // advance to the next page if this page was not empty.
854 if ( !$this->valid() && count( $this->bufferIter
) ) {
855 $this->bufferAfter
= end( $this->bufferIter
);
856 $this->bufferIter
= $this->backend
->getFileListPageInternal(
857 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
862 public function rewind() {
864 $this->bufferAfter
= null;
865 $this->bufferIter
= $this->backend
->getFileListPageInternal(
866 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
870 public function valid() {
871 return ( current( $this->bufferIter
) !== false ); // no paths can have this value