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',
71 $params['dst'], $this->maxFileSizeInternal() );
73 $status = $this->doCreateInternal( $params );
74 $this->clearCache( array( $params['dst'] ) );
76 wfProfileOut( __METHOD__
);
81 * @see FileBackendStore::createInternal()
83 abstract protected function doCreateInternal( array $params );
86 * Store a file into the backend from a file on disk.
87 * Do not call this function from places outside FileBackend and FileOp.
90 * src : source path on disk
91 * dst : destination storage path
92 * overwrite : overwrite any file that exists at the destination
94 * @param $params Array
97 final public function storeInternal( array $params ) {
98 wfProfileIn( __METHOD__
);
99 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
100 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
102 $status = $this->doStoreInternal( $params );
103 $this->clearCache( array( $params['dst'] ) );
105 wfProfileOut( __METHOD__
);
110 * @see FileBackendStore::storeInternal()
112 abstract protected function doStoreInternal( array $params );
115 * Copy a file from one storage path to another in the backend.
116 * Do not call this function from places outside FileBackend and FileOp.
119 * src : source storage path
120 * dst : destination storage path
121 * overwrite : overwrite any file that exists at the destination
123 * @param $params Array
126 final public function copyInternal( array $params ) {
127 wfProfileIn( __METHOD__
);
128 $status = $this->doCopyInternal( $params );
129 $this->clearCache( array( $params['dst'] ) );
130 wfProfileOut( __METHOD__
);
135 * @see FileBackendStore::copyInternal()
137 abstract protected function doCopyInternal( array $params );
140 * Delete a file at the storage path.
141 * Do not call this function from places outside FileBackend and FileOp.
144 * src : source storage path
145 * ignoreMissingSource : do nothing if the source file does not exist
147 * @param $params Array
150 final public function deleteInternal( array $params ) {
151 wfProfileIn( __METHOD__
);
152 $status = $this->doDeleteInternal( $params );
153 $this->clearCache( array( $params['src'] ) );
154 wfProfileOut( __METHOD__
);
159 * @see FileBackendStore::deleteInternal()
161 abstract protected function doDeleteInternal( array $params );
164 * Move a file from one storage path to another in the backend.
165 * Do not call this function from places outside FileBackend and FileOp.
168 * src : source storage path
169 * dst : destination storage path
170 * overwrite : overwrite any file that exists at the destination
172 * @param $params Array
175 final public function moveInternal( array $params ) {
176 wfProfileIn( __METHOD__
);
177 $status = $this->doMoveInternal( $params );
178 $this->clearCache( array( $params['src'], $params['dst'] ) );
179 wfProfileOut( __METHOD__
);
184 * @see FileBackendStore::moveInternal()
187 protected function doMoveInternal( array $params ) {
188 // Copy source to dest
189 $status = $this->copyInternal( $params );
190 if ( $status->isOK() ) {
191 // Delete source (only fails due to races or medium going down)
192 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
193 $status->setResult( true, $status->value
); // ignore delete() errors
199 * @see FileBackend::concatenate()
202 final public function concatenate( array $params ) {
203 wfProfileIn( __METHOD__
);
204 $status = Status
::newGood();
206 // Try to lock the source files for the scope of this function
207 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
208 if ( $status->isOK() ) {
209 // Actually do the concatenation
210 $status->merge( $this->doConcatenate( $params ) );
213 wfProfileOut( __METHOD__
);
218 * @see FileBackendStore::concatenate()
221 protected function doConcatenate( array $params ) {
222 $status = Status
::newGood();
223 $tmpPath = $params['dst']; // convenience
225 // Check that the specified temp file is valid...
226 wfSuppressWarnings();
227 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
229 if ( !$ok ) { // not present or not empty
230 $status->fatal( 'backend-fail-opentemp', $tmpPath );
234 // Build up the temp file using the source chunks (in order)...
235 $tmpHandle = fopen( $tmpPath, 'ab' );
236 if ( $tmpHandle === false ) {
237 $status->fatal( 'backend-fail-opentemp', $tmpPath );
240 foreach ( $params['srcs'] as $virtualSource ) {
241 // Get a local FS version of the chunk
242 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
244 $status->fatal( 'backend-fail-read', $virtualSource );
247 // Get a handle to the local FS version
248 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
249 if ( $sourceHandle === false ) {
250 fclose( $tmpHandle );
251 $status->fatal( 'backend-fail-read', $virtualSource );
254 // Append chunk to file (pass chunk size to avoid magic quotes)
255 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
256 fclose( $sourceHandle );
257 fclose( $tmpHandle );
258 $status->fatal( 'backend-fail-writetemp', $tmpPath );
261 fclose( $sourceHandle );
263 if ( !fclose( $tmpHandle ) ) {
264 $status->fatal( 'backend-fail-closetemp', $tmpPath );
268 clearstatcache(); // temp file changed
274 * @see FileBackend::doPrepare()
277 final protected function doPrepare( array $params ) {
278 wfProfileIn( __METHOD__
);
280 $status = Status
::newGood();
281 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
282 if ( $dir === null ) {
283 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
284 wfProfileOut( __METHOD__
);
285 return $status; // invalid storage path
288 if ( $shard !== null ) { // confined to a single container/shard
289 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
290 } else { // directory is on several shards
291 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
292 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
293 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
294 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
298 wfProfileOut( __METHOD__
);
303 * @see FileBackendStore::doPrepare()
306 protected function doPrepareInternal( $container, $dir, array $params ) {
307 return Status
::newGood();
311 * @see FileBackend::doSecure()
314 final protected function doSecure( array $params ) {
315 wfProfileIn( __METHOD__
);
316 $status = Status
::newGood();
318 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
319 if ( $dir === null ) {
320 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
321 wfProfileOut( __METHOD__
);
322 return $status; // invalid storage path
325 if ( $shard !== null ) { // confined to a single container/shard
326 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
327 } else { // directory is on several shards
328 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
329 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
330 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
331 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
335 wfProfileOut( __METHOD__
);
340 * @see FileBackendStore::doSecure()
343 protected function doSecureInternal( $container, $dir, array $params ) {
344 return Status
::newGood();
348 * @see FileBackend::doClean()
351 final protected function doClean( array $params ) {
352 wfProfileIn( __METHOD__
);
353 $status = Status
::newGood();
355 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
356 if ( $dir === null ) {
357 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
358 wfProfileOut( __METHOD__
);
359 return $status; // invalid storage path
362 // Attempt to lock this directory...
363 $filesLockEx = array( $params['dir'] );
364 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
365 if ( !$status->isOK() ) {
366 wfProfileOut( __METHOD__
);
367 return $status; // abort
370 if ( $shard !== null ) { // confined to a single container/shard
371 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
372 } else { // directory is on several shards
373 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
374 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
375 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
376 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
380 wfProfileOut( __METHOD__
);
385 * @see FileBackendStore::doClean()
388 protected function doCleanInternal( $container, $dir, array $params ) {
389 return Status
::newGood();
393 * @see FileBackend::fileExists()
396 final public function fileExists( array $params ) {
397 wfProfileIn( __METHOD__
);
398 $stat = $this->getFileStat( $params );
399 wfProfileOut( __METHOD__
);
400 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
404 * @see FileBackend::getFileTimestamp()
407 final public function getFileTimestamp( array $params ) {
408 wfProfileIn( __METHOD__
);
409 $stat = $this->getFileStat( $params );
410 wfProfileOut( __METHOD__
);
411 return $stat ?
$stat['mtime'] : false;
415 * @see FileBackend::getFileSize()
418 final public function getFileSize( array $params ) {
419 wfProfileIn( __METHOD__
);
420 $stat = $this->getFileStat( $params );
421 wfProfileOut( __METHOD__
);
422 return $stat ?
$stat['size'] : false;
426 * @see FileBackend::getFileStat()
429 final public function getFileStat( array $params ) {
430 wfProfileIn( __METHOD__
);
431 $path = self
::normalizeStoragePath( $params['src'] );
432 if ( $path === null ) {
433 wfProfileOut( __METHOD__
);
434 return false; // invalid storage path
436 $latest = !empty( $params['latest'] );
437 if ( isset( $this->cache
[$path]['stat'] ) ) {
438 // If we want the latest data, check that this cached
439 // value was in fact fetched with the latest available data.
440 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
441 wfProfileOut( __METHOD__
);
442 return $this->cache
[$path]['stat'];
445 wfProfileIn( __METHOD__
. '-miss' );
446 $stat = $this->doGetFileStat( $params );
447 wfProfileOut( __METHOD__
. '-miss' );
448 if ( is_array( $stat ) ) { // don't cache negatives
449 $this->trimCache(); // limit memory
450 $this->cache
[$path]['stat'] = $stat;
451 $this->cache
[$path]['stat']['latest'] = $latest;
453 wfProfileOut( __METHOD__
);
458 * @see FileBackendStore::getFileStat()
460 abstract protected function doGetFileStat( array $params );
463 * @see FileBackend::getFileContents()
464 * @return bool|string
466 public function getFileContents( array $params ) {
467 wfProfileIn( __METHOD__
);
468 $tmpFile = $this->getLocalReference( $params );
470 wfProfileOut( __METHOD__
);
473 wfSuppressWarnings();
474 $data = file_get_contents( $tmpFile->getPath() );
476 wfProfileOut( __METHOD__
);
481 * @see FileBackend::getFileSha1Base36()
482 * @return bool|string
484 final public function getFileSha1Base36( array $params ) {
485 wfProfileIn( __METHOD__
);
486 $path = $params['src'];
487 if ( isset( $this->cache
[$path]['sha1'] ) ) {
488 wfProfileOut( __METHOD__
);
489 return $this->cache
[$path]['sha1'];
491 wfProfileIn( __METHOD__
. '-miss' );
492 $hash = $this->doGetFileSha1Base36( $params );
493 wfProfileOut( __METHOD__
. '-miss' );
494 if ( $hash ) { // don't cache negatives
495 $this->trimCache(); // limit memory
496 $this->cache
[$path]['sha1'] = $hash;
498 wfProfileOut( __METHOD__
);
503 * @see FileBackendStore::getFileSha1Base36()
506 protected function doGetFileSha1Base36( array $params ) {
507 $fsFile = $this->getLocalReference( $params );
511 return $fsFile->getSha1Base36();
516 * @see FileBackend::getFileProps()
519 final public function getFileProps( array $params ) {
520 wfProfileIn( __METHOD__
);
521 $fsFile = $this->getLocalReference( $params );
522 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
523 wfProfileOut( __METHOD__
);
528 * @see FileBackend::getLocalReference()
529 * @return TempFSFile|null
531 public function getLocalReference( array $params ) {
532 wfProfileIn( __METHOD__
);
533 $path = $params['src'];
534 if ( isset( $this->expensiveCache
[$path]['localRef'] ) ) {
535 wfProfileOut( __METHOD__
);
536 return $this->expensiveCache
[$path]['localRef'];
538 $tmpFile = $this->getLocalCopy( $params );
539 if ( $tmpFile ) { // don't cache negatives
540 $this->trimExpensiveCache(); // limit memory
541 $this->expensiveCache
[$path]['localRef'] = $tmpFile;
543 wfProfileOut( __METHOD__
);
548 * @see FileBackend::streamFile()
551 final public function streamFile( array $params ) {
552 wfProfileIn( __METHOD__
);
553 $status = Status
::newGood();
555 $info = $this->getFileStat( $params );
556 if ( !$info ) { // let StreamFile handle the 404
557 $status->fatal( 'backend-fail-notexists', $params['src'] );
560 // Set output buffer and HTTP headers for stream
561 $extraHeaders = isset( $params['headers'] ) ?
$params['headers'] : array();
562 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
563 if ( $res == StreamFile
::NOT_MODIFIED
) {
564 // do nothing; client cache is up to date
565 } elseif ( $res == StreamFile
::READY_STREAM
) {
566 wfProfileIn( __METHOD__
. '-send' );
567 $status = $this->doStreamFile( $params );
568 wfProfileOut( __METHOD__
. '-send' );
570 $status->fatal( 'backend-fail-stream', $params['src'] );
573 wfProfileOut( __METHOD__
);
578 * @see FileBackendStore::streamFile()
581 protected function doStreamFile( array $params ) {
582 $status = Status
::newGood();
584 $fsFile = $this->getLocalReference( $params );
586 $status->fatal( 'backend-fail-stream', $params['src'] );
587 } elseif ( !readfile( $fsFile->getPath() ) ) {
588 $status->fatal( 'backend-fail-stream', $params['src'] );
595 * @copydoc FileBackend::getFileList()
596 * @return Array|null|Traversable
598 final public function getFileList( array $params ) {
599 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
600 if ( $dir === null ) { // invalid storage path
603 if ( $shard !== null ) {
604 // File listing is confined to a single container/shard
605 return $this->getFileListInternal( $fullCont, $dir, $params );
607 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
608 // File listing spans multiple containers/shards
609 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
610 return new FileBackendStoreShardListIterator( $this,
611 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
616 * Do not call this function from places outside FileBackend
618 * @see FileBackendStore::getFileList()
620 * @param $container string Resolved container name
621 * @param $dir string Resolved path relative to container
622 * @param $params Array
623 * @return Traversable|Array|null
625 abstract public function getFileListInternal( $container, $dir, array $params );
628 * Get the list of supported operations and their corresponding FileOp classes.
632 protected function supportedOperations() {
634 'store' => 'StoreFileOp',
635 'copy' => 'CopyFileOp',
636 'move' => 'MoveFileOp',
637 'delete' => 'DeleteFileOp',
638 'create' => 'CreateFileOp',
639 'null' => 'NullFileOp'
644 * Return a list of FileOp objects from a list of operations.
645 * Do not call this function from places outside FileBackend.
647 * The result must have the same number of items as the input.
648 * An exception is thrown if an unsupported operation is requested.
650 * @param $ops Array Same format as doOperations()
651 * @return Array List of FileOp objects
652 * @throws MWException
654 final public function getOperations( array $ops ) {
655 $supportedOps = $this->supportedOperations();
657 $performOps = array(); // array of FileOp objects
658 // Build up ordered array of FileOps...
659 foreach ( $ops as $operation ) {
660 $opName = $operation['op'];
661 if ( isset( $supportedOps[$opName] ) ) {
662 $class = $supportedOps[$opName];
663 // Get params for this operation
664 $params = $operation;
665 // Append the FileOp class
666 $performOps[] = new $class( $this, $params );
668 throw new MWException( "Operation `$opName` is not supported." );
676 * @see FileBackend::doOperationsInternal()
679 protected function doOperationsInternal( array $ops, array $opts ) {
680 wfProfileIn( __METHOD__
);
681 $status = Status
::newGood();
683 // Build up a list of FileOps...
684 $performOps = $this->getOperations( $ops );
686 // Acquire any locks as needed...
687 if ( empty( $opts['nonLocking'] ) ) {
688 // Build up a list of files to lock...
689 $filesLockEx = $filesLockSh = array();
690 foreach ( $performOps as $fileOp ) {
691 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
692 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
694 // Optimization: if doing an EX lock anyway, don't also set an SH one
695 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
696 // Get a shared lock on the parent directory of each path changed
697 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
698 // Try to lock those files for the scope of this function...
699 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
700 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
701 if ( !$status->isOK() ) {
702 wfProfileOut( __METHOD__
);
703 return $status; // abort
707 // Clear any cache entries (after locks acquired)
710 // Actually attempt the operation batch...
711 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
713 // Merge errors into status fields
714 $status->merge( $subStatus );
715 $status->success
= $subStatus->success
; // not done in merge()
717 wfProfileOut( __METHOD__
);
722 * @see FileBackend::clearCache()
724 final public function clearCache( array $paths = null ) {
725 if ( is_array( $paths ) ) {
726 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
727 $paths = array_filter( $paths, 'strlen' ); // remove nulls
729 if ( $paths === null ) {
730 $this->cache
= array();
731 $this->expensiveCache
= array();
733 foreach ( $paths as $path ) {
734 unset( $this->cache
[$path] );
735 unset( $this->expensiveCache
[$path] );
738 $this->doClearCache( $paths );
742 * Clears any additional stat caches for storage paths
744 * @see FileBackend::clearCache()
746 * @param $paths Array Storage paths (optional)
749 protected function doClearCache( array $paths = null ) {}
752 * Prune the inexpensive cache if it is too big to add an item
756 protected function trimCache() {
757 if ( count( $this->cache
) >= $this->maxCacheSize
) {
758 reset( $this->cache
);
759 unset( $this->cache
[key( $this->cache
)] );
764 * Prune the expensive cache if it is too big to add an item
768 protected function trimExpensiveCache() {
769 if ( count( $this->expensiveCache
) >= $this->maxExpensiveCacheSize
) {
770 reset( $this->expensiveCache
);
771 unset( $this->expensiveCache
[key( $this->expensiveCache
)] );
776 * Check if a container name is valid.
777 * This checks for for length and illegal characters.
779 * @param $container string
782 final protected static function isValidContainerName( $container ) {
783 // This accounts for Swift and S3 restrictions while leaving room
784 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
785 // This disallows directory separators or traversal characters.
786 // Note that matching strings URL encode to the same string;
787 // in Swift, the length restriction is *after* URL encoding.
788 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
792 * Splits a storage path into an internal container name,
793 * an internal relative file name, and a container shard suffix.
794 * Any shard suffix is already appended to the internal container name.
795 * This also checks that the storage path is valid and within this backend.
797 * If the container is sharded but a suffix could not be determined,
798 * this means that the path can only refer to a directory and can only
799 * be scanned by looking in all the container shards.
801 * @param $storagePath string
802 * @return Array (container, path, container suffix) or (null, null, null) if invalid
804 final protected function resolveStoragePath( $storagePath ) {
805 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
806 if ( $backend === $this->name
) { // must be for this backend
807 $relPath = self
::normalizeContainerPath( $relPath );
808 if ( $relPath !== null ) {
809 // Get shard for the normalized path if this container is sharded
810 $cShard = $this->getContainerShard( $container, $relPath );
811 // Validate and sanitize the relative path (backend-specific)
812 $relPath = $this->resolveContainerPath( $container, $relPath );
813 if ( $relPath !== null ) {
814 // Prepend any wiki ID prefix to the container name
815 $container = $this->fullContainerName( $container );
816 if ( self
::isValidContainerName( $container ) ) {
817 // Validate and sanitize the container name (backend-specific)
818 $container = $this->resolveContainerName( "{$container}{$cShard}" );
819 if ( $container !== null ) {
820 return array( $container, $relPath, $cShard );
826 return array( null, null, null );
830 * Like resolveStoragePath() except null values are returned if
831 * the container is sharded and the shard could not be determined.
833 * @see FileBackendStore::resolveStoragePath()
835 * @param $storagePath string
836 * @return Array (container, path) or (null, null) if invalid
838 final protected function resolveStoragePathReal( $storagePath ) {
839 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
840 if ( $cShard !== null ) {
841 return array( $container, $relPath );
843 return array( null, null );
847 * Get the container name shard suffix for a given path.
848 * Any empty suffix means the container is not sharded.
850 * @param $container string Container name
851 * @param $relStoragePath string Storage path relative to the container
852 * @return string|null Returns null if shard could not be determined
854 final protected function getContainerShard( $container, $relPath ) {
855 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
856 if ( $levels == 1 ||
$levels == 2 ) {
857 // Hash characters are either base 16 or 36
858 $char = ( $base == 36 ) ?
'[0-9a-z]' : '[0-9a-f]';
859 // Get a regex that represents the shard portion of paths.
860 // The concatenation of the captures gives us the shard.
861 if ( $levels === 1 ) { // 16 or 36 shards per container
862 $hashDirRegex = '(' . $char . ')';
863 } else { // 256 or 1296 shards per container
864 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
865 $hashDirRegex = $char . '/(' . $char . '{2})';
866 } else { // short hash dir format (e.g. "a/b/c")
867 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
870 // Allow certain directories to be above the hash dirs so as
871 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
872 // They must be 2+ chars to avoid any hash directory ambiguity.
874 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
875 return '.' . implode( '', array_slice( $m, 1 ) );
877 return null; // failed to match
879 return ''; // no sharding
883 * Get the sharding config for a container.
884 * If greater than 0, then all file storage paths within
885 * the container are required to be hashed accordingly.
887 * @param $container string
888 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
890 final protected function getContainerHashLevels( $container ) {
891 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
892 $config = $this->shardViaHashLevels
[$container];
893 $hashLevels = (int)$config['levels'];
894 if ( $hashLevels == 1 ||
$hashLevels == 2 ) {
895 $hashBase = (int)$config['base'];
896 if ( $hashBase == 16 ||
$hashBase == 36 ) {
897 return array( $hashLevels, $hashBase, $config['repeat'] );
901 return array( 0, 0, false ); // no sharding
905 * Get a list of full container shard suffixes for a container
907 * @param $container string
910 final protected function getContainerSuffixes( $container ) {
912 list( $digits, $base ) = $this->getContainerHashLevels( $container );
914 $numShards = pow( $base, $digits );
915 for ( $index = 0; $index < $numShards; $index++
) {
916 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
923 * Get the full container name, including the wiki ID prefix
925 * @param $container string
928 final protected function fullContainerName( $container ) {
929 if ( $this->wikiId
!= '' ) {
930 return "{$this->wikiId}-$container";
937 * Resolve a container name, checking if it's allowed by the backend.
938 * This is intended for internal use, such as encoding illegal chars.
939 * Subclasses can override this to be more restrictive.
941 * @param $container string
942 * @return string|null
944 protected function resolveContainerName( $container ) {
949 * Resolve a relative storage path, checking if it's allowed by the backend.
950 * This is intended for internal use, such as encoding illegal chars or perhaps
951 * getting absolute paths (e.g. FS based backends). Note that the relative path
952 * may be the empty string (e.g. the path is simply to the container).
954 * @param $container string Container name
955 * @param $relStoragePath string Storage path relative to the container
956 * @return string|null Path or null if not valid
958 protected function resolveContainerPath( $container, $relStoragePath ) {
959 return $relStoragePath;
964 * FileBackendStore helper function to handle file listings that span container shards.
965 * Do not use this class from places outside of FileBackendStore.
967 * @ingroup FileBackend
969 class FileBackendStoreShardListIterator
implements Iterator
{
970 /* @var FileBackendStore */
975 protected $shardSuffixes;
976 protected $container; // string
977 protected $directory; // string
979 /* @var Traversable */
981 protected $curShard = 0; // integer
982 protected $pos = 0; // integer
985 * @param $backend FileBackendStore
986 * @param $container string Full storage container name
987 * @param $dir string Storage directory relative to container
988 * @param $suffixes Array List of container shard suffixes
989 * @param $params Array
991 public function __construct(
992 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
994 $this->backend
= $backend;
995 $this->container
= $container;
996 $this->directory
= $dir;
997 $this->shardSuffixes
= $suffixes;
998 $this->params
= $params;
1002 * @see Iterator::current()
1003 * @return string|bool String or false
1005 public function current() {
1006 if ( is_array( $this->iter
) ) {
1007 return current( $this->iter
);
1009 return $this->iter
->current();
1014 * @see Iterator::key()
1017 public function key() {
1022 * @see Iterator::next()
1025 public function next() {
1027 if ( is_array( $this->iter
) ) {
1028 next( $this->iter
);
1030 $this->iter
->next();
1032 // Find the next non-empty shard if no elements are left
1033 $this->nextShardIteratorIfNotValid();
1037 * @see Iterator::rewind()
1040 public function rewind() {
1042 $this->curShard
= 0;
1043 $this->setIteratorFromCurrentShard();
1044 // Find the next non-empty shard if this one has no elements
1045 $this->nextShardIteratorIfNotValid();
1049 * @see Iterator::valid()
1052 public function valid() {
1053 if ( $this->iter
== null ) {
1054 return false; // some failure?
1055 } elseif ( is_array( $this->iter
) ) {
1056 return ( current( $this->iter
) !== false ); // no paths can have this value
1058 return $this->iter
->valid();
1063 * If the list iterator for this container shard is out of items,
1064 * then move on to the next container that has items.
1065 * If there are none, then it advances to the last container.
1067 protected function nextShardIteratorIfNotValid() {
1068 while ( !$this->valid() ) {
1069 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1070 break; // no more container shards
1072 $this->setIteratorFromCurrentShard();
1077 * Set the list iterator to that of the current container shard
1079 protected function setIteratorFromCurrentShard() {
1080 $suffix = $this->shardSuffixes
[$this->curShard
];
1081 $this->iter
= $this->backend
->getFileListInternal(
1082 "{$this->container}{$suffix}", $this->directory
, $this->params
);