b5d2aaae5024118343f1a01e919e744b9043496c
[lhc/web/wiklou.git] / includes / filerepo / backend / SwiftFileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Russ Nelson
6 * @author Aaron Schulz
7 */
8
9 /**
10 * Class for an OpenStack Swift based file backend.
11 *
12 * This requires the SwiftCloudFiles MediaWiki extension, which includes
13 * the php-cloudfiles library (https://github.com/rackspace/php-cloudfiles).
14 * php-cloudfiles requires the curl, fileinfo, and mb_string PHP extensions.
15 *
16 * Status messages should avoid mentioning the Swift account name
17 * Likewise, error suppression should be used to avoid path disclosure.
18 *
19 * @ingroup FileBackend
20 * @since 1.19
21 */
22 class SwiftFileBackend extends FileBackend {
23 /** @var CF_Authentication */
24 protected $auth; // Swift authentication handler
25 protected $authTTL; // integer seconds
26 protected $swiftAnonUser; // string; username to handle unauthenticated requests
27 protected $maxContCacheSize = 20; // integer; max containers with entries
28
29 /** @var CF_Connection */
30 protected $conn; // Swift connection handle
31 protected $connStarted = 0; // integer UNIX timestamp
32 protected $connContainers = array(); // container object cache
33
34 /**
35 * @see FileBackend::__construct()
36 * Additional $config params include:
37 * swiftAuthUrl : Swift authentication server URL
38 * swiftUser : Swift user used by MediaWiki (account:username)
39 * swiftKey : Swift authentication key for the above user
40 * swiftAuthTTL : Swift authentication TTL (seconds)
41 * swiftAnonUser : Swift user used for end-user requests (account:username)
42 * shardViaHashLevels : Map of container names to the number of hash levels
43 */
44 public function __construct( array $config ) {
45 parent::__construct( $config );
46 // Required settings
47 $this->auth = new CF_Authentication(
48 $config['swiftUser'],
49 $config['swiftKey'],
50 null, // account; unused
51 $config['swiftAuthUrl']
52 );
53 // Optional settings
54 $this->authTTL = isset( $config['swiftAuthTTL'] )
55 ? $config['swiftAuthTTL']
56 : 120; // some sane number
57 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
58 ? $config['swiftAnonUser']
59 : '';
60 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
61 ? $config['shardViaHashLevels']
62 : '';
63 }
64
65 /**
66 * @see FileBackend::resolveContainerPath()
67 */
68 protected function resolveContainerPath( $container, $relStoragePath ) {
69 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
70 return null; // too long for Swift
71 }
72 return $relStoragePath;
73 }
74
75 /**
76 * @see FileBackend::isPathUsableInternal()
77 */
78 public function isPathUsableInternal( $storagePath ) {
79 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
80 if ( $rel === null ) {
81 return false; // invalid
82 }
83
84 try {
85 $this->getContainer( $container );
86 return true; // container exists
87 } catch ( NoSuchContainerException $e ) {
88 } catch ( InvalidResponseException $e ) {
89 } catch ( Exception $e ) { // some other exception?
90 $this->logException( $e, __METHOD__, array( 'path' => $storagePath ) );
91 }
92
93 return false;
94 }
95
96 /**
97 * @see FileBackend::doCopyInternal()
98 */
99 protected function doCreateInternal( array $params ) {
100 $status = Status::newGood();
101
102 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
103 if ( $dstRel === null ) {
104 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
105 return $status;
106 }
107
108 // (a) Check the destination container and object
109 try {
110 $dContObj = $this->getContainer( $dstCont );
111 if ( empty( $params['overwrite'] ) ) {
112 $destObj = $dContObj->create_object( $dstRel );
113 // Check if the object already exists (fields populated)
114 if ( $destObj->last_modified ) {
115 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
116 return $status;
117 }
118 }
119 } catch ( NoSuchContainerException $e ) {
120 $status->fatal( 'backend-fail-create', $params['dst'] );
121 return $status;
122 } catch ( InvalidResponseException $e ) {
123 $status->fatal( 'backend-fail-connect', $this->name );
124 return $status;
125 } catch ( Exception $e ) { // some other exception?
126 $status->fatal( 'backend-fail-internal', $this->name );
127 $this->logException( $e, __METHOD__, $params );
128 return $status;
129 }
130
131 // (b) Get a SHA-1 hash of the object
132 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
133
134 // (c) Actually create the object
135 try {
136 // Create a fresh CF_Object with no fields preloaded.
137 // We don't want to preserve headers, metadata, and such.
138 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
139 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
140 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
141 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
142 // The MD5 here will be checked within Swift against its own MD5.
143 $obj->set_etag( md5( $params['content'] ) );
144 // Use the same content type as StreamFile for security
145 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
146 // Actually write the object in Swift
147 $obj->write( $params['content'] );
148 } catch ( BadContentTypeException $e ) {
149 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
150 } catch ( InvalidResponseException $e ) {
151 $status->fatal( 'backend-fail-connect', $this->name );
152 } catch ( Exception $e ) { // some other exception?
153 $status->fatal( 'backend-fail-internal', $this->name );
154 $this->logException( $e, __METHOD__, $params );
155 }
156
157 return $status;
158 }
159
160 /**
161 * @see FileBackend::doStoreInternal()
162 */
163 protected function doStoreInternal( array $params ) {
164 $status = Status::newGood();
165
166 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
167 if ( $dstRel === null ) {
168 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
169 return $status;
170 }
171
172 // (a) Check the destination container and object
173 try {
174 $dContObj = $this->getContainer( $dstCont );
175 if ( empty( $params['overwrite'] ) ) {
176 $destObj = $dContObj->create_object( $dstRel );
177 // Check if the object already exists (fields populated)
178 if ( $destObj->last_modified ) {
179 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
180 return $status;
181 }
182 }
183 } catch ( NoSuchContainerException $e ) {
184 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
185 return $status;
186 } catch ( InvalidResponseException $e ) {
187 $status->fatal( 'backend-fail-connect', $this->name );
188 return $status;
189 } catch ( Exception $e ) { // some other exception?
190 $status->fatal( 'backend-fail-internal', $this->name );
191 $this->logException( $e, __METHOD__, $params );
192 return $status;
193 }
194
195 // (b) Get a SHA-1 hash of the object
196 $sha1Hash = sha1_file( $params['src'] );
197 if ( $sha1Hash === false ) { // source doesn't exist?
198 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
199 return $status;
200 }
201 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
202
203 // (c) Actually store the object
204 try {
205 // Create a fresh CF_Object with no fields preloaded.
206 // We don't want to preserve headers, metadata, and such.
207 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
208 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
209 $obj->metadata = array( 'Sha1base36' => $sha1Hash );
210 // The MD5 here will be checked within Swift against its own MD5.
211 $obj->set_etag( md5_file( $params['src'] ) );
212 // Use the same content type as StreamFile for security
213 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
214 // Actually write the object in Swift
215 $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
216 } catch ( BadContentTypeException $e ) {
217 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
218 } catch ( IOException $e ) {
219 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
220 } catch ( InvalidResponseException $e ) {
221 $status->fatal( 'backend-fail-connect', $this->name );
222 } catch ( Exception $e ) { // some other exception?
223 $status->fatal( 'backend-fail-internal', $this->name );
224 $this->logException( $e, __METHOD__, $params );
225 }
226
227 return $status;
228 }
229
230 /**
231 * @see FileBackend::doCopyInternal()
232 */
233 protected function doCopyInternal( array $params ) {
234 $status = Status::newGood();
235
236 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
237 if ( $srcRel === null ) {
238 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
239 return $status;
240 }
241
242 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
243 if ( $dstRel === null ) {
244 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
245 return $status;
246 }
247
248 // (a) Check the source/destination containers and destination object
249 try {
250 $sContObj = $this->getContainer( $srcCont );
251 $dContObj = $this->getContainer( $dstCont );
252 if ( empty( $params['overwrite'] ) ) {
253 $destObj = $dContObj->create_object( $dstRel );
254 // Check if the object already exists (fields populated)
255 if ( $destObj->last_modified ) {
256 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
257 return $status;
258 }
259 }
260 } catch ( NoSuchContainerException $e ) {
261 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
262 return $status;
263 } catch ( InvalidResponseException $e ) {
264 $status->fatal( 'backend-fail-connect', $this->name );
265 return $status;
266 } catch ( Exception $e ) { // some other exception?
267 $status->fatal( 'backend-fail-internal', $this->name );
268 $this->logException( $e, __METHOD__, $params );
269 return $status;
270 }
271
272 // (b) Actually copy the file to the destination
273 try {
274 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
275 } catch ( NoSuchObjectException $e ) { // source object does not exist
276 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
277 } catch ( InvalidResponseException $e ) {
278 $status->fatal( 'backend-fail-connect', $this->name );
279 } catch ( Exception $e ) { // some other exception?
280 $status->fatal( 'backend-fail-internal', $this->name );
281 $this->logException( $e, __METHOD__, $params );
282 }
283
284 return $status;
285 }
286
287 /**
288 * @see FileBackend::doDeleteInternal()
289 */
290 protected function doDeleteInternal( array $params ) {
291 $status = Status::newGood();
292
293 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
294 if ( $srcRel === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
296 return $status;
297 }
298
299 try {
300 $sContObj = $this->getContainer( $srcCont );
301 $sContObj->delete_object( $srcRel );
302 } catch ( NoSuchContainerException $e ) {
303 $status->fatal( 'backend-fail-delete', $params['src'] );
304 } catch ( NoSuchObjectException $e ) {
305 if ( empty( $params['ignoreMissingSource'] ) ) {
306 $status->fatal( 'backend-fail-delete', $params['src'] );
307 }
308 } catch ( InvalidResponseException $e ) {
309 $status->fatal( 'backend-fail-connect', $this->name );
310 } catch ( Exception $e ) { // some other exception?
311 $status->fatal( 'backend-fail-internal', $this->name );
312 $this->logException( $e, __METHOD__, $params );
313 }
314
315 return $status;
316 }
317
318 /**
319 * @see FileBackend::doPrepareInternal()
320 */
321 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
322 $status = Status::newGood();
323
324 // (a) Check if container already exists
325 try {
326 $contObj = $this->getContainer( $fullCont );
327 // NoSuchContainerException not thrown: container must exist
328 return $status; // already exists
329 } catch ( NoSuchContainerException $e ) {
330 // NoSuchContainerException thrown: container does not exist
331 } catch ( InvalidResponseException $e ) {
332 $status->fatal( 'backend-fail-connect', $this->name );
333 return $status;
334 } catch ( Exception $e ) { // some other exception?
335 $status->fatal( 'backend-fail-internal', $this->name );
336 $this->logException( $e, __METHOD__, $params );
337 return $status;
338 }
339
340 // (b) Create container as needed
341 try {
342 $contObj = $this->createContainer( $fullCont );
343 if ( $this->swiftAnonUser != '' ) {
344 // Make container public to end-users...
345 $status->merge( $this->setContainerAccess(
346 $contObj,
347 array( $this->auth->username, $this->swiftAnonUser ), // read
348 array( $this->auth->username ) // write
349 ) );
350 }
351 } catch ( InvalidResponseException $e ) {
352 $status->fatal( 'backend-fail-connect', $this->name );
353 return $status;
354 } catch ( Exception $e ) { // some other exception?
355 $status->fatal( 'backend-fail-internal', $this->name );
356 $this->logException( $e, __METHOD__, $params );
357 return $status;
358 }
359
360 return $status;
361 }
362
363 /**
364 * @see FileBackend::doSecureInternal()
365 */
366 protected function doSecureInternal( $fullCont, $dir, array $params ) {
367 $status = Status::newGood();
368
369 if ( $this->swiftAnonUser != '' ) {
370 // Restrict container from end-users...
371 try {
372 // doPrepareInternal() should have been called,
373 // so the Swift container should already exist...
374 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
375 // NoSuchContainerException not thrown: container must exist
376 if ( !isset( $contObj->mw_wasSecured ) ) {
377 $status->merge( $this->setContainerAccess(
378 $contObj,
379 array( $this->auth->username ), // read
380 array( $this->auth->username ) // write
381 ) );
382 // @TODO: when php-cloudfiles supports container
383 // metadata, we can make use of that to avoid RTTs
384 $contObj->mw_wasSecured = true; // avoid useless RTTs
385 }
386 } catch ( InvalidResponseException $e ) {
387 $status->fatal( 'backend-fail-connect', $this->name );
388 } catch ( Exception $e ) { // some other exception?
389 $status->fatal( 'backend-fail-internal', $this->name );
390 $this->logException( $e, __METHOD__, $params );
391 }
392 }
393
394 return $status;
395 }
396
397 /**
398 * @see FileBackend::doCleanInternal()
399 */
400 protected function doCleanInternal( $fullCont, $dir, array $params ) {
401 $status = Status::newGood();
402
403 // Only containers themselves can be removed, all else is virtual
404 if ( $dir != '' ) {
405 return $status; // nothing to do
406 }
407
408 // (a) Check the container
409 try {
410 $contObj = $this->getContainer( $fullCont, true );
411 } catch ( NoSuchContainerException $e ) {
412 return $status; // ok, nothing to do
413 } catch ( InvalidResponseException $e ) {
414 $status->fatal( 'backend-fail-connect', $this->name );
415 return $status;
416 } catch ( Exception $e ) { // some other exception?
417 $status->fatal( 'backend-fail-internal', $this->name );
418 $this->logException( $e, __METHOD__, $params );
419 return $status;
420 }
421
422 // (b) Delete the container if empty
423 if ( $contObj->object_count == 0 ) {
424 try {
425 $this->deleteContainer( $fullCont );
426 } catch ( NoSuchContainerException $e ) {
427 return $status; // race?
428 } catch ( InvalidResponseException $e ) {
429 $status->fatal( 'backend-fail-connect', $this->name );
430 return $status;
431 } catch ( Exception $e ) { // some other exception?
432 $status->fatal( 'backend-fail-internal', $this->name );
433 $this->logException( $e, __METHOD__, $params );
434 return $status;
435 }
436 }
437
438 return $status;
439 }
440
441 /**
442 * @see FileBackend::doFileExists()
443 */
444 protected function doGetFileStat( array $params ) {
445 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
446 if ( $srcRel === null ) {
447 return false; // invalid storage path
448 }
449
450 $stat = false;
451 try {
452 $contObj = $this->getContainer( $srcCont );
453 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
454 $stat = array(
455 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
456 'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
457 'size' => $srcObj->content_length,
458 'sha1' => $srcObj->metadata['Sha1base36']
459 );
460 } catch ( NoSuchContainerException $e ) {
461 } catch ( NoSuchObjectException $e ) {
462 } catch ( InvalidResponseException $e ) {
463 $stat = null;
464 } catch ( Exception $e ) { // some other exception?
465 $stat = null;
466 $this->logException( $e, __METHOD__, $params );
467 }
468
469 return $stat;
470 }
471
472 /**
473 * @see FileBackendBase::getFileContents()
474 */
475 public function getFileContents( array $params ) {
476 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
477 if ( $srcRel === null ) {
478 return false; // invalid storage path
479 }
480
481 $data = false;
482 try {
483 $container = $this->getContainer( $srcCont );
484 $obj = $container->get_object( $srcRel );
485 $data = $obj->read( $this->headersFromParams( $params ) );
486 } catch ( NoSuchContainerException $e ) {
487 } catch ( NoSuchObjectException $e ) {
488 } catch ( InvalidResponseException $e ) {
489 } catch ( Exception $e ) { // some other exception?
490 $this->logException( $e, __METHOD__, $params );
491 }
492
493 return $data;
494 }
495
496 /**
497 * @see FileBackend::getFileListInternal()
498 */
499 public function getFileListInternal( $fullCont, $dir, array $params ) {
500 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
501 }
502
503 /**
504 * Do not call this function outside of SwiftFileBackendFileList
505 *
506 * @param $fullCont string Resolved container name
507 * @param $dir string Resolved storage directory with no trailing slash
508 * @param $after string Storage path of file to list items after
509 * @param $limit integer Max number of items to list
510 * @return Array
511 */
512 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
513 $files = array();
514
515 try {
516 $container = $this->getContainer( $fullCont );
517 $prefix = ( $dir == '' ) ? null : "{$dir}/";
518 $files = $container->list_objects( $limit, $after, $prefix );
519 } catch ( NoSuchContainerException $e ) {
520 } catch ( NoSuchObjectException $e ) {
521 } catch ( InvalidResponseException $e ) {
522 } catch ( Exception $e ) { // some other exception?
523 $this->logException( $e, __METHOD__, array( 'cont' => $fullCont, 'dir' => $dir ) );
524 }
525
526 return $files;
527 }
528
529 /**
530 * @see FileBackend::doGetFileSha1base36()
531 */
532 public function doGetFileSha1base36( array $params ) {
533 $stat = $this->getFileStat( $params );
534 if ( $stat ) {
535 return $stat['sha1'];
536 } else {
537 return false;
538 }
539 }
540
541 /**
542 * @see FileBackend::doStreamFile()
543 */
544 protected function doStreamFile( array $params ) {
545 $status = Status::newGood();
546
547 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
548 if ( $srcRel === null ) {
549 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
550 }
551
552 try {
553 $cont = $this->getContainer( $srcCont );
554 } catch ( NoSuchContainerException $e ) {
555 $status->fatal( 'backend-fail-stream', $params['src'] );
556 return $status;
557 } catch ( InvalidResponseException $e ) {
558 $status->fatal( 'backend-fail-connect', $this->name );
559 return $status;
560 } catch ( Exception $e ) { // some other exception?
561 $status->fatal( 'backend-fail-stream', $params['src'] );
562 $this->logException( $e, __METHOD__, $params );
563 return $status;
564 }
565
566 try {
567 $output = fopen( 'php://output', 'w' );
568 // FileBackend::streamFile() already checks existence
569 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
570 $obj->stream( $output, $this->headersFromParams( $params ) );
571 } catch ( InvalidResponseException $e ) { // 404? connection problem?
572 $status->fatal( 'backend-fail-stream', $params['src'] );
573 } catch ( Exception $e ) { // some other exception?
574 $status->fatal( 'backend-fail-stream', $params['src'] );
575 $this->logException( $e, __METHOD__, $params );
576 }
577
578 return $status;
579 }
580
581 /**
582 * @see FileBackend::getLocalCopy()
583 */
584 public function getLocalCopy( array $params ) {
585 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
586 if ( $srcRel === null ) {
587 return null;
588 }
589
590 $tmpFile = null;
591 try {
592 $cont = $this->getContainer( $srcCont );
593 $obj = $cont->get_object( $srcRel );
594 // Get source file extension
595 $ext = FileBackend::extensionFromPath( $srcRel );
596 // Create a new temporary file...
597 $tmpFile = TempFSFile::factory( wfBaseName( $srcRel ) . '_', $ext );
598 if ( $tmpFile ) {
599 $handle = fopen( $tmpFile->getPath(), 'wb' );
600 if ( $handle ) {
601 $obj->stream( $handle, $this->headersFromParams( $params ) );
602 fclose( $handle );
603 } else {
604 $tmpFile = null; // couldn't open temp file
605 }
606 }
607 } catch ( NoSuchContainerException $e ) {
608 $tmpFile = null;
609 } catch ( NoSuchObjectException $e ) {
610 $tmpFile = null;
611 } catch ( InvalidResponseException $e ) {
612 $tmpFile = null;
613 } catch ( Exception $e ) { // some other exception?
614 $tmpFile = null;
615 $this->logException( $e, __METHOD__, $params );
616 }
617
618 return $tmpFile;
619 }
620
621 /**
622 * Get headers to send to Swift when reading a file based
623 * on a FileBackend params array, e.g. that of getLocalCopy().
624 * $params is currently only checked for a 'latest' flag.
625 *
626 * @param $params Array
627 * @return Array
628 */
629 protected function headersFromParams( array $params ) {
630 $hdrs = array();
631 if ( !empty( $params['latest'] ) ) {
632 $hdrs[] = 'X-Newest: true';
633 }
634 return $hdrs;
635 }
636
637 /**
638 * Set read/write permissions for a Swift container
639 *
640 * @param $contObj CF_Container Swift container
641 * @param $readGrps Array Swift users who can read (account:user)
642 * @param $writeGrps Array Swift users who can write (account:user)
643 * @return Status
644 */
645 protected function setContainerAccess(
646 CF_Container $contObj, array $readGrps, array $writeGrps
647 ) {
648 $creds = $contObj->cfs_auth->export_credentials();
649
650 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
651
652 // Note: 10 second timeout consistent with php-cloudfiles
653 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
654 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
655 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
656 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
657
658 return $req->execute(); // should return 204
659 }
660
661 /**
662 * Get a connection to the Swift proxy
663 *
664 * @return CF_Connection|false
665 * @throws InvalidResponseException
666 */
667 protected function getConnection() {
668 if ( $this->conn === false ) {
669 throw new InvalidResponseException; // failed last attempt
670 }
671 // Session keys expire after a while, so we renew them periodically
672 if ( $this->conn && ( time() - $this->connStarted ) > $this->authTTL ) {
673 $this->conn->close(); // close active cURL connections
674 $this->conn = null;
675 }
676 // Authenticate with proxy and get a session key...
677 if ( $this->conn === null ) {
678 $this->connContainers = array();
679 try {
680 $this->auth->authenticate();
681 $this->conn = new CF_Connection( $this->auth );
682 $this->connStarted = time();
683 } catch ( AuthenticationException $e ) {
684 $this->conn = false; // don't keep re-trying
685 } catch ( InvalidResponseException $e ) {
686 $this->conn = false; // don't keep re-trying
687 }
688 }
689 if ( !$this->conn ) {
690 throw new InvalidResponseException; // auth/connection problem
691 }
692 return $this->conn;
693 }
694
695 /**
696 * @see FileBackend::doClearCache()
697 */
698 protected function doClearCache( array $paths = null ) {
699 $this->connContainers = array(); // clear container object cache
700 }
701
702 /**
703 * Get a Swift container object, possibly from process cache.
704 * Use $reCache if the file count or byte count is needed.
705 *
706 * @param $container string Container name
707 * @param $reCache bool Refresh the process cache
708 * @return CF_Container
709 */
710 protected function getContainer( $container, $reCache = false ) {
711 $conn = $this->getConnection(); // Swift proxy connection
712 if ( $reCache ) {
713 unset( $this->connContainers[$container] ); // purge cache
714 }
715 if ( !isset( $this->connContainers[$container] ) ) {
716 $contObj = $conn->get_container( $container );
717 // NoSuchContainerException not thrown: container must exist
718 if ( count( $this->connContainers ) >= $this->maxContCacheSize ) { // trim cache?
719 reset( $this->connContainers );
720 $key = key( $this->connContainers );
721 unset( $this->connContainers[$key] );
722 }
723 $this->connContainers[$container] = $contObj; // cache it
724 }
725 return $this->connContainers[$container];
726 }
727
728 /**
729 * Create a Swift container
730 *
731 * @param $container string Container name
732 * @return CF_Container
733 */
734 protected function createContainer( $container ) {
735 $conn = $this->getConnection(); // Swift proxy connection
736 $contObj = $conn->create_container( $container );
737 $this->connContainers[$container] = $contObj; // cache it
738 return $contObj;
739 }
740
741 /**
742 * Delete a Swift container
743 *
744 * @param $container string Container name
745 * @return void
746 */
747 protected function deleteContainer( $container ) {
748 $conn = $this->getConnection(); // Swift proxy connection
749 $conn->delete_container( $container );
750 unset( $this->connContainers[$container] ); // purge cache
751 }
752
753 /**
754 * Log an unexpected exception for this backend
755 *
756 * @param $e Exception
757 * @param $func string
758 * @param $params Array
759 * @return void
760 */
761 protected function logException( Exception $e, $func, array $params ) {
762 wfDebugLog( 'SwiftBackend',
763 get_class( $e ) . " in '{$this->name}': '{$func}' with " . serialize( $params )
764 );
765 }
766 }
767
768 /**
769 * SwiftFileBackend helper class to page through object listings.
770 * Swift also has a listing limit of 10,000 objects for sanity.
771 * Do not use this class from places outside SwiftFileBackend.
772 *
773 * @ingroup FileBackend
774 */
775 class SwiftFileBackendFileList implements Iterator {
776 /** @var Array */
777 protected $bufferIter = array();
778 protected $bufferAfter = null; // string; list items *after* this path
779 protected $pos = 0; // integer
780
781 /** @var SwiftFileBackend */
782 protected $backend;
783 protected $container; //
784 protected $dir; // string storage directory
785 protected $suffixStart; // integer
786
787 const PAGE_SIZE = 5000; // file listing buffer size
788
789 /**
790 * @param $backend SwiftFileBackend
791 * @param $fullCont string Resolved container name
792 * @param $dir string Resolved directory relative to container
793 */
794 public function __construct( SwiftFileBackend $backend, $fullCont, $dir ) {
795 $this->backend = $backend;
796 $this->container = $fullCont;
797 $this->dir = $dir;
798 if ( substr( $this->dir, -1 ) === '/' ) {
799 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
800 }
801 if ( $this->dir == '' ) { // whole container
802 $this->suffixStart = 0;
803 } else { // dir within container
804 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
805 }
806 }
807
808 public function current() {
809 return substr( current( $this->bufferIter ), $this->suffixStart );
810 }
811
812 public function key() {
813 return $this->pos;
814 }
815
816 public function next() {
817 // Advance to the next file in the page
818 next( $this->bufferIter );
819 ++$this->pos;
820 // Check if there are no files left in this page and
821 // advance to the next page if this page was not empty.
822 if ( !$this->valid() && count( $this->bufferIter ) ) {
823 $this->bufferAfter = end( $this->bufferIter );
824 $this->bufferIter = $this->backend->getFileListPageInternal(
825 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE
826 );
827 }
828 }
829
830 public function rewind() {
831 $this->pos = 0;
832 $this->bufferAfter = null;
833 $this->bufferIter = $this->backend->getFileListPageInternal(
834 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE
835 );
836 }
837
838 public function valid() {
839 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
840 }
841 }