[FileBackend]
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendStore.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * @brief Base class for all backends using particular storage medium.
10 *
11 * This class defines the methods as abstract that subclasses must implement.
12 * Outside callers should *not* use functions with "Internal" in the name.
13 *
14 * The FileBackend operations are implemented using basic functions
15 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
16 * This class is also responsible for path resolution and sanitization.
17 *
18 * @ingroup FileBackend
19 * @since 1.19
20 */
21 abstract class FileBackendStore extends FileBackend {
22 /** @var Array Map of paths to small (RAM/disk) cache items */
23 protected $cache = array(); // (storage path => key => value)
24 protected $maxCacheSize = 100; // integer; max paths with entries
25 /** @var Array Map of paths to large (RAM/disk) cache items */
26 protected $expensiveCache = array(); // (storage path => key => value)
27 protected $maxExpensiveCacheSize = 10; // integer; max paths with entries
28
29 /** @var Array Map of container names to sharding settings */
30 protected $shardViaHashLevels = array(); // (container name => config array)
31
32 protected $maxFileSize = 1000000000; // integer bytes (1GB)
33
34 /**
35 * Get the maximum allowable file size given backend
36 * medium restrictions and basic performance constraints.
37 * Do not call this function from places outside FileBackend and FileOp.
38 *
39 * @return integer Bytes
40 */
41 final public function maxFileSizeInternal() {
42 return $this->maxFileSize;
43 }
44
45 /**
46 * Check if a file can be created at a given storage path.
47 * FS backends should check if the parent directory exists and the file is writable.
48 * Backends using key/value stores should check if the container exists.
49 *
50 * @param $storagePath string
51 * @return bool
52 */
53 abstract public function isPathUsableInternal( $storagePath );
54
55 /**
56 * Create a file in the backend with the given contents.
57 * Do not call this function from places outside FileBackend and FileOp.
58 *
59 * $params include:
60 * content : the raw file contents
61 * dst : destination storage path
62 * overwrite : overwrite any file that exists at the destination
63 *
64 * @param $params Array
65 * @return Status
66 */
67 final public function createInternal( array $params ) {
68 wfProfileIn( __METHOD__ );
69 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
70 $status = Status::newFatal( 'backend-fail-create', $params['dst'] );
71 } else {
72 $status = $this->doCreateInternal( $params );
73 $this->clearCache( array( $params['dst'] ) );
74 }
75 wfProfileOut( __METHOD__ );
76 return $status;
77 }
78
79 /**
80 * @see FileBackendStore::createInternal()
81 */
82 abstract protected function doCreateInternal( array $params );
83
84 /**
85 * Store a file into the backend from a file on disk.
86 * Do not call this function from places outside FileBackend and FileOp.
87 *
88 * $params include:
89 * src : source path on disk
90 * dst : destination storage path
91 * overwrite : overwrite any file that exists at the destination
92 *
93 * @param $params Array
94 * @return Status
95 */
96 final public function storeInternal( array $params ) {
97 wfProfileIn( __METHOD__ );
98 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
99 $status = Status::newFatal( 'backend-fail-store', $params['dst'] );
100 } else {
101 $status = $this->doStoreInternal( $params );
102 $this->clearCache( array( $params['dst'] ) );
103 }
104 wfProfileOut( __METHOD__ );
105 return $status;
106 }
107
108 /**
109 * @see FileBackendStore::storeInternal()
110 */
111 abstract protected function doStoreInternal( array $params );
112
113 /**
114 * Copy a file from one storage path to another in the backend.
115 * Do not call this function from places outside FileBackend and FileOp.
116 *
117 * $params include:
118 * src : source storage path
119 * dst : destination storage path
120 * overwrite : overwrite any file that exists at the destination
121 *
122 * @param $params Array
123 * @return Status
124 */
125 final public function copyInternal( array $params ) {
126 wfProfileIn( __METHOD__ );
127 $status = $this->doCopyInternal( $params );
128 $this->clearCache( array( $params['dst'] ) );
129 wfProfileOut( __METHOD__ );
130 return $status;
131 }
132
133 /**
134 * @see FileBackendStore::copyInternal()
135 */
136 abstract protected function doCopyInternal( array $params );
137
138 /**
139 * Delete a file at the storage path.
140 * Do not call this function from places outside FileBackend and FileOp.
141 *
142 * $params include:
143 * src : source storage path
144 * ignoreMissingSource : do nothing if the source file does not exist
145 *
146 * @param $params Array
147 * @return Status
148 */
149 final public function deleteInternal( array $params ) {
150 wfProfileIn( __METHOD__ );
151 $status = $this->doDeleteInternal( $params );
152 $this->clearCache( array( $params['src'] ) );
153 wfProfileOut( __METHOD__ );
154 return $status;
155 }
156
157 /**
158 * @see FileBackendStore::deleteInternal()
159 */
160 abstract protected function doDeleteInternal( array $params );
161
162 /**
163 * Move a file from one storage path to another in the backend.
164 * Do not call this function from places outside FileBackend and FileOp.
165 *
166 * $params include:
167 * src : source storage path
168 * dst : destination storage path
169 * overwrite : overwrite any file that exists at the destination
170 *
171 * @param $params Array
172 * @return Status
173 */
174 final public function moveInternal( array $params ) {
175 wfProfileIn( __METHOD__ );
176 $status = $this->doMoveInternal( $params );
177 $this->clearCache( array( $params['src'], $params['dst'] ) );
178 wfProfileOut( __METHOD__ );
179 return $status;
180 }
181
182 /**
183 * @see FileBackendStore::moveInternal()
184 * @return Status
185 */
186 protected function doMoveInternal( array $params ) {
187 // Copy source to dest
188 $status = $this->copyInternal( $params );
189 if ( $status->isOK() ) {
190 // Delete source (only fails due to races or medium going down)
191 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
192 $status->setResult( true, $status->value ); // ignore delete() errors
193 }
194 return $status;
195 }
196
197 /**
198 * @see FileBackend::concatenate()
199 * @return Status
200 */
201 final public function concatenate( array $params ) {
202 wfProfileIn( __METHOD__ );
203 $status = Status::newGood();
204
205 // Try to lock the source files for the scope of this function
206 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
207 if ( $status->isOK() ) {
208 // Actually do the concatenation
209 $status->merge( $this->doConcatenate( $params ) );
210 }
211
212 wfProfileOut( __METHOD__ );
213 return $status;
214 }
215
216 /**
217 * @see FileBackendStore::concatenate()
218 * @return Status
219 */
220 protected function doConcatenate( array $params ) {
221 $status = Status::newGood();
222 $tmpPath = $params['dst']; // convenience
223
224 // Check that the specified temp file is valid...
225 wfSuppressWarnings();
226 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
227 wfRestoreWarnings();
228 if ( !$ok ) { // not present or not empty
229 $status->fatal( 'backend-fail-opentemp', $tmpPath );
230 return $status;
231 }
232
233 // Build up the temp file using the source chunks (in order)...
234 $tmpHandle = fopen( $tmpPath, 'ab' );
235 if ( $tmpHandle === false ) {
236 $status->fatal( 'backend-fail-opentemp', $tmpPath );
237 return $status;
238 }
239 foreach ( $params['srcs'] as $virtualSource ) {
240 // Get a local FS version of the chunk
241 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
242 if ( !$tmpFile ) {
243 $status->fatal( 'backend-fail-read', $virtualSource );
244 return $status;
245 }
246 // Get a handle to the local FS version
247 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
248 if ( $sourceHandle === false ) {
249 fclose( $tmpHandle );
250 $status->fatal( 'backend-fail-read', $virtualSource );
251 return $status;
252 }
253 // Append chunk to file (pass chunk size to avoid magic quotes)
254 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
255 fclose( $sourceHandle );
256 fclose( $tmpHandle );
257 $status->fatal( 'backend-fail-writetemp', $tmpPath );
258 return $status;
259 }
260 fclose( $sourceHandle );
261 }
262 if ( !fclose( $tmpHandle ) ) {
263 $status->fatal( 'backend-fail-closetemp', $tmpPath );
264 return $status;
265 }
266
267 clearstatcache(); // temp file changed
268
269 return $status;
270 }
271
272 /**
273 * @see FileBackend::doPrepare()
274 * @return Status
275 */
276 final protected function doPrepare( array $params ) {
277 wfProfileIn( __METHOD__ );
278
279 $status = Status::newGood();
280 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
281 if ( $dir === null ) {
282 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
283 wfProfileOut( __METHOD__ );
284 return $status; // invalid storage path
285 }
286
287 if ( $shard !== null ) { // confined to a single container/shard
288 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
289 } else { // directory is on several shards
290 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
291 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
292 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
293 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
294 }
295 }
296
297 wfProfileOut( __METHOD__ );
298 return $status;
299 }
300
301 /**
302 * @see FileBackendStore::doPrepare()
303 * @return Status
304 */
305 protected function doPrepareInternal( $container, $dir, array $params ) {
306 return Status::newGood();
307 }
308
309 /**
310 * @see FileBackend::doSecure()
311 * @return Status
312 */
313 final protected function doSecure( array $params ) {
314 wfProfileIn( __METHOD__ );
315 $status = Status::newGood();
316
317 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
318 if ( $dir === null ) {
319 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
320 wfProfileOut( __METHOD__ );
321 return $status; // invalid storage path
322 }
323
324 if ( $shard !== null ) { // confined to a single container/shard
325 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
326 } else { // directory is on several shards
327 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
328 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
329 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
330 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
331 }
332 }
333
334 wfProfileOut( __METHOD__ );
335 return $status;
336 }
337
338 /**
339 * @see FileBackendStore::doSecure()
340 * @return Status
341 */
342 protected function doSecureInternal( $container, $dir, array $params ) {
343 return Status::newGood();
344 }
345
346 /**
347 * @see FileBackend::doClean()
348 * @return Status
349 */
350 final protected function doClean( array $params ) {
351 wfProfileIn( __METHOD__ );
352 $status = Status::newGood();
353
354 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
355 if ( $dir === null ) {
356 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
357 wfProfileOut( __METHOD__ );
358 return $status; // invalid storage path
359 }
360
361 // Attempt to lock this directory...
362 $filesLockEx = array( $params['dir'] );
363 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
364 if ( !$status->isOK() ) {
365 wfProfileOut( __METHOD__ );
366 return $status; // abort
367 }
368
369 if ( $shard !== null ) { // confined to a single container/shard
370 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
371 } else { // directory is on several shards
372 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
373 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
374 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
375 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
376 }
377 }
378
379 wfProfileOut( __METHOD__ );
380 return $status;
381 }
382
383 /**
384 * @see FileBackendStore::doClean()
385 * @return Status
386 */
387 protected function doCleanInternal( $container, $dir, array $params ) {
388 return Status::newGood();
389 }
390
391 /**
392 * @see FileBackend::fileExists()
393 * @return bool|null
394 */
395 final public function fileExists( array $params ) {
396 wfProfileIn( __METHOD__ );
397 $stat = $this->getFileStat( $params );
398 wfProfileOut( __METHOD__ );
399 return ( $stat === null ) ? null : (bool)$stat; // null => failure
400 }
401
402 /**
403 * @see FileBackend::getFileTimestamp()
404 * @return bool
405 */
406 final public function getFileTimestamp( array $params ) {
407 wfProfileIn( __METHOD__ );
408 $stat = $this->getFileStat( $params );
409 wfProfileOut( __METHOD__ );
410 return $stat ? $stat['mtime'] : false;
411 }
412
413 /**
414 * @see FileBackend::getFileSize()
415 * @return bool
416 */
417 final public function getFileSize( array $params ) {
418 wfProfileIn( __METHOD__ );
419 $stat = $this->getFileStat( $params );
420 wfProfileOut( __METHOD__ );
421 return $stat ? $stat['size'] : false;
422 }
423
424 /**
425 * @see FileBackend::getFileStat()
426 * @return bool
427 */
428 final public function getFileStat( array $params ) {
429 wfProfileIn( __METHOD__ );
430 $path = self::normalizeStoragePath( $params['src'] );
431 if ( $path === null ) {
432 wfProfileOut( __METHOD__ );
433 return false; // invalid storage path
434 }
435 $latest = !empty( $params['latest'] );
436 if ( isset( $this->cache[$path]['stat'] ) ) {
437 // If we want the latest data, check that this cached
438 // value was in fact fetched with the latest available data.
439 if ( !$latest || $this->cache[$path]['stat']['latest'] ) {
440 wfProfileOut( __METHOD__ );
441 return $this->cache[$path]['stat'];
442 }
443 }
444 wfProfileIn( __METHOD__ . '-miss' );
445 $stat = $this->doGetFileStat( $params );
446 wfProfileOut( __METHOD__ . '-miss' );
447 if ( is_array( $stat ) ) { // don't cache negatives
448 $this->trimCache(); // limit memory
449 $this->cache[$path]['stat'] = $stat;
450 $this->cache[$path]['stat']['latest'] = $latest;
451 }
452 wfProfileOut( __METHOD__ );
453 return $stat;
454 }
455
456 /**
457 * @see FileBackendStore::getFileStat()
458 */
459 abstract protected function doGetFileStat( array $params );
460
461 /**
462 * @see FileBackend::getFileContents()
463 * @return bool|string
464 */
465 public function getFileContents( array $params ) {
466 wfProfileIn( __METHOD__ );
467 $tmpFile = $this->getLocalReference( $params );
468 if ( !$tmpFile ) {
469 wfProfileOut( __METHOD__ );
470 return false;
471 }
472 wfSuppressWarnings();
473 $data = file_get_contents( $tmpFile->getPath() );
474 wfRestoreWarnings();
475 wfProfileOut( __METHOD__ );
476 return $data;
477 }
478
479 /**
480 * @see FileBackend::getFileSha1Base36()
481 * @return bool|string
482 */
483 final public function getFileSha1Base36( array $params ) {
484 wfProfileIn( __METHOD__ );
485 $path = $params['src'];
486 if ( isset( $this->cache[$path]['sha1'] ) ) {
487 wfProfileOut( __METHOD__ );
488 return $this->cache[$path]['sha1'];
489 }
490 wfProfileIn( __METHOD__ . '-miss' );
491 $hash = $this->doGetFileSha1Base36( $params );
492 wfProfileOut( __METHOD__ . '-miss' );
493 if ( $hash ) { // don't cache negatives
494 $this->trimCache(); // limit memory
495 $this->cache[$path]['sha1'] = $hash;
496 }
497 wfProfileOut( __METHOD__ );
498 return $hash;
499 }
500
501 /**
502 * @see FileBackendStore::getFileSha1Base36()
503 * @return bool
504 */
505 protected function doGetFileSha1Base36( array $params ) {
506 $fsFile = $this->getLocalReference( $params );
507 if ( !$fsFile ) {
508 return false;
509 } else {
510 return $fsFile->getSha1Base36();
511 }
512 }
513
514 /**
515 * @see FileBackend::getFileProps()
516 * @return Array
517 */
518 final public function getFileProps( array $params ) {
519 wfProfileIn( __METHOD__ );
520 $fsFile = $this->getLocalReference( $params );
521 $props = $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
522 wfProfileOut( __METHOD__ );
523 return $props;
524 }
525
526 /**
527 * @see FileBackend::getLocalReference()
528 * @return TempFSFile|null
529 */
530 public function getLocalReference( array $params ) {
531 wfProfileIn( __METHOD__ );
532 $path = $params['src'];
533 if ( isset( $this->expensiveCache[$path]['localRef'] ) ) {
534 wfProfileOut( __METHOD__ );
535 return $this->expensiveCache[$path]['localRef'];
536 }
537 $tmpFile = $this->getLocalCopy( $params );
538 if ( $tmpFile ) { // don't cache negatives
539 $this->trimExpensiveCache(); // limit memory
540 $this->expensiveCache[$path]['localRef'] = $tmpFile;
541 }
542 wfProfileOut( __METHOD__ );
543 return $tmpFile;
544 }
545
546 /**
547 * @see FileBackend::streamFile()
548 * @return Status
549 */
550 final public function streamFile( array $params ) {
551 wfProfileIn( __METHOD__ );
552 $status = Status::newGood();
553
554 $info = $this->getFileStat( $params );
555 if ( !$info ) { // let StreamFile handle the 404
556 $status->fatal( 'backend-fail-notexists', $params['src'] );
557 }
558
559 // Set output buffer and HTTP headers for stream
560 $extraHeaders = isset( $params['headers'] ) ? $params['headers'] : array();
561 $res = StreamFile::prepareForStream( $params['src'], $info, $extraHeaders );
562 if ( $res == StreamFile::NOT_MODIFIED ) {
563 // do nothing; client cache is up to date
564 } elseif ( $res == StreamFile::READY_STREAM ) {
565 wfProfileIn( __METHOD__ . '-send' );
566 $status = $this->doStreamFile( $params );
567 wfProfileOut( __METHOD__ . '-send' );
568 } else {
569 $status->fatal( 'backend-fail-stream', $params['src'] );
570 }
571
572 wfProfileOut( __METHOD__ );
573 return $status;
574 }
575
576 /**
577 * @see FileBackendStore::streamFile()
578 * @return Status
579 */
580 protected function doStreamFile( array $params ) {
581 $status = Status::newGood();
582
583 $fsFile = $this->getLocalReference( $params );
584 if ( !$fsFile ) {
585 $status->fatal( 'backend-fail-stream', $params['src'] );
586 } elseif ( !readfile( $fsFile->getPath() ) ) {
587 $status->fatal( 'backend-fail-stream', $params['src'] );
588 }
589
590 return $status;
591 }
592
593 /**
594 * @copydoc FileBackend::getFileList()
595 * @return Array|null|Traversable
596 */
597 final public function getFileList( array $params ) {
598 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
599 if ( $dir === null ) { // invalid storage path
600 return null;
601 }
602 if ( $shard !== null ) {
603 // File listing is confined to a single container/shard
604 return $this->getFileListInternal( $fullCont, $dir, $params );
605 } else {
606 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
607 // File listing spans multiple containers/shards
608 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
609 return new FileBackendStoreShardListIterator( $this,
610 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
611 }
612 }
613
614 /**
615 * Do not call this function from places outside FileBackend
616 *
617 * @see FileBackendStore::getFileList()
618 *
619 * @param $container string Resolved container name
620 * @param $dir string Resolved path relative to container
621 * @param $params Array
622 * @return Traversable|Array|null
623 */
624 abstract public function getFileListInternal( $container, $dir, array $params );
625
626 /**
627 * Get the list of supported operations and their corresponding FileOp classes.
628 *
629 * @return Array
630 */
631 protected function supportedOperations() {
632 return array(
633 'store' => 'StoreFileOp',
634 'copy' => 'CopyFileOp',
635 'move' => 'MoveFileOp',
636 'delete' => 'DeleteFileOp',
637 'create' => 'CreateFileOp',
638 'null' => 'NullFileOp'
639 );
640 }
641
642 /**
643 * Return a list of FileOp objects from a list of operations.
644 * Do not call this function from places outside FileBackend.
645 *
646 * The result must have the same number of items as the input.
647 * An exception is thrown if an unsupported operation is requested.
648 *
649 * @param $ops Array Same format as doOperations()
650 * @return Array List of FileOp objects
651 * @throws MWException
652 */
653 final public function getOperations( array $ops ) {
654 $supportedOps = $this->supportedOperations();
655
656 $performOps = array(); // array of FileOp objects
657 // Build up ordered array of FileOps...
658 foreach ( $ops as $operation ) {
659 $opName = $operation['op'];
660 if ( isset( $supportedOps[$opName] ) ) {
661 $class = $supportedOps[$opName];
662 // Get params for this operation
663 $params = $operation;
664 // Append the FileOp class
665 $performOps[] = new $class( $this, $params );
666 } else {
667 throw new MWException( "Operation `$opName` is not supported." );
668 }
669 }
670
671 return $performOps;
672 }
673
674 /**
675 * @see FileBackend::doOperationsInternal()
676 * @return Status
677 */
678 protected function doOperationsInternal( array $ops, array $opts ) {
679 wfProfileIn( __METHOD__ );
680 $status = Status::newGood();
681
682 // Build up a list of FileOps...
683 $performOps = $this->getOperations( $ops );
684
685 // Acquire any locks as needed...
686 if ( empty( $opts['nonLocking'] ) ) {
687 // Build up a list of files to lock...
688 $filesLockEx = $filesLockSh = array();
689 foreach ( $performOps as $fileOp ) {
690 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
691 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
692 }
693 // Optimization: if doing an EX lock anyway, don't also set an SH one
694 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
695 // Get a shared lock on the parent directory of each path changed
696 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
697 // Try to lock those files for the scope of this function...
698 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status );
699 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
700 if ( !$status->isOK() ) {
701 wfProfileOut( __METHOD__ );
702 return $status; // abort
703 }
704 }
705
706 // Clear any cache entries (after locks acquired)
707 $this->clearCache();
708
709 // Actually attempt the operation batch...
710 $subStatus = FileOp::attemptBatch( $performOps, $opts );
711
712 // Merge errors into status fields
713 $status->merge( $subStatus );
714 $status->success = $subStatus->success; // not done in merge()
715
716 wfProfileOut( __METHOD__ );
717 return $status;
718 }
719
720 /**
721 * @see FileBackend::clearCache()
722 */
723 final public function clearCache( array $paths = null ) {
724 if ( is_array( $paths ) ) {
725 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
726 $paths = array_filter( $paths, 'strlen' ); // remove nulls
727 }
728 if ( $paths === null ) {
729 $this->cache = array();
730 $this->expensiveCache = array();
731 } else {
732 foreach ( $paths as $path ) {
733 unset( $this->cache[$path] );
734 unset( $this->expensiveCache[$path] );
735 }
736 }
737 $this->doClearCache( $paths );
738 }
739
740 /**
741 * Clears any additional stat caches for storage paths
742 *
743 * @see FileBackend::clearCache()
744 *
745 * @param $paths Array Storage paths (optional)
746 * @return void
747 */
748 protected function doClearCache( array $paths = null ) {}
749
750 /**
751 * Prune the inexpensive cache if it is too big to add an item
752 *
753 * @return void
754 */
755 protected function trimCache() {
756 if ( count( $this->cache ) >= $this->maxCacheSize ) {
757 reset( $this->cache );
758 unset( $this->cache[key( $this->cache )] );
759 }
760 }
761
762 /**
763 * Prune the expensive cache if it is too big to add an item
764 *
765 * @return void
766 */
767 protected function trimExpensiveCache() {
768 if ( count( $this->expensiveCache ) >= $this->maxExpensiveCacheSize ) {
769 reset( $this->expensiveCache );
770 unset( $this->expensiveCache[key( $this->expensiveCache )] );
771 }
772 }
773
774 /**
775 * Check if a container name is valid.
776 * This checks for for length and illegal characters.
777 *
778 * @param $container string
779 * @return bool
780 */
781 final protected static function isValidContainerName( $container ) {
782 // This accounts for Swift and S3 restrictions while leaving room
783 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
784 // This disallows directory separators or traversal characters.
785 // Note that matching strings URL encode to the same string;
786 // in Swift, the length restriction is *after* URL encoding.
787 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
788 }
789
790 /**
791 * Splits a storage path into an internal container name,
792 * an internal relative file name, and a container shard suffix.
793 * Any shard suffix is already appended to the internal container name.
794 * This also checks that the storage path is valid and within this backend.
795 *
796 * If the container is sharded but a suffix could not be determined,
797 * this means that the path can only refer to a directory and can only
798 * be scanned by looking in all the container shards.
799 *
800 * @param $storagePath string
801 * @return Array (container, path, container suffix) or (null, null, null) if invalid
802 */
803 final protected function resolveStoragePath( $storagePath ) {
804 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
805 if ( $backend === $this->name ) { // must be for this backend
806 $relPath = self::normalizeContainerPath( $relPath );
807 if ( $relPath !== null ) {
808 // Get shard for the normalized path if this container is sharded
809 $cShard = $this->getContainerShard( $container, $relPath );
810 // Validate and sanitize the relative path (backend-specific)
811 $relPath = $this->resolveContainerPath( $container, $relPath );
812 if ( $relPath !== null ) {
813 // Prepend any wiki ID prefix to the container name
814 $container = $this->fullContainerName( $container );
815 if ( self::isValidContainerName( $container ) ) {
816 // Validate and sanitize the container name (backend-specific)
817 $container = $this->resolveContainerName( "{$container}{$cShard}" );
818 if ( $container !== null ) {
819 return array( $container, $relPath, $cShard );
820 }
821 }
822 }
823 }
824 }
825 return array( null, null, null );
826 }
827
828 /**
829 * Like resolveStoragePath() except null values are returned if
830 * the container is sharded and the shard could not be determined.
831 *
832 * @see FileBackendStore::resolveStoragePath()
833 *
834 * @param $storagePath string
835 * @return Array (container, path) or (null, null) if invalid
836 */
837 final protected function resolveStoragePathReal( $storagePath ) {
838 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
839 if ( $cShard !== null ) {
840 return array( $container, $relPath );
841 }
842 return array( null, null );
843 }
844
845 /**
846 * Get the container name shard suffix for a given path.
847 * Any empty suffix means the container is not sharded.
848 *
849 * @param $container string Container name
850 * @param $relStoragePath string Storage path relative to the container
851 * @return string|null Returns null if shard could not be determined
852 */
853 final protected function getContainerShard( $container, $relPath ) {
854 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
855 if ( $levels == 1 || $levels == 2 ) {
856 // Hash characters are either base 16 or 36
857 $char = ( $base == 36 ) ? '[0-9a-z]' : '[0-9a-f]';
858 // Get a regex that represents the shard portion of paths.
859 // The concatenation of the captures gives us the shard.
860 if ( $levels === 1 ) { // 16 or 36 shards per container
861 $hashDirRegex = '(' . $char . ')';
862 } else { // 256 or 1296 shards per container
863 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
864 $hashDirRegex = $char . '/(' . $char . '{2})';
865 } else { // short hash dir format (e.g. "a/b/c")
866 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
867 }
868 }
869 // Allow certain directories to be above the hash dirs so as
870 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
871 // They must be 2+ chars to avoid any hash directory ambiguity.
872 $m = array();
873 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
874 return '.' . implode( '', array_slice( $m, 1 ) );
875 }
876 return null; // failed to match
877 }
878 return ''; // no sharding
879 }
880
881 /**
882 * Get the sharding config for a container.
883 * If greater than 0, then all file storage paths within
884 * the container are required to be hashed accordingly.
885 *
886 * @param $container string
887 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
888 */
889 final protected function getContainerHashLevels( $container ) {
890 if ( isset( $this->shardViaHashLevels[$container] ) ) {
891 $config = $this->shardViaHashLevels[$container];
892 $hashLevels = (int)$config['levels'];
893 if ( $hashLevels == 1 || $hashLevels == 2 ) {
894 $hashBase = (int)$config['base'];
895 if ( $hashBase == 16 || $hashBase == 36 ) {
896 return array( $hashLevels, $hashBase, $config['repeat'] );
897 }
898 }
899 }
900 return array( 0, 0, false ); // no sharding
901 }
902
903 /**
904 * Get a list of full container shard suffixes for a container
905 *
906 * @param $container string
907 * @return Array
908 */
909 final protected function getContainerSuffixes( $container ) {
910 $shards = array();
911 list( $digits, $base ) = $this->getContainerHashLevels( $container );
912 if ( $digits > 0 ) {
913 $numShards = pow( $base, $digits );
914 for ( $index = 0; $index < $numShards; $index++ ) {
915 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
916 }
917 }
918 return $shards;
919 }
920
921 /**
922 * Get the full container name, including the wiki ID prefix
923 *
924 * @param $container string
925 * @return string
926 */
927 final protected function fullContainerName( $container ) {
928 if ( $this->wikiId != '' ) {
929 return "{$this->wikiId}-$container";
930 } else {
931 return $container;
932 }
933 }
934
935 /**
936 * Resolve a container name, checking if it's allowed by the backend.
937 * This is intended for internal use, such as encoding illegal chars.
938 * Subclasses can override this to be more restrictive.
939 *
940 * @param $container string
941 * @return string|null
942 */
943 protected function resolveContainerName( $container ) {
944 return $container;
945 }
946
947 /**
948 * Resolve a relative storage path, checking if it's allowed by the backend.
949 * This is intended for internal use, such as encoding illegal chars or perhaps
950 * getting absolute paths (e.g. FS based backends). Note that the relative path
951 * may be the empty string (e.g. the path is simply to the container).
952 *
953 * @param $container string Container name
954 * @param $relStoragePath string Storage path relative to the container
955 * @return string|null Path or null if not valid
956 */
957 protected function resolveContainerPath( $container, $relStoragePath ) {
958 return $relStoragePath;
959 }
960 }
961
962 /**
963 * FileBackendStore helper function to handle file listings that span container shards.
964 * Do not use this class from places outside of FileBackendStore.
965 *
966 * @ingroup FileBackend
967 */
968 class FileBackendStoreShardListIterator implements Iterator {
969 /* @var FileBackendStore */
970 protected $backend;
971 /* @var Array */
972 protected $params;
973 /* @var Array */
974 protected $shardSuffixes;
975 protected $container; // string
976 protected $directory; // string
977
978 /* @var Traversable */
979 protected $iter;
980 protected $curShard = 0; // integer
981 protected $pos = 0; // integer
982
983 /**
984 * @param $backend FileBackendStore
985 * @param $container string Full storage container name
986 * @param $dir string Storage directory relative to container
987 * @param $suffixes Array List of container shard suffixes
988 * @param $params Array
989 */
990 public function __construct(
991 FileBackendStore $backend, $container, $dir, array $suffixes, array $params
992 ) {
993 $this->backend = $backend;
994 $this->container = $container;
995 $this->directory = $dir;
996 $this->shardSuffixes = $suffixes;
997 $this->params = $params;
998 }
999
1000 /**
1001 * @see Iterator::current()
1002 * @return string|bool String or false
1003 */
1004 public function current() {
1005 if ( is_array( $this->iter ) ) {
1006 return current( $this->iter );
1007 } else {
1008 return $this->iter->current();
1009 }
1010 }
1011
1012 /**
1013 * @see Iterator::key()
1014 * @return integer
1015 */
1016 public function key() {
1017 return $this->pos;
1018 }
1019
1020 /**
1021 * @see Iterator::next()
1022 * @return void
1023 */
1024 public function next() {
1025 ++$this->pos;
1026 if ( is_array( $this->iter ) ) {
1027 next( $this->iter );
1028 } else {
1029 $this->iter->next();
1030 }
1031 // Find the next non-empty shard if no elements are left
1032 $this->nextShardIteratorIfNotValid();
1033 }
1034
1035 /**
1036 * @see Iterator::rewind()
1037 * @return void
1038 */
1039 public function rewind() {
1040 $this->pos = 0;
1041 $this->curShard = 0;
1042 $this->setIteratorFromCurrentShard();
1043 // Find the next non-empty shard if this one has no elements
1044 $this->nextShardIteratorIfNotValid();
1045 }
1046
1047 /**
1048 * @see Iterator::valid()
1049 * @return bool
1050 */
1051 public function valid() {
1052 if ( $this->iter == null ) {
1053 return false; // some failure?
1054 } elseif ( is_array( $this->iter ) ) {
1055 return ( current( $this->iter ) !== false ); // no paths can have this value
1056 } else {
1057 return $this->iter->valid();
1058 }
1059 }
1060
1061 /**
1062 * If the list iterator for this container shard is out of items,
1063 * then move on to the next container that has items.
1064 * If there are none, then it advances to the last container.
1065 */
1066 protected function nextShardIteratorIfNotValid() {
1067 while ( !$this->valid() ) {
1068 if ( ++$this->curShard >= count( $this->shardSuffixes ) ) {
1069 break; // no more container shards
1070 }
1071 $this->setIteratorFromCurrentShard();
1072 }
1073 }
1074
1075 /**
1076 * Set the list iterator to that of the current container shard
1077 */
1078 protected function setIteratorFromCurrentShard() {
1079 $suffix = $this->shardSuffixes[$this->curShard];
1080 $this->iter = $this->backend->getFileListInternal(
1081 "{$this->container}{$suffix}", $this->directory, $this->params );
1082 }
1083 }