9 * @brief Base class for all backends using particular storage medium.
11 * This class defines the methods as abstract that subclasses must implement.
12 * Outside callers should *not* use functions with "Internal" in the name.
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.
18 * @ingroup FileBackend
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
29 /** @var Array Map of container names to sharding settings */
30 protected $shardViaHashLevels = array(); // (container name => config array)
32 protected $maxFileSize = 4294967296; // integer bytes (4GiB)
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.
39 * @return integer Bytes
41 final public function maxFileSizeInternal() {
42 return $this->maxFileSize
;
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.
50 * @param $storagePath string
53 abstract public function isPathUsableInternal( $storagePath );
56 * Create a file in the backend with the given contents.
57 * Do not call this function from places outside FileBackend and FileOp.
60 * content : the raw file contents
61 * dst : destination storage path
62 * overwrite : overwrite any file that exists at the destination
64 * @param $params Array
67 final public function createInternal( array $params ) {
68 wfProfileIn( __METHOD__
);
69 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
70 $status = Status
::newFatal( 'backend-fail-maxsize', $params['dst'] );
72 $status = $this->doCreateInternal( $params );
73 $this->clearCache( array( $params['dst'] ) );
75 wfProfileOut( __METHOD__
);
80 * @see FileBackendStore::createInternal()
82 abstract protected function doCreateInternal( array $params );
85 * Store a file into the backend from a file on disk.
86 * Do not call this function from places outside FileBackend and FileOp.
89 * src : source path on disk
90 * dst : destination storage path
91 * overwrite : overwrite any file that exists at the destination
93 * @param $params Array
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'] );
101 $status = $this->doStoreInternal( $params );
102 $this->clearCache( array( $params['dst'] ) );
104 wfProfileOut( __METHOD__
);
109 * @see FileBackendStore::storeInternal()
111 abstract protected function doStoreInternal( array $params );
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.
118 * src : source storage path
119 * dst : destination storage path
120 * overwrite : overwrite any file that exists at the destination
122 * @param $params Array
125 final public function copyInternal( array $params ) {
126 wfProfileIn( __METHOD__
);
127 $status = $this->doCopyInternal( $params );
128 $this->clearCache( array( $params['dst'] ) );
129 wfProfileOut( __METHOD__
);
134 * @see FileBackendStore::copyInternal()
136 abstract protected function doCopyInternal( array $params );
139 * Delete a file at the storage path.
140 * Do not call this function from places outside FileBackend and FileOp.
143 * src : source storage path
144 * ignoreMissingSource : do nothing if the source file does not exist
146 * @param $params Array
149 final public function deleteInternal( array $params ) {
150 wfProfileIn( __METHOD__
);
151 $status = $this->doDeleteInternal( $params );
152 $this->clearCache( array( $params['src'] ) );
153 wfProfileOut( __METHOD__
);
158 * @see FileBackendStore::deleteInternal()
160 abstract protected function doDeleteInternal( array $params );
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.
167 * src : source storage path
168 * dst : destination storage path
169 * overwrite : overwrite any file that exists at the destination
171 * @param $params Array
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__
);
183 * @see FileBackendStore::moveInternal()
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
198 * @see FileBackend::concatenate()
201 final public function concatenate( array $params ) {
202 wfProfileIn( __METHOD__
);
203 $status = Status
::newGood();
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 ) );
212 wfProfileOut( __METHOD__
);
217 * @see FileBackendStore::concatenate()
220 protected function doConcatenate( array $params ) {
221 $status = Status
::newGood();
222 $tmpPath = $params['dst']; // convenience
224 // Check that the specified temp file is valid...
225 wfSuppressWarnings();
226 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
228 if ( !$ok ) { // not present or not empty
229 $status->fatal( 'backend-fail-opentemp', $tmpPath );
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 );
239 foreach ( $params['srcs'] as $virtualSource ) {
240 // Get a local FS version of the chunk
241 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
243 $status->fatal( 'backend-fail-read', $virtualSource );
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 );
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 );
260 fclose( $sourceHandle );
262 if ( !fclose( $tmpHandle ) ) {
263 $status->fatal( 'backend-fail-closetemp', $tmpPath );
267 clearstatcache(); // temp file changed
273 * @see FileBackend::doPrepare()
276 final protected function doPrepare( array $params ) {
277 wfProfileIn( __METHOD__
);
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
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 ) );
297 wfProfileOut( __METHOD__
);
302 * @see FileBackendStore::doPrepare()
305 protected function doPrepareInternal( $container, $dir, array $params ) {
306 return Status
::newGood();
310 * @see FileBackend::doSecure()
313 final protected function doSecure( array $params ) {
314 wfProfileIn( __METHOD__
);
315 $status = Status
::newGood();
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
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 ) );
334 wfProfileOut( __METHOD__
);
339 * @see FileBackendStore::doSecure()
342 protected function doSecureInternal( $container, $dir, array $params ) {
343 return Status
::newGood();
347 * @see FileBackend::doClean()
350 final protected function doClean( array $params ) {
351 wfProfileIn( __METHOD__
);
352 $status = Status
::newGood();
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
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
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 ) );
379 wfProfileOut( __METHOD__
);
384 * @see FileBackendStore::doClean()
387 protected function doCleanInternal( $container, $dir, array $params ) {
388 return Status
::newGood();
392 * @see FileBackend::fileExists()
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
403 * @see FileBackend::getFileTimestamp()
406 final public function getFileTimestamp( array $params ) {
407 wfProfileIn( __METHOD__
);
408 $stat = $this->getFileStat( $params );
409 wfProfileOut( __METHOD__
);
410 return $stat ?
$stat['mtime'] : false;
414 * @see FileBackend::getFileSize()
417 final public function getFileSize( array $params ) {
418 wfProfileIn( __METHOD__
);
419 $stat = $this->getFileStat( $params );
420 wfProfileOut( __METHOD__
);
421 return $stat ?
$stat['size'] : false;
425 * @see FileBackend::getFileStat()
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
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'];
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;
452 wfProfileOut( __METHOD__
);
457 * @see FileBackendStore::getFileStat()
459 abstract protected function doGetFileStat( array $params );
462 * @see FileBackend::getFileContents()
463 * @return bool|string
465 public function getFileContents( array $params ) {
466 wfProfileIn( __METHOD__
);
467 $tmpFile = $this->getLocalReference( $params );
469 wfProfileOut( __METHOD__
);
472 wfSuppressWarnings();
473 $data = file_get_contents( $tmpFile->getPath() );
475 wfProfileOut( __METHOD__
);
480 * @see FileBackend::getFileSha1Base36()
481 * @return bool|string
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'];
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;
497 wfProfileOut( __METHOD__
);
502 * @see FileBackendStore::getFileSha1Base36()
505 protected function doGetFileSha1Base36( array $params ) {
506 $fsFile = $this->getLocalReference( $params );
510 return $fsFile->getSha1Base36();
515 * @see FileBackend::getFileProps()
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__
);
527 * @see FileBackend::getLocalReference()
528 * @return TempFSFile|null
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'];
537 $tmpFile = $this->getLocalCopy( $params );
538 if ( $tmpFile ) { // don't cache negatives
539 $this->trimExpensiveCache(); // limit memory
540 $this->expensiveCache
[$path]['localRef'] = $tmpFile;
542 wfProfileOut( __METHOD__
);
547 * @see FileBackend::streamFile()
550 final public function streamFile( array $params ) {
551 wfProfileIn( __METHOD__
);
552 $status = Status
::newGood();
554 $info = $this->getFileStat( $params );
555 if ( !$info ) { // let StreamFile handle the 404
556 $status->fatal( 'backend-fail-notexists', $params['src'] );
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' );
569 $status->fatal( 'backend-fail-stream', $params['src'] );
572 wfProfileOut( __METHOD__
);
577 * @see FileBackendStore::streamFile()
580 protected function doStreamFile( array $params ) {
581 $status = Status
::newGood();
583 $fsFile = $this->getLocalReference( $params );
585 $status->fatal( 'backend-fail-stream', $params['src'] );
586 } elseif ( !readfile( $fsFile->getPath() ) ) {
587 $status->fatal( 'backend-fail-stream', $params['src'] );
594 * @copydoc FileBackend::getFileList()
595 * @return Array|null|Traversable
597 final public function getFileList( array $params ) {
598 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
599 if ( $dir === null ) { // invalid storage path
602 if ( $shard !== null ) {
603 // File listing is confined to a single container/shard
604 return $this->getFileListInternal( $fullCont, $dir, $params );
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 );
615 * Do not call this function from places outside FileBackend
617 * @see FileBackendStore::getFileList()
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
624 abstract public function getFileListInternal( $container, $dir, array $params );
627 * Get the list of supported operations and their corresponding FileOp classes.
631 protected function supportedOperations() {
633 'store' => 'StoreFileOp',
634 'copy' => 'CopyFileOp',
635 'move' => 'MoveFileOp',
636 'delete' => 'DeleteFileOp',
637 'create' => 'CreateFileOp',
638 'null' => 'NullFileOp'
643 * Return a list of FileOp objects from a list of operations.
644 * Do not call this function from places outside FileBackend.
646 * The result must have the same number of items as the input.
647 * An exception is thrown if an unsupported operation is requested.
649 * @param $ops Array Same format as doOperations()
650 * @return Array List of FileOp objects
651 * @throws MWException
653 final public function getOperations( array $ops ) {
654 $supportedOps = $this->supportedOperations();
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 );
667 throw new MWException( "Operation `$opName` is not supported." );
675 * @see FileBackend::doOperationsInternal()
678 protected function doOperationsInternal( array $ops, array $opts ) {
679 wfProfileIn( __METHOD__
);
680 $status = Status
::newGood();
682 // Build up a list of FileOps...
683 $performOps = $this->getOperations( $ops );
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() );
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
706 // Clear any cache entries (after locks acquired)
709 // Actually attempt the operation batch...
710 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
712 // Merge errors into status fields
713 $status->merge( $subStatus );
714 $status->success
= $subStatus->success
; // not done in merge()
716 wfProfileOut( __METHOD__
);
721 * @see FileBackend::clearCache()
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
728 if ( $paths === null ) {
729 $this->cache
= array();
730 $this->expensiveCache
= array();
732 foreach ( $paths as $path ) {
733 unset( $this->cache
[$path] );
734 unset( $this->expensiveCache
[$path] );
737 $this->doClearCache( $paths );
741 * Clears any additional stat caches for storage paths
743 * @see FileBackend::clearCache()
745 * @param $paths Array Storage paths (optional)
748 protected function doClearCache( array $paths = null ) {}
751 * Prune the inexpensive cache if it is too big to add an item
755 protected function trimCache() {
756 if ( count( $this->cache
) >= $this->maxCacheSize
) {
757 reset( $this->cache
);
758 unset( $this->cache
[key( $this->cache
)] );
763 * Prune the expensive cache if it is too big to add an item
767 protected function trimExpensiveCache() {
768 if ( count( $this->expensiveCache
) >= $this->maxExpensiveCacheSize
) {
769 reset( $this->expensiveCache
);
770 unset( $this->expensiveCache
[key( $this->expensiveCache
)] );
775 * Check if a container name is valid.
776 * This checks for for length and illegal characters.
778 * @param $container string
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 );
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.
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.
800 * @param $storagePath string
801 * @return Array (container, path, container suffix) or (null, null, null) if invalid
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 );
825 return array( null, null, null );
829 * Like resolveStoragePath() except null values are returned if
830 * the container is sharded and the shard could not be determined.
832 * @see FileBackendStore::resolveStoragePath()
834 * @param $storagePath string
835 * @return Array (container, path) or (null, null) if invalid
837 final protected function resolveStoragePathReal( $storagePath ) {
838 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
839 if ( $cShard !== null ) {
840 return array( $container, $relPath );
842 return array( null, null );
846 * Get the container name shard suffix for a given path.
847 * Any empty suffix means the container is not sharded.
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
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 . ')';
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.
873 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
874 return '.' . implode( '', array_slice( $m, 1 ) );
876 return null; // failed to match
878 return ''; // no sharding
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.
886 * @param $container string
887 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
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'] );
900 return array( 0, 0, false ); // no sharding
904 * Get a list of full container shard suffixes for a container
906 * @param $container string
909 final protected function getContainerSuffixes( $container ) {
911 list( $digits, $base ) = $this->getContainerHashLevels( $container );
913 $numShards = pow( $base, $digits );
914 for ( $index = 0; $index < $numShards; $index++
) {
915 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
922 * Get the full container name, including the wiki ID prefix
924 * @param $container string
927 final protected function fullContainerName( $container ) {
928 if ( $this->wikiId
!= '' ) {
929 return "{$this->wikiId}-$container";
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.
940 * @param $container string
941 * @return string|null
943 protected function resolveContainerName( $container ) {
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).
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
957 protected function resolveContainerPath( $container, $relStoragePath ) {
958 return $relStoragePath;
963 * FileBackendStore helper function to handle file listings that span container shards.
964 * Do not use this class from places outside of FileBackendStore.
966 * @ingroup FileBackend
968 class FileBackendStoreShardListIterator
implements Iterator
{
969 /* @var FileBackendStore */
974 protected $shardSuffixes;
975 protected $container; // string
976 protected $directory; // string
978 /* @var Traversable */
980 protected $curShard = 0; // integer
981 protected $pos = 0; // integer
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
990 public function __construct(
991 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
993 $this->backend
= $backend;
994 $this->container
= $container;
995 $this->directory
= $dir;
996 $this->shardSuffixes
= $suffixes;
997 $this->params
= $params;
1001 * @see Iterator::current()
1002 * @return string|bool String or false
1004 public function current() {
1005 if ( is_array( $this->iter
) ) {
1006 return current( $this->iter
);
1008 return $this->iter
->current();
1013 * @see Iterator::key()
1016 public function key() {
1021 * @see Iterator::next()
1024 public function next() {
1026 if ( is_array( $this->iter
) ) {
1027 next( $this->iter
);
1029 $this->iter
->next();
1031 // Find the next non-empty shard if no elements are left
1032 $this->nextShardIteratorIfNotValid();
1036 * @see Iterator::rewind()
1039 public function rewind() {
1041 $this->curShard
= 0;
1042 $this->setIteratorFromCurrentShard();
1043 // Find the next non-empty shard if this one has no elements
1044 $this->nextShardIteratorIfNotValid();
1048 * @see Iterator::valid()
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
1057 return $this->iter
->valid();
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.
1066 protected function nextShardIteratorIfNotValid() {
1067 while ( !$this->valid() ) {
1068 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1069 break; // no more container shards
1071 $this->setIteratorFromCurrentShard();
1076 * Set the list iterator to that of the current container shard
1078 protected function setIteratorFromCurrentShard() {
1079 $suffix = $this->shardSuffixes
[$this->curShard
];
1080 $this->iter
= $this->backend
->getFileListInternal(
1081 "{$this->container}{$suffix}", $this->directory
, $this->params
);