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 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
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 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
44 public function __construct( array $config ) {
45 parent
::__construct( $config );
47 $this->auth
= new CF_Authentication(
50 null, // account; unused
51 $config['swiftAuthUrl']
54 $this->authTTL
= isset( $config['swiftAuthTTL'] )
55 ?
$config['swiftAuthTTL']
56 : 120; // some sane number
57 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
58 ?
$config['swiftAnonUser']
60 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
61 ?
$config['shardViaHashLevels']
66 * @see FileBackend::resolveContainerPath()
68 protected function resolveContainerPath( $container, $relStoragePath ) {
69 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
70 return null; // too long for Swift
72 return $relStoragePath;
76 * @see FileBackend::isPathUsableInternal()
78 public function isPathUsableInternal( $storagePath ) {
79 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
80 if ( $rel === null ) {
81 return false; // invalid
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 ) );
97 * @see FileBackend::doCopyInternal()
99 protected function doCreateInternal( array $params ) {
100 $status = Status
::newGood();
102 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
103 if ( $dstRel === null ) {
104 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
108 // (a) Check the destination container and object
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'] );
119 } catch ( NoSuchContainerException
$e ) {
120 $status->fatal( 'backend-fail-create', $params['dst'] );
122 } catch ( InvalidResponseException
$e ) {
123 $status->fatal( 'backend-fail-connect', $this->name
);
125 } catch ( Exception
$e ) { // some other exception?
126 $status->fatal( 'backend-fail-internal', $this->name
);
127 $this->logException( $e, __METHOD__
, $params );
131 // (b) Get a SHA-1 hash of the object
132 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
134 // (c) Actually create the object
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 );
155 * @see FileBackend::doStoreInternal()
157 protected function doStoreInternal( array $params ) {
158 $status = Status
::newGood();
160 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
161 if ( $dstRel === null ) {
162 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
166 // (a) Check the destination container and object
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'] );
177 } catch ( NoSuchContainerException
$e ) {
178 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
180 } catch ( InvalidResponseException
$e ) {
181 $status->fatal( 'backend-fail-connect', $this->name
);
183 } catch ( Exception
$e ) { // some other exception?
184 $status->fatal( 'backend-fail-internal', $this->name
);
185 $this->logException( $e, __METHOD__
, $params );
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'] );
195 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
197 // (c) Actually store the object
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 );
220 * @see FileBackend::doCopyInternal()
222 protected function doCopyInternal( array $params ) {
223 $status = Status
::newGood();
225 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
226 if ( $srcRel === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
231 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
232 if ( $dstRel === null ) {
233 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
237 // (a) Check the source/destination containers and destination object
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'] );
249 } catch ( NoSuchContainerException
$e ) {
250 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
252 } catch ( InvalidResponseException
$e ) {
253 $status->fatal( 'backend-fail-connect', $this->name
);
255 } catch ( Exception
$e ) { // some other exception?
256 $status->fatal( 'backend-fail-internal', $this->name
);
257 $this->logException( $e, __METHOD__
, $params );
261 // (b) Actually copy the file to the destination
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 );
277 * @see FileBackend::doDeleteInternal()
279 protected function doDeleteInternal( array $params ) {
280 $status = Status
::newGood();
282 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
283 if ( $srcRel === null ) {
284 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
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'] );
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 );
308 * @see FileBackend::doPrepareInternal()
310 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
311 $status = Status
::newGood();
313 // (a) Check if container already exists
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
);
323 } catch ( Exception
$e ) { // some other exception?
324 $status->fatal( 'backend-fail-internal', $this->name
);
325 $this->logException( $e, __METHOD__
, $params );
329 // (b) Create container as needed
331 $contObj = $this->createContainer( $fullCont );
332 if ( $this->swiftAnonUser
!= '' ) {
333 // Make container public to end-users...
334 $status->merge( $this->setContainerAccess(
336 array( $this->auth
->username
, $this->swiftAnonUser
), // read
337 array( $this->auth
->username
) // write
340 } catch ( InvalidResponseException
$e ) {
341 $status->fatal( 'backend-fail-connect', $this->name
);
343 } catch ( Exception
$e ) { // some other exception?
344 $status->fatal( 'backend-fail-internal', $this->name
);
345 $this->logException( $e, __METHOD__
, $params );
353 * @see FileBackend::doSecureInternal()
355 protected function doSecureInternal( $fullCont, $dir, array $params ) {
356 $status = Status
::newGood();
358 if ( $this->swiftAnonUser
!= '' ) {
359 // Restrict container from end-users...
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(
368 array( $this->auth
->username
), // read
369 array( $this->auth
->username
) // write
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
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 );
387 * @see FileBackend::doCleanInternal()
389 protected function doCleanInternal( $fullCont, $dir, array $params ) {
390 $status = Status
::newGood();
392 // Only containers themselves can be removed, all else is virtual
394 return $status; // nothing to do
397 // (a) Check the container
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
);
405 } catch ( Exception
$e ) { // some other exception?
406 $status->fatal( 'backend-fail-internal', $this->name
);
407 $this->logException( $e, __METHOD__
, $params );
411 // (b) Delete the container if empty
412 if ( $contObj->object_count
== 0 ) {
414 $this->deleteContainer( $fullCont );
415 } catch ( NoSuchContainerException
$e ) {
416 return $status; // race?
417 } catch ( InvalidResponseException
$e ) {
418 $status->fatal( 'backend-fail-connect', $this->name
);
420 } catch ( Exception
$e ) { // some other exception?
421 $status->fatal( 'backend-fail-internal', $this->name
);
422 $this->logException( $e, __METHOD__
, $params );
431 * @see FileBackend::doFileExists()
433 protected function doGetFileStat( array $params ) {
434 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
435 if ( $srcRel === null ) {
436 return false; // invalid storage path
441 $contObj = $this->getContainer( $srcCont );
442 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
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']
449 } catch ( NoSuchContainerException
$e ) {
450 } catch ( NoSuchObjectException
$e ) {
451 } catch ( InvalidResponseException
$e ) {
453 } catch ( Exception
$e ) { // some other exception?
455 $this->logException( $e, __METHOD__
, $params );
462 * @see FileBackendBase::getFileContents()
464 public function getFileContents( array $params ) {
465 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
466 if ( $srcRel === null ) {
467 return false; // invalid storage path
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 );
486 * @see FileBackend::getFileListInternal()
488 public function getFileListInternal( $fullCont, $dir, array $params ) {
489 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
493 * Do not call this function outside of SwiftFileBackendFileList
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
501 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
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 ) );
519 * @see FileBackend::doGetFileSha1base36()
521 public function doGetFileSha1base36( array $params ) {
522 $stat = $this->getFileStat( $params );
524 return $stat['sha1'];
531 * @see FileBackend::doStreamFile()
533 protected function doStreamFile( array $params ) {
534 $status = Status
::newGood();
536 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
537 if ( $srcRel === null ) {
538 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
542 $cont = $this->getContainer( $srcCont );
543 } catch ( NoSuchContainerException
$e ) {
544 $status->fatal( 'backend-fail-stream', $params['src'] );
546 } catch ( InvalidResponseException
$e ) {
547 $status->fatal( 'backend-fail-connect', $this->name
);
549 } catch ( Exception
$e ) { // some other exception?
550 $status->fatal( 'backend-fail-stream', $params['src'] );
551 $this->logException( $e, __METHOD__
, $params );
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 );
571 * @see FileBackend::getLocalCopy()
573 public function getLocalCopy( array $params ) {
574 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
575 if ( $srcRel === null ) {
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 );
588 $handle = fopen( $tmpFile->getPath(), 'w' );
590 $obj->stream( $handle, $this->headersFromParams( $params ) );
593 $tmpFile = null; // couldn't open temp file
596 } catch ( NoSuchContainerException
$e ) {
598 } catch ( NoSuchObjectException
$e ) {
600 } catch ( InvalidResponseException
$e ) {
602 } catch ( Exception
$e ) { // some other exception?
604 $this->logException( $e, __METHOD__
, $params );
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.
615 * @param $params Array
618 protected function headersFromParams( array $params ) {
620 if ( !empty( $params['latest'] ) ) {
621 $hdrs[] = 'X-Newest: true';
627 * Set read/write permissions for a Swift container
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)
634 protected function setContainerAccess(
635 CF_Container
$contObj, array $readGrps, array $writeGrps
637 $creds = $contObj->cfs_auth
->export_credentials();
639 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
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 ) );
647 return $req->execute(); // should return 204
651 * Get a connection to the Swift proxy
653 * @return CF_Connection|false
654 * @throws InvalidResponseException
656 protected function getConnection() {
657 if ( $this->conn
=== false ) {
658 return false; // failed last attempt
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
665 // Authenticate with proxy and get a session key...
666 if ( $this->conn
=== null ) {
667 $this->connContainers
= array();
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
678 if ( !$this->conn
) {
679 throw new InvalidResponseException
; // auth/connection problem
685 * @see FileBackend::doClearCache()
687 protected function doClearCache( array $paths = null ) {
688 $this->connContainers
= array(); // clear container object cache
692 * Get a Swift container object, possibly from process cache.
693 * Use $reCache if the file count or byte count is needed.
695 * @param $container string Container name
696 * @param $reCache bool Refresh the process cache
697 * @return CF_Container
699 protected function getContainer( $container, $reCache = false ) {
700 $conn = $this->getConnection(); // Swift proxy connection
702 unset( $this->connContainers
[$container] ); // purge cache
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] );
712 $this->connContainers
[$container] = $contObj; // cache it
714 return $this->connContainers
[$container];
718 * Create a Swift container
720 * @param $container string Container name
721 * @return CF_Container
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
731 * Delete a Swift container
733 * @param $container string Container name
736 protected function deleteContainer( $container ) {
737 $conn = $this->getConnection(); // Swift proxy connection
738 $conn->delete_container( $container );
739 unset( $this->connContainers
[$container] ); // purge cache
743 * Log an unexpected exception for this backend
745 * @param $e Exception
746 * @param $func string
747 * @param $params Array
750 protected function logException( Exception
$e, $func, array $params ) {
751 wfDebugLog( 'SwiftBackend',
752 get_class( $e ) . " in '{$this->name}': '{$func}' with " . serialize( $params )
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.
762 * @ingroup FileBackend
764 class SwiftFileBackendFileList
implements Iterator
{
766 protected $bufferIter = array();
767 protected $bufferAfter = null; // string; list items *after* this path
768 protected $pos = 0; // integer
770 /** @var SwiftFileBackend */
772 protected $container; //
773 protected $dir; // string storage directory
774 protected $suffixStart; // integer
776 const PAGE_SIZE
= 5000; // file listing buffer size
779 * @param $backend SwiftFileBackend
780 * @param $fullCont string Resolved container name
781 * @param $dir string Resolved directory relative to container
783 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir ) {
784 $this->backend
= $backend;
785 $this->container
= $fullCont;
787 if ( substr( $this->dir
, -1 ) === '/' ) {
788 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
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/"
797 public function current() {
798 return substr( current( $this->bufferIter
), $this->suffixStart
);
801 public function key() {
805 public function next() {
806 // Advance to the next file in the page
807 next( $this->bufferIter
);
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
819 public function rewind() {
821 $this->bufferAfter
= null;
822 $this->bufferIter
= $this->backend
->getFileListPageInternal(
823 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
827 public function valid() {
828 return ( current( $this->bufferIter
) !== false ); // no paths can have this value