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