Merge "Don't reinvent the wheel in SpecialRecentchangeslinked::getExtraOptions"
[lhc/web/wiklou.git] / includes / filebackend / 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 $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
48
49 // Rados Gateway specific options
50 protected $rgwS3AccessKey; // string; S3 access key
51 protected $rgwS3SecretKey; // string; S3 authentication key
52
53 /** @var CF_Connection */
54 protected $conn; // Swift connection handle
55 protected $sessionStarted = 0; // integer UNIX timestamp
56
57 /** @var CloudFilesException */
58 protected $connException;
59 protected $connErrorTime = 0; // UNIX timestamp
60
61 /** @var BagOStuff */
62 protected $srvCache;
63
64 /** @var ProcessCacheLRU */
65 protected $connContainerCache; // container object cache
66
67 /**
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.
104 */
105 public function __construct( array $config ) {
106 parent::__construct( $config );
107 if ( !class_exists( 'CF_Constants' ) ) {
108 throw new MWException( 'SwiftCloudFiles extension not installed.' );
109 }
110 // Required settings
111 $this->auth = new CF_Authentication(
112 $config['swiftUser'],
113 $config['swiftKey'],
114 null, // account; unused
115 $config['swiftAuthUrl']
116 );
117 // Optional settings
118 $this->authTTL = isset( $config['swiftAuthTTL'] )
119 ? $config['swiftAuthTTL']
120 : 5 * 60; // some sane number
121 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
122 ? $config['swiftAnonUser']
123 : '';
124 $this->swiftTempUrlKey = isset( $config['swiftTempUrlKey'] )
125 ? $config['swiftTempUrlKey']
126 : '';
127 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
128 ? $config['shardViaHashLevels']
129 : '';
130 $this->swiftUseCDN = isset( $config['swiftUseCDN'] )
131 ? $config['swiftUseCDN']
132 : false;
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']
138 : true;
139 $this->rgwS3AccessKey = isset( $config['rgwS3AccessKey'] )
140 ? $config['rgwS3AccessKey']
141 : '';
142 $this->rgwS3SecretKey = isset( $config['rgwS3SecretKey'] )
143 ? $config['rgwS3SecretKey']
144 : '';
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
153 } else {
154 try { // look for APC, XCache, WinCache, ect...
155 $this->srvCache = ObjectCache::newAccelerator( array() );
156 } catch ( Exception $e ) {}
157 }
158 }
159 $this->srvCache = $this->srvCache ? $this->srvCache : new EmptyBagOStuff();
160 }
161
162 /**
163 * @see FileBackendStore::resolveContainerPath()
164 * @return null
165 */
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
171 }
172 return $relStoragePath;
173 }
174
175 public function isPathUsableInternal( $storagePath ) {
176 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
177 if ( $rel === null ) {
178 return false; // invalid
179 }
180
181 try {
182 $this->getContainer( $container );
183 return true; // container exists
184 } catch ( NoSuchContainerException $e ) {
185 } catch ( CloudFilesException $e ) { // some other exception?
186 $this->handleException( $e, null, __METHOD__, array( 'path' => $storagePath ) );
187 }
188
189 return false;
190 }
191
192 /**
193 * @param array $headers
194 * @return array
195 */
196 protected function sanitizeHdrs( array $headers ) {
197 // By default, Swift has annoyingly low maximum header value limits
198 if ( isset( $headers['Content-Disposition'] ) ) {
199 $headers['Content-Disposition'] = $this->truncDisp( $headers['Content-Disposition'] );
200 }
201 return $headers;
202 }
203
204 /**
205 * @param string $disposition Content-Disposition header value
206 * @return string Truncated Content-Disposition header value to meet Swift limits
207 */
208 protected function truncDisp( $disposition ) {
209 $res = '';
210 foreach ( explode( ';', $disposition ) as $part ) {
211 $part = trim( $part );
212 $new = ( $res === '' ) ? $part : "{$res};{$part}";
213 if ( strlen( $new ) <= 255 ) {
214 $res = $new;
215 } else {
216 break; // too long; sigh
217 }
218 }
219 return $res;
220 }
221
222 protected function doCreateInternal( array $params ) {
223 $status = Status::newGood();
224
225 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
226 if ( $dstRel === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
228 return $status;
229 }
230
231 // (a) Check the destination container and object
232 try {
233 $dContObj = $this->getContainer( $dstCont );
234 } catch ( NoSuchContainerException $e ) {
235 $status->fatal( 'backend-fail-create', $params['dst'] );
236 return $status;
237 } catch ( CloudFilesException $e ) { // some other exception?
238 $this->handleException( $e, $status, __METHOD__, $params );
239 return $status;
240 }
241
242 // (b) Get a SHA-1 hash of the object
243 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
244
245 // (c) Actually create the object
246 try {
247 // Create a fresh CF_Object with no fields preloaded.
248 // We don't want to preserve headers, metadata, and such.
249 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
250 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
251 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
252 // The MD5 here will be checked within Swift against its own MD5.
253 $obj->set_etag( md5( $params['content'] ) );
254 // Use the same content type as StreamFile for security
255 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
256 if ( !strlen( $obj->content_type ) ) { // special case
257 $obj->content_type = 'unknown/unknown';
258 }
259 // Set any other custom headers if requested
260 if ( isset( $params['headers'] ) ) {
261 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
262 }
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 ) );
270 }
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 );
277 }
278
279 return $status;
280 }
281
282 /**
283 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
284 */
285 protected function _getResponseCreate( CF_Async_Op $cfOp, Status $status, array $params ) {
286 try {
287 $cfOp->getLastResponse();
288 } catch ( BadContentTypeException $e ) {
289 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
290 }
291 }
292
293 protected function doStoreInternal( array $params ) {
294 $status = Status::newGood();
295
296 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
297 if ( $dstRel === null ) {
298 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
299 return $status;
300 }
301
302 // (a) Check the destination container and object
303 try {
304 $dContObj = $this->getContainer( $dstCont );
305 } catch ( NoSuchContainerException $e ) {
306 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
307 return $status;
308 } catch ( CloudFilesException $e ) { // some other exception?
309 $this->handleException( $e, $status, __METHOD__, $params );
310 return $status;
311 }
312
313 // (b) Get a SHA-1 hash of the object
314 wfSuppressWarnings();
315 $sha1Hash = sha1_file( $params['src'] );
316 wfRestoreWarnings();
317 if ( $sha1Hash === false ) { // source doesn't exist?
318 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
319 return $status;
320 }
321 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
322
323 // (c) Actually store the object
324 try {
325 // Create a fresh CF_Object with no fields preloaded.
326 // We don't want to preserve headers, metadata, and such.
327 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
328 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
329 // The MD5 here will be checked within Swift against its own MD5.
330 $obj->set_etag( md5_file( $params['src'] ) );
331 // Use the same content type as StreamFile for security
332 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
333 if ( !strlen( $obj->content_type ) ) { // special case
334 $obj->content_type = 'unknown/unknown';
335 }
336 // Set any other custom headers if requested
337 if ( isset( $params['headers'] ) ) {
338 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
339 }
340 if ( !empty( $params['async'] ) ) { // deferred
341 wfSuppressWarnings();
342 $fp = fopen( $params['src'], 'rb' );
343 wfRestoreWarnings();
344 if ( !$fp ) {
345 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
346 } else {
347 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
348 $status->value = new SwiftFileOpHandle( $this, $params, 'Store', $op );
349 $status->value->resourcesToClose[] = $fp;
350 $status->value->affectedObjects[] = $obj;
351 }
352 } else { // actually write the object in Swift
353 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
354 $this->purgeCDNCache( array( $obj ) );
355 }
356 } catch ( CDNNotEnabledException $e ) {
357 // CDN not enabled; nothing to see here
358 } catch ( BadContentTypeException $e ) {
359 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
360 } catch ( IOException $e ) {
361 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
362 } catch ( CloudFilesException $e ) { // some other exception?
363 $this->handleException( $e, $status, __METHOD__, $params );
364 }
365
366 return $status;
367 }
368
369 /**
370 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
371 */
372 protected function _getResponseStore( CF_Async_Op $cfOp, Status $status, array $params ) {
373 try {
374 $cfOp->getLastResponse();
375 } catch ( BadContentTypeException $e ) {
376 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
377 } catch ( IOException $e ) {
378 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
379 }
380 }
381
382 protected function doCopyInternal( array $params ) {
383 $status = Status::newGood();
384
385 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
386 if ( $srcRel === null ) {
387 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
388 return $status;
389 }
390
391 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
392 if ( $dstRel === null ) {
393 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
394 return $status;
395 }
396
397 // (a) Check the source/destination containers and destination object
398 try {
399 $sContObj = $this->getContainer( $srcCont );
400 $dContObj = $this->getContainer( $dstCont );
401 } catch ( NoSuchContainerException $e ) {
402 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
403 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
404 }
405 return $status;
406 } catch ( CloudFilesException $e ) { // some other exception?
407 $this->handleException( $e, $status, __METHOD__, $params );
408 return $status;
409 }
410
411 // (b) Actually copy the file to the destination
412 try {
413 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
414 $hdrs = array(); // source file headers to override with new values
415 // Set any other custom headers if requested
416 if ( isset( $params['headers'] ) ) {
417 $hdrs += $this->sanitizeHdrs( $params['headers'] );
418 }
419 if ( !empty( $params['async'] ) ) { // deferred
420 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
421 $status->value = new SwiftFileOpHandle( $this, $params, 'Copy', $op );
422 $status->value->affectedObjects[] = $dstObj;
423 } else { // actually write the object in Swift
424 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
425 $this->purgeCDNCache( array( $dstObj ) );
426 }
427 } catch ( CDNNotEnabledException $e ) {
428 // CDN not enabled; nothing to see here
429 } catch ( NoSuchObjectException $e ) { // source object does not exist
430 if ( empty( $params['ignoreMissingSource'] ) ) {
431 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
432 }
433 } catch ( CloudFilesException $e ) { // some other exception?
434 $this->handleException( $e, $status, __METHOD__, $params );
435 }
436
437 return $status;
438 }
439
440 /**
441 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
442 */
443 protected function _getResponseCopy( CF_Async_Op $cfOp, Status $status, array $params ) {
444 try {
445 $cfOp->getLastResponse();
446 } catch ( NoSuchObjectException $e ) { // source object does not exist
447 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
448 }
449 }
450
451 protected function doMoveInternal( array $params ) {
452 $status = Status::newGood();
453
454 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
455 if ( $srcRel === null ) {
456 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
457 return $status;
458 }
459
460 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
461 if ( $dstRel === null ) {
462 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
463 return $status;
464 }
465
466 // (a) Check the source/destination containers and destination object
467 try {
468 $sContObj = $this->getContainer( $srcCont );
469 $dContObj = $this->getContainer( $dstCont );
470 } catch ( NoSuchContainerException $e ) {
471 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
472 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
473 }
474 return $status;
475 } catch ( CloudFilesException $e ) { // some other exception?
476 $this->handleException( $e, $status, __METHOD__, $params );
477 return $status;
478 }
479
480 // (b) Actually move the file to the destination
481 try {
482 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
483 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
484 $hdrs = array(); // source file headers to override with new values
485 // Set any other custom headers if requested
486 if ( isset( $params['headers'] ) ) {
487 $hdrs += $this->sanitizeHdrs( $params['headers'] );
488 }
489 if ( !empty( $params['async'] ) ) { // deferred
490 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
491 $status->value = new SwiftFileOpHandle( $this, $params, 'Move', $op );
492 $status->value->affectedObjects[] = $srcObj;
493 $status->value->affectedObjects[] = $dstObj;
494 } else { // actually write the object in Swift
495 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
496 $this->purgeCDNCache( array( $srcObj ) );
497 $this->purgeCDNCache( array( $dstObj ) );
498 }
499 } catch ( CDNNotEnabledException $e ) {
500 // CDN not enabled; nothing to see here
501 } catch ( NoSuchObjectException $e ) { // source object does not exist
502 if ( empty( $params['ignoreMissingSource'] ) ) {
503 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
504 }
505 } catch ( CloudFilesException $e ) { // some other exception?
506 $this->handleException( $e, $status, __METHOD__, $params );
507 }
508
509 return $status;
510 }
511
512 /**
513 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
514 */
515 protected function _getResponseMove( CF_Async_Op $cfOp, Status $status, array $params ) {
516 try {
517 $cfOp->getLastResponse();
518 } catch ( NoSuchObjectException $e ) { // source object does not exist
519 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
520 }
521 }
522
523 protected function doDeleteInternal( array $params ) {
524 $status = Status::newGood();
525
526 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
527 if ( $srcRel === null ) {
528 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
529 return $status;
530 }
531
532 try {
533 $sContObj = $this->getContainer( $srcCont );
534 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
535 if ( !empty( $params['async'] ) ) { // deferred
536 $op = $sContObj->delete_object_async( $srcRel );
537 $status->value = new SwiftFileOpHandle( $this, $params, 'Delete', $op );
538 $status->value->affectedObjects[] = $srcObj;
539 } else { // actually write the object in Swift
540 $sContObj->delete_object( $srcRel );
541 $this->purgeCDNCache( array( $srcObj ) );
542 }
543 } catch ( CDNNotEnabledException $e ) {
544 // CDN not enabled; nothing to see here
545 } catch ( NoSuchContainerException $e ) {
546 if ( empty( $params['ignoreMissingSource'] ) ) {
547 $status->fatal( 'backend-fail-delete', $params['src'] );
548 }
549 } catch ( NoSuchObjectException $e ) {
550 if ( empty( $params['ignoreMissingSource'] ) ) {
551 $status->fatal( 'backend-fail-delete', $params['src'] );
552 }
553 } catch ( CloudFilesException $e ) { // some other exception?
554 $this->handleException( $e, $status, __METHOD__, $params );
555 }
556
557 return $status;
558 }
559
560 /**
561 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
562 */
563 protected function _getResponseDelete( CF_Async_Op $cfOp, Status $status, array $params ) {
564 try {
565 $cfOp->getLastResponse();
566 } catch ( NoSuchContainerException $e ) {
567 $status->fatal( 'backend-fail-delete', $params['src'] );
568 } catch ( NoSuchObjectException $e ) {
569 if ( empty( $params['ignoreMissingSource'] ) ) {
570 $status->fatal( 'backend-fail-delete', $params['src'] );
571 }
572 }
573 }
574
575 protected function doDescribeInternal( array $params ) {
576 $status = Status::newGood();
577
578 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
579 if ( $srcRel === null ) {
580 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
581 return $status;
582 }
583
584 try {
585 $sContObj = $this->getContainer( $srcCont );
586 // Get the latest version of the current metadata
587 $srcObj = $sContObj->get_object( $srcRel,
588 $this->headersFromParams( array( 'latest' => true ) ) );
589 // Merge in the metadata updates...
590 if ( isset( $params['headers'] ) ) {
591 $srcObj->headers = $this->sanitizeHdrs( $params['headers'] ) + $srcObj->headers;
592 }
593 $srcObj->sync_metadata(); // save to Swift
594 $this->purgeCDNCache( array( $srcObj ) );
595 } catch ( CDNNotEnabledException $e ) {
596 // CDN not enabled; nothing to see here
597 } catch ( NoSuchContainerException $e ) {
598 $status->fatal( 'backend-fail-describe', $params['src'] );
599 } catch ( NoSuchObjectException $e ) {
600 $status->fatal( 'backend-fail-describe', $params['src'] );
601 } catch ( CloudFilesException $e ) { // some other exception?
602 $this->handleException( $e, $status, __METHOD__, $params );
603 }
604
605 return $status;
606 }
607
608 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
609 $status = Status::newGood();
610
611 // (a) Check if container already exists
612 try {
613 $this->getContainer( $fullCont );
614 // NoSuchContainerException not thrown: container must exist
615 return $status; // already exists
616 } catch ( NoSuchContainerException $e ) {
617 // NoSuchContainerException thrown: container does not exist
618 } catch ( CloudFilesException $e ) { // some other exception?
619 $this->handleException( $e, $status, __METHOD__, $params );
620 return $status;
621 }
622
623 // (b) Create container as needed
624 try {
625 $contObj = $this->createContainer( $fullCont );
626 if ( !empty( $params['noAccess'] ) ) {
627 // Make container private to end-users...
628 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
629 } else {
630 // Make container public to end-users...
631 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
632 }
633 if ( $this->swiftUseCDN ) { // Rackspace style CDN
634 $contObj->make_public( $this->swiftCDNExpiry );
635 }
636 } catch ( CDNNotEnabledException $e ) {
637 // CDN not enabled; nothing to see here
638 } catch ( CloudFilesException $e ) { // some other exception?
639 $this->handleException( $e, $status, __METHOD__, $params );
640 return $status;
641 }
642
643 return $status;
644 }
645
646 /**
647 * @see FileBackendStore::doSecureInternal()
648 * @return Status
649 */
650 protected function doSecureInternal( $fullCont, $dir, array $params ) {
651 $status = Status::newGood();
652 if ( empty( $params['noAccess'] ) ) {
653 return $status; // nothing to do
654 }
655
656 // Restrict container from end-users...
657 try {
658 // doPrepareInternal() should have been called,
659 // so the Swift container should already exist...
660 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
661 // NoSuchContainerException not thrown: container must exist
662
663 // Make container private to end-users...
664 $status->merge( $this->setContainerAccess(
665 $contObj,
666 array( $this->auth->username ), // read
667 array( $this->auth->username ) // write
668 ) );
669 if ( $this->swiftUseCDN && $contObj->is_public() ) { // Rackspace style CDN
670 $contObj->make_private();
671 }
672 } catch ( CDNNotEnabledException $e ) {
673 // CDN not enabled; nothing to see here
674 } catch ( CloudFilesException $e ) { // some other exception?
675 $this->handleException( $e, $status, __METHOD__, $params );
676 }
677
678 return $status;
679 }
680
681 /**
682 * @see FileBackendStore::doPublishInternal()
683 * @return Status
684 */
685 protected function doPublishInternal( $fullCont, $dir, array $params ) {
686 $status = Status::newGood();
687
688 // Unrestrict container from end-users...
689 try {
690 // doPrepareInternal() should have been called,
691 // so the Swift container should already exist...
692 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
693 // NoSuchContainerException not thrown: container must exist
694
695 // Make container public to end-users...
696 if ( $this->swiftAnonUser != '' ) {
697 $status->merge( $this->setContainerAccess(
698 $contObj,
699 array( $this->auth->username, $this->swiftAnonUser ), // read
700 array( $this->auth->username, $this->swiftAnonUser ) // write
701 ) );
702 } else {
703 $status->merge( $this->setContainerAccess(
704 $contObj,
705 array( $this->auth->username, '.r:*' ), // read
706 array( $this->auth->username ) // write
707 ) );
708 }
709 if ( $this->swiftUseCDN && !$contObj->is_public() ) { // Rackspace style CDN
710 $contObj->make_public();
711 }
712 } catch ( CDNNotEnabledException $e ) {
713 // CDN not enabled; nothing to see here
714 } catch ( CloudFilesException $e ) { // some other exception?
715 $this->handleException( $e, $status, __METHOD__, $params );
716 }
717
718 return $status;
719 }
720
721 protected function doCleanInternal( $fullCont, $dir, array $params ) {
722 $status = Status::newGood();
723
724 // Only containers themselves can be removed, all else is virtual
725 if ( $dir != '' ) {
726 return $status; // nothing to do
727 }
728
729 // (a) Check the container
730 try {
731 $contObj = $this->getContainer( $fullCont, true );
732 } catch ( NoSuchContainerException $e ) {
733 return $status; // ok, nothing to do
734 } catch ( CloudFilesException $e ) { // some other exception?
735 $this->handleException( $e, $status, __METHOD__, $params );
736 return $status;
737 }
738
739 // (b) Delete the container if empty
740 if ( $contObj->object_count == 0 ) {
741 try {
742 $this->deleteContainer( $fullCont );
743 } catch ( NoSuchContainerException $e ) {
744 return $status; // race?
745 } catch ( NonEmptyContainerException $e ) {
746 return $status; // race? consistency delay?
747 } catch ( CloudFilesException $e ) { // some other exception?
748 $this->handleException( $e, $status, __METHOD__, $params );
749 return $status;
750 }
751 }
752
753 return $status;
754 }
755
756 protected function doGetFileStat( array $params ) {
757 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
758 if ( $srcRel === null ) {
759 return false; // invalid storage path
760 }
761
762 $stat = false;
763 try {
764 $contObj = $this->getContainer( $srcCont );
765 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
766 $this->addMissingMetadata( $srcObj, $params['src'] );
767 $stat = array(
768 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
769 'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
770 'size' => (int)$srcObj->content_length,
771 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
772 );
773 } catch ( NoSuchContainerException $e ) {
774 } catch ( NoSuchObjectException $e ) {
775 } catch ( CloudFilesException $e ) { // some other exception?
776 $stat = null;
777 $this->handleException( $e, null, __METHOD__, $params );
778 }
779
780 return $stat;
781 }
782
783 /**
784 * Fill in any missing object metadata and save it to Swift
785 *
786 * @param CF_Object $obj
787 * @param string $path Storage path to object
788 * @return bool Success
789 * @throws Exception cloudfiles exceptions
790 */
791 protected function addMissingMetadata( CF_Object $obj, $path ) {
792 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
793 return true; // nothing to do
794 }
795 wfProfileIn( __METHOD__ );
796 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING );
797 $status = Status::newGood();
798 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager::LOCK_UW, $status );
799 if ( $status->isOK() ) {
800 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
801 if ( $tmpFile ) {
802 $hash = $tmpFile->getSha1Base36();
803 if ( $hash !== false ) {
804 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
805 $obj->sync_metadata(); // save to Swift
806 wfProfileOut( __METHOD__ );
807 return true; // success
808 }
809 }
810 }
811 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING );
812 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
813 wfProfileOut( __METHOD__ );
814 return false; // failed
815 }
816
817 protected function doGetFileContentsMulti( array $params ) {
818 $contents = array();
819
820 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
821 // Blindly create tmp files and stream to them, catching any exception if the file does
822 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
823 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
824 $cfOps = array(); // (path => CF_Async_Op)
825
826 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
827 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
828 if ( $srcRel === null ) {
829 $contents[$path] = false;
830 continue;
831 }
832 $data = false;
833 try {
834 $sContObj = $this->getContainer( $srcCont );
835 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
836 // Create a new temporary memory file...
837 $handle = fopen( 'php://temp', 'wb' );
838 if ( $handle ) {
839 $headers = $this->headersFromParams( $params );
840 if ( count( $pathBatch ) > 1 ) {
841 $cfOps[$path] = $obj->stream_async( $handle, $headers );
842 $cfOps[$path]->_file_handle = $handle; // close this later
843 } else {
844 $obj->stream( $handle, $headers );
845 rewind( $handle ); // start from the beginning
846 $data = stream_get_contents( $handle );
847 fclose( $handle );
848 }
849 } else {
850 $data = false;
851 }
852 } catch ( NoSuchContainerException $e ) {
853 $data = false;
854 } catch ( NoSuchObjectException $e ) {
855 $data = false;
856 } catch ( CloudFilesException $e ) { // some other exception?
857 $data = false;
858 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
859 }
860 $contents[$path] = $data;
861 }
862
863 $batch = new CF_Async_Op_Batch( $cfOps );
864 $cfOps = $batch->execute();
865 foreach ( $cfOps as $path => $cfOp ) {
866 try {
867 $cfOp->getLastResponse();
868 rewind( $cfOp->_file_handle ); // start from the beginning
869 $contents[$path] = stream_get_contents( $cfOp->_file_handle );
870 } catch ( NoSuchContainerException $e ) {
871 $contents[$path] = false;
872 } catch ( NoSuchObjectException $e ) {
873 $contents[$path] = false;
874 } catch ( CloudFilesException $e ) { // some other exception?
875 $contents[$path] = false;
876 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
877 }
878 fclose( $cfOp->_file_handle ); // close open handle
879 }
880 }
881
882 return $contents;
883 }
884
885 /**
886 * @see FileBackendStore::doDirectoryExists()
887 * @return bool|null
888 */
889 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
890 try {
891 $container = $this->getContainer( $fullCont );
892 $prefix = ( $dir == '' ) ? null : "{$dir}/";
893 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
894 } catch ( NoSuchContainerException $e ) {
895 return false;
896 } catch ( CloudFilesException $e ) { // some other exception?
897 $this->handleException( $e, null, __METHOD__,
898 array( 'cont' => $fullCont, 'dir' => $dir ) );
899 }
900
901 return null; // error
902 }
903
904 /**
905 * @see FileBackendStore::getDirectoryListInternal()
906 * @return SwiftFileBackendDirList
907 */
908 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
909 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
910 }
911
912 /**
913 * @see FileBackendStore::getFileListInternal()
914 * @return SwiftFileBackendFileList
915 */
916 public function getFileListInternal( $fullCont, $dir, array $params ) {
917 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
918 }
919
920 /**
921 * Do not call this function outside of SwiftFileBackendFileList
922 *
923 * @param string $fullCont Resolved container name
924 * @param string $dir Resolved storage directory with no trailing slash
925 * @param string|null $after Storage path of file to list items after
926 * @param integer $limit Max number of items to list
927 * @param array $params Parameters for getDirectoryList()
928 * @return Array List of resolved paths of directories directly under $dir
929 */
930 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
931 $dirs = array();
932 if ( $after === INF ) {
933 return $dirs; // nothing more
934 }
935
936 wfProfileIn( __METHOD__ . '-' . $this->name );
937 try {
938 $container = $this->getContainer( $fullCont );
939 $prefix = ( $dir == '' ) ? null : "{$dir}/";
940 // Non-recursive: only list dirs right under $dir
941 if ( !empty( $params['topOnly'] ) ) {
942 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
943 foreach ( $objects as $object ) { // files and directories
944 if ( substr( $object, -1 ) === '/' ) {
945 $dirs[] = $object; // directories end in '/'
946 }
947 }
948 // Recursive: list all dirs under $dir and its subdirs
949 } else {
950 // Get directory from last item of prior page
951 $lastDir = $this->getParentDir( $after ); // must be first page
952 $objects = $container->list_objects( $limit, $after, $prefix );
953 foreach ( $objects as $object ) { // files
954 $objectDir = $this->getParentDir( $object ); // directory of object
955 if ( $objectDir !== false && $objectDir !== $dir ) {
956 // Swift stores paths in UTF-8, using binary sorting.
957 // See function "create_container_table" in common/db.py.
958 // If a directory is not "greater" than the last one,
959 // then it was already listed by the calling iterator.
960 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
961 $pDir = $objectDir;
962 do { // add dir and all its parent dirs
963 $dirs[] = "{$pDir}/";
964 $pDir = $this->getParentDir( $pDir );
965 } while ( $pDir !== false // sanity
966 && strcmp( $pDir, $lastDir ) > 0 // not done already
967 && strlen( $pDir ) > strlen( $dir ) // within $dir
968 );
969 }
970 $lastDir = $objectDir;
971 }
972 }
973 }
974 // Page on the unfiltered directory listing (what is returned may be filtered)
975 if ( count( $objects ) < $limit ) {
976 $after = INF; // avoid a second RTT
977 } else {
978 $after = end( $objects ); // update last item
979 }
980 } catch ( NoSuchContainerException $e ) {
981 } catch ( CloudFilesException $e ) { // some other exception?
982 $this->handleException( $e, null, __METHOD__,
983 array( 'cont' => $fullCont, 'dir' => $dir ) );
984 }
985 wfProfileOut( __METHOD__ . '-' . $this->name );
986
987 return $dirs;
988 }
989
990 protected function getParentDir( $path ) {
991 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
992 }
993
994 /**
995 * Do not call this function outside of SwiftFileBackendFileList
996 *
997 * @param string $fullCont Resolved container name
998 * @param string $dir Resolved storage directory with no trailing slash
999 * @param string|null $after Storage path of file to list items after
1000 * @param integer $limit Max number of items to list
1001 * @param array $params Parameters for getDirectoryList()
1002 * @return Array List of resolved paths of files under $dir
1003 */
1004 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1005 $files = array();
1006 if ( $after === INF ) {
1007 return $files; // nothing more
1008 }
1009
1010 wfProfileIn( __METHOD__ . '-' . $this->name );
1011 try {
1012 $container = $this->getContainer( $fullCont );
1013 $prefix = ( $dir == '' ) ? null : "{$dir}/";
1014 // Non-recursive: only list files right under $dir
1015 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1016 if ( !empty( $params['adviseStat'] ) ) {
1017 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1018 // Note: get_objects() does not include directories
1019 $objects = $this->loadObjectListing( $params, $dir,
1020 $container->get_objects( $limit, $after, $prefix, null, '/' ) );
1021 $files = $objects;
1022 } else {
1023 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1024 foreach ( $objects as $object ) { // files and directories
1025 if ( substr( $object, -1 ) !== '/' ) {
1026 $files[] = $object; // directories end in '/'
1027 }
1028 }
1029 }
1030 // Recursive: list all files under $dir and its subdirs
1031 } else { // files
1032 if ( !empty( $params['adviseStat'] ) ) {
1033 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1034 $objects = $this->loadObjectListing( $params, $dir,
1035 $container->get_objects( $limit, $after, $prefix ) );
1036 } else {
1037 $objects = $container->list_objects( $limit, $after, $prefix );
1038 }
1039 $files = $objects;
1040 }
1041 // Page on the unfiltered object listing (what is returned may be filtered)
1042 if ( count( $objects ) < $limit ) {
1043 $after = INF; // avoid a second RTT
1044 } else {
1045 $after = end( $objects ); // update last item
1046 }
1047 } catch ( NoSuchContainerException $e ) {
1048 } catch ( CloudFilesException $e ) { // some other exception?
1049 $this->handleException( $e, null, __METHOD__,
1050 array( 'cont' => $fullCont, 'dir' => $dir ) );
1051 }
1052 wfProfileOut( __METHOD__ . '-' . $this->name );
1053
1054 return $files;
1055 }
1056
1057 /**
1058 * Load a list of objects that belong under $dir into stat cache
1059 * and return a list of the names of the objects in the same order.
1060 *
1061 * @param array $params Parameters for getDirectoryList()
1062 * @param string $dir Resolved container directory path
1063 * @param array $cfObjects List of CF_Object items
1064 * @return array List of object names
1065 */
1066 private function loadObjectListing( array $params, $dir, array $cfObjects ) {
1067 $names = array();
1068 $storageDir = rtrim( $params['dir'], '/' );
1069 $suffixStart = ( $dir === '' ) ? 0 : strlen( $dir ) + 1; // size of "path/to/dir/"
1070 // Iterate over the list *backwards* as this primes the stat cache, which is LRU.
1071 // If this fills the cache and the caller stats an uncached file before stating
1072 // the ones on the listing, there would be zero cache hits if this went forwards.
1073 for ( end( $cfObjects ); key( $cfObjects ) !== null; prev( $cfObjects ) ) {
1074 $object = current( $cfObjects );
1075 $path = "{$storageDir}/" . substr( $object->name, $suffixStart );
1076 $val = array(
1077 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
1078 'mtime' => wfTimestamp( TS_MW, $object->last_modified ),
1079 'size' => (int)$object->content_length,
1080 'latest' => false // eventually consistent
1081 );
1082 $this->cheapCache->set( $path, 'stat', $val );
1083 $names[] = $object->name;
1084 }
1085 return array_reverse( $names ); // keep the paths in original order
1086 }
1087
1088 protected function doGetFileSha1base36( array $params ) {
1089 $stat = $this->getFileStat( $params );
1090 if ( $stat ) {
1091 if ( !isset( $stat['sha1'] ) ) {
1092 // Stat entries filled by file listings don't include SHA1
1093 $this->clearCache( array( $params['src'] ) );
1094 $stat = $this->getFileStat( $params );
1095 }
1096 return $stat['sha1'];
1097 } else {
1098 return false;
1099 }
1100 }
1101
1102 protected function doStreamFile( array $params ) {
1103 $status = Status::newGood();
1104
1105 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1106 if ( $srcRel === null ) {
1107 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1108 }
1109
1110 try {
1111 $cont = $this->getContainer( $srcCont );
1112 } catch ( NoSuchContainerException $e ) {
1113 $status->fatal( 'backend-fail-stream', $params['src'] );
1114 return $status;
1115 } catch ( CloudFilesException $e ) { // some other exception?
1116 $this->handleException( $e, $status, __METHOD__, $params );
1117 return $status;
1118 }
1119
1120 try {
1121 $output = fopen( 'php://output', 'wb' );
1122 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1123 $obj->stream( $output, $this->headersFromParams( $params ) );
1124 } catch ( NoSuchObjectException $e ) {
1125 $status->fatal( 'backend-fail-stream', $params['src'] );
1126 } catch ( CloudFilesException $e ) { // some other exception?
1127 $this->handleException( $e, $status, __METHOD__, $params );
1128 }
1129
1130 return $status;
1131 }
1132
1133 protected function doGetLocalCopyMulti( array $params ) {
1134 $tmpFiles = array();
1135
1136 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1137 // Blindly create tmp files and stream to them, catching any exception if the file does
1138 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1139 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1140 $cfOps = array(); // (path => CF_Async_Op)
1141
1142 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1143 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1144 if ( $srcRel === null ) {
1145 $tmpFiles[$path] = null;
1146 continue;
1147 }
1148 $tmpFile = null;
1149 try {
1150 $sContObj = $this->getContainer( $srcCont );
1151 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1152 // Get source file extension
1153 $ext = FileBackend::extensionFromPath( $path );
1154 // Create a new temporary file...
1155 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
1156 if ( $tmpFile ) {
1157 $handle = fopen( $tmpFile->getPath(), 'wb' );
1158 if ( $handle ) {
1159 $headers = $this->headersFromParams( $params );
1160 if ( count( $pathBatch ) > 1 ) {
1161 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1162 $cfOps[$path]->_file_handle = $handle; // close this later
1163 } else {
1164 $obj->stream( $handle, $headers );
1165 fclose( $handle );
1166 }
1167 } else {
1168 $tmpFile = null;
1169 }
1170 }
1171 } catch ( NoSuchContainerException $e ) {
1172 $tmpFile = null;
1173 } catch ( NoSuchObjectException $e ) {
1174 $tmpFile = null;
1175 } catch ( CloudFilesException $e ) { // some other exception?
1176 $tmpFile = null;
1177 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1178 }
1179 $tmpFiles[$path] = $tmpFile;
1180 }
1181
1182 $batch = new CF_Async_Op_Batch( $cfOps );
1183 $cfOps = $batch->execute();
1184 foreach ( $cfOps as $path => $cfOp ) {
1185 try {
1186 $cfOp->getLastResponse();
1187 } catch ( NoSuchContainerException $e ) {
1188 $tmpFiles[$path] = null;
1189 } catch ( NoSuchObjectException $e ) {
1190 $tmpFiles[$path] = null;
1191 } catch ( CloudFilesException $e ) { // some other exception?
1192 $tmpFiles[$path] = null;
1193 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1194 }
1195 fclose( $cfOp->_file_handle ); // close open handle
1196 }
1197 }
1198
1199 return $tmpFiles;
1200 }
1201
1202 public function getFileHttpUrl( array $params ) {
1203 if ( $this->swiftTempUrlKey != '' ||
1204 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' ) )
1205 {
1206 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1207 if ( $srcRel === null ) {
1208 return null; // invalid path
1209 }
1210 try {
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(
1223 'sha1',
1224 "GET\n\n\n{$expires}\n{$spath}",
1225 $this->rgwS3SecretKey,
1226 true // raw
1227 ) );
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 ),
1233 array(
1234 'Signature' => $signature,
1235 'Expires' => $expires,
1236 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1237 );
1238 }
1239 } catch ( NoSuchContainerException $e ) {
1240 } catch ( CloudFilesException $e ) { // some other exception?
1241 $this->handleException( $e, null, __METHOD__, $params );
1242 }
1243 }
1244 return null;
1245 }
1246
1247 protected function directoriesAreVirtual() {
1248 return true;
1249 }
1250
1251 /**
1252 * Get headers to send to Swift when reading a file based
1253 * on a FileBackend params array, e.g. that of getLocalCopy().
1254 * $params is currently only checked for a 'latest' flag.
1255 *
1256 * @param array $params
1257 * @return Array
1258 */
1259 protected function headersFromParams( array $params ) {
1260 $hdrs = array();
1261 if ( !empty( $params['latest'] ) ) {
1262 $hdrs[] = 'X-Newest: true';
1263 }
1264 return $hdrs;
1265 }
1266
1267 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1268 $statuses = array();
1269
1270 $cfOps = array(); // list of CF_Async_Op objects
1271 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1272 $cfOps[$index] = $fileOpHandle->cfOp;
1273 }
1274 $batch = new CF_Async_Op_Batch( $cfOps );
1275
1276 $cfOps = $batch->execute();
1277 foreach ( $cfOps as $index => $cfOp ) {
1278 $status = Status::newGood();
1279 $function = '_getResponse' . $fileOpHandles[$index]->call;
1280 try { // catch exceptions; update status
1281 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params );
1282 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects );
1283 } catch ( CloudFilesException $e ) { // some other exception?
1284 $this->handleException( $e, $status,
1285 __CLASS__ . ":$function", $fileOpHandles[$index]->params );
1286 }
1287 $statuses[$index] = $status;
1288 }
1289
1290 return $statuses;
1291 }
1292
1293 /**
1294 * Set read/write permissions for a Swift container.
1295 *
1296 * $readGrps is a list of the possible criteria for a request to have
1297 * access to read a container. Each item is one of the following formats:
1298 * - account:user : Grants access if the request is by the given user
1299 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1300 * matches the expression and the request is not for a listing.
1301 * Setting this to '*' effectively makes a container public.
1302 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1303 * matches the expression and the request is for a listing.
1304 *
1305 * $writeGrps is a list of the possible criteria for a request to have
1306 * access to write to a container. Each item is of the following format:
1307 * - account:user : Grants access if the request is by the given user
1308 *
1309 * @see http://swift.openstack.org/misc.html#acls
1310 *
1311 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1312 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1313 *
1314 * @param CF_Container $contObj Swift container
1315 * @param array $readGrps List of read access routes
1316 * @param array $writeGrps List of write access routes
1317 * @return Status
1318 */
1319 protected function setContainerAccess(
1320 CF_Container $contObj, array $readGrps, array $writeGrps
1321 ) {
1322 $creds = $contObj->cfs_auth->export_credentials();
1323
1324 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1325
1326 // Note: 10 second timeout consistent with php-cloudfiles
1327 $req = MWHttpRequest::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1328 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1329 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1330 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1331
1332 return $req->execute(); // should return 204
1333 }
1334
1335 /**
1336 * Purge the CDN cache of affected objects if CDN caching is enabled.
1337 * This is for Rackspace/Akamai CDNs.
1338 *
1339 * @param array $objects List of CF_Object items
1340 * @return void
1341 */
1342 public function purgeCDNCache( array $objects ) {
1343 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1344 foreach ( $objects as $object ) {
1345 try {
1346 $object->purge_from_cdn();
1347 } catch ( CDNNotEnabledException $e ) {
1348 // CDN not enabled; nothing to see here
1349 } catch ( CloudFilesException $e ) {
1350 $this->handleException( $e, null, __METHOD__,
1351 array( 'cont' => $object->container->name, 'obj' => $object->name ) );
1352 }
1353 }
1354 }
1355 }
1356
1357 /**
1358 * Get an authenticated connection handle to the Swift proxy
1359 *
1360 * @throws CloudFilesException
1361 * @throws CloudFilesException|Exception
1362 * @return CF_Connection|bool False on failure
1363 */
1364 protected function getConnection() {
1365 if ( $this->connException instanceof CloudFilesException ) {
1366 if ( ( time() - $this->connErrorTime ) < 60 ) {
1367 throw $this->connException; // failed last attempt; don't bother
1368 } else { // actually retry this time
1369 $this->connException = null;
1370 $this->connErrorTime = 0;
1371 }
1372 }
1373 // Session keys expire after a while, so we renew them periodically
1374 $reAuth = ( ( time() - $this->sessionStarted ) > $this->authTTL );
1375 // Authenticate with proxy and get a session key...
1376 if ( !$this->conn || $reAuth ) {
1377 $this->sessionStarted = 0;
1378 $this->connContainerCache->clear();
1379 $cacheKey = $this->getCredsCacheKey( $this->auth->username );
1380 $creds = $this->srvCache->get( $cacheKey ); // credentials
1381 if ( is_array( $creds ) ) { // cache hit
1382 $this->auth->load_cached_credentials(
1383 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1384 $this->sessionStarted = time() - ceil( $this->authTTL / 2 ); // skew for worst case
1385 } else { // cache miss
1386 try {
1387 $this->auth->authenticate();
1388 $creds = $this->auth->export_credentials();
1389 $this->srvCache->add( $cacheKey, $creds, ceil( $this->authTTL / 2 ) ); // cache
1390 $this->sessionStarted = time();
1391 } catch ( CloudFilesException $e ) {
1392 $this->connException = $e; // don't keep re-trying
1393 $this->connErrorTime = time();
1394 throw $e; // throw it back
1395 }
1396 }
1397 if ( $this->conn ) { // re-authorizing?
1398 $this->conn->close(); // close active cURL handles in CF_Http object
1399 }
1400 $this->conn = new CF_Connection( $this->auth );
1401 }
1402 return $this->conn;
1403 }
1404
1405 /**
1406 * Close the connection to the Swift proxy
1407 *
1408 * @return void
1409 */
1410 protected function closeConnection() {
1411 if ( $this->conn ) {
1412 $this->conn->close(); // close active cURL handles in CF_Http object
1413 $this->conn = null;
1414 $this->sessionStarted = 0;
1415 $this->connContainerCache->clear();
1416 }
1417 }
1418
1419 /**
1420 * Get the cache key for a container
1421 *
1422 * @param string $username
1423 * @return string
1424 */
1425 private function getCredsCacheKey( $username ) {
1426 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1427 }
1428
1429 /**
1430 * Get a Swift container object, possibly from process cache.
1431 * Use $reCache if the file count or byte count is needed.
1432 *
1433 * @param string $container Container name
1434 * @param bool $bypassCache Bypass all caches and load from Swift
1435 * @return CF_Container
1436 * @throws CloudFilesException
1437 */
1438 protected function getContainer( $container, $bypassCache = false ) {
1439 $conn = $this->getConnection(); // Swift proxy connection
1440 if ( $bypassCache ) { // purge cache
1441 $this->connContainerCache->clear( $container );
1442 } elseif ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1443 $this->primeContainerCache( array( $container ) ); // check persistent cache
1444 }
1445 if ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1446 $contObj = $conn->get_container( $container );
1447 // NoSuchContainerException not thrown: container must exist
1448 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache it
1449 if ( !$bypassCache ) {
1450 $this->setContainerCache( $container, // update persistent cache
1451 array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
1452 );
1453 }
1454 }
1455 return $this->connContainerCache->get( $container, 'obj' );
1456 }
1457
1458 /**
1459 * Create a Swift container
1460 *
1461 * @param string $container Container name
1462 * @return CF_Container
1463 * @throws CloudFilesException
1464 */
1465 protected function createContainer( $container ) {
1466 $conn = $this->getConnection(); // Swift proxy connection
1467 $contObj = $conn->create_container( $container );
1468 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache
1469 return $contObj;
1470 }
1471
1472 /**
1473 * Delete a Swift container
1474 *
1475 * @param string $container Container name
1476 * @return void
1477 * @throws CloudFilesException
1478 */
1479 protected function deleteContainer( $container ) {
1480 $conn = $this->getConnection(); // Swift proxy connection
1481 $this->connContainerCache->clear( $container ); // purge
1482 $conn->delete_container( $container );
1483 }
1484
1485 protected function doPrimeContainerCache( array $containerInfo ) {
1486 try {
1487 $conn = $this->getConnection(); // Swift proxy connection
1488 foreach ( $containerInfo as $container => $info ) {
1489 $contObj = new CF_Container( $conn->cfs_auth, $conn->cfs_http,
1490 $container, $info['count'], $info['bytes'] );
1491 $this->connContainerCache->set( $container, 'obj', $contObj );
1492 }
1493 } catch ( CloudFilesException $e ) { // some other exception?
1494 $this->handleException( $e, null, __METHOD__, array() );
1495 }
1496 }
1497
1498 /**
1499 * Log an unexpected exception for this backend.
1500 * This also sets the Status object to have a fatal error.
1501 *
1502 * @param Exception $e
1503 * @param Status $status|null
1504 * @param string $func
1505 * @param array $params
1506 * @return void
1507 */
1508 protected function handleException( Exception $e, $status, $func, array $params ) {
1509 if ( $status instanceof Status ) {
1510 if ( $e instanceof AuthenticationException ) {
1511 $status->fatal( 'backend-fail-connect', $this->name );
1512 } else {
1513 $status->fatal( 'backend-fail-internal', $this->name );
1514 }
1515 }
1516 if ( $e->getMessage() ) {
1517 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1518 }
1519 if ( $e instanceof InvalidResponseException ) { // possibly a stale token
1520 $this->srvCache->delete( $this->getCredsCacheKey( $this->auth->username ) );
1521 $this->closeConnection(); // force a re-connect and re-auth next time
1522 }
1523 wfDebugLog( 'SwiftBackend',
1524 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1525 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1526 );
1527 }
1528 }
1529
1530 /**
1531 * @see FileBackendStoreOpHandle
1532 */
1533 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1534 /** @var CF_Async_Op */
1535 public $cfOp;
1536 /** @var Array */
1537 public $affectedObjects = array();
1538
1539 /**
1540 * @param SwiftFileBackend $backend
1541 * @param array $params
1542 * @param string $call
1543 * @param CF_Async_Op $cfOp
1544 */
1545 public function __construct(
1546 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1547 ) {
1548 $this->backend = $backend;
1549 $this->params = $params;
1550 $this->call = $call;
1551 $this->cfOp = $cfOp;
1552 }
1553 }
1554
1555 /**
1556 * SwiftFileBackend helper class to page through listings.
1557 * Swift also has a listing limit of 10,000 objects for sanity.
1558 * Do not use this class from places outside SwiftFileBackend.
1559 *
1560 * @ingroup FileBackend
1561 */
1562 abstract class SwiftFileBackendList implements Iterator {
1563 /** @var Array */
1564 protected $bufferIter = array();
1565 protected $bufferAfter = null; // string; list items *after* this path
1566 protected $pos = 0; // integer
1567 /** @var Array */
1568 protected $params = array();
1569
1570 /** @var SwiftFileBackend */
1571 protected $backend;
1572 protected $container; // string; container name
1573 protected $dir; // string; storage directory
1574 protected $suffixStart; // integer
1575
1576 const PAGE_SIZE = 9000; // file listing buffer size
1577
1578 /**
1579 * @param SwiftFileBackend $backend
1580 * @param string $fullCont Resolved container name
1581 * @param string $dir Resolved directory relative to container
1582 * @param array $params
1583 */
1584 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1585 $this->backend = $backend;
1586 $this->container = $fullCont;
1587 $this->dir = $dir;
1588 if ( substr( $this->dir, -1 ) === '/' ) {
1589 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1590 }
1591 if ( $this->dir == '' ) { // whole container
1592 $this->suffixStart = 0;
1593 } else { // dir within container
1594 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1595 }
1596 $this->params = $params;
1597 }
1598
1599 /**
1600 * @see Iterator::key()
1601 * @return integer
1602 */
1603 public function key() {
1604 return $this->pos;
1605 }
1606
1607 /**
1608 * @see Iterator::next()
1609 * @return void
1610 */
1611 public function next() {
1612 // Advance to the next file in the page
1613 next( $this->bufferIter );
1614 ++$this->pos;
1615 // Check if there are no files left in this page and
1616 // advance to the next page if this page was not empty.
1617 if ( !$this->valid() && count( $this->bufferIter ) ) {
1618 $this->bufferIter = $this->pageFromList(
1619 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1620 ); // updates $this->bufferAfter
1621 }
1622 }
1623
1624 /**
1625 * @see Iterator::rewind()
1626 * @return void
1627 */
1628 public function rewind() {
1629 $this->pos = 0;
1630 $this->bufferAfter = null;
1631 $this->bufferIter = $this->pageFromList(
1632 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1633 ); // updates $this->bufferAfter
1634 }
1635
1636 /**
1637 * @see Iterator::valid()
1638 * @return bool
1639 */
1640 public function valid() {
1641 if ( $this->bufferIter === null ) {
1642 return false; // some failure?
1643 } else {
1644 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1645 }
1646 }
1647
1648 /**
1649 * Get the given list portion (page)
1650 *
1651 * @param string $container Resolved container name
1652 * @param string $dir Resolved path relative to container
1653 * @param string $after|null
1654 * @param integer $limit
1655 * @param array $params
1656 * @return Traversable|Array|null Returns null on failure
1657 */
1658 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1659 }
1660
1661 /**
1662 * Iterator for listing directories
1663 */
1664 class SwiftFileBackendDirList extends SwiftFileBackendList {
1665 /**
1666 * @see Iterator::current()
1667 * @return string|bool String (relative path) or false
1668 */
1669 public function current() {
1670 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1671 }
1672
1673 /**
1674 * @see SwiftFileBackendList::pageFromList()
1675 * @return Array|null
1676 */
1677 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1678 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1679 }
1680 }
1681
1682 /**
1683 * Iterator for listing regular files
1684 */
1685 class SwiftFileBackendFileList extends SwiftFileBackendList {
1686 /**
1687 * @see Iterator::current()
1688 * @return string|bool String (relative path) or false
1689 */
1690 public function current() {
1691 return substr( current( $this->bufferIter ), $this->suffixStart );
1692 }
1693
1694 /**
1695 * @see SwiftFileBackendList::pageFromList()
1696 * @return Array|null
1697 */
1698 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1699 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1700 }
1701 }