9 * Base class for all file backend classes (including multi-write backends).
10 * This class defines the methods as abstract that subclasses must implement.
11 * Outside callers can assume that all backends will have these functions.
13 * All "storage paths" are of the format "mwstore://backend/container/path".
14 * The paths use UNIX file system (FS) notation, though any particular backend may
15 * not actually be using a local filesystem. Therefore, the paths are only virtual.
17 * Backend contents are stored under wiki-specific container names by default.
18 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
19 * segregation must be done by setting the container paths appropriately.
21 * FS-based backends are somewhat more restrictive due to the existence of real
22 * directory files; a regular file cannot have the same name as a directory. Other
23 * backends with virtual directories may not have this limitation. Callers should
24 * store files in such a way that no files and directories are under the same path.
26 * Methods should avoid throwing exceptions at all costs.
27 * As a corollary, external dependencies should be kept to a minimum.
29 * @ingroup FileBackend
32 abstract class FileBackendBase
{
33 protected $name; // unique backend name
34 protected $wikiId; // unique wiki name
35 protected $readOnly; // string
36 /** @var LockManager */
37 protected $lockManager;
40 * Create a new backend instance from configuration.
41 * This should only be called from within FileBackendGroup.
44 * 'name' : The unique name of this backend.
45 * 'wikiId' : Prefix to container names that is unique to this wiki.
46 * This should consist of alphanumberic, '-', and '_' chars.
47 * 'lockManager' : Registered name of a file lock manager to use.
48 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
49 * It should be an explanation for the backend being read-only.
51 * @param $config Array
53 public function __construct( array $config ) {
54 $this->name
= $config['name'];
55 $this->wikiId
= isset( $config['wikiId'] )
57 : wfWikiID(); // e.g. "my_wiki-en_"
58 $this->wikiId
= $this->resolveWikiId( $this->wikiId
);
59 $this->lockManager
= LockManagerGroup
::singleton()->get( $config['lockManager'] );
60 $this->readOnly
= isset( $config['readOnly'] )
61 ?
(string)$config['readOnly']
66 * Normalize a wiki ID by replacing characters that are
67 * not supported by the backend as part of container names.
69 * @param $wikiId string
72 protected function resolveWikiId( $wikiId ) {
77 * Get the unique backend name.
79 * We may have multiple different backends of the same type.
80 * For example, we can have two Swift backends using different proxies.
84 final public function getName() {
89 * This is the main entry point into the backend for write operations.
90 * Callers supply an ordered list of operations to perform as a transaction.
91 * If any serious errors occur, all attempted operations will be rolled back.
93 * $ops is an array of arrays. The outer array holds a list of operations.
94 * Each inner array is a set of key value pairs that specify an operation.
96 * Supported operations and their parameters:
97 * a) Create a new file in storage with the contents of a string
100 * 'dst' => <storage path>,
101 * 'content' => <string of new file contents>,
102 * 'overwriteDest' => <boolean>,
103 * 'overwriteSame' => <boolean>
105 * b) Copy a file system file into storage
108 * 'src' => <file system path>,
109 * 'dst' => <storage path>,
110 * 'overwriteDest' => <boolean>,
111 * 'overwriteSame' => <boolean>
113 * c) Copy a file within storage
116 * 'src' => <storage path>,
117 * 'dst' => <storage path>,
118 * 'overwriteDest' => <boolean>,
119 * 'overwriteSame' => <boolean>
121 * d) Move a file within storage
124 * 'src' => <storage path>,
125 * 'dst' => <storage path>,
126 * 'overwriteDest' => <boolean>,
127 * 'overwriteSame' => <boolean>
129 * e) Delete a file within storage
132 * 'src' => <storage path>,
133 * 'ignoreMissingSource' => <boolean>
135 * f) Do nothing (no-op)
140 * Boolean flags for operations (operation-specific):
141 * 'ignoreMissingSource' : The operation will simply succeed and do
142 * nothing if the source file does not exist.
143 * 'overwriteDest' : Any destination file will be overwritten.
144 * 'overwriteSame' : An error will not be given if a file already
145 * exists at the destination that has the same
146 * contents as the new contents to be written there.
148 * $opts is an associative of boolean flags, including:
149 * 'force' : Errors that would normally cause a rollback do not.
150 * The remaining operations are still attempted if any fail.
151 * 'nonLocking' : No locks are acquired for the operations.
152 * This can increase performance for non-critical writes.
153 * This has no effect unless the 'force' flag is set.
154 * 'allowStale' : Don't require the latest available data.
155 * This can increase performance for non-critical writes.
156 * This has no effect unless the 'force' flag is set.
159 * This returns a Status, which contains all warnings and fatals that occured
160 * during the operation. The 'failCount', 'successCount', and 'success' members
161 * will reflect each operation attempted. The status will be "OK" unless any
162 * of the operations failed and the 'force' parameter was not set.
164 * @param $ops Array List of operations to execute in order
165 * @param $opts Array Batch operation options
168 final public function doOperations( array $ops, array $opts = array() ) {
169 if ( $this->readOnly
!= '' ) {
170 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
172 if ( empty( $opts['force'] ) ) { // sanity
173 unset( $opts['nonLocking'] );
174 unset( $opts['allowStale'] );
176 return $this->doOperationsInternal( $ops, $opts );
180 * @see FileBackendBase::doOperations()
182 abstract protected function doOperationsInternal( array $ops, array $opts );
185 * Same as doOperations() except it takes a single operation.
186 * If you are doing a batch of operations that should either
187 * all succeed or all fail, then use that function instead.
189 * @see FileBackendBase::doOperations()
191 * @param $op Array Operation
192 * @param $opts Array Operation options
195 final public function doOperation( array $op, array $opts = array() ) {
196 return $this->doOperations( array( $op ), $opts );
200 * Performs a single create operation.
201 * This sets $params['op'] to 'create' and passes it to doOperation().
203 * @see FileBackendBase::doOperation()
205 * @param $params Array Operation parameters
206 * @param $opts Array Operation options
209 final public function create( array $params, array $opts = array() ) {
210 $params['op'] = 'create';
211 return $this->doOperation( $params, $opts );
215 * Performs a single store operation.
216 * This sets $params['op'] to 'store' and passes it to doOperation().
218 * @see FileBackendBase::doOperation()
220 * @param $params Array Operation parameters
221 * @param $opts Array Operation options
224 final public function store( array $params, array $opts = array() ) {
225 $params['op'] = 'store';
226 return $this->doOperation( $params, $opts );
230 * Performs a single copy operation.
231 * This sets $params['op'] to 'copy' and passes it to doOperation().
233 * @see FileBackendBase::doOperation()
235 * @param $params Array Operation parameters
236 * @param $opts Array Operation options
239 final public function copy( array $params, array $opts = array() ) {
240 $params['op'] = 'copy';
241 return $this->doOperation( $params, $opts );
245 * Performs a single move operation.
246 * This sets $params['op'] to 'move' and passes it to doOperation().
248 * @see FileBackendBase::doOperation()
250 * @param $params Array Operation parameters
251 * @param $opts Array Operation options
254 final public function move( array $params, array $opts = array() ) {
255 $params['op'] = 'move';
256 return $this->doOperation( $params, $opts );
260 * Performs a single delete operation.
261 * This sets $params['op'] to 'delete' and passes it to doOperation().
263 * @see FileBackendBase::doOperation()
265 * @param $params Array Operation parameters
266 * @param $opts Array Operation options
269 final public function delete( array $params, array $opts = array() ) {
270 $params['op'] = 'delete';
271 return $this->doOperation( $params, $opts );
275 * Concatenate a list of storage files into a single file on the file system
277 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
278 * dst : file system path to 0-byte temp file
280 * @param $params Array Operation parameters
283 abstract public function concatenate( array $params );
286 * Prepare a storage path for usage. This will create containers
287 * that don't yet exist or, on FS backends, create parent directories.
290 * dir : storage directory
292 * @param $params Array
295 final public function prepare( array $params ) {
296 if ( $this->readOnly
!= '' ) {
297 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
299 return $this->doPrepare( $params );
303 * @see FileBackendBase::prepare()
305 abstract protected function doPrepare( array $params );
308 * Take measures to block web access to a directory and
309 * the container it belongs to. FS backends might add .htaccess
310 * files wheras backends like Swift this might restrict container
311 * access to backend user that represents end-users in web request.
312 * This is not guaranteed to actually do anything.
315 * dir : storage directory
316 * noAccess : try to deny file access
317 * noListing : try to deny file listing
319 * @param $params Array
322 final public function secure( array $params ) {
323 if ( $this->readOnly
!= '' ) {
324 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
326 return $this->doSecure( $params );
330 * @see FileBackendBase::secure()
332 abstract protected function doSecure( array $params );
335 * Clean up an empty storage directory.
336 * On FS backends, the directory will be deleted. Others may do nothing.
339 * dir : storage directory
341 * @param $params Array
344 final public function clean( array $params ) {
345 if ( $this->readOnly
!= '' ) {
346 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
348 return $this->doClean( $params );
352 * @see FileBackendBase::clean()
354 abstract protected function doClean( array $params );
357 * Check if a file exists at a storage path in the backend.
358 * This returns false if only a directory exists at the path.
361 * src : source storage path
362 * latest : use the latest available data
364 * @param $params Array
365 * @return bool|null Returns null on failure
367 abstract public function fileExists( array $params );
370 * Get the last-modified timestamp of the file at a storage path.
373 * src : source storage path
374 * latest : use the latest available data
376 * @param $params Array
377 * @return string|false TS_MW timestamp or false on failure
379 abstract public function getFileTimestamp( array $params );
382 * Get the contents of a file at a storage path in the backend.
383 * This should be avoided for potentially large files.
386 * src : source storage path
387 * latest : use the latest available data
389 * @param $params Array
390 * @return string|false Returns false on failure
392 abstract public function getFileContents( array $params );
395 * Get the size (bytes) of a file at a storage path in the backend.
398 * src : source storage path
399 * latest : use the latest available data
401 * @param $params Array
402 * @return integer|false Returns false on failure
404 abstract public function getFileSize( array $params );
407 * Get quick information about a file at a storage path in the backend.
408 * If the file does not exist, then this returns false.
409 * Otherwise, the result is an associative array that includes:
410 * mtime : the last-modified timestamp (TS_MW)
411 * size : the file size (bytes)
414 * src : source storage path
415 * latest : use the latest available data
417 * @param $params Array
418 * @return Array|false|null Returns null on failure
420 abstract public function getFileStat( array $params );
423 * Get a SHA-1 hash of the file at a storage path in the backend.
426 * src : source storage path
427 * latest : use the latest available data
429 * @param $params Array
430 * @return string|false Hash string or false on failure
432 abstract public function getFileSha1Base36( array $params );
435 * Get the properties of the file at a storage path in the backend.
436 * Returns FSFile::placeholderProps() on failure.
439 * src : source storage path
440 * latest : use the latest available data
442 * @param $params Array
445 abstract public function getFileProps( array $params );
448 * Stream the file at a storage path in the backend.
449 * If the file does not exists, a 404 error will be given.
450 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
451 * must be sent if streaming began, while none should be sent otherwise.
452 * Implementations should flush the output buffer before sending data.
455 * src : source storage path
456 * headers : additional HTTP headers to send on success
457 * latest : use the latest available data
459 * @param $params Array
462 abstract public function streamFile( array $params );
465 * Returns a file system file, identical to the file at a storage path.
466 * The file returned is either:
467 * a) A local copy of the file at a storage path in the backend.
468 * The temporary copy will have the same extension as the source.
469 * b) An original of the file at a storage path in the backend.
470 * Temporary files may be purged when the file object falls out of scope.
472 * Write operations should *never* be done on this file as some backends
473 * may do internal tracking or may be instances of FileBackendMultiWrite.
474 * In that later case, there are copies of the file that must stay in sync.
477 * src : source storage path
478 * latest : use the latest available data
480 * @param $params Array
481 * @return FSFile|null Returns null on failure
483 abstract public function getLocalReference( array $params );
486 * Get a local copy on disk of the file at a storage path in the backend.
487 * The temporary copy will have the same file extension as the source.
488 * Temporary files may be purged when the file object falls out of scope.
491 * src : source storage path
492 * latest : use the latest available data
494 * @param $params Array
495 * @return TempFSFile|null Returns null on failure
497 abstract public function getLocalCopy( array $params );
500 * Get an iterator to list out all stored files under a storage directory.
501 * If the directory is of the form "mwstore://container", then all items in
502 * the container should be listed. If of the form "mwstore://container/dir",
503 * then all items under that container directory should be listed.
504 * Results should be storage paths relative to the given directory.
507 * dir : storage path directory
509 * @return Traversable|Array|null Returns null on failure
511 abstract public function getFileList( array $params );
514 * Invalidate any in-process file existence and property cache.
515 * If $paths is given, then only the cache for those files will be cleared.
517 * @param $paths Array Storage paths
519 abstract public function clearCache( array $paths = null );
522 * Lock the files at the given storage paths in the backend.
523 * This will either lock all the files or none (on failure).
525 * Callers should consider using getScopedFileLocks() instead.
527 * @param $paths Array Storage paths
528 * @param $type integer LockManager::LOCK_* constant
531 final public function lockFiles( array $paths, $type ) {
532 return $this->lockManager
->lock( $paths, $type );
536 * Unlock the files at the given storage paths in the backend.
538 * @param $paths Array Storage paths
539 * @param $type integer LockManager::LOCK_* constant
542 final public function unlockFiles( array $paths, $type ) {
543 return $this->lockManager
->unlock( $paths, $type );
547 * Lock the files at the given storage paths in the backend.
548 * This will either lock all the files or none (on failure).
549 * On failure, the status object will be updated with errors.
551 * Once the return value goes out scope, the locks will be released and
552 * the status updated. Unlock fatals will not change the status "OK" value.
554 * @param $paths Array Storage paths
555 * @param $type integer LockManager::LOCK_* constant
556 * @param $status Status Status to update on lock/unlock
557 * @return ScopedLock|null Returns null on failure
559 final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
560 return ScopedLock
::factory( $this->lockManager
, $paths, $type, $status );
565 * Base class for all single-write backends.
566 * This class defines the methods as abstract that subclasses must implement.
567 * Callers outside of FileBackend and its helper classes, such as FileOp,
568 * should only call functions that are present in FileBackendBase.
570 * The FileBackendBase operations are implemented using primitive functions
571 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
572 * This class is also responsible for path resolution and sanitization.
574 * @ingroup FileBackend
577 abstract class FileBackend
extends FileBackendBase
{
579 protected $cache = array(); // (storage path => key => value)
580 protected $maxCacheSize = 75; // integer; max paths with entries
582 protected $shardViaHashLevels = array(); // (container name => integer)
585 * Create a file in the backend with the given contents.
586 * Do not call this function from places outside FileBackend and FileOp.
589 * content : the raw file contents
590 * dst : destination storage path
591 * overwriteDest : overwrite any file that exists at the destination
593 * @param $params Array
596 final public function createInternal( array $params ) {
597 $status = $this->doCreateInternal( $params );
598 $this->clearCache( array( $params['dst'] ) );
603 * @see FileBackend::createInternal()
605 abstract protected function doCreateInternal( array $params );
608 * Store a file into the backend from a file on disk.
609 * Do not call this function from places outside FileBackend and FileOp.
612 * src : source path on disk
613 * dst : destination storage path
614 * overwriteDest : overwrite any file that exists at the destination
616 * @param $params Array
619 final public function storeInternal( array $params ) {
620 $status = $this->doStoreInternal( $params );
621 $this->clearCache( array( $params['dst'] ) );
626 * @see FileBackend::storeInternal()
628 abstract protected function doStoreInternal( array $params );
631 * Copy a file from one storage path to another in the backend.
632 * Do not call this function from places outside FileBackend and FileOp.
635 * src : source storage path
636 * dst : destination storage path
637 * overwriteDest : overwrite any file that exists at the destination
639 * @param $params Array
642 final public function copyInternal( array $params ) {
643 $status = $this->doCopyInternal( $params );
644 $this->clearCache( array( $params['dst'] ) );
649 * @see FileBackend::copyInternal()
651 abstract protected function doCopyInternal( array $params );
654 * Delete a file at the storage path.
655 * Do not call this function from places outside FileBackend and FileOp.
658 * src : source storage path
659 * ignoreMissingSource : do nothing if the source file does not exist
661 * @param $params Array
664 final public function deleteInternal( array $params ) {
665 $status = $this->doDeleteInternal( $params );
666 $this->clearCache( array( $params['src'] ) );
671 * @see FileBackend::deleteInternal()
673 abstract protected function doDeleteInternal( array $params );
676 * Move a file from one storage path to another in the backend.
677 * Do not call this function from places outside FileBackend and FileOp.
680 * src : source storage path
681 * dst : destination storage path
682 * overwriteDest : overwrite any file that exists at the destination
684 * @param $params Array
687 final public function moveInternal( array $params ) {
688 $status = $this->doMoveInternal( $params );
689 $this->clearCache( array( $params['src'], $params['dst'] ) );
694 * @see FileBackend::moveInternal()
696 protected function doMoveInternal( array $params ) {
697 // Copy source to dest
698 $status = $this->copyInternal( $params );
699 if ( !$status->isOK() ) {
702 // Delete source (only fails due to races or medium going down)
703 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
704 $status->setResult( true, $status->value
); // ignore delete() errors
709 * @see FileBackendBase::concatenate()
711 final public function concatenate( array $params ) {
712 $status = Status
::newGood();
714 // Try to lock the source files for the scope of this function
715 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
716 if ( !$status->isOK() ) {
717 return $status; // abort
720 // Actually do the concatenation
721 $status->merge( $this->doConcatenate( $params ) );
727 * @see FileBackend::concatenate()
729 protected function doConcatenate( array $params ) {
730 $status = Status
::newGood();
731 $tmpPath = $params['dst']; // convenience
733 // Check that the specified temp file is valid...
734 wfSuppressWarnings();
735 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
737 if ( !$ok ) { // not present or not empty
738 $status->fatal( 'backend-fail-opentemp', $tmpPath );
742 // Build up the temp file using the source chunks (in order)...
743 $tmpHandle = fopen( $tmpPath, 'a' );
744 if ( $tmpHandle === false ) {
745 $status->fatal( 'backend-fail-opentemp', $tmpPath );
748 foreach ( $params['srcs'] as $virtualSource ) {
749 // Get a local FS version of the chunk
750 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
752 $status->fatal( 'backend-fail-read', $virtualSource );
755 // Get a handle to the local FS version
756 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
757 if ( $sourceHandle === false ) {
758 fclose( $tmpHandle );
759 $status->fatal( 'backend-fail-read', $virtualSource );
762 // Append chunk to file (pass chunk size to avoid magic quotes)
763 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
764 fclose( $sourceHandle );
765 fclose( $tmpHandle );
766 $status->fatal( 'backend-fail-writetemp', $tmpPath );
769 fclose( $sourceHandle );
771 if ( !fclose( $tmpHandle ) ) {
772 $status->fatal( 'backend-fail-closetemp', $tmpPath );
780 * @see FileBackendBase::doPrepare()
782 final protected function doPrepare( array $params ) {
783 $status = Status
::newGood();
784 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
785 if ( $dir === null ) {
786 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
787 return $status; // invalid storage path
789 if ( $shard !== null ) { // confined to a single container/shard
790 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
791 } else { // directory is on several shards
792 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
793 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
794 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
795 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
802 * @see FileBackend::doPrepare()
804 protected function doPrepareInternal( $container, $dir, array $params ) {
805 return Status
::newGood();
809 * @see FileBackendBase::doSecure()
811 final protected function doSecure( array $params ) {
812 $status = Status
::newGood();
813 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
814 if ( $dir === null ) {
815 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
816 return $status; // invalid storage path
818 if ( $shard !== null ) { // confined to a single container/shard
819 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
820 } else { // directory is on several shards
821 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
822 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
823 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
824 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
831 * @see FileBackend::doSecure()
833 protected function doSecureInternal( $container, $dir, array $params ) {
834 return Status
::newGood();
838 * @see FileBackendBase::doClean()
840 final protected function doClean( array $params ) {
841 $status = Status
::newGood();
842 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
843 if ( $dir === null ) {
844 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
845 return $status; // invalid storage path
847 if ( $shard !== null ) { // confined to a single container/shard
848 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
849 } else { // directory is on several shards
850 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
851 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
852 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
853 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
860 * @see FileBackend::doClean()
862 protected function doCleanInternal( $container, $dir, array $params ) {
863 return Status
::newGood();
867 * @see FileBackendBase::fileExists()
869 final public function fileExists( array $params ) {
870 $stat = $this->getFileStat( $params );
871 if ( $stat === null ) {
872 return null; // failure
878 * @see FileBackendBase::getFileTimestamp()
880 final public function getFileTimestamp( array $params ) {
881 $stat = $this->getFileStat( $params );
883 return $stat['mtime'];
890 * @see FileBackendBase::getFileSize()
892 final public function getFileSize( array $params ) {
893 $stat = $this->getFileStat( $params );
895 return $stat['size'];
902 * @see FileBackendBase::getFileStat()
904 final public function getFileStat( array $params ) {
905 $path = $params['src'];
906 if ( isset( $this->cache
[$path]['stat'] ) ) {
907 return $this->cache
[$path]['stat'];
909 $stat = $this->doGetFileStat( $params );
910 if ( is_array( $stat ) ) { // don't cache negatives
911 $this->trimCache(); // limit memory
912 $this->cache
[$path]['stat'] = $stat;
918 * @see FileBackend::getFileStat()
920 abstract protected function doGetFileStat( array $params );
923 * @see FileBackendBase::getFileContents()
925 public function getFileContents( array $params ) {
926 $tmpFile = $this->getLocalReference( $params );
930 wfSuppressWarnings();
931 $data = file_get_contents( $tmpFile->getPath() );
937 * @see FileBackendBase::getFileSha1Base36()
939 final public function getFileSha1Base36( array $params ) {
940 $path = $params['src'];
941 if ( isset( $this->cache
[$path]['sha1'] ) ) {
942 return $this->cache
[$path]['sha1'];
944 $hash = $this->doGetFileSha1Base36( $params );
945 if ( $hash ) { // don't cache negatives
946 $this->trimCache(); // limit memory
947 $this->cache
[$path]['sha1'] = $hash;
953 * @see FileBackend::getFileSha1Base36()
955 protected function doGetFileSha1Base36( array $params ) {
956 $fsFile = $this->getLocalReference( $params );
960 return $fsFile->getSha1Base36();
965 * @see FileBackendBase::getFileProps()
967 public function getFileProps( array $params ) {
968 $fsFile = $this->getLocalReference( $params );
970 return FSFile
::placeholderProps();
972 return $fsFile->getProps();
977 * @see FileBackendBase::getLocalReference()
979 public function getLocalReference( array $params ) {
980 $path = $params['src'];
981 if ( isset( $this->cache
[$path]['localRef'] ) ) {
982 return $this->cache
[$path]['localRef'];
984 $tmpFile = $this->getLocalCopy( $params );
985 if ( $tmpFile ) { // don't cache negatives
986 $this->trimCache(); // limit memory
987 $this->cache
[$path]['localRef'] = $tmpFile;
993 * @see FileBackendBase::streamFile()
995 final public function streamFile( array $params ) {
996 $status = Status
::newGood();
998 $info = $this->getFileStat( $params );
999 if ( !$info ) { // let StreamFile handle the 404
1000 $status->fatal( 'backend-fail-notexists', $params['src'] );
1003 // Set output buffer and HTTP headers for stream
1004 $extraHeaders = $params['headers'] ?
$params['headers'] : array();
1005 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
1006 if ( $res == StreamFile
::NOT_MODIFIED
) {
1007 // do nothing; client cache is up to date
1008 } elseif ( $res == StreamFile
::READY_STREAM
) {
1009 $status = $this->doStreamFile( $params );
1011 $status->fatal( 'backend-fail-stream', $params['src'] );
1018 * @see FileBackend::streamFile()
1020 protected function doStreamFile( array $params ) {
1021 $status = Status
::newGood();
1023 $fsFile = $this->getLocalReference( $params );
1025 $status->fatal( 'backend-fail-stream', $params['src'] );
1026 } elseif ( !readfile( $fsFile->getPath() ) ) {
1027 $status->fatal( 'backend-fail-stream', $params['src'] );
1034 * @see FileBackendBase::getFileList()
1036 final public function getFileList( array $params ) {
1037 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1038 if ( $dir === null ) { // invalid storage path
1041 if ( $shard !== null ) {
1042 // File listing is confined to a single container/shard
1043 return $this->getFileListInternal( $fullCont, $dir, $params );
1045 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1046 // File listing spans multiple containers/shards
1047 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1048 return new ContainerShardListIterator( $this,
1049 $fullCont, $this->getContainerSuffixes( $shortCont ), $params );
1054 * Do not call this function from places outside FileBackend and ContainerFileListIterator
1056 * @param $container string Resolved container name
1057 * @param $dir string Resolved path relative to container
1058 * @param $params Array
1059 * @see FileBackend::getFileList()
1061 abstract public function getFileListInternal( $container, $dir, array $params );
1064 * Get the list of supported operations and their corresponding FileOp classes.
1068 protected function supportedOperations() {
1070 'store' => 'StoreFileOp',
1071 'copy' => 'CopyFileOp',
1072 'move' => 'MoveFileOp',
1073 'delete' => 'DeleteFileOp',
1074 'create' => 'CreateFileOp',
1075 'null' => 'NullFileOp'
1080 * Return a list of FileOp objects from a list of operations.
1081 * Do not call this function from places outside FileBackend.
1083 * The result must have the same number of items as the input.
1084 * An exception is thrown if an unsupported operation is requested.
1086 * @param $ops Array Same format as doOperations()
1087 * @return Array List of FileOp objects
1088 * @throws MWException
1090 final public function getOperations( array $ops ) {
1091 $supportedOps = $this->supportedOperations();
1093 $performOps = array(); // array of FileOp objects
1094 // Build up ordered array of FileOps...
1095 foreach ( $ops as $operation ) {
1096 $opName = $operation['op'];
1097 if ( isset( $supportedOps[$opName] ) ) {
1098 $class = $supportedOps[$opName];
1099 // Get params for this operation
1100 $params = $operation;
1101 // Append the FileOp class
1102 $performOps[] = new $class( $this, $params );
1104 throw new MWException( "Operation `$opName` is not supported." );
1112 * @see FileBackendBase::doOperationsInternal()
1114 protected function doOperationsInternal( array $ops, array $opts ) {
1115 $status = Status
::newGood();
1117 // Build up a list of FileOps...
1118 $performOps = $this->getOperations( $ops );
1120 // Acquire any locks as needed...
1121 if ( empty( $opts['nonLocking'] ) ) {
1122 // Build up a list of files to lock...
1123 $filesLockEx = $filesLockSh = array();
1124 foreach ( $performOps as $fileOp ) {
1125 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1126 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1128 // Optimization: if doing an EX lock anyway, don't also set an SH one
1129 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1130 // Try to lock those files for the scope of this function...
1131 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
1132 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1133 if ( !$status->isOK() ) {
1134 return $status; // abort
1138 // Clear any cache entries (after locks acquired)
1139 $this->clearCache();
1141 // Actually attempt the operation batch...
1142 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
1144 // Merge errors into status fields
1145 $status->merge( $subStatus );
1146 $status->success
= $subStatus->success
; // not done in merge()
1152 * @see FileBackendBase::clearCache()
1154 final public function clearCache( array $paths = null ) {
1155 if ( $paths === null ) {
1156 $this->cache
= array();
1158 foreach ( $paths as $path ) {
1159 unset( $this->cache
[$path] );
1165 * Prune the cache if it is too big to add an item
1167 protected function trimCache() {
1168 if ( count( $this->cache
) >= $this->maxCacheSize
) {
1169 reset( $this->cache
);
1170 $key = key( $this->cache
);
1171 unset( $this->cache
[$key] );
1176 * Check if a given path is a mwstore:// path.
1177 * This does not do any actual validation or existence checks.
1179 * @param $path string
1182 final public static function isStoragePath( $path ) {
1183 return ( strpos( $path, 'mwstore://' ) === 0 );
1187 * Split a storage path (e.g. "mwstore://backend/container/path/to/object")
1188 * into a backend name, a container name, and a relative object path.
1190 * @param $storagePath string
1191 * @return Array (backend, container, rel object) or (null, null, null)
1193 final public static function splitStoragePath( $storagePath ) {
1194 if ( self
::isStoragePath( $storagePath ) ) {
1195 // Note: strlen( 'mwstore://' ) = 10
1196 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
1197 if ( count( $parts ) == 3 ) {
1198 return $parts; // e.g. "backend/container/path"
1199 } elseif ( count( $parts ) == 2 ) {
1200 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
1203 return array( null, null, null );
1207 * Check if a container name is valid.
1208 * This checks for for length and illegal characters.
1210 * @param $container string
1213 final protected static function isValidContainerName( $container ) {
1214 // This accounts for Swift and S3 restrictions while leaving room
1215 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1216 // Note that matching strings URL encode to the same string;
1217 // in Swift, the length resriction is *after* URL encoding.
1218 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1222 * Validate and normalize a relative storage path.
1223 * Null is returned if the path involves directory traversal.
1224 * Traversal is insecure for FS backends and broken for others.
1226 * @param $path string
1227 * @return string|null
1229 final protected static function normalizeStoragePath( $path ) {
1230 // Normalize directory separators
1231 $path = strtr( $path, '\\', '/' );
1232 // Use the same traversal protection as Title::secureAndSplit()
1233 if ( strpos( $path, '.' ) !== false ) {
1237 strpos( $path, './' ) === 0 ||
1238 strpos( $path, '../' ) === 0 ||
1239 strpos( $path, '/./' ) !== false ||
1240 strpos( $path, '/../' ) !== false
1249 * Splits a storage path into an internal container name,
1250 * an internal relative file name, and a container shard suffix.
1251 * Any shard suffix is already appended to the internal container name.
1252 * This also checks that the storage path is valid and within this backend.
1254 * If the container is sharded but a suffix could not be determined,
1255 * this means that the path can only refer to a directory and can only
1256 * be scanned by looking in all the container shards.
1258 * @param $storagePath string
1259 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1261 final protected function resolveStoragePath( $storagePath ) {
1262 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1263 if ( $backend === $this->name
) { // must be for this backend
1264 $relPath = self
::normalizeStoragePath( $relPath );
1265 if ( $relPath !== null ) {
1266 // Get shard for the normalized path if this container is sharded
1267 $cShard = $this->getContainerShard( $container, $relPath );
1268 // Validate and sanitize the relative path (backend-specific)
1269 $relPath = $this->resolveContainerPath( $container, $relPath );
1270 if ( $relPath !== null ) {
1271 // Prepend any wiki ID prefix to the container name
1272 $container = $this->fullContainerName( $container );
1273 if ( self
::isValidContainerName( $container ) ) {
1274 // Validate and sanitize the container name (backend-specific)
1275 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1276 if ( $container !== null ) {
1277 return array( $container, $relPath, $cShard );
1283 return array( null, null, null );
1287 * Like resolveStoragePath() except null values are returned if
1288 * the container is sharded and the shard could not be determined.
1290 * @see FileBackend::resolveStoragePath()
1292 * @param $storagePath string
1293 * @return Array (container, path) or (null, null) if invalid
1295 final protected function resolveStoragePathReal( $storagePath ) {
1296 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1297 if ( $cShard !== null ) {
1298 return array( $container, $relPath );
1300 return array( null, null );
1304 * Get the container name shard suffix for a given path.
1305 * Any empty suffix means the container is not sharded.
1307 * @param $container string Container name
1308 * @param $relStoragePath string Storage path relative to the container
1309 * @return string|null Returns null if shard could not be determined
1311 final protected function getContainerShard( $container, $relPath ) {
1312 $hashLevels = $this->getContainerHashLevels( $container );
1313 if ( $hashLevels === 1 ) { // 16 shards per container
1314 $hashDirRegex = '(?P<shard>[0-9a-f])';
1315 } elseif ( $hashLevels === 2 ) { // 256 shards per container
1316 $hashDirRegex = '[0-9a-f]/(?P<shard>[0-9a-f]{2})';
1318 return ''; // no sharding
1320 // Allow certain directories to be above the hash dirs so as
1321 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1322 // They must be 2+ chars to avoid any hash directory ambiguity.
1323 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1324 return '.' . str_pad( $m['shard'], $hashLevels, '0', STR_PAD_LEFT
);
1326 return null; // failed to match
1330 * Get the number of hash levels for a container.
1331 * If greater than 0, then all file storage paths within
1332 * the container are required to be hashed accordingly.
1334 * @param $container string
1337 final protected function getContainerHashLevels( $container ) {
1338 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1339 $hashLevels = (int)$this->shardViaHashLevels
[$container];
1340 if ( $hashLevels >= 0 && $hashLevels <= 2 ) {
1344 return 0; // no sharding
1348 * Get a list of full container shard suffixes for a container
1350 * @param $container string
1353 final protected function getContainerSuffixes( $container ) {
1355 $digits = $this->getContainerHashLevels( $container );
1356 if ( $digits > 0 ) {
1357 $numShards = 1 << ( $digits * 4 );
1358 for ( $index = 0; $index < $numShards; $index++
) {
1359 $shards[] = '.' . str_pad( dechex( $index ), $digits, '0', STR_PAD_LEFT
);
1366 * Get the full container name, including the wiki ID prefix
1368 * @param $container string
1371 final protected function fullContainerName( $container ) {
1372 if ( $this->wikiId
!= '' ) {
1373 return "{$this->wikiId}-$container";
1380 * Resolve a container name, checking if it's allowed by the backend.
1381 * This is intended for internal use, such as encoding illegal chars.
1382 * Subclasses can override this to be more restrictive.
1384 * @param $container string
1385 * @return string|null
1387 protected function resolveContainerName( $container ) {
1392 * Resolve a relative storage path, checking if it's allowed by the backend.
1393 * This is intended for internal use, such as encoding illegal chars or perhaps
1394 * getting absolute paths (e.g. FS based backends). Note that the relative path
1395 * may be the empty string (e.g. the path is simply to the container).
1397 * @param $container string Container name
1398 * @param $relStoragePath string Storage path relative to the container
1399 * @return string|null Path or null if not valid
1401 protected function resolveContainerPath( $container, $relStoragePath ) {
1402 return $relStoragePath;
1406 * Get the final extension from a storage or FS path
1408 * @param $path string
1411 final public static function extensionFromPath( $path ) {
1412 $i = strrpos( $path, '.' );
1413 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
1418 * FileBackend helper function to handle file listings that span container shards.
1419 * Do not use this class from places outside of FileBackend.
1421 * @ingroup FileBackend
1423 class ContainerShardListIterator
implements Iterator
{
1424 /* @var FileBackend */
1429 protected $shardSuffixes;
1430 protected $container; // string
1431 protected $directory; // string
1433 /* @var Traversable */
1435 protected $curShard = 0; // integer
1436 protected $pos = 0; // integer
1439 * @param $backend FileBackend
1440 * @param $container string Full storage container name
1441 * @param $dir string Storage directory relative to container
1442 * @param $suffixes Array List of container shard suffixes
1443 * @param $params Array
1445 public function __construct(
1446 FileBackend
$backend, $container, $dir, array $suffixes, array $params
1448 $this->backend
= $backend;
1449 $this->container
= $container;
1450 $this->directory
= $dir;
1451 $this->shardSuffixes
= $suffixes;
1452 $this->params
= $params;
1455 public function current() {
1456 if ( is_array( $this->iter
) ) {
1457 return current( $this->iter
);
1459 return $this->iter
->current();
1463 public function key() {
1467 public function next() {
1469 if ( is_array( $this->iter
) ) {
1470 next( $this->iter
);
1472 $this->iter
->next();
1474 // Find the next non-empty shard if no elements are left
1475 $this->nextShardIteratorIfNotValid();
1479 * If the iterator for this container shard is out of items,
1480 * then move on to the next container that has items.
1481 * If there are none, then it advances to the last container.
1483 protected function nextShardIteratorIfNotValid() {
1484 while ( !$this->valid() ) {
1485 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1486 break; // no more container shards
1488 $this->setIteratorFromCurrentShard();
1492 protected function setIteratorFromCurrentShard() {
1493 $suffix = $this->shardSuffixes
[$this->curShard
];
1494 $this->iter
= $this->backend
->getFileListInternal(
1495 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1498 public function rewind() {
1500 $this->curShard
= 0;
1501 $this->setIteratorFromCurrentShard();
1502 // Find the next non-empty shard if this one has no elements
1503 $this->nextShardIteratorIfNotValid();
1506 public function valid() {
1507 if ( $this->iter
== null ) {
1508 return false; // some failure?
1509 } elseif ( is_array( $this->iter
) ) {
1510 return ( current( $this->iter
) !== false ); // no paths can have this value
1512 return $this->iter
->valid();