2011-12-20 03:52:06 +00:00
|
|
|
<?php
|
2012-02-08 15:51:16 +00:00
|
|
|
/**
|
|
|
|
|
* @defgroup FileBackend File backend
|
|
|
|
|
*
|
2012-02-09 17:54:14 +00:00
|
|
|
* File backend is used to interact with file storage systems,
|
|
|
|
|
* such as the local file system, NFS, or cloud storage systems.
|
externalstore: Improve overall documentation
* Consolidate various duplicate docs to the main entry point.
Especially the duplication that followed after ExternalStoreFactory
and ExternalStoreAccess were introduced with ExternalStore
left as deprecated wrapper.
* Officially mark ExternalStoreFactory as internal, pointing
to ExternalStoreAccess instead. And document why the latter exists,
since it seems all its methods could trivially move to the factory
class. Follows-up I40c3b553, where it was explained that the factory
is meant to be an internal implementation detail, with the access
class meant to resemble ExternalStoreMedium.
* Use consistent terms for "protocol", "location" and "object"
(reduce use of words like medium, backend, type, blob, revision,
etc. within this code).
* Document that ExternalStore is fundamentally designed as an
append-only store (per T247383).
Bug: T247383
Change-Id: I1094af7d35a14f9e47b8791f3e4cc888e8cbfc7f
2021-12-15 01:01:20 +00:00
|
|
|
* See [the architecture doc](@ref filebackendarch) for more information.
|
2012-02-08 15:51:16 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-05-07 07:11:33 +00:00
|
|
|
* Base class for all file backends.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup FileBackend
|
|
|
|
|
*/
|
2019-08-16 10:00:15 +00:00
|
|
|
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
|
2016-09-18 21:40:38 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-06-28 16:45:15 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-03-03 19:14:50 +00:00
|
|
|
* @brief Base class for all file backend classes (including multi-write backends).
|
2012-02-08 15:51:16 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* This class defines the methods as abstract that subclasses must implement.
|
|
|
|
|
* Outside callers can assume that all backends will have these functions.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-02-19 23:40:02 +00:00
|
|
|
* All "storage paths" are of the format "mwstore://<backend>/<container>/<path>".
|
2016-09-18 21:40:38 +00:00
|
|
|
* The "backend" portion is unique name for the application to refer to a backend, while
|
2013-02-05 07:20:18 +00:00
|
|
|
* the "container" portion is a top-level directory of the backend. The "path" portion
|
|
|
|
|
* is a relative path that uses UNIX file system (FS) notation, though any particular
|
|
|
|
|
* backend may not actually be using a local filesystem. Therefore, the relative paths
|
|
|
|
|
* are only virtual.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2016-09-18 21:40:38 +00:00
|
|
|
* Backend contents are stored under "domain"-specific container names by default.
|
|
|
|
|
* A domain is simply a logical umbrella for entities, such as those belonging to a certain
|
|
|
|
|
* application or portion of a website, for example. A domain can be local or global.
|
|
|
|
|
* Global (qualified) backends are achieved by configuring the "domain ID" to a constant.
|
|
|
|
|
* Global domains are simpler, but local domains can be used by choosing a domain ID based on
|
|
|
|
|
* the current context, such as which language of a website is being used.
|
|
|
|
|
*
|
2013-02-05 07:20:18 +00:00
|
|
|
* For legacy reasons, the FSFileBackend class allows manually setting the paths of
|
2016-09-18 21:40:38 +00:00
|
|
|
* containers to ones that do not respect the "domain ID".
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-24 23:49:05 +00:00
|
|
|
* In key/value (object) stores, containers are the only hierarchy (the rest is emulated).
|
2011-12-20 03:52:06 +00:00
|
|
|
* FS-based backends are somewhat more restrictive due to the existence of real
|
|
|
|
|
* directory files; a regular file cannot have the same name as a directory. Other
|
2011-12-27 02:24:27 +00:00
|
|
|
* backends with virtual directories may not have this limitation. Callers should
|
2012-01-07 01:33:23 +00:00
|
|
|
* store files in such a way that no files and directories are under the same path.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-24 23:49:05 +00:00
|
|
|
* In general, this class allows for callers to access storage through the same
|
|
|
|
|
* interface, without regard to the underlying storage system. However, calling code
|
|
|
|
|
* must follow certain patterns and be aware of certain things to ensure compatibility:
|
|
|
|
|
* - a) Always call prepare() on the parent directory before trying to put a file there;
|
|
|
|
|
* key/value stores only need the container to exist first, but filesystems need
|
|
|
|
|
* all the parent directories to exist first (prepare() is aware of all this)
|
|
|
|
|
* - b) Always call clean() on a directory when it might become empty to avoid empty
|
|
|
|
|
* directory buildup on filesystems; key/value stores never have empty directories,
|
|
|
|
|
* so doing this helps preserve consistency in both cases
|
|
|
|
|
* - c) Likewise, do not rely on the existence of empty directories for anything;
|
|
|
|
|
* calling directoryExists() on a path that prepare() was previously called on
|
|
|
|
|
* will return false for key/value stores if there are no files under that path
|
|
|
|
|
* - d) Never alter the resulting FSFile returned from getLocalReference(), as it could
|
|
|
|
|
* either be a copy of the source file in /tmp or the original source file itself
|
|
|
|
|
* - e) Use a file layout that results in never attempting to store files over directories
|
|
|
|
|
* or directories over files; key/value stores allow this but filesystems do not
|
|
|
|
|
* - f) Use ASCII file names (e.g. base32, IDs, hashes) to avoid Unicode issues in Windows
|
|
|
|
|
* - g) Do not assume that move operations are atomic (difficult with key/value stores)
|
|
|
|
|
* - h) Do not assume that file stat or read operations always have immediate consistency;
|
|
|
|
|
* various methods have a "latest" flag that should always be used if up-to-date
|
|
|
|
|
* information is required (this trades performance for correctness as needed)
|
|
|
|
|
* - i) Do not assume that directory listings have immediate consistency
|
|
|
|
|
*
|
2012-09-16 06:38:34 +00:00
|
|
|
* Methods of subclasses should avoid throwing exceptions at all costs.
|
2011-12-20 03:52:06 +00:00
|
|
|
* As a corollary, external dependencies should be kept to a minimum.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-09-27 00:56:33 +00:00
|
|
|
* See [the architecture doc](@ref filebackendarch) for more information.
|
|
|
|
|
*
|
2020-07-13 09:00:30 +00:00
|
|
|
* @stable to extend
|
2020-07-03 12:07:47 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @ingroup FileBackend
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
2016-09-18 21:40:38 +00:00
|
|
|
abstract class FileBackend implements LoggerAwareInterface {
|
2013-11-24 23:49:05 +00:00
|
|
|
/** @var string Unique backend name */
|
2013-11-23 18:23:32 +00:00
|
|
|
protected $name;
|
|
|
|
|
|
2016-09-18 21:40:38 +00:00
|
|
|
/** @var string Unique domain name */
|
|
|
|
|
protected $domainId;
|
2013-11-23 18:23:32 +00:00
|
|
|
|
|
|
|
|
/** @var string Read-only explanation message */
|
|
|
|
|
protected $readOnly;
|
|
|
|
|
|
|
|
|
|
/** @var string When to do operations in parallel */
|
|
|
|
|
protected $parallelize;
|
|
|
|
|
|
|
|
|
|
/** @var int How many operations can be done in parallel */
|
|
|
|
|
protected $concurrency;
|
2012-04-11 17:51:02 +00:00
|
|
|
|
2019-08-16 10:00:15 +00:00
|
|
|
/** @var TempFSFileFactory */
|
|
|
|
|
protected $tmpFileFactory;
|
2016-09-21 03:39:55 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/** @var LockManager */
|
|
|
|
|
protected $lockManager;
|
2016-09-18 21:40:38 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
protected $logger;
|
2019-03-13 18:14:19 +00:00
|
|
|
/** @var callable|null */
|
2016-09-18 21:40:38 +00:00
|
|
|
protected $profiler;
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2016-09-18 22:49:34 +00:00
|
|
|
/** @var callable */
|
|
|
|
|
protected $obResetFunc;
|
|
|
|
|
/** @var callable */
|
|
|
|
|
protected $streamMimeFunc;
|
2016-09-16 22:55:40 +00:00
|
|
|
/** @var callable */
|
|
|
|
|
protected $statusWrapper;
|
|
|
|
|
|
2014-04-12 06:50:58 +00:00
|
|
|
/** Bitfield flags for supported features */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const ATTR_HEADERS = 1; // files can be tagged with standard HTTP headers
|
|
|
|
|
public const ATTR_METADATA = 2; // files can be stored with metadata key/values
|
|
|
|
|
public const ATTR_UNICODE_PATHS = 4; // files can have Unicode paths (not just ASCII)
|
2013-09-30 07:12:10 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no info; non-existant file" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
protected const STAT_ABSENT = false;
|
2019-08-30 07:01:29 +00:00
|
|
|
|
|
|
|
|
/** @var null Idiom for "no info; I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const STAT_ERROR = null;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var null Idiom for "no file/directory list; I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const LIST_ERROR = null;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var null Idiom for "no temp URL; not supported or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const TEMPURL_ERROR = null;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var null Idiom for "existence unknown; I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const EXISTENCE_ERROR = null;
|
2019-08-30 07:01:29 +00:00
|
|
|
|
|
|
|
|
/** @var false Idiom for "no timestamp; missing file or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const TIMESTAMP_FAIL = false;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no content; missing file or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const CONTENT_FAIL = false;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no metadata; missing file or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const XATTRS_FAIL = false;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no size; missing file or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const SIZE_FAIL = false;
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no SHA1 hash; missing file or I/O errors" (since 1.34) */
|
2020-05-16 15:17:42 +00:00
|
|
|
public const SHA1_FAIL = false;
|
2019-08-29 08:14:24 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-07 01:33:23 +00:00
|
|
|
* Create a new backend instance from configuration.
|
2011-12-20 03:52:06 +00:00
|
|
|
* This should only be called from within FileBackendGroup.
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $config Parameters include:
|
2016-09-18 22:49:34 +00:00
|
|
|
* - name : The unique name of this backend.
|
|
|
|
|
* This should consist of alphanumberic, '-', and '_' characters.
|
2021-08-12 01:33:53 +00:00
|
|
|
* This name should not be changed after use.
|
2016-09-18 22:49:34 +00:00
|
|
|
* Note that the name is *not* used in actual container names.
|
|
|
|
|
* - domainId : Prefix to container names that is unique to this backend.
|
|
|
|
|
* It should only consist of alphanumberic, '-', and '_' characters.
|
|
|
|
|
* This ID is what avoids collisions if multiple logical backends
|
|
|
|
|
* use the same storage system, so this should be set carefully.
|
2013-12-03 20:55:44 +00:00
|
|
|
* - lockManager : LockManager object to use for any file locking.
|
2016-09-18 22:49:34 +00:00
|
|
|
* If not provided, then no file locking will be enforced.
|
|
|
|
|
* - readOnly : Write operations are disallowed if this is a non-empty string.
|
|
|
|
|
* It should be an explanation for the backend being read-only.
|
2012-07-17 03:56:49 +00:00
|
|
|
* - parallelize : When to do file operations in parallel (when possible).
|
2016-09-18 22:49:34 +00:00
|
|
|
* Allowed values are "implicit", "explicit" and "off".
|
2012-07-17 03:56:49 +00:00
|
|
|
* - concurrency : How many file operations can be done in parallel.
|
2019-08-16 10:00:15 +00:00
|
|
|
* - tmpDirectory : Directory to use for temporary files.
|
2019-08-30 07:01:29 +00:00
|
|
|
* - tmpFileFactory : Optional TempFSFileFactory object. Only has an effect if
|
|
|
|
|
* tmpDirectory is not set. If both are unset or null, then the backend will
|
|
|
|
|
* try to discover a usable temporary directory.
|
2016-09-18 22:49:34 +00:00
|
|
|
* - obResetFunc : alternative callback to clear the output buffer
|
|
|
|
|
* - streamMimeFunc : alternative method to determine the content type from the path
|
|
|
|
|
* - logger : Optional PSR logger object.
|
2019-03-13 18:14:19 +00:00
|
|
|
* - profiler : Optional callback that takes a section name argument and returns
|
|
|
|
|
* a ScopedCallback instance that ends the profile section in its destructor.
|
2019-10-28 17:41:39 +00:00
|
|
|
* - statusWrapper : Optional callback that is used to wrap returned StatusValues
|
2016-09-21 04:44:29 +00:00
|
|
|
* @throws InvalidArgumentException
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $config ) {
|
2020-05-05 07:51:29 +00:00
|
|
|
if ( !array_key_exists( 'name', $config ) ) {
|
|
|
|
|
throw new InvalidArgumentException( 'Backend name not specified.' );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->name = $config['name'];
|
2019-03-22 00:21:22 +00:00
|
|
|
$this->domainId = $config['domainId'] // e.g. "my_wiki-en_"
|
2020-05-05 07:51:29 +00:00
|
|
|
?? $config['wikiId'] // b/c alias
|
|
|
|
|
?? null;
|
2012-03-01 21:55:22 +00:00
|
|
|
if ( !preg_match( '!^[a-zA-Z0-9-_]{1,255}$!', $this->name ) ) {
|
2016-09-21 04:44:29 +00:00
|
|
|
throw new InvalidArgumentException( "Backend name '{$this->name}' is invalid." );
|
2020-05-05 07:51:29 +00:00
|
|
|
}
|
|
|
|
|
if ( !is_string( $this->domainId ) ) {
|
2016-09-18 21:40:38 +00:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"Backend domain ID not provided for '{$this->name}'." );
|
2013-12-03 20:55:44 +00:00
|
|
|
}
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->lockManager = $config['lockManager'] ?? new NullLockManager( [] );
|
2012-01-04 02:15:07 +00:00
|
|
|
$this->readOnly = isset( $config['readOnly'] )
|
|
|
|
|
? (string)$config['readOnly']
|
|
|
|
|
: '';
|
2012-04-11 17:51:02 +00:00
|
|
|
$this->parallelize = isset( $config['parallelize'] )
|
|
|
|
|
? (string)$config['parallelize']
|
|
|
|
|
: 'off';
|
|
|
|
|
$this->concurrency = isset( $config['concurrency'] )
|
|
|
|
|
? (int)$config['concurrency']
|
|
|
|
|
: 50;
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->obResetFunc = $config['obResetFunc'] ?? [ $this, 'resetOutputBuffer' ];
|
|
|
|
|
$this->streamMimeFunc = $config['streamMimeFunc'] ?? null;
|
|
|
|
|
|
|
|
|
|
$this->profiler = $config['profiler'] ?? null;
|
2019-03-13 18:14:19 +00:00
|
|
|
if ( !is_callable( $this->profiler ) ) {
|
|
|
|
|
$this->profiler = null;
|
|
|
|
|
}
|
2019-06-28 16:45:15 +00:00
|
|
|
$this->logger = $config['logger'] ?? new NullLogger();
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->statusWrapper = $config['statusWrapper'] ?? null;
|
2019-08-16 10:00:15 +00:00
|
|
|
// tmpDirectory gets precedence for backward compatibility
|
|
|
|
|
if ( isset( $config['tmpDirectory'] ) ) {
|
|
|
|
|
$this->tmpFileFactory = new TempFSFileFactory( $config['tmpDirectory'] );
|
|
|
|
|
} else {
|
|
|
|
|
$this->tmpFileFactory = $config['tmpFileFactory'] ?? new TempFSFileFactory();
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 21:40:38 +00:00
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2019-08-29 08:14:24 +00:00
|
|
|
* Get the unique backend name
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* We may have multiple different backends of the same type.
|
|
|
|
|
* For example, we can have two Swift backends using different proxies.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
final public function getName() {
|
|
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 22:38:37 +00:00
|
|
|
/**
|
2016-09-18 21:40:38 +00:00
|
|
|
* Get the domain identifier used for this backend (possibly empty).
|
2012-09-24 22:38:37 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2016-09-18 21:40:38 +00:00
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
final public function getDomainId() {
|
|
|
|
|
return $this->domainId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Alias to getDomainId()
|
2019-08-29 08:14:24 +00:00
|
|
|
*
|
2016-09-18 21:40:38 +00:00
|
|
|
* @return string
|
2012-09-24 22:38:37 +00:00
|
|
|
* @since 1.20
|
2019-07-05 01:20:35 +00:00
|
|
|
* @deprecated Since 1.34 Use getDomainId()
|
2012-09-24 22:38:37 +00:00
|
|
|
*/
|
|
|
|
|
final public function getWikiId() {
|
2016-09-18 21:40:38 +00:00
|
|
|
return $this->getDomainId();
|
2012-09-24 22:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-06 05:25:26 +00:00
|
|
|
/**
|
|
|
|
|
* Check if this backend is read-only
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-02-06 05:25:26 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final public function isReadOnly() {
|
|
|
|
|
return ( $this->readOnly != '' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an explanatory message if this backend is read-only
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-03-14 21:04:28 +00:00
|
|
|
* @return string|bool Returns false if the backend is not read-only
|
2012-02-06 05:25:26 +00:00
|
|
|
*/
|
|
|
|
|
final public function getReadOnlyReason() {
|
|
|
|
|
return ( $this->readOnly != '' ) ? $this->readOnly : false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
/**
|
|
|
|
|
* Get the a bitfield of extra features supported by the backend medium
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-09-30 07:12:10 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @return int Bitfield of FileBackend::ATTR_* flags
|
2013-09-30 07:12:10 +00:00
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
public function getFeatures() {
|
2014-04-12 06:50:58 +00:00
|
|
|
return self::ATTR_UNICODE_PATHS;
|
2013-09-30 07:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the backend medium supports a field of extra features
|
|
|
|
|
*
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param int $bitfield Bitfield of FileBackend::ATTR_* flags
|
2013-09-30 07:12:10 +00:00
|
|
|
* @return bool
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
final public function hasFeatures( $bitfield ) {
|
|
|
|
|
return ( $this->getFeatures() & $bitfield ) === $bitfield;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* This is the main entry point into the backend for write operations.
|
|
|
|
|
* Callers supply an ordered list of operations to perform as a transaction.
|
2012-01-26 21:05:03 +00:00
|
|
|
* Files will be locked, the stat cache cleared, and then the operations attempted.
|
2011-12-20 03:52:06 +00:00
|
|
|
* If any serious errors occur, all attempted operations will be rolled back.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* $ops is an array of arrays. The outer array holds a list of operations.
|
|
|
|
|
* Each inner array is a set of key value pairs that specify an operation.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-07-10 14:46:22 +00:00
|
|
|
* Supported operations and their parameters. The supported actions are:
|
|
|
|
|
* - create
|
|
|
|
|
* - store
|
|
|
|
|
* - copy
|
|
|
|
|
* - move
|
|
|
|
|
* - delete
|
2012-10-24 08:18:29 +00:00
|
|
|
* - describe (since 1.21)
|
2012-07-10 14:46:22 +00:00
|
|
|
* - null
|
|
|
|
|
*
|
2016-02-27 01:33:42 +00:00
|
|
|
* FSFile/TempFSFile object support was added in 1.27.
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* a) Create a new file in storage with the contents of a string
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'create',
|
|
|
|
|
* 'dst' => <storage path>,
|
|
|
|
|
* 'content' => <string of new file contents>,
|
2012-01-19 02:24:49 +00:00
|
|
|
* 'overwrite' => <boolean>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'overwriteSame' => <boolean>,
|
2012-11-20 01:38:17 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* b) Copy a file system file into storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'store',
|
2016-02-27 01:33:42 +00:00
|
|
|
* 'src' => <file system path, FSFile, or TempFSFile>,
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'dst' => <storage path>,
|
2012-01-19 02:24:49 +00:00
|
|
|
* 'overwrite' => <boolean>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'overwriteSame' => <boolean>,
|
2012-11-20 01:38:17 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* c) Copy a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'copy',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'dst' => <storage path>,
|
2012-01-19 02:24:49 +00:00
|
|
|
* 'overwrite' => <boolean>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'overwriteSame' => <boolean>,
|
2012-10-29 19:57:04 +00:00
|
|
|
* 'ignoreMissingSource' => <boolean>, # since 1.21
|
2013-03-09 23:09:25 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* d) Move a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'move',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'dst' => <storage path>,
|
2012-01-19 02:24:49 +00:00
|
|
|
* 'overwrite' => <boolean>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'overwriteSame' => <boolean>,
|
2012-10-29 19:57:04 +00:00
|
|
|
* 'ignoreMissingSource' => <boolean>, # since 1.21
|
2013-03-09 23:09:25 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* e) Delete a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'delete',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'ignoreMissingSource' => <boolean>
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2012-10-24 08:18:29 +00:00
|
|
|
* f) Update metadata for a file within storage
|
|
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-10-24 08:18:29 +00:00
|
|
|
* 'op' => 'describe',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'headers' => <HTTP header name/value map>
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-10-24 08:18:29 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* g) Do nothing (no-op)
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2011-12-20 03:52:06 +00:00
|
|
|
* 'op' => 'null',
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* Boolean flags for operations (operation-specific):
|
2012-07-17 03:56:49 +00:00
|
|
|
* - ignoreMissingSource : The operation will simply succeed and do
|
|
|
|
|
* nothing if the source file does not exist.
|
|
|
|
|
* - overwrite : Any destination file will be overwritten.
|
2013-03-20 23:25:05 +00:00
|
|
|
* - overwriteSame : If a file already exists at the destination with the
|
|
|
|
|
* same contents, then do nothing to the destination file
|
|
|
|
|
* instead of giving an error. This does not compare headers.
|
|
|
|
|
* This option is ignored if 'overwrite' is already provided.
|
2013-03-26 01:54:56 +00:00
|
|
|
* - headers : If supplied, the result of merging these headers with any
|
|
|
|
|
* existing source file headers (replacing conflicting ones)
|
|
|
|
|
* will be set as the destination file headers. Headers are
|
|
|
|
|
* deleted if their value is set to the empty string. When a
|
|
|
|
|
* file has headers they are included in responses to GET and
|
|
|
|
|
* HEAD requests to the backing store for that file.
|
|
|
|
|
* Header values should be no larger than 255 bytes, except for
|
|
|
|
|
* Content-Disposition. The system might ignore or truncate any
|
|
|
|
|
* headers that are too long to store (exact limits will vary).
|
2012-11-20 01:38:17 +00:00
|
|
|
* Backends that don't support metadata ignore this. (since 1.21)
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-23 09:43:28 +00:00
|
|
|
* $opts is an associative of boolean flags, including:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - force : Operation precondition errors no longer trigger an abort.
|
|
|
|
|
* Any remaining operations are still attempted. Unexpected
|
2013-02-20 21:30:58 +00:00
|
|
|
* failures may still cause remaining operations to be aborted.
|
2012-07-17 03:56:49 +00:00
|
|
|
* - nonLocking : No locks are acquired for the operations.
|
|
|
|
|
* This can increase performance for non-critical writes.
|
|
|
|
|
* This has no effect unless the 'force' flag is set.
|
2012-07-12 19:48:16 +00:00
|
|
|
* - parallelize : Try to do operations in parallel when possible.
|
2012-11-20 01:38:17 +00:00
|
|
|
* - bypassReadOnly : Allow writes in read-only mode. (since 1.20)
|
2012-09-14 04:31:59 +00:00
|
|
|
* - preserveCache : Don't clear the process cache before checking files.
|
|
|
|
|
* This should only be used if all entries in the process
|
2012-11-20 01:38:17 +00:00
|
|
|
* cache were added after the files were already locked. (since 1.20)
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2018-08-07 10:12:13 +00:00
|
|
|
* @note Remarks on locking:
|
2012-01-15 22:45:14 +00:00
|
|
|
* File system paths given to operations should refer to files that are
|
2012-01-26 21:05:03 +00:00
|
|
|
* already locked or otherwise safe from modification from other processes.
|
2012-01-15 22:45:14 +00:00
|
|
|
* Normally these files will be new temp files, which should be adequate.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-07-10 14:46:22 +00:00
|
|
|
* @par Return value:
|
|
|
|
|
*
|
2012-08-10 08:59:55 +00:00
|
|
|
* This returns a Status, which contains all warnings and fatals that occurred
|
2011-12-20 03:52:06 +00:00
|
|
|
* during the operation. The 'failCount', 'successCount', and 'success' members
|
2012-07-10 14:46:22 +00:00
|
|
|
* will reflect each operation attempted.
|
|
|
|
|
*
|
2016-09-16 22:55:40 +00:00
|
|
|
* The StatusValue will be "OK" unless:
|
2012-07-18 19:08:30 +00:00
|
|
|
* - a) unexpected operation errors occurred (network partitions, disk full...)
|
2019-08-23 15:45:54 +00:00
|
|
|
* - b) predicted operation errors occurred and 'force' was not set
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-10-11 14:45:54 +00:00
|
|
|
* @param array[] $ops List of operations to execute in order
|
|
|
|
|
* @phan-param array<int,array{ignoreMissingSource?:bool,overwrite?:bool,overwriteSame?:bool,headers?:bool}> $ops
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $opts Batch operation options
|
2021-04-04 19:18:22 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
2021-08-12 01:33:53 +00:00
|
|
|
* @phan-param array{force?:bool,nonLocking?:bool,parallelize?:bool,bypassReadOnly?:bool,preserveCache?:bool} $opts
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function doOperations( array $ops, array $opts = [] ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-01-04 02:15:07 +00:00
|
|
|
}
|
2019-01-09 16:24:36 +00:00
|
|
|
if ( $ops === [] ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus(); // nothing to do
|
2013-05-10 20:55:39 +00:00
|
|
|
}
|
2016-02-27 01:33:42 +00:00
|
|
|
|
|
|
|
|
$ops = $this->resolveFSFileObjects( $ops );
|
2021-11-19 23:19:42 +00:00
|
|
|
if ( empty( $opts['force'] ) ) {
|
2011-12-23 09:43:28 +00:00
|
|
|
unset( $opts['nonLocking'] );
|
|
|
|
|
}
|
2016-02-27 01:33:42 +00:00
|
|
|
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2016-02-27 01:33:42 +00:00
|
|
|
|
2011-12-23 09:43:28 +00:00
|
|
|
return $this->doOperationsInternal( $ops, $opts );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperations()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $ops
|
|
|
|
|
* @param array $opts
|
2019-01-15 11:36:04 +00:00
|
|
|
* @return StatusValue
|
2011-12-23 09:43:28 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doOperationsInternal( array $ops, array $opts );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-12-21 09:16:28 +00:00
|
|
|
* Same as doOperations() except it takes a single operation.
|
|
|
|
|
* If you are doing a batch of operations that should either
|
|
|
|
|
* all succeed or all fail, then use that function instead.
|
2011-12-20 03:52:06 +00:00
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperations()
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $op Operation
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function doOperation( array $op, array $opts = [] ) {
|
|
|
|
|
return $this->doOperations( [ $op ], $opts );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-08 09:25:15 +00:00
|
|
|
/**
|
|
|
|
|
* Performs a single create operation.
|
|
|
|
|
* This sets $params['op'] to 'create' and passes it to doOperation().
|
|
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperation()
|
2012-01-08 09:25:15 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-01-08 09:25:15 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function create( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'create' ] + $params, $opts );
|
2012-01-08 09:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-21 09:16:28 +00:00
|
|
|
/**
|
|
|
|
|
* Performs a single store operation.
|
|
|
|
|
* This sets $params['op'] to 'store' and passes it to doOperation().
|
|
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperation()
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-21 09:16:28 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function store( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'store' ] + $params, $opts );
|
2011-12-21 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single copy operation.
|
|
|
|
|
* This sets $params['op'] to 'copy' and passes it to doOperation().
|
|
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperation()
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-21 09:16:28 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function copy( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'copy' ] + $params, $opts );
|
2011-12-21 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single move operation.
|
|
|
|
|
* This sets $params['op'] to 'move' and passes it to doOperation().
|
|
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperation()
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-21 09:16:28 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function move( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'move' ] + $params, $opts );
|
2011-12-21 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single delete operation.
|
|
|
|
|
* This sets $params['op'] to 'delete' and passes it to doOperation().
|
|
|
|
|
*
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::doOperation()
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-21 09:16:28 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function delete( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'delete' ] + $params, $opts );
|
2011-12-21 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-21 04:58:47 +00:00
|
|
|
/**
|
|
|
|
|
* Performs a single describe operation.
|
|
|
|
|
* This sets $params['op'] to 'describe' and passes it to doOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
|
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-11-21 04:58:47 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function describe( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doOperation( [ 'op' => 'describe' ] + $params, $opts );
|
2012-11-21 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-16 00:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Perform a set of independent file operations on some files.
|
|
|
|
|
*
|
2021-08-12 01:33:53 +00:00
|
|
|
* This does no locking, and possibly no stat calls.
|
2012-05-16 00:42:20 +00:00
|
|
|
* Any destination files that already exist will be overwritten.
|
|
|
|
|
* This should *only* be used on non-original files, like cache files.
|
|
|
|
|
*
|
|
|
|
|
* Supported operations and their parameters:
|
2012-07-10 14:46:22 +00:00
|
|
|
* - create
|
|
|
|
|
* - store
|
|
|
|
|
* - copy
|
|
|
|
|
* - move
|
|
|
|
|
* - delete
|
2012-10-24 08:18:29 +00:00
|
|
|
* - describe (since 1.21)
|
2012-07-10 14:46:22 +00:00
|
|
|
* - null
|
2012-07-18 04:24:19 +00:00
|
|
|
*
|
2016-02-27 01:33:42 +00:00
|
|
|
* FSFile/TempFSFile object support was added in 1.27.
|
|
|
|
|
*
|
2012-05-16 00:42:20 +00:00
|
|
|
* a) Create a new file in storage with the contents of a string
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'create',
|
|
|
|
|
* 'dst' => <storage path>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'content' => <string of new file contents>,
|
2012-11-20 01:38:17 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-10-24 08:18:29 +00:00
|
|
|
*
|
2012-05-16 00:42:20 +00:00
|
|
|
* b) Copy a file system file into storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'store',
|
2016-02-27 01:33:42 +00:00
|
|
|
* 'src' => <file system path, FSFile, or TempFSFile>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'dst' => <storage path>,
|
2012-11-20 01:38:17 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-10-24 08:18:29 +00:00
|
|
|
*
|
2012-05-16 00:42:20 +00:00
|
|
|
* c) Copy a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'copy',
|
|
|
|
|
* 'src' => <storage path>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'dst' => <storage path>,
|
2012-10-29 19:57:04 +00:00
|
|
|
* 'ignoreMissingSource' => <boolean>, # since 1.21
|
2013-03-09 23:09:25 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-10-24 08:18:29 +00:00
|
|
|
*
|
2012-05-16 00:42:20 +00:00
|
|
|
* d) Move a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'move',
|
|
|
|
|
* 'src' => <storage path>,
|
2012-08-31 22:35:39 +00:00
|
|
|
* 'dst' => <storage path>,
|
2012-10-29 19:57:04 +00:00
|
|
|
* 'ignoreMissingSource' => <boolean>, # since 1.21
|
2013-03-09 23:09:25 +00:00
|
|
|
* 'headers' => <HTTP header name/value map> # since 1.21
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-10-24 08:18:29 +00:00
|
|
|
*
|
2012-05-16 00:42:20 +00:00
|
|
|
* e) Delete a file within storage
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'delete',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'ignoreMissingSource' => <boolean>
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-10-24 08:18:29 +00:00
|
|
|
*
|
|
|
|
|
* f) Update metadata for a file within storage
|
|
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-10-24 08:18:29 +00:00
|
|
|
* 'op' => 'describe',
|
|
|
|
|
* 'src' => <storage path>,
|
|
|
|
|
* 'headers' => <HTTP header name/value map>
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-10-24 08:18:29 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* g) Do nothing (no-op)
|
2012-07-10 14:46:22 +00:00
|
|
|
* @code
|
2016-09-04 22:14:26 +00:00
|
|
|
* [
|
2012-05-16 00:42:20 +00:00
|
|
|
* 'op' => 'null',
|
2016-09-04 22:14:26 +00:00
|
|
|
* ]
|
2012-07-10 14:46:22 +00:00
|
|
|
* @endcode
|
2012-05-16 00:42:20 +00:00
|
|
|
*
|
2012-07-10 14:46:22 +00:00
|
|
|
* @par Boolean flags for operations (operation-specific):
|
2012-07-17 03:56:49 +00:00
|
|
|
* - ignoreMissingSource : The operation will simply succeed and do
|
|
|
|
|
* nothing if the source file does not exist.
|
2012-12-20 19:50:35 +00:00
|
|
|
* - headers : If supplied with a header name/value map, the backend will
|
|
|
|
|
* reply with these headers when GETs/HEADs of the destination
|
|
|
|
|
* file are made. Header values should be smaller than 256 bytes.
|
2013-03-09 23:09:25 +00:00
|
|
|
* Content-Disposition headers can be longer, though the system
|
|
|
|
|
* might ignore or truncate ones that are too long to store.
|
2012-11-21 04:58:47 +00:00
|
|
|
* Existing headers will remain, but these will replace any
|
|
|
|
|
* conflicting previous headers, and headers will be removed
|
|
|
|
|
* if they are set to an empty string.
|
2012-11-20 01:38:17 +00:00
|
|
|
* Backends that don't support metadata ignore this. (since 1.21)
|
2012-05-16 00:42:20 +00:00
|
|
|
*
|
2012-07-12 19:48:16 +00:00
|
|
|
* $opts is an associative of boolean flags, including:
|
2012-07-18 04:24:19 +00:00
|
|
|
* - bypassReadOnly : Allow writes in read-only mode (since 1.20)
|
2012-07-12 19:48:16 +00:00
|
|
|
*
|
2012-07-10 14:46:22 +00:00
|
|
|
* @par Return value:
|
2012-08-10 08:59:55 +00:00
|
|
|
* This returns a Status, which contains all warnings and fatals that occurred
|
2012-05-16 00:42:20 +00:00
|
|
|
* during the operation. The 'failCount', 'successCount', and 'success' members
|
2016-09-16 22:55:40 +00:00
|
|
|
* will reflect each operation attempted for the given files. The StatusValue will be
|
2012-08-10 08:59:55 +00:00
|
|
|
* considered "OK" as long as no fatal errors occurred.
|
2012-05-16 00:42:20 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $ops Set of operations to execute
|
2019-12-22 07:25:07 +00:00
|
|
|
* @phan-param list<array{op:?string,src?:string,dst?:string,ignoreMissingSource?:bool,headers?:array}> $ops
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $opts Batch operation options
|
2019-08-30 18:17:32 +00:00
|
|
|
* @phan-param array{bypassReadOnly?:bool} $opts
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
2012-05-16 00:42:20 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
final public function doQuickOperations( array $ops, array $opts = [] ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-05-16 00:42:20 +00:00
|
|
|
}
|
2019-01-09 16:24:36 +00:00
|
|
|
if ( $ops === [] ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus(); // nothing to do
|
2013-05-10 20:55:39 +00:00
|
|
|
}
|
2016-02-27 01:33:42 +00:00
|
|
|
|
|
|
|
|
$ops = $this->resolveFSFileObjects( $ops );
|
2012-05-16 00:42:20 +00:00
|
|
|
foreach ( $ops as &$op ) {
|
|
|
|
|
$op['overwrite'] = true; // avoids RTTs in key/value stores
|
|
|
|
|
}
|
2016-02-27 01:33:42 +00:00
|
|
|
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2016-02-27 01:33:42 +00:00
|
|
|
|
2019-10-28 17:41:39 +00:00
|
|
|
return $this->doQuickOperationsInternal( $ops, $opts );
|
2012-05-16 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackend::doQuickOperations()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $ops
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts
|
2019-01-15 11:36:04 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
2012-05-16 00:42:20 +00:00
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
abstract protected function doQuickOperationsInternal( array $ops, array $opts );
|
2012-05-16 00:42:20 +00:00
|
|
|
|
2012-06-05 22:58:54 +00:00
|
|
|
/**
|
|
|
|
|
* Same as doQuickOperations() except it takes a single operation.
|
|
|
|
|
* If you are doing a batch of operations, then use that function instead.
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperations()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $op Operation
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Batch operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function doQuickOperation( array $op, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperations( [ $op ], $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single quick create operation.
|
|
|
|
|
* This sets $params['op'] to 'create' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickCreate( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'create' ] + $params, $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single quick store operation.
|
|
|
|
|
* This sets $params['op'] to 'store' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickStore( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'store' ] + $params, $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single quick copy operation.
|
|
|
|
|
* This sets $params['op'] to 'copy' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickCopy( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'copy' ] + $params, $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single quick move operation.
|
|
|
|
|
* This sets $params['op'] to 'move' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickMove( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'move' ] + $params, $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a single quick delete operation.
|
|
|
|
|
* This sets $params['op'] to 'delete' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-06-05 22:58:54 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickDelete( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'delete' ] + $params, $opts );
|
2012-06-05 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-21 04:58:47 +00:00
|
|
|
/**
|
|
|
|
|
* Performs a single quick describe operation.
|
|
|
|
|
* This sets $params['op'] to 'describe' and passes it to doQuickOperation().
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doQuickOperation()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $params Operation parameters
|
2019-10-28 17:41:39 +00:00
|
|
|
* @param array $opts Operation options
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-11-21 04:58:47 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
2019-10-28 17:41:39 +00:00
|
|
|
final public function quickDescribe( array $params, array $opts = [] ) {
|
|
|
|
|
return $this->doQuickOperation( [ 'op' => 'describe' ] + $params, $opts );
|
2012-11-21 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-21 09:16:28 +00:00
|
|
|
/**
|
2012-01-29 21:28:31 +00:00
|
|
|
* Concatenate a list of storage files into a single file system file.
|
|
|
|
|
* The target path should refer to a file that is already locked or
|
|
|
|
|
* otherwise safe from modification from other processes. Normally,
|
|
|
|
|
* the file will be a new temp file, which should be adequate.
|
2011-12-21 09:16:28 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Operation parameters, include:
|
2012-09-22 18:46:48 +00:00
|
|
|
* - srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
|
|
|
|
|
* - dst : file system path to 0-byte temp file
|
|
|
|
|
* - parallelize : try to do operations in parallel when possible
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-21 09:16:28 +00:00
|
|
|
*/
|
2012-01-08 09:25:15 +00:00
|
|
|
abstract public function concatenate( array $params );
|
2011-12-21 09:16:28 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-17 05:34:01 +00:00
|
|
|
* Prepare a storage directory for usage.
|
|
|
|
|
* This will create any required containers and parent directories.
|
|
|
|
|
* Backends using key/value stores only need to create the container.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-05-21 22:19:06 +00:00
|
|
|
* The 'noAccess' and 'noListing' parameters works the same as in secure(),
|
|
|
|
|
* except they are only applied *if* the directory/container had to be created.
|
|
|
|
|
* These flags should always be set for directories that have private files.
|
2013-04-01 23:39:36 +00:00
|
|
|
* However, setting them is not guaranteed to actually do anything.
|
|
|
|
|
* Additional server configuration may be needed to achieve the desired effect.
|
2012-05-21 22:19:06 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-12 19:48:16 +00:00
|
|
|
* - dir : storage directory
|
2012-07-18 04:24:19 +00:00
|
|
|
* - noAccess : try to deny file access (since 1.20)
|
|
|
|
|
* - noListing : try to deny file listing (since 1.20)
|
|
|
|
|
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
|
Use DeletePage in FileDeleteForm and fix output of ApiDelete
- Use DeletePage in FileDeleteForm instead of
WikiPage::doDeleteArticleReal
- Properly handle scheduled deletions in FileDeleteForm: previously, a
null status value could indicate a missing page OR a scheduled
deletion, but the code always assumed the first, and it would generate
a duplicated log entry. The API response would also not contain the
"delete-scheduled" message. This has been broken since the introduction
of scheduled deletion.
- In ApiDelete, for file deletions, check whether the status is OK not
good. The two might be equivalent, but this way it's more consistent.
- Add some documentation for the Status objects returned by file-related
methods. This is still incomplete, as there are many methods using
Status and none of them says what the status could be. In particular,
this means that for now we keep checking whether the status is OK
instead of good, even though it's unclear what could produce a
non-fatal error.
- In LocalFileDeleteBatch, avoid using a class property for the returned
status, as that's hard to follow. Instead, use a local variable and
pass it around when needed.
Bug: T288758
Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
2021-10-12 12:42:16 +00:00
|
|
|
* @return StatusValue Good status without value for success, fatal otherwise.
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-09 00:20:28 +00:00
|
|
|
final public function prepare( array $params ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-01-09 00:20:28 +00:00
|
|
|
}
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2012-01-09 00:20:28 +00:00
|
|
|
return $this->doPrepare( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::prepare()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $params
|
Use DeletePage in FileDeleteForm and fix output of ApiDelete
- Use DeletePage in FileDeleteForm instead of
WikiPage::doDeleteArticleReal
- Properly handle scheduled deletions in FileDeleteForm: previously, a
null status value could indicate a missing page OR a scheduled
deletion, but the code always assumed the first, and it would generate
a duplicated log entry. The API response would also not contain the
"delete-scheduled" message. This has been broken since the introduction
of scheduled deletion.
- In ApiDelete, for file deletions, check whether the status is OK not
good. The two might be equivalent, but this way it's more consistent.
- Add some documentation for the Status objects returned by file-related
methods. This is still incomplete, as there are many methods using
Status and none of them says what the status could be. In particular,
this means that for now we keep checking whether the status is OK
instead of good, even though it's unclear what could produce a
non-fatal error.
- In LocalFileDeleteBatch, avoid using a class property for the returned
status, as that's hard to follow. Instead, use a local variable and
pass it around when needed.
Bug: T288758
Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
2021-10-12 12:42:16 +00:00
|
|
|
* @return StatusValue Good status without value for success, fatal otherwise.
|
2012-01-09 00:20:28 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doPrepare( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-01-13 04:32:28 +00:00
|
|
|
* Take measures to block web access to a storage directory and
|
2011-12-20 03:52:06 +00:00
|
|
|
* the container it belongs to. FS backends might add .htaccess
|
2012-05-21 22:19:06 +00:00
|
|
|
* files whereas key/value store backends might revoke container
|
|
|
|
|
* access to the storage user representing end-users in web requests.
|
2013-04-01 23:39:36 +00:00
|
|
|
*
|
2019-01-27 09:15:42 +00:00
|
|
|
* This is not guaranteed to actually make files or listings publicly hidden.
|
2013-04-01 23:39:36 +00:00
|
|
|
* Additional server configuration may be needed to achieve the desired effect.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-12 19:48:16 +00:00
|
|
|
* - dir : storage directory
|
|
|
|
|
* - noAccess : try to deny file access
|
|
|
|
|
* - noListing : try to deny file listing
|
2012-07-18 04:24:19 +00:00
|
|
|
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-09 00:20:28 +00:00
|
|
|
final public function secure( array $params ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-01-09 00:20:28 +00:00
|
|
|
}
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2012-05-21 22:19:06 +00:00
|
|
|
return $this->doSecure( $params );
|
2012-01-09 00:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::secure()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $params
|
2019-01-15 11:36:04 +00:00
|
|
|
* @return StatusValue
|
2012-01-09 00:20:28 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doSecure( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2012-05-21 22:19:06 +00:00
|
|
|
/**
|
|
|
|
|
* Remove measures to block web access to a storage directory and
|
|
|
|
|
* the container it belongs to. FS backends might remove .htaccess
|
|
|
|
|
* files whereas key/value store backends might grant container
|
|
|
|
|
* access to the storage user representing end-users in web requests.
|
|
|
|
|
* This essentially can undo the result of secure() calls.
|
|
|
|
|
*
|
2019-01-27 09:15:42 +00:00
|
|
|
* This is not guaranteed to actually make files or listings publicly viewable.
|
2013-04-01 23:39:36 +00:00
|
|
|
* Additional server configuration may be needed to achieve the desired effect.
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-12 19:48:16 +00:00
|
|
|
* - dir : storage directory
|
|
|
|
|
* - access : try to allow file access
|
|
|
|
|
* - listing : try to allow file listing
|
2012-07-18 04:24:19 +00:00
|
|
|
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-05-21 22:19:06 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
final public function publish( array $params ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-05-21 22:19:06 +00:00
|
|
|
}
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2012-05-21 22:19:06 +00:00
|
|
|
return $this->doPublish( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackend::publish()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $params
|
2019-01-15 11:36:04 +00:00
|
|
|
* @return StatusValue
|
2012-05-21 22:19:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doPublish( array $params );
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-17 05:34:01 +00:00
|
|
|
* Delete a storage directory if it is empty.
|
|
|
|
|
* Backends using key/value stores may do nothing unless the directory
|
2012-09-16 06:38:34 +00:00
|
|
|
* is that of an empty container, in which case it will be deleted.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-12 19:48:16 +00:00
|
|
|
* - dir : storage directory
|
2012-07-18 04:24:19 +00:00
|
|
|
* - recursive : recursively delete empty subdirectories first (since 1.20)
|
|
|
|
|
* - bypassReadOnly : allow writes in read-only mode (since 1.20)
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-09 00:20:28 +00:00
|
|
|
final public function clean( array $params ) {
|
2012-07-12 19:48:16 +00:00
|
|
|
if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
|
2012-01-09 00:20:28 +00:00
|
|
|
}
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-10-29 21:18:29 +00:00
|
|
|
$scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
|
2012-01-09 00:20:28 +00:00
|
|
|
return $this->doClean( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackend::clean()
|
2015-09-11 19:38:37 +00:00
|
|
|
* @param array $params
|
2019-01-15 11:36:04 +00:00
|
|
|
* @return StatusValue
|
2012-01-09 00:20:28 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doClean( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a file exists at a storage path in the backend.
|
2012-01-04 08:07:52 +00:00
|
|
|
* This returns false if only a directory exists at the path.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* Callers that only care if a file is readily accessible can use non-strict
|
|
|
|
|
* comparisons on the result. If "does not exist" and "existence is unknown"
|
|
|
|
|
* must be distinguished, then strict comparisons to true/null should be used.
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::EXISTENCE_ERROR
|
|
|
|
|
* @see FileBackend::directoryExists()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return bool|null Whether the file exists or null (I/O error)
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function fileExists( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-04 02:15:07 +00:00
|
|
|
* Get the last-modified timestamp of the file at a storage path.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::TIMESTAMP_FAIL
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|false TS_MW timestamp or false (missing file or I/O error)
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-04 02:15:07 +00:00
|
|
|
abstract public function getFileTimestamp( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-01-04 02:15:07 +00:00
|
|
|
* Get the contents of a file at a storage path in the backend.
|
|
|
|
|
* This should be avoided for potentially large files.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::CONTENT_FAIL
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|false Content string or false (missing file or I/O error)
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-09-22 18:01:58 +00:00
|
|
|
final public function getFileContents( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
$contents = $this->getFileContentsMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
|
2012-09-22 18:01:58 +00:00
|
|
|
|
|
|
|
|
return $contents[$params['src']];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like getFileContents() except it takes an array of storage paths
|
2019-08-30 07:01:29 +00:00
|
|
|
* and returns an order preserved map of storage paths to their content.
|
2012-09-22 18:01:58 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackend::getFileContents()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-09-22 18:01:58 +00:00
|
|
|
* - srcs : list of source storage paths
|
|
|
|
|
* - latest : use the latest available data
|
|
|
|
|
* - parallelize : try to do operations in parallel when possible
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string[]|false[] Map of (path name => file content or false on failure)
|
2012-09-22 18:01:58 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getFileContentsMulti( array $params );
|
2012-01-04 02:15:07 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
/**
|
2013-09-30 07:12:10 +00:00
|
|
|
* Get metadata about a file at a storage path in the backend.
|
|
|
|
|
* If the file does not exist, then this returns false.
|
|
|
|
|
* Otherwise, the result is an associative array that includes:
|
|
|
|
|
* - headers : map of HTTP headers used for GET/HEAD requests (name => value)
|
|
|
|
|
* - metadata : map of file metadata (name => value)
|
|
|
|
|
* Metadata keys and headers names will be returned in all lower-case.
|
|
|
|
|
* Additional values may be included for internal use only.
|
|
|
|
|
*
|
|
|
|
|
* Use FileBackend::hasFeatures() to check how well this is supported.
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::XATTRS_FAIL
|
|
|
|
|
*
|
2013-09-30 07:12:10 +00:00
|
|
|
* @param array $params
|
|
|
|
|
* $params include:
|
|
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return array|false File metadata array or false (missing file or I/O error)
|
2013-09-30 07:12:10 +00:00
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getFileXAttributes( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-08 08:40:00 +00:00
|
|
|
* Get the size (bytes) of a file at a storage path in the backend.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::SIZE_FAIL
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return int|false File size in bytes or false (missing file or I/O error)
|
2012-01-08 08:40:00 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileSize( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get quick information about a file at a storage path in the backend.
|
2012-01-10 02:18:27 +00:00
|
|
|
* If the file does not exist, then this returns false.
|
|
|
|
|
* Otherwise, the result is an associative array that includes:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - mtime : the last-modified timestamp (TS_MW)
|
|
|
|
|
* - size : the file size (bytes)
|
2012-01-19 23:18:03 +00:00
|
|
|
* Additional values may be included for internal use only.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::STAT_ABSENT
|
|
|
|
|
* @see FileBackend::STAT_ERROR
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return array|false|null Attribute map, false (missing file), or null (I/O error)
|
2012-01-08 08:40:00 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileStat( array $params );
|
|
|
|
|
|
2012-01-04 02:15:07 +00:00
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Get a SHA-1 hash of the content of the file at a storage path in the backend.
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::SHA1_FAIL
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|false Hash string or false (missing file or I/O error)
|
2012-01-04 02:15:07 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileSha1Base36( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Get the properties of the content of the file at a storage path in the backend.
|
2012-11-21 23:30:54 +00:00
|
|
|
* This gives the result of FSFile::getProps() on a local copy of the file.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return array Properties map; FSFile::placeholderProps() if file missing or on I/O error
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileProps( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Stream the content of the file at a storage path in the backend.
|
2014-04-20 08:40:06 +00:00
|
|
|
*
|
2012-09-16 06:38:34 +00:00
|
|
|
* If the file does not exists, an HTTP 404 error will be given.
|
2011-12-20 03:52:06 +00:00
|
|
|
* Appropriate HTTP headers (Status, Content-Type, Content-Length)
|
2012-09-16 06:38:34 +00:00
|
|
|
* will be sent if streaming began, while none will be sent otherwise.
|
2011-12-20 03:52:06 +00:00
|
|
|
* Implementations should flush the output buffer before sending data.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2014-04-20 08:40:06 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - headers : list of additional HTTP headers to send if the file exists
|
|
|
|
|
* - options : HTTP request header map with lower case keys (since 1.28). Supports:
|
|
|
|
|
* range : format is "bytes=(\d*-\d*)"
|
|
|
|
|
* if-modified-since : format is an HTTP date
|
|
|
|
|
* - headless : only include the body (and headers from "headers") (since 1.28)
|
|
|
|
|
* - latest : use the latest available data
|
|
|
|
|
* - allowOB : preserve any output buffers (since 1.28)
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function streamFile( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Returns a file system file, identical in content to the file at a storage path.
|
2011-12-20 03:52:06 +00:00
|
|
|
* The file returned is either:
|
2019-08-30 07:01:29 +00:00
|
|
|
* - a) A TempFSFile local copy of the file at a storage path in the backend.
|
2012-07-20 10:13:43 +00:00
|
|
|
* The temporary copy will have the same extension as the source.
|
2019-08-30 07:01:29 +00:00
|
|
|
* Temporary files may be purged when the file object falls out of scope.
|
|
|
|
|
* - b) An FSFile pointing to the original file at a storage path in the backend.
|
|
|
|
|
* This is applicable for backends layered directly on top of file systems.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* Never modify the returned file since it might be the original, it might be shared
|
|
|
|
|
* among multiple callers of this method, or the backend might internally keep FSFile
|
|
|
|
|
* references for deferred operations.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return FSFile|null Local file copy or null (missing file or I/O error)
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-09-18 18:21:30 +00:00
|
|
|
final public function getLocalReference( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
$fsFiles = $this->getLocalReferenceMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
return $fsFiles[$params['src']];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Like getLocalReference() except it takes an array of storage paths and
|
|
|
|
|
* yields an order-preserved map of storage paths to temporary local file copies.
|
|
|
|
|
*
|
|
|
|
|
* Never modify the returned files since they might be originals, they might be shared
|
|
|
|
|
* among multiple callers of this method, or the backend might internally keep FSFile
|
|
|
|
|
* references for deferred operations.
|
2012-09-18 18:21:30 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackend::getLocalReference()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-09-18 18:21:30 +00:00
|
|
|
* - srcs : list of source storage paths
|
|
|
|
|
* - latest : use the latest available data
|
|
|
|
|
* - parallelize : try to do operations in parallel when possible
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array Map of (path name => FSFile or null on failure)
|
2012-09-18 18:21:30 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getLocalReferenceMulti( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a local copy on disk of the file at a storage path in the backend.
|
|
|
|
|
* The temporary copy will have the same file extension as the source.
|
|
|
|
|
* Temporary files may be purged when the file object falls out of scope.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* Multiple calls to this method for the same path will create new copies.
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return TempFSFile|null Temporary local file copy or null (missing file or I/O error)
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-09-18 18:21:30 +00:00
|
|
|
final public function getLocalCopy( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
$tmpFiles = $this->getLocalCopyMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
return $tmpFiles[$params['src']];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Like getLocalCopy() except it takes an array of storage paths and yields
|
|
|
|
|
* an order preserved-map of storage paths to temporary local file copies.
|
|
|
|
|
*
|
|
|
|
|
* Multiple calls to this method for the same path will create new copies.
|
2012-09-18 18:21:30 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackend::getLocalCopy()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-09-18 18:21:30 +00:00
|
|
|
* - srcs : list of source storage paths
|
|
|
|
|
* - latest : use the latest available data
|
|
|
|
|
* - parallelize : try to do operations in parallel when possible
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array Map of (path name => TempFSFile or null on failure)
|
2012-09-18 18:21:30 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getLocalCopyMulti( array $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2012-11-06 20:49:40 +00:00
|
|
|
/**
|
|
|
|
|
* Return an HTTP URL to a given file that requires no authentication to use.
|
|
|
|
|
* The URL may be pre-authenticated (via some token in the URL) and temporary.
|
|
|
|
|
* This will return null if the backend cannot make an HTTP URL for the file.
|
|
|
|
|
*
|
|
|
|
|
* This is useful for key/value stores when using scripts that seek around
|
|
|
|
|
* large files and those scripts (and the backend) support HTTP Range headers.
|
|
|
|
|
* Otherwise, one would need to use getLocalReference(), which involves loading
|
|
|
|
|
* the entire file on to local disk.
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::TEMPURL_ERROR
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-11-06 20:49:40 +00:00
|
|
|
* - src : source storage path
|
2013-03-07 04:47:30 +00:00
|
|
|
* - ttl : lifetime (seconds) if pre-authenticated; default is 1 day
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|null URL or null (not supported or I/O error)
|
2012-11-06 20:49:40 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getFileHttpUrl( array $params );
|
|
|
|
|
|
2011-12-23 09:43:28 +00:00
|
|
|
/**
|
2019-08-29 08:14:24 +00:00
|
|
|
* Check if a directory exists at a given storage path
|
|
|
|
|
*
|
|
|
|
|
* For backends using key/value stores, a directory is said to exist whenever
|
|
|
|
|
* there exist any files with paths using the given directory path as a prefix
|
|
|
|
|
* followed by a forward slash. For example, if there is a file called
|
|
|
|
|
* "mwstore://backend/container/dir/path.svg" then directories are said to exist
|
|
|
|
|
* at "mwstore://backend/container" and "mwstore://backend/container/dir". These
|
|
|
|
|
* can be thought of as "virtual" directories.
|
|
|
|
|
*
|
|
|
|
|
* Backends that directly use a filesystem layer might enumerate empty directories.
|
|
|
|
|
* The clean() method should always be used when files are deleted or moved if this
|
|
|
|
|
* is a concern. This is a trade-off to avoid write amplication/contention on file
|
|
|
|
|
* changes or read amplification when calling this method.
|
2012-04-05 05:56:08 +00:00
|
|
|
*
|
|
|
|
|
* Storage backends with eventual consistency might return stale data.
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::EXISTENCE_ERROR
|
2019-08-29 08:14:24 +00:00
|
|
|
* @see FileBackend::clean()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - dir : storage directory
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return bool|null Whether a directory exists or null (I/O error)
|
2012-04-05 05:56:08 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
abstract public function directoryExists( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-29 08:14:24 +00:00
|
|
|
* Get an iterator to list *all* directories under a storage directory
|
|
|
|
|
*
|
2012-04-05 05:56:08 +00:00
|
|
|
* If the directory is of the form "mwstore://backend/container",
|
2012-09-16 06:38:34 +00:00
|
|
|
* then all directories in the container will be listed.
|
2012-04-05 05:56:08 +00:00
|
|
|
* If the directory is of form "mwstore://backend/container/dir",
|
2012-09-16 06:38:34 +00:00
|
|
|
* then all directories directly under that directory will be listed.
|
|
|
|
|
* Results will be storage directories relative to the given directory.
|
2012-04-05 05:56:08 +00:00
|
|
|
*
|
|
|
|
|
* Storage backends with eventual consistency might return stale data.
|
|
|
|
|
*
|
2013-06-16 06:01:47 +00:00
|
|
|
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::LIST_ERROR
|
2019-08-29 08:14:24 +00:00
|
|
|
* @see FileBackend::directoryExists()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - dir : storage directory
|
|
|
|
|
* - topOnly : only return direct child dirs of the directory
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return Traversable|array|null Directory list enumerator or null (initial I/O error)
|
2012-04-05 05:56:08 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getDirectoryList( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as FileBackend::getDirectoryList() except only lists
|
|
|
|
|
* directories that are immediately under the given directory.
|
|
|
|
|
*
|
|
|
|
|
* Storage backends with eventual consistency might return stale data.
|
|
|
|
|
*
|
2013-06-16 06:01:47 +00:00
|
|
|
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::LIST_ERROR
|
2019-08-29 08:14:24 +00:00
|
|
|
* @see FileBackend::directoryExists()
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2012-07-17 03:56:49 +00:00
|
|
|
* - dir : storage directory
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return Traversable|array|null Directory list enumerator or null (initial I/O error)
|
2012-04-05 05:56:08 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
final public function getTopDirectoryList( array $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->getDirectoryList( [ 'topOnly' => true ] + $params );
|
2012-04-05 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-29 08:14:24 +00:00
|
|
|
* Get an iterator to list *all* stored files under a storage directory
|
|
|
|
|
*
|
|
|
|
|
* If the directory is of the form "mwstore://backend/container", then all
|
|
|
|
|
* files in the container will be listed. If the directory is of form
|
|
|
|
|
* "mwstore://backend/container/dir", then all files under that directory will
|
|
|
|
|
* be listed. Results will be storage paths relative to the given directory.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2012-02-08 22:13:03 +00:00
|
|
|
* Storage backends with eventual consistency might return stale data.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-16 06:01:47 +00:00
|
|
|
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::LIST_ERROR
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2013-04-30 18:39:40 +00:00
|
|
|
* - dir : storage directory
|
|
|
|
|
* - topOnly : only return direct child files of the directory (since 1.20)
|
|
|
|
|
* - adviseStat : set to true if stat requests will be made on the files (since 1.22)
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return Traversable|array|null File list enumerator or null (initial I/O error)
|
2011-12-23 09:43:28 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileList( array $params );
|
|
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
/**
|
|
|
|
|
* Same as FileBackend::getFileList() except only lists
|
|
|
|
|
* files that are immediately under the given directory.
|
|
|
|
|
*
|
|
|
|
|
* Storage backends with eventual consistency might return stale data.
|
|
|
|
|
*
|
2013-06-16 06:01:47 +00:00
|
|
|
* Failures during iteration can result in FileBackendError exceptions (since 1.22).
|
|
|
|
|
*
|
2019-08-30 07:01:29 +00:00
|
|
|
* @see FileBackend::LIST_ERROR
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params Parameters include:
|
2013-04-30 18:39:40 +00:00
|
|
|
* - dir : storage directory
|
|
|
|
|
* - adviseStat : set to true if stat requests will be made on the files (since 1.22)
|
2019-08-29 08:14:24 +00:00
|
|
|
* @return Traversable|array|null File list enumerator or null on failure
|
2012-04-05 05:56:08 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
final public function getTopFileList( array $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->getFileList( [ 'topOnly' => true ] + $params );
|
2012-04-05 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-08 22:10:53 +00:00
|
|
|
/**
|
2014-01-16 00:46:01 +00:00
|
|
|
* Preload persistent file stat cache and property cache into in-process cache.
|
2012-08-24 06:17:16 +00:00
|
|
|
* This should be used when stat calls will be made on a known list of a many files.
|
|
|
|
|
*
|
2014-01-16 00:46:01 +00:00
|
|
|
* @see FileBackend::getFileStat()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $paths Storage paths
|
2012-08-24 06:17:16 +00:00
|
|
|
*/
|
2014-04-12 22:42:34 +00:00
|
|
|
abstract public function preloadCache( array $paths );
|
2012-08-24 06:17:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invalidate any in-process file stat and property cache.
|
2012-01-08 22:10:53 +00:00
|
|
|
* If $paths is given, then only the cache for those files will be cleared.
|
|
|
|
|
*
|
2014-01-16 00:46:01 +00:00
|
|
|
* @see FileBackend::getFileStat()
|
|
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param array|null $paths Storage paths (optional)
|
2012-01-08 22:10:53 +00:00
|
|
|
*/
|
2014-04-12 22:42:34 +00:00
|
|
|
abstract public function clearCache( array $paths = null );
|
2012-01-08 22:10:53 +00:00
|
|
|
|
2014-01-16 00:46:01 +00:00
|
|
|
/**
|
|
|
|
|
* Preload file stat information (concurrently if possible) into in-process cache.
|
2015-01-25 09:19:24 +00:00
|
|
|
*
|
2014-01-16 00:46:01 +00:00
|
|
|
* This should be used when stat calls will be made on a known list of a many files.
|
2015-01-25 09:19:24 +00:00
|
|
|
* This does not make use of the persistent file stat cache.
|
2014-01-16 00:46:01 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackend::getFileStat()
|
|
|
|
|
*
|
|
|
|
|
* @param array $params Parameters include:
|
|
|
|
|
* - srcs : list of source storage paths
|
|
|
|
|
* - latest : use the latest available data
|
2019-08-29 08:14:24 +00:00
|
|
|
* @return bool Whether all requests proceeded without I/O errors (since 1.24)
|
2014-01-16 00:46:01 +00:00
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
2014-04-18 18:34:39 +00:00
|
|
|
abstract public function preloadFileStat( array $params );
|
2014-01-16 00:46:01 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* Lock the files at the given storage paths in the backend.
|
|
|
|
|
* This will either lock all the files or none (on failure).
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* Callers should consider using getScopedFileLocks() instead.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $paths Storage paths
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param int $type LockManager::LOCK_* constant
|
2014-05-20 21:22:52 +00:00
|
|
|
* @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2014-05-20 21:22:52 +00:00
|
|
|
final public function lockFiles( array $paths, $type, $timeout = 0 ) {
|
2020-12-31 11:37:36 +00:00
|
|
|
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-09-18 04:42:56 +00:00
|
|
|
return $this->wrapStatus( $this->lockManager->lock( $paths, $type, $timeout ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unlock the files at the given storage paths in the backend.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $paths Storage paths
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param int $type LockManager::LOCK_* constant
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final public function unlockFiles( array $paths, $type ) {
|
2020-12-31 11:37:36 +00:00
|
|
|
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-09-18 04:42:56 +00:00
|
|
|
return $this->wrapStatus( $this->lockManager->unlock( $paths, $type ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lock the files at the given storage paths in the backend.
|
|
|
|
|
* This will either lock all the files or none (on failure).
|
2016-09-16 22:55:40 +00:00
|
|
|
* On failure, the StatusValue object will be updated with errors.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* Once the return value goes out scope, the locks will be released and
|
2016-09-16 22:55:40 +00:00
|
|
|
* the StatusValue updated. Unlock fatals will not change the StatusValue "OK" value.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-12 19:31:15 +00:00
|
|
|
* @see ScopedLock::factory()
|
|
|
|
|
*
|
|
|
|
|
* @param array $paths List of storage paths or map of lock types to path lists
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param int|string $type LockManager::LOCK_* constant or "mixed"
|
2016-09-16 22:55:40 +00:00
|
|
|
* @param StatusValue $status StatusValue to update on lock/unlock
|
2014-05-20 21:22:52 +00:00
|
|
|
* @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return ScopedLock|null RAII-style self-unlocking lock or null on failure
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2016-09-16 22:55:40 +00:00
|
|
|
final public function getScopedFileLocks(
|
|
|
|
|
array $paths, $type, StatusValue $status, $timeout = 0
|
|
|
|
|
) {
|
2013-07-06 22:06:38 +00:00
|
|
|
if ( $type === 'mixed' ) {
|
|
|
|
|
foreach ( $paths as &$typePaths ) {
|
2020-12-31 11:37:36 +00:00
|
|
|
$typePaths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $typePaths );
|
2013-07-06 22:06:38 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-31 11:37:36 +00:00
|
|
|
$paths = array_map( [ __CLASS__, 'normalizeStoragePath' ], $paths );
|
2013-07-06 22:06:38 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2014-05-20 21:22:52 +00:00
|
|
|
return ScopedLock::factory( $this->lockManager, $paths, $type, $status, $timeout );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-01-29 21:28:31 +00:00
|
|
|
|
2012-04-28 19:49:46 +00:00
|
|
|
/**
|
|
|
|
|
* Get an array of scoped locks needed for a batch of file operations.
|
|
|
|
|
*
|
|
|
|
|
* Normally, FileBackend::doOperations() handles locking, unless
|
|
|
|
|
* the 'nonLocking' param is passed in. This function is useful if you
|
|
|
|
|
* want the files to be locked for a broader scope than just when the
|
|
|
|
|
* files are changing. For example, if you need to update DB metadata,
|
|
|
|
|
* you may want to keep the files locked until finished.
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackend::doOperations()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $ops List of file operations to FileBackend::doOperations()
|
2016-09-16 22:55:40 +00:00
|
|
|
* @param StatusValue $status StatusValue to update on lock/unlock
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return ScopedLock|null RAII-style self-unlocking lock or null on failure
|
2012-04-28 19:49:46 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2016-09-16 22:55:40 +00:00
|
|
|
abstract public function getScopedLocksForOps( array $ops, StatusValue $status );
|
2012-04-28 19:49:46 +00:00
|
|
|
|
2012-04-20 02:57:58 +00:00
|
|
|
/**
|
|
|
|
|
* Get the root storage path of this backend.
|
|
|
|
|
* All container paths are "subdirectories" of this path.
|
|
|
|
|
*
|
|
|
|
|
* @return string Storage path
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
final public function getRootStoragePath() {
|
|
|
|
|
return "mwstore://{$this->name}";
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-10 22:06:19 +00:00
|
|
|
/**
|
|
|
|
|
* Get the storage path for the given container for this backend
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Container name
|
2012-10-10 22:06:19 +00:00
|
|
|
* @return string Storage path
|
|
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
final public function getContainerStoragePath( $container ) {
|
|
|
|
|
return $this->getRootStoragePath() . "/{$container}";
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 01:33:42 +00:00
|
|
|
/**
|
|
|
|
|
* Convert FSFile 'src' paths to string paths (with an 'srcRef' field set to the FSFile)
|
|
|
|
|
*
|
|
|
|
|
* The 'srcRef' field keeps any TempFSFile objects in scope for the backend to have it
|
|
|
|
|
* around as long it needs (which may vary greatly depending on configuration)
|
|
|
|
|
*
|
|
|
|
|
* @param array $ops File operation batch for FileBaclend::doOperations()
|
|
|
|
|
* @return array File operation batch
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveFSFileObjects( array $ops ) {
|
|
|
|
|
foreach ( $ops as &$op ) {
|
2017-10-06 22:17:58 +00:00
|
|
|
$src = $op['src'] ?? null;
|
2016-02-27 01:33:42 +00:00
|
|
|
if ( $src instanceof FSFile ) {
|
|
|
|
|
$op['srcRef'] = $src;
|
|
|
|
|
$op['src'] = $src->getPath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset( $op );
|
|
|
|
|
|
|
|
|
|
return $ops;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-29 21:28:31 +00:00
|
|
|
/**
|
|
|
|
|
* Check if a given path is a "mwstore://" path.
|
|
|
|
|
* This does not do any further validation or any existence checks.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $path
|
2012-01-29 21:28:31 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final public static function isStoragePath( $path ) {
|
|
|
|
|
return ( strpos( $path, 'mwstore://' ) === 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-20 02:57:58 +00:00
|
|
|
* Split a storage path into a backend name, a container name,
|
2012-01-29 21:28:31 +00:00
|
|
|
* and a relative file path. The relative path may be the empty string.
|
|
|
|
|
* This does not do any path normalization or traversal checks.
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array (backend, container, rel object) or (null, null, null)
|
2012-01-29 21:28:31 +00:00
|
|
|
*/
|
|
|
|
|
final public static function splitStoragePath( $storagePath ) {
|
|
|
|
|
if ( self::isStoragePath( $storagePath ) ) {
|
|
|
|
|
// Remove the "mwstore://" prefix and split the path
|
|
|
|
|
$parts = explode( '/', substr( $storagePath, 10 ), 3 );
|
|
|
|
|
if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
|
|
|
|
|
if ( count( $parts ) == 3 ) {
|
|
|
|
|
return $parts; // e.g. "backend/container/path"
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $parts[0], $parts[1], '' ]; // e.g. "backend/container"
|
2012-01-29 21:28:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ null, null, null ];
|
2012-01-29 21:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize a storage path by cleaning up directory separators.
|
|
|
|
|
* Returns null if the path is not of the format of a valid storage path.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|null Normalized storage path or null on failure
|
2012-01-29 21:28:31 +00:00
|
|
|
*/
|
|
|
|
|
final public static function normalizeStoragePath( $storagePath ) {
|
|
|
|
|
list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
|
|
|
|
|
if ( $relPath !== null ) { // must be for this backend
|
|
|
|
|
$relPath = self::normalizeContainerPath( $relPath );
|
|
|
|
|
if ( $relPath !== null ) {
|
|
|
|
|
return ( $relPath != '' )
|
|
|
|
|
? "mwstore://{$backend}/{$container}/{$relPath}"
|
|
|
|
|
: "mwstore://{$backend}/{$container}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-01-29 21:28:31 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 19:14:50 +00:00
|
|
|
/**
|
|
|
|
|
* Get the parent storage directory of a storage path.
|
|
|
|
|
* This returns a path like "mwstore://backend/container",
|
|
|
|
|
* "mwstore://backend/container/...", or null if there is no parent.
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|null Parent storage path or null on failure
|
2012-03-03 19:14:50 +00:00
|
|
|
*/
|
|
|
|
|
final public static function parentStoragePath( $storagePath ) {
|
2019-10-30 11:30:38 +00:00
|
|
|
// XXX dirname() depends on platform and locale! If nothing enforces that the storage path
|
|
|
|
|
// doesn't contain characters like '\', behavior can vary by platform. We should use
|
|
|
|
|
// explode() instead.
|
2012-03-03 19:14:50 +00:00
|
|
|
$storagePath = dirname( $storagePath );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , , $rel ) = self::splitStoragePath( $storagePath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 19:14:50 +00:00
|
|
|
return ( $rel === null ) ? null : $storagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the final extension from a storage or FS path
|
2012-04-20 02:57:58 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $path
|
2014-04-22 02:30:44 +00:00
|
|
|
* @param string $case One of (rawcase, uppercase, lowercase) (since 1.24)
|
2012-03-03 19:14:50 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-04-22 02:30:44 +00:00
|
|
|
final public static function extensionFromPath( $path, $case = 'lowercase' ) {
|
2019-10-30 11:30:38 +00:00
|
|
|
// This will treat a string starting with . as not having an extension, but store paths have
|
|
|
|
|
// to start with 'mwstore://', so "garbage in, garbage out".
|
2012-03-03 19:14:50 +00:00
|
|
|
$i = strrpos( $path, '.' );
|
2014-04-22 02:30:44 +00:00
|
|
|
$ext = $i ? substr( $path, $i + 1 ) : '';
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2014-04-22 02:30:44 +00:00
|
|
|
if ( $case === 'lowercase' ) {
|
|
|
|
|
$ext = strtolower( $ext );
|
|
|
|
|
} elseif ( $case === 'uppercase' ) {
|
|
|
|
|
$ext = strtoupper( $ext );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ext;
|
2012-03-03 19:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-05 04:10:50 +00:00
|
|
|
/**
|
|
|
|
|
* Check if a relative path has no directory traversals
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $path
|
2012-04-05 04:10:50 +00:00
|
|
|
* @return bool
|
2012-04-05 05:56:08 +00:00
|
|
|
* @since 1.20
|
2012-04-05 04:10:50 +00:00
|
|
|
*/
|
|
|
|
|
final public static function isPathTraversalFree( $path ) {
|
|
|
|
|
return ( self::normalizeContainerPath( $path ) !== null );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-31 22:35:39 +00:00
|
|
|
/**
|
2012-09-06 17:15:10 +00:00
|
|
|
* Build a Content-Disposition header value per RFC 6266.
|
2012-08-31 22:35:39 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $type One of (attachment, inline)
|
|
|
|
|
* @param string $filename Suggested file name (should not contain slashes)
|
2019-08-29 05:32:59 +00:00
|
|
|
* @throws InvalidArgumentException
|
2012-08-31 22:35:39 +00:00
|
|
|
* @return string
|
2012-09-02 07:33:19 +00:00
|
|
|
* @since 1.20
|
2012-08-31 22:35:39 +00:00
|
|
|
*/
|
2012-09-06 17:15:10 +00:00
|
|
|
final public static function makeContentDisposition( $type, $filename = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$parts = [];
|
2012-09-06 17:15:10 +00:00
|
|
|
|
2012-08-31 23:24:19 +00:00
|
|
|
$type = strtolower( $type );
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !in_array( $type, [ 'inline', 'attachment' ] ) ) {
|
2016-09-21 04:44:29 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid Content-Disposition type '$type'." );
|
2012-09-06 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
$parts[] = $type;
|
|
|
|
|
|
|
|
|
|
if ( strlen( $filename ) ) {
|
|
|
|
|
$parts[] = "filename*=UTF-8''" . rawurlencode( basename( $filename ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode( ';', $parts );
|
2012-08-31 22:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-29 21:28:31 +00:00
|
|
|
/**
|
|
|
|
|
* Validate and normalize a relative storage path.
|
|
|
|
|
* Null is returned if the path involves directory traversal.
|
|
|
|
|
* Traversal is insecure for FS backends and broken for others.
|
|
|
|
|
*
|
2012-04-05 04:10:50 +00:00
|
|
|
* This uses the same traversal protection as Title::secureAndSplit().
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path Storage path relative to a container
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string|null Normalized container path or null on failure
|
2012-01-29 21:28:31 +00:00
|
|
|
*/
|
|
|
|
|
final protected static function normalizeContainerPath( $path ) {
|
|
|
|
|
// Normalize directory separators
|
|
|
|
|
$path = strtr( $path, '\\', '/' );
|
|
|
|
|
// Collapse any consecutive directory separators
|
|
|
|
|
$path = preg_replace( '![/]{2,}!', '/', $path );
|
|
|
|
|
// Remove any leading directory separator
|
|
|
|
|
$path = ltrim( $path, '/' );
|
|
|
|
|
// Use the same traversal protection as Title::secureAndSplit()
|
|
|
|
|
if ( strpos( $path, '.' ) !== false ) {
|
|
|
|
|
if (
|
|
|
|
|
$path === '.' ||
|
|
|
|
|
$path === '..' ||
|
|
|
|
|
strpos( $path, './' ) === 0 ||
|
|
|
|
|
strpos( $path, '../' ) === 0 ||
|
|
|
|
|
strpos( $path, '/./' ) !== false ||
|
|
|
|
|
strpos( $path, '/../' ) !== false
|
2012-04-20 02:57:58 +00:00
|
|
|
) {
|
2012-01-29 21:28:31 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-01-29 21:28:31 +00:00
|
|
|
return $path;
|
|
|
|
|
}
|
2016-09-16 22:55:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Yields the result of the status wrapper callback on either:
|
|
|
|
|
* - StatusValue::newGood() if this method is called without parameters
|
|
|
|
|
* - StatusValue::newFatal() with all parameters to this method if passed in
|
|
|
|
|
*
|
2022-03-08 22:57:00 +00:00
|
|
|
* @param mixed ...$args
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
|
|
|
|
*/
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
final protected function newStatus( ...$args ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
if ( count( $args ) ) {
|
2018-06-08 02:58:35 +00:00
|
|
|
$sv = StatusValue::newFatal( ...$args );
|
2016-09-16 22:55:40 +00:00
|
|
|
} else {
|
|
|
|
|
$sv = StatusValue::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->wrapStatus( $sv );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param StatusValue $sv
|
|
|
|
|
* @return StatusValue Modified status or StatusValue subclass
|
|
|
|
|
*/
|
|
|
|
|
final protected function wrapStatus( StatusValue $sv ) {
|
|
|
|
|
return $this->statusWrapper ? call_user_func( $this->statusWrapper, $sv ) : $sv;
|
|
|
|
|
}
|
2016-09-18 21:40:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $section
|
|
|
|
|
* @return ScopedCallback|null
|
|
|
|
|
*/
|
|
|
|
|
protected function scopedProfileSection( $section ) {
|
2019-03-13 18:14:19 +00:00
|
|
|
return $this->profiler ? ( $this->profiler )( $section ) : null;
|
2016-09-18 21:40:38 +00:00
|
|
|
}
|
2016-09-21 23:12:27 +00:00
|
|
|
|
2019-10-30 11:30:38 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore Let's not reset output buffering during tests
|
|
|
|
|
*/
|
2016-09-21 23:12:27 +00:00
|
|
|
protected function resetOutputBuffer() {
|
2019-10-30 11:30:38 +00:00
|
|
|
// XXX According to documentation, ob_get_status() always returns a non-empty array and this
|
|
|
|
|
// condition will always be true
|
2016-09-21 23:12:27 +00:00
|
|
|
while ( ob_get_status() ) {
|
|
|
|
|
if ( !ob_end_clean() ) {
|
|
|
|
|
// Could not remove output buffer handler; abort now
|
|
|
|
|
// to avoid getting in some kind of infinite loop.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|