Merge "[FileBackend] Added doQuickOperations() function for things like purging thumb...
[lhc/web/wiklou.git] / includes / filerepo / backend / SwiftFileBackend.php
1 <?php
2 /**
3 * OpenStack Swift based file backend.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Russ Nelson
23 * @author Aaron Schulz
24 */
25
26 /**
27 * @brief Class for an OpenStack Swift based file backend.
28 *
29 * This requires the SwiftCloudFiles MediaWiki extension, which includes
30 * the php-cloudfiles library (https://github.com/rackspace/php-cloudfiles).
31 * php-cloudfiles requires the curl, fileinfo, and mb_string PHP extensions.
32 *
33 * Status messages should avoid mentioning the Swift account name.
34 * Likewise, error suppression should be used to avoid path disclosure.
35 *
36 * @ingroup FileBackend
37 * @since 1.19
38 */
39 class SwiftFileBackend extends FileBackendStore {
40 /** @var CF_Authentication */
41 protected $auth; // Swift authentication handler
42 protected $authTTL; // integer seconds
43 protected $swiftAnonUser; // string; username to handle unauthenticated requests
44 protected $swiftUseCDN; // boolean; whether CloudFiles CDN is enabled
45 protected $maxContCacheSize = 300; // integer; max containers with entries
46
47 /** @var CF_Connection */
48 protected $conn; // Swift connection handle
49 protected $connStarted = 0; // integer UNIX timestamp
50 protected $connContainers = array(); // container object cache
51
52 /**
53 * @see FileBackendStore::__construct()
54 * Additional $config params include:
55 * swiftAuthUrl : Swift authentication server URL
56 * swiftUser : Swift user used by MediaWiki (account:username)
57 * swiftKey : Swift authentication key for the above user
58 * swiftAuthTTL : Swift authentication TTL (seconds)
59 * swiftAnonUser : Swift user used for end-user requests (account:username)
60 * swiftUseCDN : Whether a Cloud Files Content Delivery Network is set up
61 * shardViaHashLevels : Map of container names to sharding config with:
62 * 'base' : base of hash characters, 16 or 36
63 * 'levels' : the number of hash levels (and digits)
64 * 'repeat' : hash subdirectories are prefixed with all the
65 * parent hash directory names (e.g. "a/ab/abc")
66 */
67 public function __construct( array $config ) {
68 parent::__construct( $config );
69 if ( !MWInit::classExists( 'CF_Constants' ) ) {
70 throw new MWException( 'SwiftCloudFiles extension not installed.' );
71 }
72 // Required settings
73 $this->auth = new CF_Authentication(
74 $config['swiftUser'],
75 $config['swiftKey'],
76 null, // account; unused
77 $config['swiftAuthUrl']
78 );
79 // Optional settings
80 $this->authTTL = isset( $config['swiftAuthTTL'] )
81 ? $config['swiftAuthTTL']
82 : 5 * 60; // some sane number
83 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
84 ? $config['swiftAnonUser']
85 : '';
86 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
87 ? $config['shardViaHashLevels']
88 : '';
89 $this->swiftUseCDN = isset( $config['swiftUseCDN'] )
90 ? $config['swiftUseCDN']
91 : false;
92 // Cache container info to mask latency
93 $this->memCache = wfGetMainCache();
94 }
95
96 /**
97 * @see FileBackendStore::resolveContainerPath()
98 * @return null
99 */
100 protected function resolveContainerPath( $container, $relStoragePath ) {
101 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
102 return null; // too long for Swift
103 }
104 return $relStoragePath;
105 }
106
107 /**
108 * @see FileBackendStore::isPathUsableInternal()
109 * @return bool
110 */
111 public function isPathUsableInternal( $storagePath ) {
112 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
113 if ( $rel === null ) {
114 return false; // invalid
115 }
116
117 try {
118 $this->getContainer( $container );
119 return true; // container exists
120 } catch ( NoSuchContainerException $e ) {
121 } catch ( CloudFilesException $e ) { // some other exception?
122 $this->handleException( $e, null, __METHOD__, array( 'path' => $storagePath ) );
123 }
124
125 return false;
126 }
127
128 /**
129 * @see FileBackendStore::doCreateInternal()
130 * @return Status
131 */
132 protected function doCreateInternal( array $params ) {
133 $status = Status::newGood();
134
135 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
136 if ( $dstRel === null ) {
137 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
138 return $status;
139 }
140
141 // (a) Check the destination container and object
142 try {
143 $dContObj = $this->getContainer( $dstCont );
144 if ( empty( $params['overwrite'] ) &&
145 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
146 {
147 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
148 return $status;
149 }
150 } catch ( NoSuchContainerException $e ) {
151 $status->fatal( 'backend-fail-create', $params['dst'] );
152 return $status;
153 } catch ( CloudFilesException $e ) { // some other exception?
154 $this->handleException( $e, $status, __METHOD__, $params );
155 return $status;
156 }
157
158 // (b) Get a SHA-1 hash of the object
159 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
160
161 // (c) Actually create the object
162 try {
163 // Create a fresh CF_Object with no fields preloaded.
164 // We don't want to preserve headers, metadata, and such.
165 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
166 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
167 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
168 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
169 // The MD5 here will be checked within Swift against its own MD5.
170 $obj->set_etag( md5( $params['content'] ) );
171 // Use the same content type as StreamFile for security
172 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
173 if ( !empty( $params['async'] ) ) { // deferred
174 $handle = $obj->write_async( $params['content'] );
175 $status->value = new SwiftFileOpHandle( $this, $params, 'Create', $handle );
176 $status->value->affectedObjects[] = $obj;
177 } else { // actually write the object in Swift
178 $obj->write( $params['content'] );
179 $this->purgeCDNCache( array( $obj ) );
180 }
181 } catch ( CDNNotEnabledException $e ) {
182 // CDN not enabled; nothing to see here
183 } catch ( BadContentTypeException $e ) {
184 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
185 } catch ( CloudFilesException $e ) { // some other exception?
186 $this->handleException( $e, $status, __METHOD__, $params );
187 }
188
189 return $status;
190 }
191
192 /**
193 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
194 */
195 protected function _getResponseCreate( CF_Async_Op $cfOp, Status $status, array $params ) {
196 try {
197 $cfOp->getLastResponse();
198 } catch ( BadContentTypeException $e ) {
199 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
200 }
201 }
202
203 /**
204 * @see FileBackendStore::doStoreInternal()
205 * @return Status
206 */
207 protected function doStoreInternal( array $params ) {
208 $status = Status::newGood();
209
210 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
211 if ( $dstRel === null ) {
212 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
213 return $status;
214 }
215
216 // (a) Check the destination container and object
217 try {
218 $dContObj = $this->getContainer( $dstCont );
219 if ( empty( $params['overwrite'] ) &&
220 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
221 {
222 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
223 return $status;
224 }
225 } catch ( NoSuchContainerException $e ) {
226 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
227 return $status;
228 } catch ( CloudFilesException $e ) { // some other exception?
229 $this->handleException( $e, $status, __METHOD__, $params );
230 return $status;
231 }
232
233 // (b) Get a SHA-1 hash of the object
234 $sha1Hash = sha1_file( $params['src'] );
235 if ( $sha1Hash === false ) { // source doesn't exist?
236 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
237 return $status;
238 }
239 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
240
241 // (c) Actually store the object
242 try {
243 // Create a fresh CF_Object with no fields preloaded.
244 // We don't want to preserve headers, metadata, and such.
245 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
246 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
247 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
248 // The MD5 here will be checked within Swift against its own MD5.
249 $obj->set_etag( md5_file( $params['src'] ) );
250 // Use the same content type as StreamFile for security
251 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
252 if ( !empty( $params['async'] ) ) { // deferred
253 wfSuppressWarnings();
254 $fp = fopen( $params['src'], 'rb' );
255 wfRestoreWarnings();
256 if ( !$fp ) {
257 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
258 } else {
259 $handle = $obj->write_async( $fp, filesize( $params['src'] ), true );
260 $status->value = new SwiftFileOpHandle( $this, $params, 'Store', $handle );
261 $status->value->resourcesToClose[] = $fp;
262 $status->value->affectedObjects[] = $obj;
263 }
264 } else { // actually write the object in Swift
265 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
266 $this->purgeCDNCache( array( $obj ) );
267 }
268 } catch ( CDNNotEnabledException $e ) {
269 // CDN not enabled; nothing to see here
270 } catch ( BadContentTypeException $e ) {
271 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
272 } catch ( IOException $e ) {
273 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
274 } catch ( CloudFilesException $e ) { // some other exception?
275 $this->handleException( $e, $status, __METHOD__, $params );
276 }
277
278 return $status;
279 }
280
281 /**
282 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
283 */
284 protected function _getResponseStore( CF_Async_Op $cfOp, Status $status, array $params ) {
285 try {
286 $cfOp->getLastResponse();
287 } catch ( BadContentTypeException $e ) {
288 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
289 } catch ( IOException $e ) {
290 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
291 }
292 }
293
294 /**
295 * @see FileBackendStore::doCopyInternal()
296 * @return Status
297 */
298 protected function doCopyInternal( array $params ) {
299 $status = Status::newGood();
300
301 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
302 if ( $srcRel === null ) {
303 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
304 return $status;
305 }
306
307 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
308 if ( $dstRel === null ) {
309 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
310 return $status;
311 }
312
313 // (a) Check the source/destination containers and destination object
314 try {
315 $sContObj = $this->getContainer( $srcCont );
316 $dContObj = $this->getContainer( $dstCont );
317 if ( empty( $params['overwrite'] ) &&
318 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
319 {
320 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
321 return $status;
322 }
323 } catch ( NoSuchContainerException $e ) {
324 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
325 return $status;
326 } catch ( CloudFilesException $e ) { // some other exception?
327 $this->handleException( $e, $status, __METHOD__, $params );
328 return $status;
329 }
330
331 // (b) Actually copy the file to the destination
332 try {
333 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
334 if ( !empty( $params['async'] ) ) { // deferred
335 $handle = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel );
336 $status->value = new SwiftFileOpHandle( $this, $params, 'Copy', $handle );
337 $status->value->affectedObjects[] = $dstObj;
338 } else { // actually write the object in Swift
339 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
340 $this->purgeCDNCache( array( $dstObj ) );
341 }
342 } catch ( CDNNotEnabledException $e ) {
343 // CDN not enabled; nothing to see here
344 } catch ( NoSuchObjectException $e ) { // source object does not exist
345 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
346 } catch ( CloudFilesException $e ) { // some other exception?
347 $this->handleException( $e, $status, __METHOD__, $params );
348 }
349
350 return $status;
351 }
352
353 /**
354 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
355 */
356 protected function _getResponseCopy( CF_Async_Op $cfOp, Status $status, array $params ) {
357 try {
358 $cfOp->getLastResponse();
359 } catch ( NoSuchObjectException $e ) { // source object does not exist
360 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
361 }
362 }
363
364 /**
365 * @see FileBackendStore::doMoveInternal()
366 * @return Status
367 */
368 protected function doMoveInternal( array $params ) {
369 $status = Status::newGood();
370
371 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
372 if ( $srcRel === null ) {
373 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
374 return $status;
375 }
376
377 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
378 if ( $dstRel === null ) {
379 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
380 return $status;
381 }
382
383 // (a) Check the source/destination containers and destination object
384 try {
385 $sContObj = $this->getContainer( $srcCont );
386 $dContObj = $this->getContainer( $dstCont );
387 if ( empty( $params['overwrite'] ) &&
388 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
389 {
390 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
391 return $status;
392 }
393 } catch ( NoSuchContainerException $e ) {
394 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
395 return $status;
396 } catch ( CloudFilesException $e ) { // some other exception?
397 $this->handleException( $e, $status, __METHOD__, $params );
398 return $status;
399 }
400
401 // (b) Actually move the file to the destination
402 try {
403 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
404 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
405 if ( !empty( $params['async'] ) ) { // deferred
406 $handle = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel );
407 $status->value = new SwiftFileOpHandle( $this, $params, 'Move', $handle );
408 $status->value->affectedObjects[] = $srcObj;
409 $status->value->affectedObjects[] = $dstObj;
410 } else { // actually write the object in Swift
411 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel );
412 $this->purgeCDNCache( array( $srcObj, $dstObj ) );
413 }
414 } catch ( CDNNotEnabledException $e ) {
415 // CDN not enabled; nothing to see here
416 } catch ( NoSuchObjectException $e ) { // source object does not exist
417 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
418 } catch ( CloudFilesException $e ) { // some other exception?
419 $this->handleException( $e, $status, __METHOD__, $params );
420 }
421
422 return $status;
423 }
424
425 /**
426 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
427 */
428 protected function _getResponseMove( CF_Async_Op $cfOp, Status $status, array $params ) {
429 try {
430 $cfOp->getLastResponse();
431 } catch ( NoSuchObjectException $e ) { // source object does not exist
432 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
433 }
434 }
435
436 /**
437 * @see FileBackendStore::doDeleteInternal()
438 * @return Status
439 */
440 protected function doDeleteInternal( array $params ) {
441 $status = Status::newGood();
442
443 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
444 if ( $srcRel === null ) {
445 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
446 return $status;
447 }
448
449 try {
450 $sContObj = $this->getContainer( $srcCont );
451 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
452 if ( !empty( $params['async'] ) ) { // deferred
453 $handle = $sContObj->delete_object_async( $srcRel );
454 $status->value = new SwiftFileOpHandle( $this, $params, 'Delete', $handle );
455 $status->value->affectedObjects[] = $srcObj;
456 } else { // actually write the object in Swift
457 $sContObj->delete_object( $srcRel );
458 $this->purgeCDNCache( array( $srcObj ) );
459 }
460 } catch ( CDNNotEnabledException $e ) {
461 // CDN not enabled; nothing to see here
462 } catch ( NoSuchContainerException $e ) {
463 $status->fatal( 'backend-fail-delete', $params['src'] );
464 } catch ( NoSuchObjectException $e ) {
465 if ( empty( $params['ignoreMissingSource'] ) ) {
466 $status->fatal( 'backend-fail-delete', $params['src'] );
467 }
468 } catch ( CloudFilesException $e ) { // some other exception?
469 $this->handleException( $e, $status, __METHOD__, $params );
470 }
471
472 return $status;
473 }
474
475 /**
476 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
477 */
478 protected function _getResponseDelete( CF_Async_Op $cfOp, Status $status, array $params ) {
479 try {
480 $cfOp->getLastResponse();
481 } catch ( NoSuchContainerException $e ) {
482 $status->fatal( 'backend-fail-delete', $params['src'] );
483 } catch ( NoSuchObjectException $e ) {
484 if ( empty( $params['ignoreMissingSource'] ) ) {
485 $status->fatal( 'backend-fail-delete', $params['src'] );
486 }
487 }
488 }
489
490 /**
491 * @see FileBackendStore::doPrepareInternal()
492 * @return Status
493 */
494 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
495 $status = Status::newGood();
496
497 // (a) Check if container already exists
498 try {
499 $contObj = $this->getContainer( $fullCont );
500 // NoSuchContainerException not thrown: container must exist
501 return $status; // already exists
502 } catch ( NoSuchContainerException $e ) {
503 // NoSuchContainerException thrown: container does not exist
504 } catch ( CloudFilesException $e ) { // some other exception?
505 $this->handleException( $e, $status, __METHOD__, $params );
506 return $status;
507 }
508
509 // (b) Create container as needed
510 try {
511 $contObj = $this->createContainer( $fullCont );
512 // Make container public to end-users...
513 if ( $this->swiftAnonUser != '' ) {
514 $status->merge( $this->setContainerAccess(
515 $contObj,
516 array( $this->auth->username, $this->swiftAnonUser ), // read
517 array( $this->auth->username ) // write
518 ) );
519 }
520 if ( $this->swiftUseCDN ) { // Rackspace style CDN
521 $contObj->make_public();
522 }
523 } catch ( CDNNotEnabledException $e ) {
524 // CDN not enabled; nothing to see here
525 } catch ( CloudFilesException $e ) { // some other exception?
526 $this->handleException( $e, $status, __METHOD__, $params );
527 return $status;
528 }
529
530 return $status;
531 }
532
533 /**
534 * @see FileBackendStore::doSecureInternal()
535 * @return Status
536 */
537 protected function doSecureInternal( $fullCont, $dir, array $params ) {
538 $status = Status::newGood();
539
540 // Restrict container from end-users...
541 try {
542 // doPrepareInternal() should have been called,
543 // so the Swift container should already exist...
544 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
545 // NoSuchContainerException not thrown: container must exist
546
547 // Make container private to end-users...
548 if ( $this->swiftAnonUser != '' && !isset( $contObj->mw_wasSecured ) ) {
549 $status->merge( $this->setContainerAccess(
550 $contObj,
551 array( $this->auth->username ), // read
552 array( $this->auth->username ) // write
553 ) );
554 // @TODO: when php-cloudfiles supports container
555 // metadata, we can make use of that to avoid RTTs
556 $contObj->mw_wasSecured = true; // avoid useless RTTs
557 }
558 if ( $this->swiftUseCDN && $contObj->is_public() ) { // Rackspace style CDN
559 $contObj->make_private();
560 }
561 } catch ( CDNNotEnabledException $e ) {
562 // CDN not enabled; nothing to see here
563 } catch ( CloudFilesException $e ) { // some other exception?
564 $this->handleException( $e, $status, __METHOD__, $params );
565 }
566
567 return $status;
568 }
569
570 /**
571 * @see FileBackendStore::doCleanInternal()
572 * @return Status
573 */
574 protected function doCleanInternal( $fullCont, $dir, array $params ) {
575 $status = Status::newGood();
576
577 // Only containers themselves can be removed, all else is virtual
578 if ( $dir != '' ) {
579 return $status; // nothing to do
580 }
581
582 // (a) Check the container
583 try {
584 $contObj = $this->getContainer( $fullCont, true );
585 } catch ( NoSuchContainerException $e ) {
586 return $status; // ok, nothing to do
587 } catch ( CloudFilesException $e ) { // some other exception?
588 $this->handleException( $e, $status, __METHOD__, $params );
589 return $status;
590 }
591
592 // (b) Delete the container if empty
593 if ( $contObj->object_count == 0 ) {
594 try {
595 $this->deleteContainer( $fullCont );
596 } catch ( NoSuchContainerException $e ) {
597 return $status; // race?
598 } catch ( NonEmptyContainerException $e ) {
599 return $status; // race? consistency delay?
600 } catch ( CloudFilesException $e ) { // some other exception?
601 $this->handleException( $e, $status, __METHOD__, $params );
602 return $status;
603 }
604 }
605
606 return $status;
607 }
608
609 /**
610 * @see FileBackendStore::doFileExists()
611 * @return array|bool|null
612 */
613 protected function doGetFileStat( array $params ) {
614 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
615 if ( $srcRel === null ) {
616 return false; // invalid storage path
617 }
618
619 $stat = false;
620 try {
621 $contObj = $this->getContainer( $srcCont );
622 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
623 $this->addMissingMetadata( $srcObj, $params['src'] );
624 $stat = array(
625 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
626 'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
627 'size' => $srcObj->content_length,
628 'sha1' => $srcObj->metadata['Sha1base36']
629 );
630 } catch ( NoSuchContainerException $e ) {
631 } catch ( NoSuchObjectException $e ) {
632 } catch ( CloudFilesException $e ) { // some other exception?
633 $stat = null;
634 $this->handleException( $e, null, __METHOD__, $params );
635 }
636
637 return $stat;
638 }
639
640 /**
641 * Fill in any missing object metadata and save it to Swift
642 *
643 * @param $obj CF_Object
644 * @param $path string Storage path to object
645 * @return bool Success
646 * @throws Exception cloudfiles exceptions
647 */
648 protected function addMissingMetadata( CF_Object $obj, $path ) {
649 if ( isset( $obj->metadata['Sha1base36'] ) ) {
650 return true; // nothing to do
651 }
652 $status = Status::newGood();
653 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager::LOCK_UW, $status );
654 if ( $status->isOK() ) {
655 # Do not stat the file in getLocalCopy() to avoid infinite loops
656 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1, 'nostat' => 1 ) );
657 if ( $tmpFile ) {
658 $hash = $tmpFile->getSha1Base36();
659 if ( $hash !== false ) {
660 $obj->metadata['Sha1base36'] = $hash;
661 $obj->sync_metadata(); // save to Swift
662 return true; // success
663 }
664 }
665 }
666 $obj->metadata['Sha1base36'] = false;
667 return false; // failed
668 }
669
670 /**
671 * @see FileBackend::getFileContents()
672 * @return bool|null|string
673 */
674 public function getFileContents( array $params ) {
675 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
676 if ( $srcRel === null ) {
677 return false; // invalid storage path
678 }
679
680 if ( !$this->fileExists( $params ) ) {
681 return null;
682 }
683
684 $data = false;
685 try {
686 $sContObj = $this->getContainer( $srcCont );
687 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
688 $data = $obj->read( $this->headersFromParams( $params ) );
689 } catch ( NoSuchContainerException $e ) {
690 } catch ( CloudFilesException $e ) { // some other exception?
691 $this->handleException( $e, null, __METHOD__, $params );
692 }
693
694 return $data;
695 }
696
697 /**
698 * @see FileBackendStore::doDirectoryExists()
699 * @return bool|null
700 */
701 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
702 try {
703 $container = $this->getContainer( $fullCont );
704 $prefix = ( $dir == '' ) ? null : "{$dir}/";
705 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
706 } catch ( NoSuchContainerException $e ) {
707 return false;
708 } catch ( CloudFilesException $e ) { // some other exception?
709 $this->handleException( $e, null, __METHOD__,
710 array( 'cont' => $fullCont, 'dir' => $dir ) );
711 }
712
713 return null; // error
714 }
715
716 /**
717 * @see FileBackendStore::getDirectoryListInternal()
718 * @return SwiftFileBackendDirList
719 */
720 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
721 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
722 }
723
724 /**
725 * @see FileBackendStore::getFileListInternal()
726 * @return SwiftFileBackendFileList
727 */
728 public function getFileListInternal( $fullCont, $dir, array $params ) {
729 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
730 }
731
732 /**
733 * Do not call this function outside of SwiftFileBackendFileList
734 *
735 * @param $fullCont string Resolved container name
736 * @param $dir string Resolved storage directory with no trailing slash
737 * @param $after string|null Storage path of file to list items after
738 * @param $limit integer Max number of items to list
739 * @param $params Array Includes flag for 'topOnly'
740 * @return Array List of relative paths of dirs directly under $dir
741 */
742 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
743 $dirs = array();
744
745 try {
746 $container = $this->getContainer( $fullCont );
747 $prefix = ( $dir == '' ) ? null : "{$dir}/";
748 // Non-recursive: only list dirs right under $dir
749 if ( !empty( $params['topOnly'] ) ) {
750 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
751 foreach ( $objects as $object ) { // files and dirs
752 if ( substr( $object, -1 ) === '/' ) {
753 $dirs[] = $object; // directories end in '/'
754 }
755 $after = $object; // update last item
756 }
757 // Recursive: list all dirs under $dir and its subdirs
758 } else {
759 // Get directory from last item of prior page
760 $lastDir = $this->getParentDir( $after ); // must be first page
761 $objects = $container->list_objects( $limit, $after, $prefix );
762 foreach ( $objects as $object ) { // files
763 $objectDir = $this->getParentDir( $object ); // directory of object
764 if ( $objectDir !== false ) { // file has a parent dir
765 // Swift stores paths in UTF-8, using binary sorting.
766 // See function "create_container_table" in common/db.py.
767 // If a directory is not "greater" than the last one,
768 // then it was already listed by the calling iterator.
769 if ( $objectDir > $lastDir ) {
770 $pDir = $objectDir;
771 do { // add dir and all its parent dirs
772 $dirs[] = "{$pDir}/";
773 $pDir = $this->getParentDir( $pDir );
774 } while ( $pDir !== false // sanity
775 && $pDir > $lastDir // not done already
776 && strlen( $pDir ) > strlen( $dir ) // within $dir
777 );
778 }
779 $lastDir = $objectDir;
780 }
781 $after = $object; // update last item
782 }
783 }
784 } catch ( NoSuchContainerException $e ) {
785 } catch ( CloudFilesException $e ) { // some other exception?
786 $this->handleException( $e, null, __METHOD__,
787 array( 'cont' => $fullCont, 'dir' => $dir ) );
788 }
789
790 return $dirs;
791 }
792
793 protected function getParentDir( $path ) {
794 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
795 }
796
797 /**
798 * Do not call this function outside of SwiftFileBackendFileList
799 *
800 * @param $fullCont string Resolved container name
801 * @param $dir string Resolved storage directory with no trailing slash
802 * @param $after string|null Storage path of file to list items after
803 * @param $limit integer Max number of items to list
804 * @param $params Array Includes flag for 'topOnly'
805 * @return Array List of relative paths of files under $dir
806 */
807 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
808 $files = array();
809
810 try {
811 $container = $this->getContainer( $fullCont );
812 $prefix = ( $dir == '' ) ? null : "{$dir}/";
813 // Non-recursive: only list files right under $dir
814 if ( !empty( $params['topOnly'] ) ) { // files and dirs
815 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
816 foreach ( $objects as $object ) {
817 if ( substr( $object, -1 ) !== '/' ) {
818 $files[] = $object; // directories end in '/'
819 }
820 }
821 // Recursive: list all files under $dir and its subdirs
822 } else { // files
823 $files = $container->list_objects( $limit, $after, $prefix );
824 }
825 $after = end( $files ); // update last item
826 reset( $files ); // reset pointer
827 } catch ( NoSuchContainerException $e ) {
828 } catch ( CloudFilesException $e ) { // some other exception?
829 $this->handleException( $e, null, __METHOD__,
830 array( 'cont' => $fullCont, 'dir' => $dir ) );
831 }
832
833 return $files;
834 }
835
836 /**
837 * @see FileBackendStore::doGetFileSha1base36()
838 * @return bool
839 */
840 protected function doGetFileSha1base36( array $params ) {
841 $stat = $this->getFileStat( $params );
842 if ( $stat ) {
843 return $stat['sha1'];
844 } else {
845 return false;
846 }
847 }
848
849 /**
850 * @see FileBackendStore::doStreamFile()
851 * @return Status
852 */
853 protected function doStreamFile( array $params ) {
854 $status = Status::newGood();
855
856 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
857 if ( $srcRel === null ) {
858 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
859 }
860
861 try {
862 $cont = $this->getContainer( $srcCont );
863 } catch ( NoSuchContainerException $e ) {
864 $status->fatal( 'backend-fail-stream', $params['src'] );
865 return $status;
866 } catch ( CloudFilesException $e ) { // some other exception?
867 $this->handleException( $e, $status, __METHOD__, $params );
868 return $status;
869 }
870
871 try {
872 $output = fopen( 'php://output', 'wb' );
873 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
874 $obj->stream( $output, $this->headersFromParams( $params ) );
875 } catch ( CloudFilesException $e ) { // some other exception?
876 $this->handleException( $e, $status, __METHOD__, $params );
877 }
878
879 return $status;
880 }
881
882 /**
883 * @see FileBackendStore::getLocalCopy()
884 * @return null|TempFSFile
885 */
886 public function getLocalCopy( array $params ) {
887 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
888 if ( $srcRel === null ) {
889 return null;
890 }
891
892 # Check the recursion guard to avoid loops when filling metadata
893 if ( empty( $params['nostat'] ) && !$this->fileExists( $params ) ) {
894 return null;
895 }
896
897 $tmpFile = null;
898 try {
899 $sContObj = $this->getContainer( $srcCont );
900 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
901 // Get source file extension
902 $ext = FileBackend::extensionFromPath( $srcRel );
903 // Create a new temporary file...
904 $tmpFile = TempFSFile::factory( wfBaseName( $srcRel ) . '_', $ext );
905 if ( $tmpFile ) {
906 $handle = fopen( $tmpFile->getPath(), 'wb' );
907 if ( $handle ) {
908 $obj->stream( $handle, $this->headersFromParams( $params ) );
909 fclose( $handle );
910 } else {
911 $tmpFile = null; // couldn't open temp file
912 }
913 }
914 } catch ( NoSuchContainerException $e ) {
915 $tmpFile = null;
916 } catch ( CloudFilesException $e ) { // some other exception?
917 $tmpFile = null;
918 $this->handleException( $e, null, __METHOD__, $params );
919 }
920
921 return $tmpFile;
922 }
923
924 /**
925 * @see FileBackendStore::directoriesAreVirtual()
926 * @return bool
927 */
928 protected function directoriesAreVirtual() {
929 return true;
930 }
931
932 /**
933 * Get headers to send to Swift when reading a file based
934 * on a FileBackend params array, e.g. that of getLocalCopy().
935 * $params is currently only checked for a 'latest' flag.
936 *
937 * @param $params Array
938 * @return Array
939 */
940 protected function headersFromParams( array $params ) {
941 $hdrs = array();
942 if ( !empty( $params['latest'] ) ) {
943 $hdrs[] = 'X-Newest: true';
944 }
945 return $hdrs;
946 }
947
948 /**
949 * @see FileBackendStore::doExecuteOpHandlesInternal()
950 * @return Array List of corresponding Status objects
951 */
952 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
953 $statuses = array();
954
955 $cfOps = array(); // list of CF_Async_Op objects
956 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
957 $cfOps[$index] = $fileOpHandle->cfOp;
958 }
959 $batch = new CF_Async_Op_Batch( $cfOps );
960
961 $cfOps = $batch->execute();
962 foreach ( $cfOps as $index => $cfOp ) {
963 $status = Status::newGood();
964 try { // catch exceptions; update status
965 $function = '_getResponse' . $fileOpHandles[$index]->call;
966 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params );
967 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects );
968 } catch ( CloudFilesException $e ) { // some other exception?
969 $this->handleException( $e, $status,
970 __CLASS__ . ":$function", $fileOpHandles[$index]->params );
971 }
972 $statuses[$index] = $status;
973 }
974
975 foreach ( $fileOpHandles as $fileOpHandle ) {
976 $fileOpHandle->closeResources();
977 }
978
979 return $statuses;
980 }
981
982 /**
983 * Set read/write permissions for a Swift container
984 *
985 * @param $contObj CF_Container Swift container
986 * @param $readGrps Array Swift users who can read (account:user)
987 * @param $writeGrps Array Swift users who can write (account:user)
988 * @return Status
989 */
990 protected function setContainerAccess(
991 CF_Container $contObj, array $readGrps, array $writeGrps
992 ) {
993 $creds = $contObj->cfs_auth->export_credentials();
994
995 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
996
997 // Note: 10 second timeout consistent with php-cloudfiles
998 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
999 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1000 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1001 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1002
1003 return $req->execute(); // should return 204
1004 }
1005
1006 /**
1007 * Purge the CDN cache of affected objects if CDN caching is enabled
1008 *
1009 * @param $objects Array List of CF_Object items
1010 * @return void
1011 */
1012 public function purgeCDNCache( array $objects ) {
1013 if ( $this->swiftUseCDN ) { // Rackspace style CDN
1014 foreach ( $objects as $object ) {
1015 try {
1016 $object->purge_from_cdn();
1017 } catch ( CDNNotEnabledException $e ) {
1018 // CDN not enabled; nothing to see here
1019 } catch ( CloudFilesException $e ) {
1020 $this->handleException( $e, null, __METHOD__,
1021 array( 'cont' => $object->container->name, 'obj' => $object->name ) );
1022 }
1023 }
1024 }
1025 }
1026
1027 /**
1028 * Get a connection to the Swift proxy
1029 *
1030 * @return CF_Connection|bool False on failure
1031 * @throws InvalidResponseException
1032 */
1033 protected function getConnection() {
1034 if ( $this->conn === false ) {
1035 throw new InvalidResponseException; // failed last attempt
1036 }
1037 // Session keys expire after a while, so we renew them periodically
1038 if ( $this->conn && ( time() - $this->connStarted ) > $this->authTTL ) {
1039 $this->conn->close(); // close active cURL connections
1040 $this->conn = null;
1041 }
1042 // Authenticate with proxy and get a session key...
1043 if ( $this->conn === null ) {
1044 $this->connContainers = array();
1045 try {
1046 $this->auth->authenticate();
1047 $this->conn = new CF_Connection( $this->auth );
1048 $this->connStarted = time();
1049 } catch ( AuthenticationException $e ) {
1050 $this->conn = false; // don't keep re-trying
1051 } catch ( InvalidResponseException $e ) {
1052 $this->conn = false; // don't keep re-trying
1053 }
1054 }
1055 if ( !$this->conn ) {
1056 throw new InvalidResponseException; // auth/connection problem
1057 }
1058 return $this->conn;
1059 }
1060
1061 /**
1062 * @see FileBackendStore::doClearCache()
1063 */
1064 protected function doClearCache( array $paths = null ) {
1065 $this->connContainers = array(); // clear container object cache
1066 }
1067
1068 /**
1069 * Get a Swift container object, possibly from process cache.
1070 * Use $reCache if the file count or byte count is needed.
1071 *
1072 * @param $container string Container name
1073 * @param $bypassCache bool Bypass all caches and load from Swift
1074 * @return CF_Container
1075 * @throws NoSuchContainerException
1076 * @throws InvalidResponseException
1077 */
1078 protected function getContainer( $container, $bypassCache = false ) {
1079 $conn = $this->getConnection(); // Swift proxy connection
1080 if ( $bypassCache ) { // purge cache
1081 unset( $this->connContainers[$container] );
1082 } elseif ( !isset( $this->connContainers[$container] ) ) {
1083 $this->primeContainerCache( array( $container ) ); // check persistent cache
1084 }
1085 if ( !isset( $this->connContainers[$container] ) ) {
1086 $contObj = $conn->get_container( $container );
1087 // NoSuchContainerException not thrown: container must exist
1088 if ( count( $this->connContainers ) >= $this->maxContCacheSize ) { // trim cache?
1089 reset( $this->connContainers );
1090 unset( $this->connContainers[key( $this->connContainers )] );
1091 }
1092 $this->connContainers[$container] = $contObj; // cache it
1093 if ( !$bypassCache ) {
1094 $this->setContainerCache( $container, // update persistent cache
1095 array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
1096 );
1097 }
1098 }
1099 return $this->connContainers[$container];
1100 }
1101
1102 /**
1103 * Create a Swift container
1104 *
1105 * @param $container string Container name
1106 * @return CF_Container
1107 * @throws InvalidResponseException
1108 */
1109 protected function createContainer( $container ) {
1110 $conn = $this->getConnection(); // Swift proxy connection
1111 $contObj = $conn->create_container( $container );
1112 $this->connContainers[$container] = $contObj; // cache it
1113 return $contObj;
1114 }
1115
1116 /**
1117 * Delete a Swift container
1118 *
1119 * @param $container string Container name
1120 * @return void
1121 * @throws InvalidResponseException
1122 */
1123 protected function deleteContainer( $container ) {
1124 $conn = $this->getConnection(); // Swift proxy connection
1125 $conn->delete_container( $container );
1126 unset( $this->connContainers[$container] ); // purge cache
1127 }
1128
1129 /**
1130 * @see FileBackendStore::doPrimeContainerCache()
1131 * @return void
1132 */
1133 protected function doPrimeContainerCache( array $containerInfo ) {
1134 try {
1135 $conn = $this->getConnection(); // Swift proxy connection
1136 foreach ( $containerInfo as $container => $info ) {
1137 $this->connContainers[$container] = new CF_Container(
1138 $conn->cfs_auth,
1139 $conn->cfs_http,
1140 $container,
1141 $info['count'],
1142 $info['bytes']
1143 );
1144 }
1145 } catch ( CloudFilesException $e ) { // some other exception?
1146 $this->handleException( $e, null, __METHOD__, array() );
1147 }
1148 }
1149
1150 /**
1151 * Log an unexpected exception for this backend.
1152 * This also sets the Status object to have a fatal error.
1153 *
1154 * @param $e Exception
1155 * @param $status Status|null
1156 * @param $func string
1157 * @param $params Array
1158 * @return void
1159 */
1160 protected function handleException( Exception $e, $status, $func, array $params ) {
1161 if ( $status instanceof Status ) {
1162 if ( $e instanceof AuthenticationException ) {
1163 $status->fatal( 'backend-fail-connect', $this->name );
1164 } else {
1165 $status->fatal( 'backend-fail-internal', $this->name );
1166 }
1167 }
1168 if ( $e->getMessage() ) {
1169 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1170 }
1171 wfDebugLog( 'SwiftBackend',
1172 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1173 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1174 );
1175 }
1176 }
1177
1178 /**
1179 * @see FileBackendStoreOpHandle
1180 */
1181 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1182 /** @var CF_Async_Op */
1183 public $cfOp;
1184 /** @var Array */
1185 public $affectedObjects = array();
1186
1187 public function __construct( $backend, array $params, $call, CF_Async_Op $cfOp ) {
1188 $this->backend = $backend;
1189 $this->params = $params;
1190 $this->call = $call;
1191 $this->cfOp = $cfOp;
1192 }
1193 }
1194
1195 /**
1196 * SwiftFileBackend helper class to page through listings.
1197 * Swift also has a listing limit of 10,000 objects for sanity.
1198 * Do not use this class from places outside SwiftFileBackend.
1199 *
1200 * @ingroup FileBackend
1201 */
1202 abstract class SwiftFileBackendList implements Iterator {
1203 /** @var Array */
1204 protected $bufferIter = array();
1205 protected $bufferAfter = null; // string; list items *after* this path
1206 protected $pos = 0; // integer
1207 /** @var Array */
1208 protected $params = array();
1209
1210 /** @var SwiftFileBackend */
1211 protected $backend;
1212 protected $container; // string; container name
1213 protected $dir; // string; storage directory
1214 protected $suffixStart; // integer
1215
1216 const PAGE_SIZE = 5000; // file listing buffer size
1217
1218 /**
1219 * @param $backend SwiftFileBackend
1220 * @param $fullCont string Resolved container name
1221 * @param $dir string Resolved directory relative to container
1222 * @param $params Array
1223 */
1224 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1225 $this->backend = $backend;
1226 $this->container = $fullCont;
1227 $this->dir = $dir;
1228 if ( substr( $this->dir, -1 ) === '/' ) {
1229 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1230 }
1231 if ( $this->dir == '' ) { // whole container
1232 $this->suffixStart = 0;
1233 } else { // dir within container
1234 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1235 }
1236 $this->params = $params;
1237 }
1238
1239 /**
1240 * @see Iterator::key()
1241 * @return integer
1242 */
1243 public function key() {
1244 return $this->pos;
1245 }
1246
1247 /**
1248 * @see Iterator::next()
1249 * @return void
1250 */
1251 public function next() {
1252 // Advance to the next file in the page
1253 next( $this->bufferIter );
1254 ++$this->pos;
1255 // Check if there are no files left in this page and
1256 // advance to the next page if this page was not empty.
1257 if ( !$this->valid() && count( $this->bufferIter ) ) {
1258 $this->bufferIter = $this->pageFromList(
1259 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1260 ); // updates $this->bufferAfter
1261 }
1262 }
1263
1264 /**
1265 * @see Iterator::rewind()
1266 * @return void
1267 */
1268 public function rewind() {
1269 $this->pos = 0;
1270 $this->bufferAfter = null;
1271 $this->bufferIter = $this->pageFromList(
1272 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1273 ); // updates $this->bufferAfter
1274 }
1275
1276 /**
1277 * @see Iterator::valid()
1278 * @return bool
1279 */
1280 public function valid() {
1281 if ( $this->bufferIter === null ) {
1282 return false; // some failure?
1283 } else {
1284 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1285 }
1286 }
1287
1288 /**
1289 * Get the given list portion (page)
1290 *
1291 * @param $container string Resolved container name
1292 * @param $dir string Resolved path relative to container
1293 * @param $after string|null
1294 * @param $limit integer
1295 * @param $params Array
1296 * @return Traversable|Array|null Returns null on failure
1297 */
1298 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1299 }
1300
1301 /**
1302 * Iterator for listing directories
1303 */
1304 class SwiftFileBackendDirList extends SwiftFileBackendList {
1305 /**
1306 * @see Iterator::current()
1307 * @return string|bool String (relative path) or false
1308 */
1309 public function current() {
1310 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1311 }
1312
1313 /**
1314 * @see SwiftFileBackendList::pageFromList()
1315 * @return Array|null
1316 */
1317 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1318 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1319 }
1320 }
1321
1322 /**
1323 * Iterator for listing regular files
1324 */
1325 class SwiftFileBackendFileList extends SwiftFileBackendList {
1326 /**
1327 * @see Iterator::current()
1328 * @return string|bool String (relative path) or false
1329 */
1330 public function current() {
1331 return substr( current( $this->bufferIter ), $this->suffixStart );
1332 }
1333
1334 /**
1335 * @see SwiftFileBackendList::pageFromList()
1336 * @return Array|null
1337 */
1338 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1339 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1340 }
1341 }