3 * OpenStack Swift based file backend.
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.
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.
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
21 * @ingroup FileBackend
23 * @author Aaron Schulz
27 * @brief Class for an OpenStack Swift based file backend.
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.
33 * Status messages should avoid mentioning the Swift account name.
34 * Likewise, error suppression should be used to avoid path disclosure.
36 * @ingroup FileBackend
39 class SwiftFileBackend
extends FileBackendStore
{
40 /** @var CF_Authentication */
41 protected $auth; // Swift authentication handler
42 protected $authTTL; // integer seconds
43 protected $swiftTempUrlKey; // string; shared secret value for making temp urls
44 protected $swiftAnonUser; // string; username to handle unauthenticated requests
45 protected $swiftUseCDN; // boolean; whether CloudFiles CDN is enabled
46 protected $swiftCDNExpiry; // integer; how long to cache things in the CDN
47 protected $swiftCDNPurgable; // boolean; whether object CDN purging is enabled
49 // Rados Gateway specific options
50 protected $rgwS3AccessKey; // string; S3 access key
51 protected $rgwS3SecretKey; // string; S3 authentication key
53 /** @var CF_Connection */
54 protected $conn; // Swift connection handle
55 protected $sessionStarted = 0; // integer UNIX timestamp
57 /** @var CloudFilesException */
58 protected $connException;
59 protected $connErrorTime = 0; // UNIX timestamp
64 /** @var ProcessCacheLRU */
65 protected $connContainerCache; // container object cache
68 * @see FileBackendStore::__construct()
69 * Additional $config params include:
70 * - swiftAuthUrl : Swift authentication server URL
71 * - swiftUser : Swift user used by MediaWiki (account:username)
72 * - swiftKey : Swift authentication key for the above user
73 * - swiftAuthTTL : Swift authentication TTL (seconds)
74 * - swiftTempUrlKey : Swift "X-Account-Meta-Temp-URL-Key" value on the account.
75 * Do not set this until it has been set in the backend.
76 * - swiftAnonUser : Swift user used for end-user requests (account:username).
77 * If set, then views of public containers are assumed to go
78 * through this user. If not set, then public containers are
79 * accessible to unauthenticated requests via ".r:*" in the ACL.
80 * - swiftUseCDN : Whether a Cloud Files Content Delivery Network is set up
81 * - swiftCDNExpiry : How long (in seconds) to store content in the CDN.
82 * If files may likely change, this should probably not exceed
83 * a few days. For example, deletions may take this long to apply.
84 * If object purging is enabled, however, this is not an issue.
85 * - swiftCDNPurgable : Whether object purge requests are allowed by the CDN.
86 * - shardViaHashLevels : Map of container names to sharding config with:
87 * - base : base of hash characters, 16 or 36
88 * - levels : the number of hash levels (and digits)
89 * - repeat : hash subdirectories are prefixed with all the
90 * parent hash directory names (e.g. "a/ab/abc")
91 * - cacheAuthInfo : Whether to cache authentication tokens in APC, XCache, ect.
92 * If those are not available, then the main cache will be used.
93 * This is probably insecure in shared hosting environments.
94 * - rgwS3AccessKey : Ragos Gateway S3 "access key" value on the account.
95 * Do not set this until it has been set in the backend.
96 * This is used for generating expiring pre-authenticated URLs.
97 * Only use this when using rgw and to work around
98 * http://tracker.newdream.net/issues/3454.
99 * - rgwS3SecretKey : Ragos Gateway S3 "secret key" value on the account.
100 * Do not set this until it has been set in the backend.
101 * This is used for generating expiring pre-authenticated URLs.
102 * Only use this when using rgw and to work around
103 * http://tracker.newdream.net/issues/3454.
105 public function __construct( array $config ) {
106 parent
::__construct( $config );
107 if ( !MWInit
::classExists( 'CF_Constants' ) ) {
108 throw new MWException( 'SwiftCloudFiles extension not installed.' );
111 $this->auth
= new CF_Authentication(
112 $config['swiftUser'],
114 null, // account; unused
115 $config['swiftAuthUrl']
118 $this->authTTL
= isset( $config['swiftAuthTTL'] )
119 ?
$config['swiftAuthTTL']
120 : 5 * 60; // some sane number
121 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
122 ?
$config['swiftAnonUser']
124 $this->swiftTempUrlKey
= isset( $config['swiftTempUrlKey'] )
125 ?
$config['swiftTempUrlKey']
127 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
128 ?
$config['shardViaHashLevels']
130 $this->swiftUseCDN
= isset( $config['swiftUseCDN'] )
131 ?
$config['swiftUseCDN']
133 $this->swiftCDNExpiry
= isset( $config['swiftCDNExpiry'] )
134 ?
$config['swiftCDNExpiry']
135 : 12*3600; // 12 hours is safe (tokens last 24 hours per http://docs.openstack.org)
136 $this->swiftCDNPurgable
= isset( $config['swiftCDNPurgable'] )
137 ?
$config['swiftCDNPurgable']
139 $this->rgwS3AccessKey
= isset( $config['rgwS3AccessKey'] )
140 ?
$config['rgwS3AccessKey']
142 $this->rgwS3SecretKey
= isset( $config['rgwS3SecretKey'] )
143 ?
$config['rgwS3SecretKey']
145 // Cache container information to mask latency
146 $this->memCache
= wfGetMainCache();
147 // Process cache for container info
148 $this->connContainerCache
= new ProcessCacheLRU( 300 );
149 // Cache auth token information to avoid RTTs
150 if ( !empty( $config['cacheAuthInfo'] ) ) {
151 if ( PHP_SAPI
=== 'cli' ) {
152 $this->srvCache
= wfGetMainCache(); // preferrably memcached
154 try { // look for APC, XCache, WinCache, ect...
155 $this->srvCache
= ObjectCache
::newAccelerator( array() );
156 } catch ( Exception
$e ) {}
159 $this->srvCache
= $this->srvCache ?
$this->srvCache
: new EmptyBagOStuff();
163 * @see FileBackendStore::resolveContainerPath()
166 protected function resolveContainerPath( $container, $relStoragePath ) {
167 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) { // mb_string required by CF
168 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
169 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
170 return null; // too long for Swift
172 return $relStoragePath;
176 * @see FileBackendStore::isPathUsableInternal()
179 public function isPathUsableInternal( $storagePath ) {
180 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
181 if ( $rel === null ) {
182 return false; // invalid
186 $this->getContainer( $container );
187 return true; // container exists
188 } catch ( NoSuchContainerException
$e ) {
189 } catch ( CloudFilesException
$e ) { // some other exception?
190 $this->handleException( $e, null, __METHOD__
, array( 'path' => $storagePath ) );
197 * @param $disposition string Content-Disposition header value
198 * @return string Truncated Content-Disposition header value to meet Swift limits
200 protected function truncDisp( $disposition ) {
202 foreach ( explode( ';', $disposition ) as $part ) {
203 $part = trim( $part );
204 $new = ( $res === '' ) ?
$part : "{$res};{$part}";
205 if ( strlen( $new ) <= 255 ) {
208 break; // too long; sigh
215 * @see FileBackendStore::doCreateInternal()
218 protected function doCreateInternal( array $params ) {
219 $status = Status
::newGood();
221 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
222 if ( $dstRel === null ) {
223 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
227 // (a) Check the destination container and object
229 $dContObj = $this->getContainer( $dstCont );
230 } catch ( NoSuchContainerException
$e ) {
231 $status->fatal( 'backend-fail-create', $params['dst'] );
233 } catch ( CloudFilesException
$e ) { // some other exception?
234 $this->handleException( $e, $status, __METHOD__
, $params );
238 // (b) Get a SHA-1 hash of the object
239 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
241 // (c) Actually create the object
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 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
247 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
248 // The MD5 here will be checked within Swift against its own MD5.
249 $obj->set_etag( md5( $params['content'] ) );
250 // Use the same content type as StreamFile for security
251 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
252 if ( !strlen( $obj->content_type
) ) { // special case
253 $obj->content_type
= 'unknown/unknown';
255 // Set the Content-Disposition header if requested
256 if ( isset( $params['disposition'] ) ) {
257 $obj->headers
['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
259 // Set any other custom headers if requested
260 if ( isset( $params['headers'] ) ) {
261 $obj->headers +
= $params['headers'];
263 if ( !empty( $params['async'] ) ) { // deferred
264 $op = $obj->write_async( $params['content'] );
265 $status->value
= new SwiftFileOpHandle( $this, $params, 'Create', $op );
266 $status->value
->affectedObjects
[] = $obj;
267 } else { // actually write the object in Swift
268 $obj->write( $params['content'] );
269 $this->purgeCDNCache( array( $obj ) );
271 } catch ( CDNNotEnabledException
$e ) {
272 // CDN not enabled; nothing to see here
273 } catch ( BadContentTypeException
$e ) {
274 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
275 } catch ( CloudFilesException
$e ) { // some other exception?
276 $this->handleException( $e, $status, __METHOD__
, $params );
283 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
285 protected function _getResponseCreate( CF_Async_Op
$cfOp, Status
$status, array $params ) {
287 $cfOp->getLastResponse();
288 } catch ( BadContentTypeException
$e ) {
289 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
294 * @see FileBackendStore::doStoreInternal()
297 protected function doStoreInternal( array $params ) {
298 $status = Status
::newGood();
300 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
301 if ( $dstRel === null ) {
302 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
306 // (a) Check the destination container and object
308 $dContObj = $this->getContainer( $dstCont );
309 } catch ( NoSuchContainerException
$e ) {
310 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
312 } catch ( CloudFilesException
$e ) { // some other exception?
313 $this->handleException( $e, $status, __METHOD__
, $params );
317 // (b) Get a SHA-1 hash of the object
318 wfSuppressWarnings();
319 $sha1Hash = sha1_file( $params['src'] );
321 if ( $sha1Hash === false ) { // source doesn't exist?
322 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
325 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
327 // (c) Actually store the object
329 // Create a fresh CF_Object with no fields preloaded.
330 // We don't want to preserve headers, metadata, and such.
331 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
332 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
333 // The MD5 here will be checked within Swift against its own MD5.
334 $obj->set_etag( md5_file( $params['src'] ) );
335 // Use the same content type as StreamFile for security
336 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
337 if ( !strlen( $obj->content_type
) ) { // special case
338 $obj->content_type
= 'unknown/unknown';
340 // Set the Content-Disposition header if requested
341 if ( isset( $params['disposition'] ) ) {
342 $obj->headers
['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
344 // Set any other custom headers if requested
345 if ( isset( $params['headers'] ) ) {
346 $obj->headers +
= $params['headers'];
348 if ( !empty( $params['async'] ) ) { // deferred
349 wfSuppressWarnings();
350 $fp = fopen( $params['src'], 'rb' );
353 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
355 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
356 $status->value
= new SwiftFileOpHandle( $this, $params, 'Store', $op );
357 $status->value
->resourcesToClose
[] = $fp;
358 $status->value
->affectedObjects
[] = $obj;
360 } else { // actually write the object in Swift
361 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
362 $this->purgeCDNCache( array( $obj ) );
364 } catch ( CDNNotEnabledException
$e ) {
365 // CDN not enabled; nothing to see here
366 } catch ( BadContentTypeException
$e ) {
367 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
368 } catch ( IOException
$e ) {
369 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
370 } catch ( CloudFilesException
$e ) { // some other exception?
371 $this->handleException( $e, $status, __METHOD__
, $params );
378 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
380 protected function _getResponseStore( CF_Async_Op
$cfOp, Status
$status, array $params ) {
382 $cfOp->getLastResponse();
383 } catch ( BadContentTypeException
$e ) {
384 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
385 } catch ( IOException
$e ) {
386 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
391 * @see FileBackendStore::doCopyInternal()
394 protected function doCopyInternal( array $params ) {
395 $status = Status
::newGood();
397 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
398 if ( $srcRel === null ) {
399 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
403 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
404 if ( $dstRel === null ) {
405 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
409 // (a) Check the source/destination containers and destination object
411 $sContObj = $this->getContainer( $srcCont );
412 $dContObj = $this->getContainer( $dstCont );
413 } catch ( NoSuchContainerException
$e ) {
414 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
416 } catch ( CloudFilesException
$e ) { // some other exception?
417 $this->handleException( $e, $status, __METHOD__
, $params );
421 // (b) Actually copy the file to the destination
423 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
424 $hdrs = array(); // source file headers to override with new values
425 if ( isset( $params['disposition'] ) ) {
426 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
428 if ( !empty( $params['async'] ) ) { // deferred
429 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
430 $status->value
= new SwiftFileOpHandle( $this, $params, 'Copy', $op );
431 $status->value
->affectedObjects
[] = $dstObj;
432 } else { // actually write the object in Swift
433 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
434 $this->purgeCDNCache( array( $dstObj ) );
436 } catch ( CDNNotEnabledException
$e ) {
437 // CDN not enabled; nothing to see here
438 } catch ( NoSuchObjectException
$e ) { // source object does not exist
439 if ( empty( $params['ignoreMissingSource'] ) ) {
440 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
442 } catch ( CloudFilesException
$e ) { // some other exception?
443 $this->handleException( $e, $status, __METHOD__
, $params );
450 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
452 protected function _getResponseCopy( CF_Async_Op
$cfOp, Status
$status, array $params ) {
454 $cfOp->getLastResponse();
455 } catch ( NoSuchObjectException
$e ) { // source object does not exist
456 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
461 * @see FileBackendStore::doMoveInternal()
464 protected function doMoveInternal( array $params ) {
465 $status = Status
::newGood();
467 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
468 if ( $srcRel === null ) {
469 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
473 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
474 if ( $dstRel === null ) {
475 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
479 // (a) Check the source/destination containers and destination object
481 $sContObj = $this->getContainer( $srcCont );
482 $dContObj = $this->getContainer( $dstCont );
483 } catch ( NoSuchContainerException
$e ) {
484 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
486 } catch ( CloudFilesException
$e ) { // some other exception?
487 $this->handleException( $e, $status, __METHOD__
, $params );
491 // (b) Actually move the file to the destination
493 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
494 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
495 $hdrs = array(); // source file headers to override with new values
496 if ( isset( $params['disposition'] ) ) {
497 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
499 if ( !empty( $params['async'] ) ) { // deferred
500 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
501 $status->value
= new SwiftFileOpHandle( $this, $params, 'Move', $op );
502 $status->value
->affectedObjects
[] = $srcObj;
503 $status->value
->affectedObjects
[] = $dstObj;
504 } else { // actually write the object in Swift
505 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
506 $this->purgeCDNCache( array( $srcObj ) );
507 $this->purgeCDNCache( array( $dstObj ) );
509 } catch ( CDNNotEnabledException
$e ) {
510 // CDN not enabled; nothing to see here
511 } catch ( NoSuchObjectException
$e ) { // source object does not exist
512 if ( empty( $params['ignoreMissingSource'] ) ) {
513 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
515 } catch ( CloudFilesException
$e ) { // some other exception?
516 $this->handleException( $e, $status, __METHOD__
, $params );
523 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
525 protected function _getResponseMove( CF_Async_Op
$cfOp, Status
$status, array $params ) {
527 $cfOp->getLastResponse();
528 } catch ( NoSuchObjectException
$e ) { // source object does not exist
529 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
534 * @see FileBackendStore::doDeleteInternal()
537 protected function doDeleteInternal( array $params ) {
538 $status = Status
::newGood();
540 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
541 if ( $srcRel === null ) {
542 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
547 $sContObj = $this->getContainer( $srcCont );
548 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
549 if ( !empty( $params['async'] ) ) { // deferred
550 $op = $sContObj->delete_object_async( $srcRel );
551 $status->value
= new SwiftFileOpHandle( $this, $params, 'Delete', $op );
552 $status->value
->affectedObjects
[] = $srcObj;
553 } else { // actually write the object in Swift
554 $sContObj->delete_object( $srcRel );
555 $this->purgeCDNCache( array( $srcObj ) );
557 } catch ( CDNNotEnabledException
$e ) {
558 // CDN not enabled; nothing to see here
559 } catch ( NoSuchContainerException
$e ) {
560 $status->fatal( 'backend-fail-delete', $params['src'] );
561 } catch ( NoSuchObjectException
$e ) {
562 if ( empty( $params['ignoreMissingSource'] ) ) {
563 $status->fatal( 'backend-fail-delete', $params['src'] );
565 } catch ( CloudFilesException
$e ) { // some other exception?
566 $this->handleException( $e, $status, __METHOD__
, $params );
573 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
575 protected function _getResponseDelete( CF_Async_Op
$cfOp, Status
$status, array $params ) {
577 $cfOp->getLastResponse();
578 } catch ( NoSuchContainerException
$e ) {
579 $status->fatal( 'backend-fail-delete', $params['src'] );
580 } catch ( NoSuchObjectException
$e ) {
581 if ( empty( $params['ignoreMissingSource'] ) ) {
582 $status->fatal( 'backend-fail-delete', $params['src'] );
588 * @see FileBackendStore::doDescribeInternal()
591 protected function doDescribeInternal( array $params ) {
592 $status = Status
::newGood();
594 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
595 if ( $srcRel === null ) {
596 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
600 $hdrs = isset( $params['headers'] ) ?
$params['headers'] : array();
601 // Set the Content-Disposition header if requested
602 if ( isset( $params['disposition'] ) ) {
603 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
607 $sContObj = $this->getContainer( $srcCont );
608 // Get the latest version of the current metadata
609 $srcObj = $sContObj->get_object( $srcRel,
610 $this->headersFromParams( array( 'latest' => true ) ) );
611 // Merge in the metadata updates...
612 $srcObj->headers
= $hdrs +
$srcObj->headers
;
613 $srcObj->sync_metadata(); // save to Swift
614 $this->purgeCDNCache( array( $srcObj ) );
615 } catch ( CDNNotEnabledException
$e ) {
616 // CDN not enabled; nothing to see here
617 } catch ( NoSuchContainerException
$e ) {
618 $status->fatal( 'backend-fail-describe', $params['src'] );
619 } catch ( NoSuchObjectException
$e ) {
620 $status->fatal( 'backend-fail-describe', $params['src'] );
621 } catch ( CloudFilesException
$e ) { // some other exception?
622 $this->handleException( $e, $status, __METHOD__
, $params );
629 * @see FileBackendStore::doPrepareInternal()
632 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
633 $status = Status
::newGood();
635 // (a) Check if container already exists
637 $this->getContainer( $fullCont );
638 // NoSuchContainerException not thrown: container must exist
639 return $status; // already exists
640 } catch ( NoSuchContainerException
$e ) {
641 // NoSuchContainerException thrown: container does not exist
642 } catch ( CloudFilesException
$e ) { // some other exception?
643 $this->handleException( $e, $status, __METHOD__
, $params );
647 // (b) Create container as needed
649 $contObj = $this->createContainer( $fullCont );
650 if ( !empty( $params['noAccess'] ) ) {
651 // Make container private to end-users...
652 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
654 // Make container public to end-users...
655 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
657 if ( $this->swiftUseCDN
) { // Rackspace style CDN
658 $contObj->make_public( $this->swiftCDNExpiry
);
660 } catch ( CDNNotEnabledException
$e ) {
661 // CDN not enabled; nothing to see here
662 } catch ( CloudFilesException
$e ) { // some other exception?
663 $this->handleException( $e, $status, __METHOD__
, $params );
671 * @see FileBackendStore::doSecureInternal()
674 protected function doSecureInternal( $fullCont, $dir, array $params ) {
675 $status = Status
::newGood();
676 if ( empty( $params['noAccess'] ) ) {
677 return $status; // nothing to do
680 // Restrict container from end-users...
682 // doPrepareInternal() should have been called,
683 // so the Swift container should already exist...
684 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
685 // NoSuchContainerException not thrown: container must exist
687 // Make container private to end-users...
688 $status->merge( $this->setContainerAccess(
690 array( $this->auth
->username
), // read
691 array( $this->auth
->username
) // write
693 if ( $this->swiftUseCDN
&& $contObj->is_public() ) { // Rackspace style CDN
694 $contObj->make_private();
696 } catch ( CDNNotEnabledException
$e ) {
697 // CDN not enabled; nothing to see here
698 } catch ( CloudFilesException
$e ) { // some other exception?
699 $this->handleException( $e, $status, __METHOD__
, $params );
706 * @see FileBackendStore::doPublishInternal()
709 protected function doPublishInternal( $fullCont, $dir, array $params ) {
710 $status = Status
::newGood();
712 // Unrestrict container from end-users...
714 // doPrepareInternal() should have been called,
715 // so the Swift container should already exist...
716 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
717 // NoSuchContainerException not thrown: container must exist
719 // Make container public to end-users...
720 if ( $this->swiftAnonUser
!= '' ) {
721 $status->merge( $this->setContainerAccess(
723 array( $this->auth
->username
, $this->swiftAnonUser
), // read
724 array( $this->auth
->username
, $this->swiftAnonUser
) // write
727 $status->merge( $this->setContainerAccess(
729 array( $this->auth
->username
, '.r:*' ), // read
730 array( $this->auth
->username
) // write
733 if ( $this->swiftUseCDN
&& !$contObj->is_public() ) { // Rackspace style CDN
734 $contObj->make_public();
736 } catch ( CDNNotEnabledException
$e ) {
737 // CDN not enabled; nothing to see here
738 } catch ( CloudFilesException
$e ) { // some other exception?
739 $this->handleException( $e, $status, __METHOD__
, $params );
746 * @see FileBackendStore::doCleanInternal()
749 protected function doCleanInternal( $fullCont, $dir, array $params ) {
750 $status = Status
::newGood();
752 // Only containers themselves can be removed, all else is virtual
754 return $status; // nothing to do
757 // (a) Check the container
759 $contObj = $this->getContainer( $fullCont, true );
760 } catch ( NoSuchContainerException
$e ) {
761 return $status; // ok, nothing to do
762 } catch ( CloudFilesException
$e ) { // some other exception?
763 $this->handleException( $e, $status, __METHOD__
, $params );
767 // (b) Delete the container if empty
768 if ( $contObj->object_count
== 0 ) {
770 $this->deleteContainer( $fullCont );
771 } catch ( NoSuchContainerException
$e ) {
772 return $status; // race?
773 } catch ( NonEmptyContainerException
$e ) {
774 return $status; // race? consistency delay?
775 } catch ( CloudFilesException
$e ) { // some other exception?
776 $this->handleException( $e, $status, __METHOD__
, $params );
785 * @see FileBackendStore::doFileExists()
786 * @return array|bool|null
788 protected function doGetFileStat( array $params ) {
789 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
790 if ( $srcRel === null ) {
791 return false; // invalid storage path
796 $contObj = $this->getContainer( $srcCont );
797 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
798 $this->addMissingMetadata( $srcObj, $params['src'] );
800 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
801 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
802 'size' => (int)$srcObj->content_length
,
803 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
805 } catch ( NoSuchContainerException
$e ) {
806 } catch ( NoSuchObjectException
$e ) {
807 } catch ( CloudFilesException
$e ) { // some other exception?
809 $this->handleException( $e, null, __METHOD__
, $params );
816 * Fill in any missing object metadata and save it to Swift
818 * @param $obj CF_Object
819 * @param $path string Storage path to object
820 * @return bool Success
821 * @throws Exception cloudfiles exceptions
823 protected function addMissingMetadata( CF_Object
$obj, $path ) {
824 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
825 return true; // nothing to do
827 wfProfileIn( __METHOD__
);
828 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING
);
829 $status = Status
::newGood();
830 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
831 if ( $status->isOK() ) {
832 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
834 $hash = $tmpFile->getSha1Base36();
835 if ( $hash !== false ) {
836 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
837 $obj->sync_metadata(); // save to Swift
838 wfProfileOut( __METHOD__
);
839 return true; // success
843 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING
);
844 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
845 wfProfileOut( __METHOD__
);
846 return false; // failed
850 * @see FileBackendStore::doGetFileContentsMulti()
853 protected function doGetFileContentsMulti( array $params ) {
856 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
857 // Blindly create tmp files and stream to them, catching any exception if the file does
858 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
859 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
860 $cfOps = array(); // (path => CF_Async_Op)
862 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
863 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
864 if ( $srcRel === null ) {
865 $contents[$path] = false;
870 $sContObj = $this->getContainer( $srcCont );
871 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
872 // Create a new temporary memory file...
873 $handle = fopen( 'php://temp', 'wb' );
875 $headers = $this->headersFromParams( $params );
876 if ( count( $pathBatch ) > 1 ) {
877 $cfOps[$path] = $obj->stream_async( $handle, $headers );
878 $cfOps[$path]->_file_handle
= $handle; // close this later
880 $obj->stream( $handle, $headers );
881 rewind( $handle ); // start from the beginning
882 $data = stream_get_contents( $handle );
888 } catch ( NoSuchContainerException
$e ) {
890 } catch ( NoSuchObjectException
$e ) {
892 } catch ( CloudFilesException
$e ) { // some other exception?
894 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
896 $contents[$path] = $data;
899 $batch = new CF_Async_Op_Batch( $cfOps );
900 $cfOps = $batch->execute();
901 foreach ( $cfOps as $path => $cfOp ) {
903 $cfOp->getLastResponse();
904 rewind( $cfOp->_file_handle
); // start from the beginning
905 $contents[$path] = stream_get_contents( $cfOp->_file_handle
);
906 } catch ( NoSuchContainerException
$e ) {
907 $contents[$path] = false;
908 } catch ( NoSuchObjectException
$e ) {
909 $contents[$path] = false;
910 } catch ( CloudFilesException
$e ) { // some other exception?
911 $contents[$path] = false;
912 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
914 fclose( $cfOp->_file_handle
); // close open handle
922 * @see FileBackendStore::doDirectoryExists()
925 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
927 $container = $this->getContainer( $fullCont );
928 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
929 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
930 } catch ( NoSuchContainerException
$e ) {
932 } catch ( CloudFilesException
$e ) { // some other exception?
933 $this->handleException( $e, null, __METHOD__
,
934 array( 'cont' => $fullCont, 'dir' => $dir ) );
937 return null; // error
941 * @see FileBackendStore::getDirectoryListInternal()
942 * @return SwiftFileBackendDirList
944 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
945 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
949 * @see FileBackendStore::getFileListInternal()
950 * @return SwiftFileBackendFileList
952 public function getFileListInternal( $fullCont, $dir, array $params ) {
953 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
957 * Do not call this function outside of SwiftFileBackendFileList
959 * @param $fullCont string Resolved container name
960 * @param $dir string Resolved storage directory with no trailing slash
961 * @param $after string|null Storage path of file to list items after
962 * @param $limit integer Max number of items to list
963 * @param $params Array Includes flag for 'topOnly'
964 * @return Array List of relative paths of dirs directly under $dir
966 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
968 if ( $after === INF
) {
969 return $dirs; // nothing more
971 wfProfileIn( __METHOD__
. '-' . $this->name
);
974 $container = $this->getContainer( $fullCont );
975 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
976 // Non-recursive: only list dirs right under $dir
977 if ( !empty( $params['topOnly'] ) ) {
978 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
979 foreach ( $objects as $object ) { // files and dirs
980 if ( substr( $object, -1 ) === '/' ) {
981 $dirs[] = $object; // directories end in '/'
984 // Recursive: list all dirs under $dir and its subdirs
986 // Get directory from last item of prior page
987 $lastDir = $this->getParentDir( $after ); // must be first page
988 $objects = $container->list_objects( $limit, $after, $prefix );
989 foreach ( $objects as $object ) { // files
990 $objectDir = $this->getParentDir( $object ); // directory of object
991 if ( $objectDir !== false && $objectDir !== $dir ) {
992 // Swift stores paths in UTF-8, using binary sorting.
993 // See function "create_container_table" in common/db.py.
994 // If a directory is not "greater" than the last one,
995 // then it was already listed by the calling iterator.
996 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
998 do { // add dir and all its parent dirs
999 $dirs[] = "{$pDir}/";
1000 $pDir = $this->getParentDir( $pDir );
1001 } while ( $pDir !== false // sanity
1002 && strcmp( $pDir, $lastDir ) > 0 // not done already
1003 && strlen( $pDir ) > strlen( $dir ) // within $dir
1006 $lastDir = $objectDir;
1010 if ( count( $objects ) < $limit ) {
1011 $after = INF
; // avoid a second RTT
1013 $after = end( $objects ); // update last item
1015 } catch ( NoSuchContainerException
$e ) {
1016 } catch ( CloudFilesException
$e ) { // some other exception?
1017 $this->handleException( $e, null, __METHOD__
,
1018 array( 'cont' => $fullCont, 'dir' => $dir ) );
1021 wfProfileOut( __METHOD__
. '-' . $this->name
);
1025 protected function getParentDir( $path ) {
1026 return ( strpos( $path, '/' ) !== false ) ?
dirname( $path ) : false;
1030 * Do not call this function outside of SwiftFileBackendFileList
1032 * @param $fullCont string Resolved container name
1033 * @param $dir string Resolved storage directory with no trailing slash
1034 * @param $after string|null Storage path of file to list items after
1035 * @param $limit integer Max number of items to list
1036 * @param $params Array Includes flag for 'topOnly'
1037 * @return Array List of relative paths of files under $dir
1039 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1041 if ( $after === INF
) {
1042 return $files; // nothing more
1044 wfProfileIn( __METHOD__
. '-' . $this->name
);
1047 $container = $this->getContainer( $fullCont );
1048 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
1049 // Non-recursive: only list files right under $dir
1050 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1051 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1052 foreach ( $objects as $object ) {
1053 if ( substr( $object, -1 ) !== '/' ) {
1054 $files[] = $object; // directories end in '/'
1057 // Recursive: list all files under $dir and its subdirs
1059 $objects = $container->list_objects( $limit, $after, $prefix );
1062 if ( count( $objects ) < $limit ) {
1063 $after = INF
; // avoid a second RTT
1065 $after = end( $objects ); // update last item
1067 } catch ( NoSuchContainerException
$e ) {
1068 } catch ( CloudFilesException
$e ) { // some other exception?
1069 $this->handleException( $e, null, __METHOD__
,
1070 array( 'cont' => $fullCont, 'dir' => $dir ) );
1073 wfProfileOut( __METHOD__
. '-' . $this->name
);
1078 * @see FileBackendStore::doGetFileSha1base36()
1081 protected function doGetFileSha1base36( array $params ) {
1082 $stat = $this->getFileStat( $params );
1084 return $stat['sha1'];
1091 * @see FileBackendStore::doStreamFile()
1094 protected function doStreamFile( array $params ) {
1095 $status = Status
::newGood();
1097 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1098 if ( $srcRel === null ) {
1099 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1103 $cont = $this->getContainer( $srcCont );
1104 } catch ( NoSuchContainerException
$e ) {
1105 $status->fatal( 'backend-fail-stream', $params['src'] );
1107 } catch ( CloudFilesException
$e ) { // some other exception?
1108 $this->handleException( $e, $status, __METHOD__
, $params );
1113 $output = fopen( 'php://output', 'wb' );
1114 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1115 $obj->stream( $output, $this->headersFromParams( $params ) );
1116 } catch ( NoSuchObjectException
$e ) {
1117 $status->fatal( 'backend-fail-stream', $params['src'] );
1118 } catch ( CloudFilesException
$e ) { // some other exception?
1119 $this->handleException( $e, $status, __METHOD__
, $params );
1126 * @see FileBackendStore::doGetLocalCopyMulti()
1127 * @return null|TempFSFile
1129 protected function doGetLocalCopyMulti( array $params ) {
1130 $tmpFiles = array();
1132 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1133 // Blindly create tmp files and stream to them, catching any exception if the file does
1134 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1135 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1136 $cfOps = array(); // (path => CF_Async_Op)
1138 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1139 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1140 if ( $srcRel === null ) {
1141 $tmpFiles[$path] = null;
1146 $sContObj = $this->getContainer( $srcCont );
1147 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1148 // Get source file extension
1149 $ext = FileBackend
::extensionFromPath( $path );
1150 // Create a new temporary file...
1151 $tmpFile = TempFSFile
::factory( 'localcopy_', $ext );
1153 $handle = fopen( $tmpFile->getPath(), 'wb' );
1155 $headers = $this->headersFromParams( $params );
1156 if ( count( $pathBatch ) > 1 ) {
1157 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1158 $cfOps[$path]->_file_handle
= $handle; // close this later
1160 $obj->stream( $handle, $headers );
1167 } catch ( NoSuchContainerException
$e ) {
1169 } catch ( NoSuchObjectException
$e ) {
1171 } catch ( CloudFilesException
$e ) { // some other exception?
1173 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1175 $tmpFiles[$path] = $tmpFile;
1178 $batch = new CF_Async_Op_Batch( $cfOps );
1179 $cfOps = $batch->execute();
1180 foreach ( $cfOps as $path => $cfOp ) {
1182 $cfOp->getLastResponse();
1183 } catch ( NoSuchContainerException
$e ) {
1184 $tmpFiles[$path] = null;
1185 } catch ( NoSuchObjectException
$e ) {
1186 $tmpFiles[$path] = null;
1187 } catch ( CloudFilesException
$e ) { // some other exception?
1188 $tmpFiles[$path] = null;
1189 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1191 fclose( $cfOp->_file_handle
); // close open handle
1199 * @see FileBackendStore::getFileHttpUrl()
1200 * @return string|null
1202 public function getFileHttpUrl( array $params ) {
1203 if ( $this->swiftTempUrlKey
!= '' ||
1204 ( $this->rgwS3AccessKey
!= '' && $this->rgwS3SecretKey
!= '' ) )
1206 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1207 if ( $srcRel === null ) {
1208 return null; // invalid path
1211 $ttl = isset( $params['ttl'] ) ?
$params['ttl'] : 86400;
1212 $sContObj = $this->getContainer( $srcCont );
1213 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1214 if ( $this->swiftTempUrlKey
!= '' ) {
1215 return $obj->get_temp_url( $this->swiftTempUrlKey
, $ttl, "GET" );
1216 } else { // give S3 API URL for rgw
1217 $expires = time() +
$ttl;
1218 // Path for signature starts with the bucket
1219 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1220 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1221 // Calculate the hash
1222 $signature = base64_encode( hash_hmac(
1224 "GET\n\n\n{$expires}\n{$spath}",
1225 $this->rgwS3SecretKey
,
1228 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1229 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1230 return wfAppendQuery(
1231 str_replace( '/swift/v1', '', // S3 API is the rgw default
1232 $sContObj->cfs_http
->getStorageUrl() . $spath ),
1234 'Signature' => $signature,
1235 'Expires' => $expires,
1236 'AWSAccessKeyId' => $this->rgwS3AccessKey
)
1239 } catch ( NoSuchContainerException
$e ) {
1240 } catch ( CloudFilesException
$e ) { // some other exception?
1241 $this->handleException( $e, null, __METHOD__
, $params );
1248 * @see FileBackendStore::directoriesAreVirtual()
1251 protected function directoriesAreVirtual() {
1256 * Get headers to send to Swift when reading a file based
1257 * on a FileBackend params array, e.g. that of getLocalCopy().
1258 * $params is currently only checked for a 'latest' flag.
1260 * @param $params Array
1263 protected function headersFromParams( array $params ) {
1265 if ( !empty( $params['latest'] ) ) {
1266 $hdrs[] = 'X-Newest: true';
1272 * @see FileBackendStore::doExecuteOpHandlesInternal()
1273 * @return Array List of corresponding Status objects
1275 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1276 $statuses = array();
1278 $cfOps = array(); // list of CF_Async_Op objects
1279 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1280 $cfOps[$index] = $fileOpHandle->cfOp
;
1282 $batch = new CF_Async_Op_Batch( $cfOps );
1284 $cfOps = $batch->execute();
1285 foreach ( $cfOps as $index => $cfOp ) {
1286 $status = Status
::newGood();
1287 $function = '_getResponse' . $fileOpHandles[$index]->call
;
1288 try { // catch exceptions; update status
1289 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params
);
1290 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects
);
1291 } catch ( CloudFilesException
$e ) { // some other exception?
1292 $this->handleException( $e, $status,
1293 __CLASS__
. ":$function", $fileOpHandles[$index]->params
);
1295 $statuses[$index] = $status;
1302 * Set read/write permissions for a Swift container.
1304 * $readGrps is a list of the possible criteria for a request to have
1305 * access to read a container. Each item is one of the following formats:
1306 * - account:user : Grants access if the request is by the given user
1307 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1308 * matches the expression and the request is not for a listing.
1309 * Setting this to '*' effectively makes a container public.
1310 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1311 * matches the expression and the request for a listing.
1313 * $writeGrps is a list of the possible criteria for a request to have
1314 * access to write to a container. Each item is of the following format:
1315 * - account:user : Grants access if the request is by the given user
1317 * @see http://swift.openstack.org/misc.html#acls
1319 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1320 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1322 * @param $contObj CF_Container Swift container
1323 * @param $readGrps Array List of read access routes
1324 * @param $writeGrps Array List of write access routes
1327 protected function setContainerAccess(
1328 CF_Container
$contObj, array $readGrps, array $writeGrps
1330 $creds = $contObj->cfs_auth
->export_credentials();
1332 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
1334 // Note: 10 second timeout consistent with php-cloudfiles
1335 $req = MWHttpRequest
::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1336 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1337 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1338 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1340 return $req->execute(); // should return 204
1344 * Purge the CDN cache of affected objects if CDN caching is enabled.
1345 * This is for Rackspace/Akamai CDNs.
1347 * @param $objects Array List of CF_Object items
1350 public function purgeCDNCache( array $objects ) {
1351 if ( $this->swiftUseCDN
&& $this->swiftCDNPurgable
) {
1352 foreach ( $objects as $object ) {
1354 $object->purge_from_cdn();
1355 } catch ( CDNNotEnabledException
$e ) {
1356 // CDN not enabled; nothing to see here
1357 } catch ( CloudFilesException
$e ) {
1358 $this->handleException( $e, null, __METHOD__
,
1359 array( 'cont' => $object->container
->name
, 'obj' => $object->name
) );
1366 * Get an authenticated connection handle to the Swift proxy
1368 * @throws CloudFilesException
1369 * @throws CloudFilesException|Exception
1370 * @return CF_Connection|bool False on failure
1372 protected function getConnection() {
1373 if ( $this->connException
instanceof CloudFilesException
) {
1374 if ( ( time() - $this->connErrorTime
) < 60 ) {
1375 throw $this->connException
; // failed last attempt; don't bother
1376 } else { // actually retry this time
1377 $this->connException
= null;
1378 $this->connErrorTime
= 0;
1381 // Session keys expire after a while, so we renew them periodically
1382 $reAuth = ( ( time() - $this->sessionStarted
) > $this->authTTL
);
1383 // Authenticate with proxy and get a session key...
1384 if ( !$this->conn ||
$reAuth ) {
1385 $this->sessionStarted
= 0;
1386 $this->connContainerCache
->clear();
1387 $cacheKey = $this->getCredsCacheKey( $this->auth
->username
);
1388 $creds = $this->srvCache
->get( $cacheKey ); // credentials
1389 if ( is_array( $creds ) ) { // cache hit
1390 $this->auth
->load_cached_credentials(
1391 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1392 $this->sessionStarted
= time() - ceil( $this->authTTL
/2 ); // skew for worst case
1393 } else { // cache miss
1395 $this->auth
->authenticate();
1396 $creds = $this->auth
->export_credentials();
1397 $this->srvCache
->add( $cacheKey, $creds, ceil( $this->authTTL
/2 ) ); // cache
1398 $this->sessionStarted
= time();
1399 } catch ( CloudFilesException
$e ) {
1400 $this->connException
= $e; // don't keep re-trying
1401 $this->connErrorTime
= time();
1402 throw $e; // throw it back
1405 if ( $this->conn
) { // re-authorizing?
1406 $this->conn
->close(); // close active cURL handles in CF_Http object
1408 $this->conn
= new CF_Connection( $this->auth
);
1414 * Close the connection to the Swift proxy
1418 protected function closeConnection() {
1419 if ( $this->conn
) {
1420 $this->conn
->close(); // close active cURL handles in CF_Http object
1422 $this->sessionStarted
= 0;
1423 $this->connContainerCache
->clear();
1428 * Get the cache key for a container
1430 * @param $username string
1433 private function getCredsCacheKey( $username ) {
1434 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1438 * Get a Swift container object, possibly from process cache.
1439 * Use $reCache if the file count or byte count is needed.
1441 * @param $container string Container name
1442 * @param $bypassCache bool Bypass all caches and load from Swift
1443 * @return CF_Container
1444 * @throws CloudFilesException
1446 protected function getContainer( $container, $bypassCache = false ) {
1447 $conn = $this->getConnection(); // Swift proxy connection
1448 if ( $bypassCache ) { // purge cache
1449 $this->connContainerCache
->clear( $container );
1450 } elseif ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1451 $this->primeContainerCache( array( $container ) ); // check persistent cache
1453 if ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1454 $contObj = $conn->get_container( $container );
1455 // NoSuchContainerException not thrown: container must exist
1456 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache it
1457 if ( !$bypassCache ) {
1458 $this->setContainerCache( $container, // update persistent cache
1459 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
1463 return $this->connContainerCache
->get( $container, 'obj' );
1467 * Create a Swift container
1469 * @param $container string Container name
1470 * @return CF_Container
1471 * @throws CloudFilesException
1473 protected function createContainer( $container ) {
1474 $conn = $this->getConnection(); // Swift proxy connection
1475 $contObj = $conn->create_container( $container );
1476 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache
1481 * Delete a Swift container
1483 * @param $container string Container name
1485 * @throws CloudFilesException
1487 protected function deleteContainer( $container ) {
1488 $conn = $this->getConnection(); // Swift proxy connection
1489 $this->connContainerCache
->clear( $container ); // purge
1490 $conn->delete_container( $container );
1494 * @see FileBackendStore::doPrimeContainerCache()
1497 protected function doPrimeContainerCache( array $containerInfo ) {
1499 $conn = $this->getConnection(); // Swift proxy connection
1500 foreach ( $containerInfo as $container => $info ) {
1501 $contObj = new CF_Container( $conn->cfs_auth
, $conn->cfs_http
,
1502 $container, $info['count'], $info['bytes'] );
1503 $this->connContainerCache
->set( $container, 'obj', $contObj );
1505 } catch ( CloudFilesException
$e ) { // some other exception?
1506 $this->handleException( $e, null, __METHOD__
, array() );
1511 * Log an unexpected exception for this backend.
1512 * This also sets the Status object to have a fatal error.
1514 * @param $e Exception
1515 * @param $status Status|null
1516 * @param $func string
1517 * @param $params Array
1520 protected function handleException( Exception
$e, $status, $func, array $params ) {
1521 if ( $status instanceof Status
) {
1522 if ( $e instanceof AuthenticationException
) {
1523 $status->fatal( 'backend-fail-connect', $this->name
);
1525 $status->fatal( 'backend-fail-internal', $this->name
);
1528 if ( $e->getMessage() ) {
1529 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING
);
1531 if ( $e instanceof InvalidResponseException
) { // possibly a stale token
1532 $this->srvCache
->delete( $this->getCredsCacheKey( $this->auth
->username
) );
1533 $this->closeConnection(); // force a re-connect and re-auth next time
1535 wfDebugLog( 'SwiftBackend',
1536 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
1537 ( $e->getMessage() ?
": {$e->getMessage()}" : "" )
1543 * @see FileBackendStoreOpHandle
1545 class SwiftFileOpHandle
extends FileBackendStoreOpHandle
{
1546 /** @var CF_Async_Op */
1549 public $affectedObjects = array();
1551 public function __construct( $backend, array $params, $call, CF_Async_Op
$cfOp ) {
1552 $this->backend
= $backend;
1553 $this->params
= $params;
1554 $this->call
= $call;
1555 $this->cfOp
= $cfOp;
1560 * SwiftFileBackend helper class to page through listings.
1561 * Swift also has a listing limit of 10,000 objects for sanity.
1562 * Do not use this class from places outside SwiftFileBackend.
1564 * @ingroup FileBackend
1566 abstract class SwiftFileBackendList
implements Iterator
{
1568 protected $bufferIter = array();
1569 protected $bufferAfter = null; // string; list items *after* this path
1570 protected $pos = 0; // integer
1572 protected $params = array();
1574 /** @var SwiftFileBackend */
1576 protected $container; // string; container name
1577 protected $dir; // string; storage directory
1578 protected $suffixStart; // integer
1580 const PAGE_SIZE
= 9000; // file listing buffer size
1583 * @param $backend SwiftFileBackend
1584 * @param $fullCont string Resolved container name
1585 * @param $dir string Resolved directory relative to container
1586 * @param $params Array
1588 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
1589 $this->backend
= $backend;
1590 $this->container
= $fullCont;
1592 if ( substr( $this->dir
, -1 ) === '/' ) {
1593 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
1595 if ( $this->dir
== '' ) { // whole container
1596 $this->suffixStart
= 0;
1597 } else { // dir within container
1598 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
1600 $this->params
= $params;
1604 * @see Iterator::key()
1607 public function key() {
1612 * @see Iterator::next()
1615 public function next() {
1616 // Advance to the next file in the page
1617 next( $this->bufferIter
);
1619 // Check if there are no files left in this page and
1620 // advance to the next page if this page was not empty.
1621 if ( !$this->valid() && count( $this->bufferIter
) ) {
1622 $this->bufferIter
= $this->pageFromList(
1623 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1624 ); // updates $this->bufferAfter
1629 * @see Iterator::rewind()
1632 public function rewind() {
1634 $this->bufferAfter
= null;
1635 $this->bufferIter
= $this->pageFromList(
1636 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1637 ); // updates $this->bufferAfter
1641 * @see Iterator::valid()
1644 public function valid() {
1645 if ( $this->bufferIter
=== null ) {
1646 return false; // some failure?
1648 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
1653 * Get the given list portion (page)
1655 * @param $container string Resolved container name
1656 * @param $dir string Resolved path relative to container
1657 * @param $after string|null
1658 * @param $limit integer
1659 * @param $params Array
1660 * @return Traversable|Array|null Returns null on failure
1662 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1666 * Iterator for listing directories
1668 class SwiftFileBackendDirList
extends SwiftFileBackendList
{
1670 * @see Iterator::current()
1671 * @return string|bool String (relative path) or false
1673 public function current() {
1674 return substr( current( $this->bufferIter
), $this->suffixStart
, -1 );
1678 * @see SwiftFileBackendList::pageFromList()
1679 * @return Array|null
1681 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1682 return $this->backend
->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1687 * Iterator for listing regular files
1689 class SwiftFileBackendFileList
extends SwiftFileBackendList
{
1691 * @see Iterator::current()
1692 * @return string|bool String (relative path) or false
1694 public function current() {
1695 return substr( current( $this->bufferIter
), $this->suffixStart
);
1699 * @see SwiftFileBackendList::pageFromList()
1700 * @return Array|null
1702 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1703 return $this->backend
->getFileListPageInternal( $container, $dir, $after, $limit, $params );