Go to the documentation of this file.
31 use Psr\Log\LoggerAwareInterface;
32 use Psr\Log\LoggerInterface;
33 use Wikimedia\ScopedCallback;
34 use Psr\Log\NullLogger;
135 const STAT_ABSENT =
false;
138 const STAT_ERROR =
null;
140 const LIST_ERROR =
null;
142 const TEMPURL_ERROR =
null;
144 const EXISTENCE_ERROR =
null;
147 const TIMESTAMP_FAIL =
false;
149 const CONTENT_FAIL =
false;
151 const XATTRS_FAIL =
false;
153 const SIZE_FAIL =
false;
155 const SHA1_FAIL =
false;
191 $this->name = $config[
'name'];
192 $this->domainId = $config[
'domainId']
193 ?? $config[
'wikiId'];
194 if ( !preg_match(
'!^[a-zA-Z0-9-_]{1,255}$!', $this->name ) ) {
195 throw new InvalidArgumentException(
"Backend name '{$this->name}' is invalid." );
196 } elseif ( !is_string( $this->domainId ) ) {
197 throw new InvalidArgumentException(
198 "Backend domain ID not provided for '{$this->name}'." );
200 $this->lockManager = $config[
'lockManager'] ??
new NullLockManager( [] );
201 $this->fileJournal = $config[
'fileJournal']
203 $this->readOnly = isset( $config[
'readOnly'] )
204 ? (string)$config[
'readOnly']
206 $this->parallelize = isset( $config[
'parallelize'] )
207 ? (string)$config[
'parallelize']
209 $this->concurrency = isset( $config[
'concurrency'] )
210 ? (int)$config[
'concurrency']
212 $this->obResetFunc = $config[
'obResetFunc'] ?? [ $this,
'resetOutputBuffer' ];
213 $this->streamMimeFunc = $config[
'streamMimeFunc'] ??
null;
214 $this->statusWrapper = $config[
'statusWrapper'] ??
null;
216 $this->profiler = $config[
'profiler'] ??
null;
217 if ( !is_callable( $this->profiler ) ) {
218 $this->profiler =
null;
220 $this->logger = $config[
'logger'] ??
new NullLogger();
221 $this->statusWrapper = $config[
'statusWrapper'] ??
null;
223 if ( isset( $config[
'tmpDirectory'] ) ) {
273 return ( $this->readOnly !=
'' );
282 return ( $this->readOnly !=
'' ) ? $this->readOnly :
false;
303 return ( $this->
getFeatures() & $bitfield ) === $bitfield;
459 if ( empty( $opts[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
460 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
467 if ( empty( $opts[
'force'] ) ) {
468 unset( $opts[
'nonLocking'] );
472 $scope = ScopedCallback::newScopedIgnoreUserAbort();
496 final public function doOperation( array $op, array $opts = [] ) {
510 final public function create( array $params, array $opts = [] ) {
511 return $this->
doOperation( [
'op' =>
'create' ] + $params, $opts );
524 final public function store( array $params, array $opts = [] ) {
525 return $this->
doOperation( [
'op' =>
'store' ] + $params, $opts );
538 final public function copy( array $params, array $opts = [] ) {
539 return $this->
doOperation( [
'op' =>
'copy' ] + $params, $opts );
552 final public function move( array $params, array $opts = [] ) {
553 return $this->
doOperation( [
'op' =>
'move' ] + $params, $opts );
566 final public function delete( array $params, array $opts = [] ) {
567 return $this->
doOperation( [
'op' =>
'delete' ] + $params, $opts );
581 final public function describe( array $params, array $opts = [] ) {
582 return $this->
doOperation( [
'op' =>
'describe' ] + $params, $opts );
700 if ( empty( $opts[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
701 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
708 foreach ( $ops as &$op ) {
709 $op[
'overwrite'] =
true;
713 $scope = ScopedCallback::newScopedIgnoreUserAbort();
836 abstract public function concatenate( array $params );
856 final public function prepare( array $params ) {
857 if ( empty( $params[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
858 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
861 $scope = ScopedCallback::newScopedIgnoreUserAbort();
870 abstract protected function doPrepare( array $params );
888 final public function secure( array $params ) {
889 if ( empty( $params[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
890 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
893 $scope = ScopedCallback::newScopedIgnoreUserAbort();
902 abstract protected function doSecure( array $params );
922 final public function publish( array $params ) {
923 if ( empty( $params[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
924 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
927 $scope = ScopedCallback::newScopedIgnoreUserAbort();
936 abstract protected function doPublish( array $params );
949 final public function clean( array $params ) {
950 if ( empty( $params[
'bypassReadOnly'] ) && $this->
isReadOnly() ) {
951 return $this->
newStatus(
'backend-fail-readonly', $this->name, $this->readOnly );
954 $scope = ScopedCallback::newScopedIgnoreUserAbort();
955 return $this->
doClean( $params );
963 abstract protected function doClean( array $params );
981 abstract public function fileExists( array $params );
1009 return $contents[$params[
'src']];
1059 abstract public function getFileSize( array $params );
1077 abstract public function getFileStat( array $params );
1100 abstract public function getFileProps( array $params );
1121 abstract public function streamFile( array $params );
1144 return $fsFiles[$params[
'src']];
1179 $tmpFiles = $this->
getLocalCopyMulti( [
'srcs' => [ $params[
'src'] ] ] + $params );
1181 return $tmpFiles[$params[
'src']];
1312 abstract public function getFileList( array $params );
1331 return $this->
getFileList( [
'topOnly' =>
true ] + $params );
1352 abstract public function clearCache( array $paths =
null );
1382 $paths = array_map(
'FileBackend::normalizeStoragePath', $paths );
1384 return $this->
wrapStatus( $this->lockManager->lock( $paths,
$type, $timeout ) );
1395 $paths = array_map(
'FileBackend::normalizeStoragePath', $paths );
1397 return $this->
wrapStatus( $this->lockManager->unlock( $paths,
$type ) );
1419 if (
$type ===
'mixed' ) {
1420 foreach ( $paths as &$typePaths ) {
1421 $typePaths = array_map(
'FileBackend::normalizeStoragePath', $typePaths );
1424 $paths = array_map(
'FileBackend::normalizeStoragePath', $paths );
1456 return "mwstore://{$this->name}";
1489 foreach ( $ops as &$op ) {
1490 $src = $op[
'src'] ??
null;
1491 if ( $src instanceof
FSFile ) {
1492 $op[
'srcRef'] = $src;
1493 $op[
'src'] = $src->getPath();
1509 return ( strpos(
$path,
'mwstore://' ) === 0 );
1521 if ( self::isStoragePath( $storagePath ) ) {
1523 $parts = explode(
'/', substr( $storagePath, 10 ), 3 );
1524 if ( count( $parts ) >= 2 && $parts[0] !=
'' && $parts[1] !=
'' ) {
1525 if ( count( $parts ) == 3 ) {
1528 return [ $parts[0], $parts[1],
'' ];
1533 return [
null,
null, null ];
1545 if ( $relPath !==
null ) {
1547 if ( $relPath !==
null ) {
1548 return ( $relPath !=
'' )
1549 ?
"mwstore://{$backend}/{$container}/{$relPath}"
1550 :
"mwstore://{$backend}/{$container}";
1566 $storagePath = dirname( $storagePath );
1569 return ( $rel ===
null ) ? null : $storagePath;
1580 $i = strrpos(
$path,
'.' );
1581 $ext = $i ? substr(
$path, $i + 1 ) :
'';
1583 if ( $case ===
'lowercase' ) {
1585 } elseif ( $case ===
'uppercase' ) {
1600 return ( self::normalizeContainerPath(
$path ) !==
null );
1616 if ( !in_array(
$type, [
'inline',
'attachment' ] ) ) {
1617 throw new InvalidArgumentException(
"Invalid Content-Disposition type '$type'." );
1621 if ( strlen( $filename ) ) {
1622 $parts[] =
"filename*=UTF-8''" . rawurlencode( basename( $filename ) );
1625 return implode(
';', $parts );
1642 $path = preg_replace(
'![/]{2,}!',
'/',
$path );
1646 if ( strpos(
$path,
'.' ) !==
false ) {
1650 strpos(
$path,
'./' ) === 0 ||
1651 strpos(
$path,
'../' ) === 0 ||
1652 strpos(
$path,
'/./' ) !==
false ||
1653 strpos(
$path,
'/../' ) !==
false
1671 if ( count(
$args ) ) {
1685 return $this->statusWrapper ? call_user_func( $this->statusWrapper, $sv ) : $sv;
1697 while ( ob_get_status() ) {
1698 if ( !ob_end_clean() ) {
streamFile(array $params)
Stream the content of the file at a storage path in the backend.
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
Class for handling resource locking.
TempFSFileFactory $tmpFileFactory
doOperations(array $ops, array $opts=[])
This is the main entry point into the backend for write operations.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
static normalizeContainerPath( $path)
Validate and normalize a relative storage path.
doQuickOperationsInternal(array $ops)
static newFatal( $message,... $parameters)
Factory function for fatal errors.
preloadFileStat(array $params)
Preload file stat information (concurrently if possible) into in-process cache.
Base class for all file backend classes (including multi-write backends).
describe(array $params, array $opts=[])
Performs a single describe operation.
unlockFiles(array $paths, $type)
Unlock the files at the given storage paths in the backend.
const ATTR_HEADERS
Bitfield flags for supported features.
directoryExists(array $params)
Check if a directory exists at a given storage path.
getLocalReferenceMulti(array $params)
Like getLocalReference() except it takes an array of storage paths and yields an order-preserved map ...
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
getFileXAttributes(array $params)
Get metadata about a file at a storage path in the backend.
publish(array $params)
Remove measures to block web access to a storage directory and the container it belongs to.
wrapStatus(StatusValue $sv)
resolveFSFileObjects(array $ops)
Convert FSFile 'src' paths to string paths (with an 'srcRef' field set to the FSFile)
getName()
Get the unique backend name.
getDirectoryList(array $params)
Get an iterator to list all directories under a storage directory.
getScopedFileLocks(array $paths, $type, StatusValue $status, $timeout=0)
Lock the files at the given storage paths in the backend.
getDomainId()
Get the domain identifier used for this backend (possibly empty).
string $readOnly
Read-only explanation message.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static normalizeStoragePath( $storagePath)
Normalize a storage path by cleaning up directory separators.
quickMove(array $params)
Performs a single quick move operation.
getReadOnlyReason()
Get an explanatory message if this backend is read-only.
getFileHttpUrl(array $params)
Return an HTTP URL to a given file that requires no authentication to use.
static factory(LockManager $manager, array $paths, $type, StatusValue $status, $timeout=0)
Get a ScopedLock object representing a lock on resource paths.
setLogger(LoggerInterface $logger)
move(array $params, array $opts=[])
Performs a single move operation.
quickDelete(array $params)
Performs a single quick delete operation.
store(array $params, array $opts=[])
Performs a single store operation.
getFileContentsMulti(array $params)
Like getFileContents() except it takes an array of storage paths and returns an order preserved map o...
doOperation(array $op, array $opts=[])
Same as doOperations() except it takes a single operation.
Simple version of LockManager that only does lock reference counting.
getFileTimestamp(array $params)
Get the last-modified timestamp of the file at a storage path.
getFileSize(array $params)
Get the size (bytes) of a file at a storage path in the backend.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
int $concurrency
How many operations can be done in parallel.
create(array $params, array $opts=[])
Performs a single create operation.
Class for handling file operation journaling.
getFileSha1Base36(array $params)
Get a SHA-1 hash of the content of the file at a storage path in the backend.
getTopFileList(array $params)
Same as FileBackend::getFileList() except only lists files that are immediately under the given direc...
__construct(array $config)
Create a new backend instance from configuration.
hasFeatures( $bitfield)
Check if the backend medium supports a field of extra features.
quickStore(array $params)
Performs a single quick store operation.
doQuickOperations(array $ops, array $opts=[])
Perform a set of independent file operations on some files.
quickCreate(array $params)
Performs a single quick create operation.
lockFiles(array $paths, $type, $timeout=0)
Lock the files at the given storage paths in the backend.
static newGood( $value=null)
Factory function for good results.
quickCopy(array $params)
Performs a single quick copy operation.
quickDescribe(array $params)
Performs a single quick describe operation.
getLocalCopyMulti(array $params)
Like getLocalCopy() except it takes an array of storage paths and yields an order preserved-map of st...
fileExists(array $params)
Check if a file exists at a storage path in the backend.
preloadCache(array $paths)
Preload persistent file stat cache and property cache into in-process cache.
string $parallelize
When to do operations in parallel.
prepare(array $params)
Prepare a storage directory for usage.
Class representing a non-directory file on the file system.
doOperationsInternal(array $ops, array $opts)
newStatus(... $args)
Yields the result of the status wrapper callback on either:
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
getFileList(array $params)
Get an iterator to list all stored files under a storage directory.
getRootStoragePath()
Get the root storage path of this backend.
getScopedLocksForOps(array $ops, StatusValue $status)
Get an array of scoped locks needed for a batch of file operations.
string $name
Unique backend name.
clean(array $params)
Delete a storage directory if it is empty.
getLocalCopy(array $params)
Get a local copy on disk of the file at a storage path in the backend.
getFileProps(array $params)
Get the properties of the content of the file at a storage path in the backend.
static isPathTraversalFree( $path)
Check if a relative path has no directory traversals.
getJournal()
Get the file journal object for this backend.
concatenate(array $params)
Concatenate a list of storage files into a single file system file.
getTopDirectoryList(array $params)
Same as FileBackend::getDirectoryList() except only lists directories that are immediately under the ...
if(!is_readable( $file)) $ext
getLocalReference(array $params)
Returns a file system file, identical in content to the file at a storage path.
copy(array $params, array $opts=[])
Performs a single copy operation.
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
static factory(array $config, $backend)
Create an appropriate FileJournal object from config.
getContainerStoragePath( $container)
Get the storage path for the given container for this backend.
getFileContents(array $params)
Get the contents of a file at a storage path in the backend.
doQuickOperation(array $op)
Same as doQuickOperations() except it takes a single operation.
getWikiId()
Alias to getDomainId()
scopedProfileSection( $section)
secure(array $params)
Take measures to block web access to a storage directory and the container it belongs to.
static parentStoragePath( $storagePath)
Get the parent storage directory of a storage path.
string $domainId
Unique domain name.
isReadOnly()
Check if this backend is read-only.
static makeContentDisposition( $type, $filename='')
Build a Content-Disposition header value per RFC 6266.