Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / libs / filebackend / FileBackendMultiWrite.php
1 <?php
2 /**
3 * Proxy backend that mirrors writes to several internal backends.
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 */
23
24 /**
25 * @brief Proxy backend that mirrors writes to several internal backends.
26 *
27 * This class defines a multi-write backend. Multiple backends can be
28 * registered to this proxy backend and it will act as a single backend.
29 * Use this when all access to those backends is through this proxy backend.
30 * At least one of the backends must be declared the "master" backend.
31 *
32 * Only use this class when transitioning from one storage system to another.
33 *
34 * Read operations are only done on the 'master' backend for consistency.
35 * Write operations are performed on all backends, starting with the master.
36 * This makes a best-effort to have transactional semantics, but since requests
37 * may sometimes fail, the use of "autoResync" or background scripts to fix
38 * inconsistencies is important.
39 *
40 * @ingroup FileBackend
41 * @since 1.19
42 */
43 class FileBackendMultiWrite extends FileBackend {
44 /** @var FileBackendStore[] Prioritized list of FileBackendStore objects */
45 protected $backends = [];
46
47 /** @var int Index of master backend */
48 protected $masterIndex = -1;
49 /** @var int Index of read affinity backend */
50 protected $readIndex = -1;
51
52 /** @var int Bitfield */
53 protected $syncChecks = 0;
54 /** @var string|bool */
55 protected $autoResync = false;
56
57 /** @var bool */
58 protected $asyncWrites = false;
59
60 /* Possible internal backend consistency checks */
61 const CHECK_SIZE = 1;
62 const CHECK_TIME = 2;
63 const CHECK_SHA1 = 4;
64
65 /**
66 * Construct a proxy backend that consists of several internal backends.
67 * Locking, journaling, and read-only checks are handled by the proxy backend.
68 *
69 * Additional $config params include:
70 * - backends : Array of backend config and multi-backend settings.
71 * Each value is the config used in the constructor of a
72 * FileBackendStore class, but with these additional settings:
73 * - class : The name of the backend class
74 * - isMultiMaster : This must be set for one backend.
75 * - readAffinity : Use this for reads without 'latest' set.
76 * - syncChecks : Integer bitfield of internal backend sync checks to perform.
77 * Possible bits include the FileBackendMultiWrite::CHECK_* constants.
78 * There are constants for SIZE, TIME, and SHA1.
79 * The checks are done before allowing any file operations.
80 * - autoResync : Automatically resync the clone backends to the master backend
81 * when pre-operation sync checks fail. This should only be used
82 * if the master backend is stable and not missing any files.
83 * Use "conservative" to limit resyncing to copying newer master
84 * backend files over older (or non-existing) clone backend files.
85 * Cases that cannot be handled will result in operation abortion.
86 * - replication : Set to 'async' to defer file operations on the non-master backends.
87 * This will apply such updates post-send for web requests. Note that
88 * any checks from "syncChecks" are still synchronous.
89 *
90 * Bogus warning
91 * @suppress PhanAccessMethodProtected
92 *
93 * @param array $config
94 * @throws FileBackendError
95 */
96 public function __construct( array $config ) {
97 parent::__construct( $config );
98 $this->syncChecks = isset( $config['syncChecks'] )
99 ? $config['syncChecks']
100 : self::CHECK_SIZE;
101 $this->autoResync = isset( $config['autoResync'] )
102 ? $config['autoResync']
103 : false;
104 $this->asyncWrites = isset( $config['replication'] ) && $config['replication'] === 'async';
105 // Construct backends here rather than via registration
106 // to keep these backends hidden from outside the proxy.
107 $namesUsed = [];
108 foreach ( $config['backends'] as $index => $config ) {
109 $name = $config['name'];
110 if ( isset( $namesUsed[$name] ) ) { // don't break FileOp predicates
111 throw new LogicException( "Two or more backends defined with the name $name." );
112 }
113 $namesUsed[$name] = 1;
114 // Alter certain sub-backend settings for sanity
115 unset( $config['readOnly'] ); // use proxy backend setting
116 unset( $config['fileJournal'] ); // use proxy backend journal
117 unset( $config['lockManager'] ); // lock under proxy backend
118 $config['domainId'] = $this->domainId; // use the proxy backend wiki ID
119 if ( !empty( $config['isMultiMaster'] ) ) {
120 if ( $this->masterIndex >= 0 ) {
121 throw new LogicException( 'More than one master backend defined.' );
122 }
123 $this->masterIndex = $index; // this is the "master"
124 $config['fileJournal'] = $this->fileJournal; // log under proxy backend
125 }
126 if ( !empty( $config['readAffinity'] ) ) {
127 $this->readIndex = $index; // prefer this for reads
128 }
129 // Create sub-backend object
130 if ( !isset( $config['class'] ) ) {
131 throw new InvalidArgumentException( 'No class given for a backend config.' );
132 }
133 $class = $config['class'];
134 $this->backends[$index] = new $class( $config );
135 }
136 if ( $this->masterIndex < 0 ) { // need backends and must have a master
137 throw new LogicException( 'No master backend defined.' );
138 }
139 if ( $this->readIndex < 0 ) {
140 $this->readIndex = $this->masterIndex; // default
141 }
142 }
143
144 final protected function doOperationsInternal( array $ops, array $opts ) {
145 $status = $this->newStatus();
146
147 $mbe = $this->backends[$this->masterIndex]; // convenience
148
149 // Try to lock those files for the scope of this function...
150 $scopeLock = null;
151 if ( empty( $opts['nonLocking'] ) ) {
152 // Try to lock those files for the scope of this function...
153 /** @noinspection PhpUnusedLocalVariableInspection */
154 $scopeLock = $this->getScopedLocksForOps( $ops, $status );
155 if ( !$status->isOK() ) {
156 return $status; // abort
157 }
158 }
159 // Clear any cache entries (after locks acquired)
160 $this->clearCache();
161 $opts['preserveCache'] = true; // only locked files are cached
162 // Get the list of paths to read/write...
163 $relevantPaths = $this->fileStoragePathsForOps( $ops );
164 // Check if the paths are valid and accessible on all backends...
165 $status->merge( $this->accessibilityCheck( $relevantPaths ) );
166 if ( !$status->isOK() ) {
167 return $status; // abort
168 }
169 // Do a consistency check to see if the backends are consistent...
170 $syncStatus = $this->consistencyCheck( $relevantPaths );
171 if ( !$syncStatus->isOK() ) {
172 wfDebugLog( 'FileOperation', static::class .
173 " failed sync check: " . FormatJson::encode( $relevantPaths ) );
174 // Try to resync the clone backends to the master on the spot...
175 if ( $this->autoResync === false
176 || !$this->resyncFiles( $relevantPaths, $this->autoResync )->isOK()
177 ) {
178 $status->merge( $syncStatus );
179
180 return $status; // abort
181 }
182 }
183 // Actually attempt the operation batch on the master backend...
184 $realOps = $this->substOpBatchPaths( $ops, $mbe );
185 $masterStatus = $mbe->doOperations( $realOps, $opts );
186 $status->merge( $masterStatus );
187 // Propagate the operations to the clone backends if there were no unexpected errors
188 // and if there were either no expected errors or if the 'force' option was used.
189 // However, if nothing succeeded at all, then don't replicate any of the operations.
190 // If $ops only had one operation, this might avoid backend sync inconsistencies.
191 if ( $masterStatus->isOK() && $masterStatus->successCount > 0 ) {
192 foreach ( $this->backends as $index => $backend ) {
193 if ( $index === $this->masterIndex ) {
194 continue; // done already
195 }
196
197 $realOps = $this->substOpBatchPaths( $ops, $backend );
198 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
199 // Bind $scopeLock to the callback to preserve locks
200 DeferredUpdates::addCallableUpdate(
201 function () use ( $backend, $realOps, $opts, $scopeLock, $relevantPaths ) {
202 wfDebugLog( 'FileOperationReplication',
203 "'{$backend->getName()}' async replication; paths: " .
204 FormatJson::encode( $relevantPaths ) );
205 $backend->doOperations( $realOps, $opts );
206 }
207 );
208 } else {
209 wfDebugLog( 'FileOperationReplication',
210 "'{$backend->getName()}' sync replication; paths: " .
211 FormatJson::encode( $relevantPaths ) );
212 $status->merge( $backend->doOperations( $realOps, $opts ) );
213 }
214 }
215 }
216 // Make 'success', 'successCount', and 'failCount' fields reflect
217 // the overall operation, rather than all the batches for each backend.
218 // Do this by only using success values from the master backend's batch.
219 $status->success = $masterStatus->success;
220 $status->successCount = $masterStatus->successCount;
221 $status->failCount = $masterStatus->failCount;
222
223 return $status;
224 }
225
226 /**
227 * Check that a set of files are consistent across all internal backends
228 *
229 * @param array $paths List of storage paths
230 * @return StatusValue
231 */
232 public function consistencyCheck( array $paths ) {
233 $status = $this->newStatus();
234 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
235 return $status; // skip checks
236 }
237
238 // Preload all of the stat info in as few round trips as possible...
239 foreach ( $this->backends as $backend ) {
240 $realPaths = $this->substPaths( $paths, $backend );
241 $backend->preloadFileStat( [ 'srcs' => $realPaths, 'latest' => true ] );
242 }
243
244 $mBackend = $this->backends[$this->masterIndex];
245 foreach ( $paths as $path ) {
246 $params = [ 'src' => $path, 'latest' => true ];
247 $mParams = $this->substOpPaths( $params, $mBackend );
248 // Stat the file on the 'master' backend
249 $mStat = $mBackend->getFileStat( $mParams );
250 if ( $this->syncChecks & self::CHECK_SHA1 ) {
251 $mSha1 = $mBackend->getFileSha1Base36( $mParams );
252 } else {
253 $mSha1 = false;
254 }
255 // Check if all clone backends agree with the master...
256 foreach ( $this->backends as $index => $cBackend ) {
257 if ( $index === $this->masterIndex ) {
258 continue; // master
259 }
260 $cParams = $this->substOpPaths( $params, $cBackend );
261 $cStat = $cBackend->getFileStat( $cParams );
262 if ( $mStat ) { // file is in master
263 if ( !$cStat ) { // file should exist
264 $status->fatal( 'backend-fail-synced', $path );
265 continue;
266 }
267 if ( $this->syncChecks & self::CHECK_SIZE ) {
268 if ( $cStat['size'] != $mStat['size'] ) { // wrong size
269 $status->fatal( 'backend-fail-synced', $path );
270 continue;
271 }
272 }
273 if ( $this->syncChecks & self::CHECK_TIME ) {
274 $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] );
275 $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] );
276 if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere
277 $status->fatal( 'backend-fail-synced', $path );
278 continue;
279 }
280 }
281 if ( $this->syncChecks & self::CHECK_SHA1 ) {
282 if ( $cBackend->getFileSha1Base36( $cParams ) !== $mSha1 ) { // wrong SHA1
283 $status->fatal( 'backend-fail-synced', $path );
284 continue;
285 }
286 }
287 } else { // file is not in master
288 if ( $cStat ) { // file should not exist
289 $status->fatal( 'backend-fail-synced', $path );
290 }
291 }
292 }
293 }
294
295 return $status;
296 }
297
298 /**
299 * Check that a set of file paths are usable across all internal backends
300 *
301 * @param array $paths List of storage paths
302 * @return StatusValue
303 */
304 public function accessibilityCheck( array $paths ) {
305 $status = $this->newStatus();
306 if ( count( $this->backends ) <= 1 ) {
307 return $status; // skip checks
308 }
309
310 foreach ( $paths as $path ) {
311 foreach ( $this->backends as $backend ) {
312 $realPath = $this->substPaths( $path, $backend );
313 if ( !$backend->isPathUsableInternal( $realPath ) ) {
314 $status->fatal( 'backend-fail-usable', $path );
315 }
316 }
317 }
318
319 return $status;
320 }
321
322 /**
323 * Check that a set of files are consistent across all internal backends
324 * and re-synchronize those files against the "multi master" if needed.
325 *
326 * @param array $paths List of storage paths
327 * @param string|bool $resyncMode False, True, or "conservative"; see __construct()
328 * @return StatusValue
329 */
330 public function resyncFiles( array $paths, $resyncMode = true ) {
331 $status = $this->newStatus();
332
333 $mBackend = $this->backends[$this->masterIndex];
334 foreach ( $paths as $path ) {
335 $mPath = $this->substPaths( $path, $mBackend );
336 $mSha1 = $mBackend->getFileSha1Base36( [ 'src' => $mPath, 'latest' => true ] );
337 $mStat = $mBackend->getFileStat( [ 'src' => $mPath, 'latest' => true ] );
338 if ( $mStat === null || ( $mSha1 !== false && !$mStat ) ) { // sanity
339 $status->fatal( 'backend-fail-internal', $this->name );
340 wfDebugLog( 'FileOperation', __METHOD__
341 . ': File is not available on the master backend' );
342 continue; // file is not available on the master backend...
343 }
344 // Check of all clone backends agree with the master...
345 foreach ( $this->backends as $index => $cBackend ) {
346 if ( $index === $this->masterIndex ) {
347 continue; // master
348 }
349 $cPath = $this->substPaths( $path, $cBackend );
350 $cSha1 = $cBackend->getFileSha1Base36( [ 'src' => $cPath, 'latest' => true ] );
351 $cStat = $cBackend->getFileStat( [ 'src' => $cPath, 'latest' => true ] );
352 if ( $cStat === null || ( $cSha1 !== false && !$cStat ) ) { // sanity
353 $status->fatal( 'backend-fail-internal', $cBackend->getName() );
354 wfDebugLog( 'FileOperation', __METHOD__ .
355 ': File is not available on the clone backend' );
356 continue; // file is not available on the clone backend...
357 }
358 if ( $mSha1 === $cSha1 ) {
359 // already synced; nothing to do
360 } elseif ( $mSha1 !== false ) { // file is in master
361 if ( $resyncMode === 'conservative'
362 && $cStat && $cStat['mtime'] > $mStat['mtime']
363 ) {
364 $status->fatal( 'backend-fail-synced', $path );
365 continue; // don't rollback data
366 }
367 $fsFile = $mBackend->getLocalReference(
368 [ 'src' => $mPath, 'latest' => true ] );
369 $status->merge( $cBackend->quickStore(
370 [ 'src' => $fsFile->getPath(), 'dst' => $cPath ]
371 ) );
372 } elseif ( $mStat === false ) { // file is not in master
373 if ( $resyncMode === 'conservative' ) {
374 $status->fatal( 'backend-fail-synced', $path );
375 continue; // don't delete data
376 }
377 $status->merge( $cBackend->quickDelete( [ 'src' => $cPath ] ) );
378 }
379 }
380 }
381
382 if ( !$status->isOK() ) {
383 wfDebugLog( 'FileOperation', static::class .
384 " failed to resync: " . FormatJson::encode( $paths ) );
385 }
386
387 return $status;
388 }
389
390 /**
391 * Get a list of file storage paths to read or write for a list of operations
392 *
393 * @param array $ops Same format as doOperations()
394 * @return array List of storage paths to files (does not include directories)
395 */
396 protected function fileStoragePathsForOps( array $ops ) {
397 $paths = [];
398 foreach ( $ops as $op ) {
399 if ( isset( $op['src'] ) ) {
400 // For things like copy/move/delete with "ignoreMissingSource" and there
401 // is no source file, nothing should happen and there should be no errors.
402 if ( empty( $op['ignoreMissingSource'] )
403 || $this->fileExists( [ 'src' => $op['src'] ] )
404 ) {
405 $paths[] = $op['src'];
406 }
407 }
408 if ( isset( $op['srcs'] ) ) {
409 $paths = array_merge( $paths, $op['srcs'] );
410 }
411 if ( isset( $op['dst'] ) ) {
412 $paths[] = $op['dst'];
413 }
414 }
415
416 return array_values( array_unique( array_filter( $paths, 'FileBackend::isStoragePath' ) ) );
417 }
418
419 /**
420 * Substitute the backend name in storage path parameters
421 * for a set of operations with that of a given internal backend.
422 *
423 * @param array $ops List of file operation arrays
424 * @param FileBackendStore $backend
425 * @return array
426 */
427 protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) {
428 $newOps = []; // operations
429 foreach ( $ops as $op ) {
430 $newOp = $op; // operation
431 foreach ( [ 'src', 'srcs', 'dst', 'dir' ] as $par ) {
432 if ( isset( $newOp[$par] ) ) { // string or array
433 $newOp[$par] = $this->substPaths( $newOp[$par], $backend );
434 }
435 }
436 $newOps[] = $newOp;
437 }
438
439 return $newOps;
440 }
441
442 /**
443 * Same as substOpBatchPaths() but for a single operation
444 *
445 * @param array $ops File operation array
446 * @param FileBackendStore $backend
447 * @return array
448 */
449 protected function substOpPaths( array $ops, FileBackendStore $backend ) {
450 $newOps = $this->substOpBatchPaths( [ $ops ], $backend );
451
452 return $newOps[0];
453 }
454
455 /**
456 * Substitute the backend of storage paths with an internal backend's name
457 *
458 * @param array|string $paths List of paths or single string path
459 * @param FileBackendStore $backend
460 * @return array|string
461 */
462 protected function substPaths( $paths, FileBackendStore $backend ) {
463 return preg_replace(
464 '!^mwstore://' . preg_quote( $this->name, '!' ) . '/!',
465 StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ),
466 $paths // string or array
467 );
468 }
469
470 /**
471 * Substitute the backend of internal storage paths with the proxy backend's name
472 *
473 * @param array|string $paths List of paths or single string path
474 * @return array|string
475 */
476 protected function unsubstPaths( $paths ) {
477 return preg_replace(
478 '!^mwstore://([^/]+)!',
479 StringUtils::escapeRegexReplacement( "mwstore://{$this->name}" ),
480 $paths // string or array
481 );
482 }
483
484 /**
485 * @param array $ops File operations for FileBackend::doOperations()
486 * @return bool Whether there are file path sources with outside lifetime/ownership
487 */
488 protected function hasVolatileSources( array $ops ) {
489 foreach ( $ops as $op ) {
490 if ( $op['op'] === 'store' && !isset( $op['srcRef'] ) ) {
491 return true; // source file might be deleted anytime after do*Operations()
492 }
493 }
494
495 return false;
496 }
497
498 protected function doQuickOperationsInternal( array $ops ) {
499 $status = $this->newStatus();
500 // Do the operations on the master backend; setting StatusValue fields...
501 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
502 $masterStatus = $this->backends[$this->masterIndex]->doQuickOperations( $realOps );
503 $status->merge( $masterStatus );
504 // Propagate the operations to the clone backends...
505 foreach ( $this->backends as $index => $backend ) {
506 if ( $index === $this->masterIndex ) {
507 continue; // done already
508 }
509
510 $realOps = $this->substOpBatchPaths( $ops, $backend );
511 if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) {
512 DeferredUpdates::addCallableUpdate(
513 function () use ( $backend, $realOps ) {
514 $backend->doQuickOperations( $realOps );
515 }
516 );
517 } else {
518 $status->merge( $backend->doQuickOperations( $realOps ) );
519 }
520 }
521 // Make 'success', 'successCount', and 'failCount' fields reflect
522 // the overall operation, rather than all the batches for each backend.
523 // Do this by only using success values from the master backend's batch.
524 $status->success = $masterStatus->success;
525 $status->successCount = $masterStatus->successCount;
526 $status->failCount = $masterStatus->failCount;
527
528 return $status;
529 }
530
531 protected function doPrepare( array $params ) {
532 return $this->doDirectoryOp( 'prepare', $params );
533 }
534
535 protected function doSecure( array $params ) {
536 return $this->doDirectoryOp( 'secure', $params );
537 }
538
539 protected function doPublish( array $params ) {
540 return $this->doDirectoryOp( 'publish', $params );
541 }
542
543 protected function doClean( array $params ) {
544 return $this->doDirectoryOp( 'clean', $params );
545 }
546
547 /**
548 * @param string $method One of (doPrepare,doSecure,doPublish,doClean)
549 * @param array $params Method arguments
550 * @return StatusValue
551 */
552 protected function doDirectoryOp( $method, array $params ) {
553 $status = $this->newStatus();
554
555 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
556 $masterStatus = $this->backends[$this->masterIndex]->$method( $realParams );
557 $status->merge( $masterStatus );
558
559 foreach ( $this->backends as $index => $backend ) {
560 if ( $index === $this->masterIndex ) {
561 continue; // already done
562 }
563
564 $realParams = $this->substOpPaths( $params, $backend );
565 if ( $this->asyncWrites ) {
566 DeferredUpdates::addCallableUpdate(
567 function () use ( $backend, $method, $realParams ) {
568 $backend->$method( $realParams );
569 }
570 );
571 } else {
572 $status->merge( $backend->$method( $realParams ) );
573 }
574 }
575
576 return $status;
577 }
578
579 public function concatenate( array $params ) {
580 $status = $this->newStatus();
581 // We are writing to an FS file, so we don't need to do this per-backend
582 $index = $this->getReadIndexFromParams( $params );
583 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
584
585 $status->merge( $this->backends[$index]->concatenate( $realParams ) );
586
587 return $status;
588 }
589
590 public function fileExists( array $params ) {
591 $index = $this->getReadIndexFromParams( $params );
592 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
593
594 return $this->backends[$index]->fileExists( $realParams );
595 }
596
597 public function getFileTimestamp( array $params ) {
598 $index = $this->getReadIndexFromParams( $params );
599 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
600
601 return $this->backends[$index]->getFileTimestamp( $realParams );
602 }
603
604 public function getFileSize( array $params ) {
605 $index = $this->getReadIndexFromParams( $params );
606 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
607
608 return $this->backends[$index]->getFileSize( $realParams );
609 }
610
611 public function getFileStat( array $params ) {
612 $index = $this->getReadIndexFromParams( $params );
613 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
614
615 return $this->backends[$index]->getFileStat( $realParams );
616 }
617
618 public function getFileXAttributes( array $params ) {
619 $index = $this->getReadIndexFromParams( $params );
620 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
621
622 return $this->backends[$index]->getFileXAttributes( $realParams );
623 }
624
625 public function getFileContentsMulti( array $params ) {
626 $index = $this->getReadIndexFromParams( $params );
627 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
628
629 $contentsM = $this->backends[$index]->getFileContentsMulti( $realParams );
630
631 $contents = []; // (path => FSFile) mapping using the proxy backend's name
632 foreach ( $contentsM as $path => $data ) {
633 $contents[$this->unsubstPaths( $path )] = $data;
634 }
635
636 return $contents;
637 }
638
639 public function getFileSha1Base36( array $params ) {
640 $index = $this->getReadIndexFromParams( $params );
641 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
642
643 return $this->backends[$index]->getFileSha1Base36( $realParams );
644 }
645
646 public function getFileProps( array $params ) {
647 $index = $this->getReadIndexFromParams( $params );
648 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
649
650 return $this->backends[$index]->getFileProps( $realParams );
651 }
652
653 public function streamFile( array $params ) {
654 $index = $this->getReadIndexFromParams( $params );
655 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
656
657 return $this->backends[$index]->streamFile( $realParams );
658 }
659
660 public function getLocalReferenceMulti( array $params ) {
661 $index = $this->getReadIndexFromParams( $params );
662 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
663
664 $fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams );
665
666 $fsFiles = []; // (path => FSFile) mapping using the proxy backend's name
667 foreach ( $fsFilesM as $path => $fsFile ) {
668 $fsFiles[$this->unsubstPaths( $path )] = $fsFile;
669 }
670
671 return $fsFiles;
672 }
673
674 public function getLocalCopyMulti( array $params ) {
675 $index = $this->getReadIndexFromParams( $params );
676 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
677
678 $tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams );
679
680 $tempFiles = []; // (path => TempFSFile) mapping using the proxy backend's name
681 foreach ( $tempFilesM as $path => $tempFile ) {
682 $tempFiles[$this->unsubstPaths( $path )] = $tempFile;
683 }
684
685 return $tempFiles;
686 }
687
688 public function getFileHttpUrl( array $params ) {
689 $index = $this->getReadIndexFromParams( $params );
690 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
691
692 return $this->backends[$index]->getFileHttpUrl( $realParams );
693 }
694
695 public function directoryExists( array $params ) {
696 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
697
698 return $this->backends[$this->masterIndex]->directoryExists( $realParams );
699 }
700
701 public function getDirectoryList( array $params ) {
702 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
703
704 return $this->backends[$this->masterIndex]->getDirectoryList( $realParams );
705 }
706
707 public function getFileList( array $params ) {
708 $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] );
709
710 return $this->backends[$this->masterIndex]->getFileList( $realParams );
711 }
712
713 public function getFeatures() {
714 return $this->backends[$this->masterIndex]->getFeatures();
715 }
716
717 public function clearCache( array $paths = null ) {
718 foreach ( $this->backends as $backend ) {
719 $realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null;
720 $backend->clearCache( $realPaths );
721 }
722 }
723
724 public function preloadCache( array $paths ) {
725 $realPaths = $this->substPaths( $paths, $this->backends[$this->readIndex] );
726 $this->backends[$this->readIndex]->preloadCache( $realPaths );
727 }
728
729 public function preloadFileStat( array $params ) {
730 $index = $this->getReadIndexFromParams( $params );
731 $realParams = $this->substOpPaths( $params, $this->backends[$index] );
732
733 return $this->backends[$index]->preloadFileStat( $realParams );
734 }
735
736 public function getScopedLocksForOps( array $ops, StatusValue $status ) {
737 $realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
738 $fileOps = $this->backends[$this->masterIndex]->getOperationsInternal( $realOps );
739 // Get the paths to lock from the master backend
740 $paths = $this->backends[$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
741 // Get the paths under the proxy backend's name
742 $pbPaths = [
743 LockManager::LOCK_UW => $this->unsubstPaths( $paths[LockManager::LOCK_UW] ),
744 LockManager::LOCK_EX => $this->unsubstPaths( $paths[LockManager::LOCK_EX] )
745 ];
746
747 // Actually acquire the locks
748 return $this->getScopedFileLocks( $pbPaths, 'mixed', $status );
749 }
750
751 /**
752 * @param array $params
753 * @return int The master or read affinity backend index, based on $params['latest']
754 */
755 protected function getReadIndexFromParams( array $params ) {
756 return !empty( $params['latest'] ) ? $this->masterIndex : $this->readIndex;
757 }
758 }