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