filebackend: clean up some comments and remove unused FileBackendStoreOpHandle field
[lhc/web/wiklou.git] / includes / libs / filebackend / FileBackend.php
1 <?php
2 /**
3 * @defgroup FileBackend File backend
4 *
5 * File backend is used to interact with file storage systems,
6 * such as the local file system, NFS, or cloud storage systems.
7 */
8
9 /**
10 * Base class for all file backends.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup FileBackend
29 */
30 use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
31 use Psr\Log\LoggerAwareInterface;
32 use Psr\Log\LoggerInterface;
33 use Wikimedia\ScopedCallback;
34 use Psr\Log\NullLogger;
35
36 /**
37 * @brief Base class for all file backend classes (including multi-write backends).
38 *
39 * This class defines the methods as abstract that subclasses must implement.
40 * Outside callers can assume that all backends will have these functions.
41 *
42 * All "storage paths" are of the format "mwstore://<backend>/<container>/<path>".
43 * The "backend" portion is unique name for the application to refer to a backend, while
44 * the "container" portion is a top-level directory of the backend. The "path" portion
45 * is a relative path that uses UNIX file system (FS) notation, though any particular
46 * backend may not actually be using a local filesystem. Therefore, the relative paths
47 * are only virtual.
48 *
49 * Backend contents are stored under "domain"-specific container names by default.
50 * A domain is simply a logical umbrella for entities, such as those belonging to a certain
51 * application or portion of a website, for example. A domain can be local or global.
52 * Global (qualified) backends are achieved by configuring the "domain ID" to a constant.
53 * Global domains are simpler, but local domains can be used by choosing a domain ID based on
54 * the current context, such as which language of a website is being used.
55 *
56 * For legacy reasons, the FSFileBackend class allows manually setting the paths of
57 * containers to ones that do not respect the "domain ID".
58 *
59 * In key/value (object) stores, containers are the only hierarchy (the rest is emulated).
60 * FS-based backends are somewhat more restrictive due to the existence of real
61 * directory files; a regular file cannot have the same name as a directory. Other
62 * backends with virtual directories may not have this limitation. Callers should
63 * store files in such a way that no files and directories are under the same path.
64 *
65 * In general, this class allows for callers to access storage through the same
66 * interface, without regard to the underlying storage system. However, calling code
67 * must follow certain patterns and be aware of certain things to ensure compatibility:
68 * - a) Always call prepare() on the parent directory before trying to put a file there;
69 * key/value stores only need the container to exist first, but filesystems need
70 * all the parent directories to exist first (prepare() is aware of all this)
71 * - b) Always call clean() on a directory when it might become empty to avoid empty
72 * directory buildup on filesystems; key/value stores never have empty directories,
73 * so doing this helps preserve consistency in both cases
74 * - c) Likewise, do not rely on the existence of empty directories for anything;
75 * calling directoryExists() on a path that prepare() was previously called on
76 * will return false for key/value stores if there are no files under that path
77 * - d) Never alter the resulting FSFile returned from getLocalReference(), as it could
78 * either be a copy of the source file in /tmp or the original source file itself
79 * - e) Use a file layout that results in never attempting to store files over directories
80 * or directories over files; key/value stores allow this but filesystems do not
81 * - f) Use ASCII file names (e.g. base32, IDs, hashes) to avoid Unicode issues in Windows
82 * - g) Do not assume that move operations are atomic (difficult with key/value stores)
83 * - h) Do not assume that file stat or read operations always have immediate consistency;
84 * various methods have a "latest" flag that should always be used if up-to-date
85 * information is required (this trades performance for correctness as needed)
86 * - i) Do not assume that directory listings have immediate consistency
87 *
88 * Methods of subclasses should avoid throwing exceptions at all costs.
89 * As a corollary, external dependencies should be kept to a minimum.
90 *
91 * @ingroup FileBackend
92 * @since 1.19
93 */
94 abstract class FileBackend implements LoggerAwareInterface {
95 /** @var string Unique backend name */
96 protected $name;
97
98 /** @var string Unique domain name */
99 protected $domainId;
100
101 /** @var string Read-only explanation message */
102 protected $readOnly;
103
104 /** @var string When to do operations in parallel */
105 protected $parallelize;
106
107 /** @var int How many operations can be done in parallel */
108 protected $concurrency;
109
110 /** @var TempFSFileFactory */
111 protected $tmpFileFactory;
112
113 /** @var LockManager */
114 protected $lockManager;
115 /** @var FileJournal */
116 protected $fileJournal;
117 /** @var LoggerInterface */
118 protected $logger;
119 /** @var callable|null */
120 protected $profiler;
121
122 /** @var callable */
123 protected $obResetFunc;
124 /** @var callable */
125 protected $streamMimeFunc;
126 /** @var callable */
127 protected $statusWrapper;
128
129 /** Bitfield flags for supported features */
130 const ATTR_HEADERS = 1; // files can be tagged with standard HTTP headers
131 const ATTR_METADATA = 2; // files can be stored with metadata key/values
132 const ATTR_UNICODE_PATHS = 4; // files can have Unicode paths (not just ASCII)
133
134 /** @var false Idiom for "no info; non-existant file" (since 1.34) */
135 const STAT_ABSENT = false;
136
137 /** @var null Idiom for "no info; I/O errors" (since 1.34) */
138 const STAT_ERROR = null;
139 /** @var null Idiom for "no file/directory list; I/O errors" (since 1.34) */
140 const LIST_ERROR = null;
141 /** @var null Idiom for "no temp URL; not supported or I/O errors" (since 1.34) */
142 const TEMPURL_ERROR = null;
143 /** @var null Idiom for "existence unknown; I/O errors" (since 1.34) */
144 const EXISTENCE_ERROR = null;
145
146 /** @var false Idiom for "no timestamp; missing file or I/O errors" (since 1.34) */
147 const TIMESTAMP_FAIL = false;
148 /** @var false Idiom for "no content; missing file or I/O errors" (since 1.34) */
149 const CONTENT_FAIL = false;
150 /** @var false Idiom for "no metadata; missing file or I/O errors" (since 1.34) */
151 const XATTRS_FAIL = false;
152 /** @var false Idiom for "no size; missing file or I/O errors" (since 1.34) */
153 const SIZE_FAIL = false;
154 /** @var false Idiom for "no SHA1 hash; missing file or I/O errors" (since 1.34) */
155 const SHA1_FAIL = false;
156
157 /**
158 * Create a new backend instance from configuration.
159 * This should only be called from within FileBackendGroup.
160 *
161 * @param array $config Parameters include:
162 * - name : The unique name of this backend.
163 * This should consist of alphanumberic, '-', and '_' characters.
164 * This name should not be changed after use (e.g. with journaling).
165 * Note that the name is *not* used in actual container names.
166 * - domainId : Prefix to container names that is unique to this backend.
167 * It should only consist of alphanumberic, '-', and '_' characters.
168 * This ID is what avoids collisions if multiple logical backends
169 * use the same storage system, so this should be set carefully.
170 * - lockManager : LockManager object to use for any file locking.
171 * If not provided, then no file locking will be enforced.
172 * - fileJournal : FileJournal object to use for logging changes to files.
173 * If not provided, then change journaling will be disabled.
174 * - readOnly : Write operations are disallowed if this is a non-empty string.
175 * It should be an explanation for the backend being read-only.
176 * - parallelize : When to do file operations in parallel (when possible).
177 * Allowed values are "implicit", "explicit" and "off".
178 * - concurrency : How many file operations can be done in parallel.
179 * - tmpDirectory : Directory to use for temporary files.
180 * - tmpFileFactory : Optional TempFSFileFactory object. Only has an effect if
181 * tmpDirectory is not set. If both are unset or null, then the backend will
182 * try to discover a usable temporary directory.
183 * - obResetFunc : alternative callback to clear the output buffer
184 * - streamMimeFunc : alternative method to determine the content type from the path
185 * - logger : Optional PSR logger object.
186 * - profiler : Optional callback that takes a section name argument and returns
187 * a ScopedCallback instance that ends the profile section in its destructor.
188 * @throws InvalidArgumentException
189 */
190 public function __construct( array $config ) {
191 $this->name = $config['name'];
192 $this->domainId = $config['domainId'] // e.g. "my_wiki-en_"
193 ?? $config['wikiId']; // b/c alias
194 if ( !preg_match( '!^[a-zA-Z0-9-_]{1,255}$!', $this->name ) ) {
195 throw new InvalidArgumentException( "Backend name '{$this->name}' is invalid." );
196 } elseif ( !is_string( $this->domainId ) ) {
197 throw new InvalidArgumentException(
198 "Backend domain ID not provided for '{$this->name}'." );
199 }
200 $this->lockManager = $config['lockManager'] ?? new NullLockManager( [] );
201 $this->fileJournal = $config['fileJournal']
202 ?? FileJournal::factory( [ 'class' => NullFileJournal::class ], $this->name );
203 $this->readOnly = isset( $config['readOnly'] )
204 ? (string)$config['readOnly']
205 : '';
206 $this->parallelize = isset( $config['parallelize'] )
207 ? (string)$config['parallelize']
208 : 'off';
209 $this->concurrency = isset( $config['concurrency'] )
210 ? (int)$config['concurrency']
211 : 50;
212 $this->obResetFunc = $config['obResetFunc'] ?? [ $this, 'resetOutputBuffer' ];
213 $this->streamMimeFunc = $config['streamMimeFunc'] ?? null;
214 $this->statusWrapper = $config['statusWrapper'] ?? null;
215
216 $this->profiler = $config['profiler'] ?? null;
217 if ( !is_callable( $this->profiler ) ) {
218 $this->profiler = null;
219 }
220 $this->logger = $config['logger'] ?? new NullLogger();
221 $this->statusWrapper = $config['statusWrapper'] ?? null;
222 // tmpDirectory gets precedence for backward compatibility
223 if ( isset( $config['tmpDirectory'] ) ) {
224 $this->tmpFileFactory = new TempFSFileFactory( $config['tmpDirectory'] );
225 } else {
226 $this->tmpFileFactory = $config['tmpFileFactory'] ?? new TempFSFileFactory();
227 }
228 }
229
230 public function setLogger( LoggerInterface $logger ) {
231 $this->logger = $logger;
232 }
233
234 /**
235 * Get the unique backend name
236 *
237 * We may have multiple different backends of the same type.
238 * For example, we can have two Swift backends using different proxies.
239 *
240 * @return string
241 */
242 final public function getName() {
243 return $this->name;
244 }
245
246 /**
247 * Get the domain identifier used for this backend (possibly empty).
248 *
249 * @return string
250 * @since 1.28
251 */
252 final public function getDomainId() {
253 return $this->domainId;
254 }
255
256 /**
257 * Alias to getDomainId()
258 *
259 * @return string
260 * @since 1.20
261 * @deprecated Since 1.34 Use getDomainId()
262 */
263 final public function getWikiId() {
264 return $this->getDomainId();
265 }
266
267 /**
268 * Check if this backend is read-only
269 *
270 * @return bool
271 */
272 final public function isReadOnly() {
273 return ( $this->readOnly != '' );
274 }
275
276 /**
277 * Get an explanatory message if this backend is read-only
278 *
279 * @return string|bool Returns false if the backend is not read-only
280 */
281 final public function getReadOnlyReason() {
282 return ( $this->readOnly != '' ) ? $this->readOnly : false;
283 }
284
285 /**
286 * Get the a bitfield of extra features supported by the backend medium
287 *
288 * @return int Bitfield of FileBackend::ATTR_* flags
289 * @since 1.23
290 */
291 public function getFeatures() {
292 return self::ATTR_UNICODE_PATHS;
293 }
294
295 /**
296 * Check if the backend medium supports a field of extra features
297 *
298 * @param int $bitfield Bitfield of FileBackend::ATTR_* flags
299 * @return bool
300 * @since 1.23
301 */
302 final public function hasFeatures( $bitfield ) {
303 return ( $this->getFeatures() & $bitfield ) === $bitfield;
304 }
305
306 /**
307 * This is the main entry point into the backend for write operations.
308 * Callers supply an ordered list of operations to perform as a transaction.
309 * Files will be locked, the stat cache cleared, and then the operations attempted.
310 * If any serious errors occur, all attempted operations will be rolled back.
311 *
312 * $ops is an array of arrays. The outer array holds a list of operations.
313 * Each inner array is a set of key value pairs that specify an operation.
314 *
315 * Supported operations and their parameters. The supported actions are:
316 * - create
317 * - store
318 * - copy
319 * - move
320 * - delete
321 * - describe (since 1.21)
322 * - null
323 *
324 * FSFile/TempFSFile object support was added in 1.27.
325 *
326 * a) Create a new file in storage with the contents of a string
327 * @code
328 * [
329 * 'op' => 'create',
330 * 'dst' => <storage path>,
331 * 'content' => <string of new file contents>,
332 * 'overwrite' => <boolean>,
333 * 'overwriteSame' => <boolean>,
334 * 'headers' => <HTTP header name/value map> # since 1.21
335 * ]
336 * @endcode
337 *
338 * b) Copy a file system file into storage
339 * @code
340 * [
341 * 'op' => 'store',
342 * 'src' => <file system path, FSFile, or TempFSFile>,
343 * 'dst' => <storage path>,
344 * 'overwrite' => <boolean>,
345 * 'overwriteSame' => <boolean>,
346 * 'headers' => <HTTP header name/value map> # since 1.21
347 * ]
348 * @endcode
349 *
350 * c) Copy a file within storage
351 * @code
352 * [
353 * 'op' => 'copy',
354 * 'src' => <storage path>,
355 * 'dst' => <storage path>,
356 * 'overwrite' => <boolean>,
357 * 'overwriteSame' => <boolean>,
358 * 'ignoreMissingSource' => <boolean>, # since 1.21
359 * 'headers' => <HTTP header name/value map> # since 1.21
360 * ]
361 * @endcode
362 *
363 * d) Move a file within storage
364 * @code
365 * [
366 * 'op' => 'move',
367 * 'src' => <storage path>,
368 * 'dst' => <storage path>,
369 * 'overwrite' => <boolean>,
370 * 'overwriteSame' => <boolean>,
371 * 'ignoreMissingSource' => <boolean>, # since 1.21
372 * 'headers' => <HTTP header name/value map> # since 1.21
373 * ]
374 * @endcode
375 *
376 * e) Delete a file within storage
377 * @code
378 * [
379 * 'op' => 'delete',
380 * 'src' => <storage path>,
381 * 'ignoreMissingSource' => <boolean>
382 * ]
383 * @endcode
384 *
385 * f) Update metadata for a file within storage
386 * @code
387 * [
388 * 'op' => 'describe',
389 * 'src' => <storage path>,
390 * 'headers' => <HTTP header name/value map>
391 * ]
392 * @endcode
393 *
394 * g) Do nothing (no-op)
395 * @code
396 * [
397 * 'op' => 'null',
398 * ]
399 * @endcode
400 *
401 * Boolean flags for operations (operation-specific):
402 * - ignoreMissingSource : The operation will simply succeed and do
403 * nothing if the source file does not exist.
404 * - overwrite : Any destination file will be overwritten.
405 * - overwriteSame : If a file already exists at the destination with the
406 * same contents, then do nothing to the destination file
407 * instead of giving an error. This does not compare headers.
408 * This option is ignored if 'overwrite' is already provided.
409 * - headers : If supplied, the result of merging these headers with any
410 * existing source file headers (replacing conflicting ones)
411 * will be set as the destination file headers. Headers are
412 * deleted if their value is set to the empty string. When a
413 * file has headers they are included in responses to GET and
414 * HEAD requests to the backing store for that file.
415 * Header values should be no larger than 255 bytes, except for
416 * Content-Disposition. The system might ignore or truncate any
417 * headers that are too long to store (exact limits will vary).
418 * Backends that don't support metadata ignore this. (since 1.21)
419 *
420 * $opts is an associative of boolean flags, including:
421 * - force : Operation precondition errors no longer trigger an abort.
422 * Any remaining operations are still attempted. Unexpected
423 * failures may still cause remaining operations to be aborted.
424 * - nonLocking : No locks are acquired for the operations.
425 * This can increase performance for non-critical writes.
426 * This has no effect unless the 'force' flag is set.
427 * - nonJournaled : Don't log this operation batch in the file journal.
428 * This limits the ability of recovery scripts.
429 * - parallelize : Try to do operations in parallel when possible.
430 * - bypassReadOnly : Allow writes in read-only mode. (since 1.20)
431 * - preserveCache : Don't clear the process cache before checking files.
432 * This should only be used if all entries in the process
433 * cache were added after the files were already locked. (since 1.20)
434 *
435 * @note Remarks on locking:
436 * File system paths given to operations should refer to files that are
437 * already locked or otherwise safe from modification from other processes.
438 * Normally these files will be new temp files, which should be adequate.
439 *
440 * @par Return value:
441 *
442 * This returns a Status, which contains all warnings and fatals that occurred
443 * during the operation. The 'failCount', 'successCount', and 'success' members
444 * will reflect each operation attempted.
445 *
446 * The StatusValue will be "OK" unless:
447 * - a) unexpected operation errors occurred (network partitions, disk full...)
448 * - b) predicted operation errors occurred and 'force' was not set
449 *
450 * @param array $ops List of operations to execute in order
451 * @codingStandardsIgnoreStart
452 * @phan-param array{ignoreMissingSource?:bool,overwrite?:bool,overwriteSame?:bool,headers?:bool} $ops
453 * @param array $opts Batch operation options
454 * @phan-param array{force?:bool,nonLocking?:bool,nonJournaled?:bool,parallelize?:bool,bypassReadOnly?:bool,preserveCache?:bool} $opts
455 * @codingStandardsIgnoreEnd
456 * @return StatusValue
457 */
458 final public function doOperations( array $ops, array $opts = [] ) {
459 if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
460 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
461 }
462 if ( $ops === [] ) {
463 return $this->newStatus(); // nothing to do
464 }
465
466 $ops = $this->resolveFSFileObjects( $ops );
467 if ( empty( $opts['force'] ) ) { // sanity
468 unset( $opts['nonLocking'] );
469 }
470
471 /** @noinspection PhpUnusedLocalVariableInspection */
472 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
473
474 return $this->doOperationsInternal( $ops, $opts );
475 }
476
477 /**
478 * @see FileBackend::doOperations()
479 * @param array $ops
480 * @param array $opts
481 * @return StatusValue
482 */
483 abstract protected function doOperationsInternal( array $ops, array $opts );
484
485 /**
486 * Same as doOperations() except it takes a single operation.
487 * If you are doing a batch of operations that should either
488 * all succeed or all fail, then use that function instead.
489 *
490 * @see FileBackend::doOperations()
491 *
492 * @param array $op Operation
493 * @param array $opts Operation options
494 * @return StatusValue
495 */
496 final public function doOperation( array $op, array $opts = [] ) {
497 return $this->doOperations( [ $op ], $opts );
498 }
499
500 /**
501 * Performs a single create operation.
502 * This sets $params['op'] to 'create' and passes it to doOperation().
503 *
504 * @see FileBackend::doOperation()
505 *
506 * @param array $params Operation parameters
507 * @param array $opts Operation options
508 * @return StatusValue
509 */
510 final public function create( array $params, array $opts = [] ) {
511 return $this->doOperation( [ 'op' => 'create' ] + $params, $opts );
512 }
513
514 /**
515 * Performs a single store operation.
516 * This sets $params['op'] to 'store' and passes it to doOperation().
517 *
518 * @see FileBackend::doOperation()
519 *
520 * @param array $params Operation parameters
521 * @param array $opts Operation options
522 * @return StatusValue
523 */
524 final public function store( array $params, array $opts = [] ) {
525 return $this->doOperation( [ 'op' => 'store' ] + $params, $opts );
526 }
527
528 /**
529 * Performs a single copy operation.
530 * This sets $params['op'] to 'copy' and passes it to doOperation().
531 *
532 * @see FileBackend::doOperation()
533 *
534 * @param array $params Operation parameters
535 * @param array $opts Operation options
536 * @return StatusValue
537 */
538 final public function copy( array $params, array $opts = [] ) {
539 return $this->doOperation( [ 'op' => 'copy' ] + $params, $opts );
540 }
541
542 /**
543 * Performs a single move operation.
544 * This sets $params['op'] to 'move' and passes it to doOperation().
545 *
546 * @see FileBackend::doOperation()
547 *
548 * @param array $params Operation parameters
549 * @param array $opts Operation options
550 * @return StatusValue
551 */
552 final public function move( array $params, array $opts = [] ) {
553 return $this->doOperation( [ 'op' => 'move' ] + $params, $opts );
554 }
555
556 /**
557 * Performs a single delete operation.
558 * This sets $params['op'] to 'delete' and passes it to doOperation().
559 *
560 * @see FileBackend::doOperation()
561 *
562 * @param array $params Operation parameters
563 * @param array $opts Operation options
564 * @return StatusValue
565 */
566 final public function delete( array $params, array $opts = [] ) {
567 return $this->doOperation( [ 'op' => 'delete' ] + $params, $opts );
568 }
569
570 /**
571 * Performs a single describe operation.
572 * This sets $params['op'] to 'describe' and passes it to doOperation().
573 *
574 * @see FileBackend::doOperation()
575 *
576 * @param array $params Operation parameters
577 * @param array $opts Operation options
578 * @return StatusValue
579 * @since 1.21
580 */
581 final public function describe( array $params, array $opts = [] ) {
582 return $this->doOperation( [ 'op' => 'describe' ] + $params, $opts );
583 }
584
585 /**
586 * Perform a set of independent file operations on some files.
587 *
588 * This does no locking, nor journaling, and possibly no stat calls.
589 * Any destination files that already exist will be overwritten.
590 * This should *only* be used on non-original files, like cache files.
591 *
592 * Supported operations and their parameters:
593 * - create
594 * - store
595 * - copy
596 * - move
597 * - delete
598 * - describe (since 1.21)
599 * - null
600 *
601 * FSFile/TempFSFile object support was added in 1.27.
602 *
603 * a) Create a new file in storage with the contents of a string
604 * @code
605 * [
606 * 'op' => 'create',
607 * 'dst' => <storage path>,
608 * 'content' => <string of new file contents>,
609 * 'headers' => <HTTP header name/value map> # since 1.21
610 * ]
611 * @endcode
612 *
613 * b) Copy a file system file into storage
614 * @code
615 * [
616 * 'op' => 'store',
617 * 'src' => <file system path, FSFile, or TempFSFile>,
618 * 'dst' => <storage path>,
619 * 'headers' => <HTTP header name/value map> # since 1.21
620 * ]
621 * @endcode
622 *
623 * c) Copy a file within storage
624 * @code
625 * [
626 * 'op' => 'copy',
627 * 'src' => <storage path>,
628 * 'dst' => <storage path>,
629 * 'ignoreMissingSource' => <boolean>, # since 1.21
630 * 'headers' => <HTTP header name/value map> # since 1.21
631 * ]
632 * @endcode
633 *
634 * d) Move a file within storage
635 * @code
636 * [
637 * 'op' => 'move',
638 * 'src' => <storage path>,
639 * 'dst' => <storage path>,
640 * 'ignoreMissingSource' => <boolean>, # since 1.21
641 * 'headers' => <HTTP header name/value map> # since 1.21
642 * ]
643 * @endcode
644 *
645 * e) Delete a file within storage
646 * @code
647 * [
648 * 'op' => 'delete',
649 * 'src' => <storage path>,
650 * 'ignoreMissingSource' => <boolean>
651 * ]
652 * @endcode
653 *
654 * f) Update metadata for a file within storage
655 * @code
656 * [
657 * 'op' => 'describe',
658 * 'src' => <storage path>,
659 * 'headers' => <HTTP header name/value map>
660 * ]
661 * @endcode
662 *
663 * g) Do nothing (no-op)
664 * @code
665 * [
666 * 'op' => 'null',
667 * ]
668 * @endcode
669 *
670 * @par Boolean flags for operations (operation-specific):
671 * - ignoreMissingSource : The operation will simply succeed and do
672 * nothing if the source file does not exist.
673 * - headers : If supplied with a header name/value map, the backend will
674 * reply with these headers when GETs/HEADs of the destination
675 * file are made. Header values should be smaller than 256 bytes.
676 * Content-Disposition headers can be longer, though the system
677 * might ignore or truncate ones that are too long to store.
678 * Existing headers will remain, but these will replace any
679 * conflicting previous headers, and headers will be removed
680 * if they are set to an empty string.
681 * Backends that don't support metadata ignore this. (since 1.21)
682 *
683 * $opts is an associative of boolean flags, including:
684 * - bypassReadOnly : Allow writes in read-only mode (since 1.20)
685 *
686 * @par Return value:
687 * This returns a Status, which contains all warnings and fatals that occurred
688 * during the operation. The 'failCount', 'successCount', and 'success' members
689 * will reflect each operation attempted for the given files. The StatusValue will be
690 * considered "OK" as long as no fatal errors occurred.
691 *
692 * @param array $ops Set of operations to execute
693 * @phan-param array{ignoreMissingSource?:bool,headers?:bool} $ops
694 * @param array $opts Batch operation options
695 * @phan-param array{bypassReadOnly?:bool} $opts
696 * @return StatusValue
697 * @since 1.20
698 */
699 final public function doQuickOperations( array $ops, array $opts = [] ) {
700 if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
701 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
702 }
703 if ( $ops === [] ) {
704 return $this->newStatus(); // nothing to do
705 }
706
707 $ops = $this->resolveFSFileObjects( $ops );
708 foreach ( $ops as &$op ) {
709 $op['overwrite'] = true; // avoids RTTs in key/value stores
710 }
711
712 /** @noinspection PhpUnusedLocalVariableInspection */
713 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
714
715 return $this->doQuickOperationsInternal( $ops );
716 }
717
718 /**
719 * @see FileBackend::doQuickOperations()
720 * @param array $ops
721 * @return StatusValue
722 * @since 1.20
723 */
724 abstract protected function doQuickOperationsInternal( array $ops );
725
726 /**
727 * Same as doQuickOperations() except it takes a single operation.
728 * If you are doing a batch of operations, then use that function instead.
729 *
730 * @see FileBackend::doQuickOperations()
731 *
732 * @param array $op Operation
733 * @return StatusValue
734 * @since 1.20
735 */
736 final public function doQuickOperation( array $op ) {
737 return $this->doQuickOperations( [ $op ] );
738 }
739
740 /**
741 * Performs a single quick create operation.
742 * This sets $params['op'] to 'create' and passes it to doQuickOperation().
743 *
744 * @see FileBackend::doQuickOperation()
745 *
746 * @param array $params Operation parameters
747 * @return StatusValue
748 * @since 1.20
749 */
750 final public function quickCreate( array $params ) {
751 return $this->doQuickOperation( [ 'op' => 'create' ] + $params );
752 }
753
754 /**
755 * Performs a single quick store operation.
756 * This sets $params['op'] to 'store' and passes it to doQuickOperation().
757 *
758 * @see FileBackend::doQuickOperation()
759 *
760 * @param array $params Operation parameters
761 * @return StatusValue
762 * @since 1.20
763 */
764 final public function quickStore( array $params ) {
765 return $this->doQuickOperation( [ 'op' => 'store' ] + $params );
766 }
767
768 /**
769 * Performs a single quick copy operation.
770 * This sets $params['op'] to 'copy' and passes it to doQuickOperation().
771 *
772 * @see FileBackend::doQuickOperation()
773 *
774 * @param array $params Operation parameters
775 * @return StatusValue
776 * @since 1.20
777 */
778 final public function quickCopy( array $params ) {
779 return $this->doQuickOperation( [ 'op' => 'copy' ] + $params );
780 }
781
782 /**
783 * Performs a single quick move operation.
784 * This sets $params['op'] to 'move' and passes it to doQuickOperation().
785 *
786 * @see FileBackend::doQuickOperation()
787 *
788 * @param array $params Operation parameters
789 * @return StatusValue
790 * @since 1.20
791 */
792 final public function quickMove( array $params ) {
793 return $this->doQuickOperation( [ 'op' => 'move' ] + $params );
794 }
795
796 /**
797 * Performs a single quick delete operation.
798 * This sets $params['op'] to 'delete' and passes it to doQuickOperation().
799 *
800 * @see FileBackend::doQuickOperation()
801 *
802 * @param array $params Operation parameters
803 * @return StatusValue
804 * @since 1.20
805 */
806 final public function quickDelete( array $params ) {
807 return $this->doQuickOperation( [ 'op' => 'delete' ] + $params );
808 }
809
810 /**
811 * Performs a single quick describe operation.
812 * This sets $params['op'] to 'describe' and passes it to doQuickOperation().
813 *
814 * @see FileBackend::doQuickOperation()
815 *
816 * @param array $params Operation parameters
817 * @return StatusValue
818 * @since 1.21
819 */
820 final public function quickDescribe( array $params ) {
821 return $this->doQuickOperation( [ 'op' => 'describe' ] + $params );
822 }
823
824 /**
825 * Concatenate a list of storage files into a single file system file.
826 * The target path should refer to a file that is already locked or
827 * otherwise safe from modification from other processes. Normally,
828 * the file will be a new temp file, which should be adequate.
829 *
830 * @param array $params Operation parameters, include:
831 * - srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
832 * - dst : file system path to 0-byte temp file
833 * - parallelize : try to do operations in parallel when possible
834 * @return StatusValue
835 */
836 abstract public function concatenate( array $params );
837
838 /**
839 * Prepare a storage directory for usage.
840 * This will create any required containers and parent directories.
841 * Backends using key/value stores only need to create the container.
842 *
843 * The 'noAccess' and 'noListing' parameters works the same as in secure(),
844 * except they are only applied *if* the directory/container had to be created.
845 * These flags should always be set for directories that have private files.
846 * However, setting them is not guaranteed to actually do anything.
847 * Additional server configuration may be needed to achieve the desired effect.
848 *
849 * @param array $params Parameters include:
850 * - dir : storage directory
851 * - noAccess : try to deny file access (since 1.20)
852 * - noListing : try to deny file listing (since 1.20)
853 * - bypassReadOnly : allow writes in read-only mode (since 1.20)
854 * @return StatusValue
855 */
856 final public function prepare( array $params ) {
857 if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
858 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
859 }
860 /** @noinspection PhpUnusedLocalVariableInspection */
861 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
862 return $this->doPrepare( $params );
863 }
864
865 /**
866 * @see FileBackend::prepare()
867 * @param array $params
868 * @return StatusValue
869 */
870 abstract protected function doPrepare( array $params );
871
872 /**
873 * Take measures to block web access to a storage directory and
874 * the container it belongs to. FS backends might add .htaccess
875 * files whereas key/value store backends might revoke container
876 * access to the storage user representing end-users in web requests.
877 *
878 * This is not guaranteed to actually make files or listings publicly hidden.
879 * Additional server configuration may be needed to achieve the desired effect.
880 *
881 * @param array $params Parameters include:
882 * - dir : storage directory
883 * - noAccess : try to deny file access
884 * - noListing : try to deny file listing
885 * - bypassReadOnly : allow writes in read-only mode (since 1.20)
886 * @return StatusValue
887 */
888 final public function secure( array $params ) {
889 if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
890 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
891 }
892 /** @noinspection PhpUnusedLocalVariableInspection */
893 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
894 return $this->doSecure( $params );
895 }
896
897 /**
898 * @see FileBackend::secure()
899 * @param array $params
900 * @return StatusValue
901 */
902 abstract protected function doSecure( array $params );
903
904 /**
905 * Remove measures to block web access to a storage directory and
906 * the container it belongs to. FS backends might remove .htaccess
907 * files whereas key/value store backends might grant container
908 * access to the storage user representing end-users in web requests.
909 * This essentially can undo the result of secure() calls.
910 *
911 * This is not guaranteed to actually make files or listings publicly viewable.
912 * Additional server configuration may be needed to achieve the desired effect.
913 *
914 * @param array $params Parameters include:
915 * - dir : storage directory
916 * - access : try to allow file access
917 * - listing : try to allow file listing
918 * - bypassReadOnly : allow writes in read-only mode (since 1.20)
919 * @return StatusValue
920 * @since 1.20
921 */
922 final public function publish( array $params ) {
923 if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
924 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
925 }
926 /** @noinspection PhpUnusedLocalVariableInspection */
927 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
928 return $this->doPublish( $params );
929 }
930
931 /**
932 * @see FileBackend::publish()
933 * @param array $params
934 * @return StatusValue
935 */
936 abstract protected function doPublish( array $params );
937
938 /**
939 * Delete a storage directory if it is empty.
940 * Backends using key/value stores may do nothing unless the directory
941 * is that of an empty container, in which case it will be deleted.
942 *
943 * @param array $params Parameters include:
944 * - dir : storage directory
945 * - recursive : recursively delete empty subdirectories first (since 1.20)
946 * - bypassReadOnly : allow writes in read-only mode (since 1.20)
947 * @return StatusValue
948 */
949 final public function clean( array $params ) {
950 if ( empty( $params['bypassReadOnly'] ) && $this->isReadOnly() ) {
951 return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
952 }
953 /** @noinspection PhpUnusedLocalVariableInspection */
954 $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
955 return $this->doClean( $params );
956 }
957
958 /**
959 * @see FileBackend::clean()
960 * @param array $params
961 * @return StatusValue
962 */
963 abstract protected function doClean( array $params );
964
965 /**
966 * Check if a file exists at a storage path in the backend.
967 * This returns false if only a directory exists at the path.
968 *
969 * Callers that only care if a file is readily accessible can use non-strict
970 * comparisons on the result. If "does not exist" and "existence is unknown"
971 * must be distinguished, then strict comparisons to true/null should be used.
972 *
973 * @see FileBackend::EXISTENCE_ERROR
974 * @see FileBackend::directoryExists()
975 *
976 * @param array $params Parameters include:
977 * - src : source storage path
978 * - latest : use the latest available data
979 * @return bool|null Whether the file exists or null (I/O error)
980 */
981 abstract public function fileExists( array $params );
982
983 /**
984 * Get the last-modified timestamp of the file at a storage path.
985 *
986 * @see FileBackend::TIMESTAMP_FAIL
987 *
988 * @param array $params Parameters include:
989 * - src : source storage path
990 * - latest : use the latest available data
991 * @return string|false TS_MW timestamp or false (missing file or I/O error)
992 */
993 abstract public function getFileTimestamp( array $params );
994
995 /**
996 * Get the contents of a file at a storage path in the backend.
997 * This should be avoided for potentially large files.
998 *
999 * @see FileBackend::CONTENT_FAIL
1000 *
1001 * @param array $params Parameters include:
1002 * - src : source storage path
1003 * - latest : use the latest available data
1004 * @return string|false Content string or false (missing file or I/O error)
1005 */
1006 final public function getFileContents( array $params ) {
1007 $contents = $this->getFileContentsMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
1008
1009 return $contents[$params['src']];
1010 }
1011
1012 /**
1013 * Like getFileContents() except it takes an array of storage paths
1014 * and returns an order preserved map of storage paths to their content.
1015 *
1016 * @see FileBackend::getFileContents()
1017 *
1018 * @param array $params Parameters include:
1019 * - srcs : list of source storage paths
1020 * - latest : use the latest available data
1021 * - parallelize : try to do operations in parallel when possible
1022 * @return string[]|false[] Map of (path name => file content or false on failure)
1023 * @since 1.20
1024 */
1025 abstract public function getFileContentsMulti( array $params );
1026
1027 /**
1028 * Get metadata about a file at a storage path in the backend.
1029 * If the file does not exist, then this returns false.
1030 * Otherwise, the result is an associative array that includes:
1031 * - headers : map of HTTP headers used for GET/HEAD requests (name => value)
1032 * - metadata : map of file metadata (name => value)
1033 * Metadata keys and headers names will be returned in all lower-case.
1034 * Additional values may be included for internal use only.
1035 *
1036 * Use FileBackend::hasFeatures() to check how well this is supported.
1037 *
1038 * @see FileBackend::XATTRS_FAIL
1039 *
1040 * @param array $params
1041 * $params include:
1042 * - src : source storage path
1043 * - latest : use the latest available data
1044 * @return array|false File metadata array or false (missing file or I/O error)
1045 * @since 1.23
1046 */
1047 abstract public function getFileXAttributes( array $params );
1048
1049 /**
1050 * Get the size (bytes) of a file at a storage path in the backend.
1051 *
1052 * @see FileBackend::SIZE_FAIL
1053 *
1054 * @param array $params Parameters include:
1055 * - src : source storage path
1056 * - latest : use the latest available data
1057 * @return int|false File size in bytes or false (missing file or I/O error)
1058 */
1059 abstract public function getFileSize( array $params );
1060
1061 /**
1062 * Get quick information about a file at a storage path in the backend.
1063 * If the file does not exist, then this returns false.
1064 * Otherwise, the result is an associative array that includes:
1065 * - mtime : the last-modified timestamp (TS_MW)
1066 * - size : the file size (bytes)
1067 * Additional values may be included for internal use only.
1068 *
1069 * @see FileBackend::STAT_ABSENT
1070 * @see FileBackend::STAT_ERROR
1071 *
1072 * @param array $params Parameters include:
1073 * - src : source storage path
1074 * - latest : use the latest available data
1075 * @return array|false|null Attribute map, false (missing file), or null (I/O error)
1076 */
1077 abstract public function getFileStat( array $params );
1078
1079 /**
1080 * Get a SHA-1 hash of the content of the file at a storage path in the backend.
1081 *
1082 * @see FileBackend::SHA1_FAIL
1083 *
1084 * @param array $params Parameters include:
1085 * - src : source storage path
1086 * - latest : use the latest available data
1087 * @return string|false Hash string or false (missing file or I/O error)
1088 */
1089 abstract public function getFileSha1Base36( array $params );
1090
1091 /**
1092 * Get the properties of the content of the file at a storage path in the backend.
1093 * This gives the result of FSFile::getProps() on a local copy of the file.
1094 *
1095 * @param array $params Parameters include:
1096 * - src : source storage path
1097 * - latest : use the latest available data
1098 * @return array Properties map; FSFile::placeholderProps() if file missing or on I/O error
1099 */
1100 abstract public function getFileProps( array $params );
1101
1102 /**
1103 * Stream the content of the file at a storage path in the backend.
1104 *
1105 * If the file does not exists, an HTTP 404 error will be given.
1106 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
1107 * will be sent if streaming began, while none will be sent otherwise.
1108 * Implementations should flush the output buffer before sending data.
1109 *
1110 * @param array $params Parameters include:
1111 * - src : source storage path
1112 * - headers : list of additional HTTP headers to send if the file exists
1113 * - options : HTTP request header map with lower case keys (since 1.28). Supports:
1114 * range : format is "bytes=(\d*-\d*)"
1115 * if-modified-since : format is an HTTP date
1116 * - headless : only include the body (and headers from "headers") (since 1.28)
1117 * - latest : use the latest available data
1118 * - allowOB : preserve any output buffers (since 1.28)
1119 * @return StatusValue
1120 */
1121 abstract public function streamFile( array $params );
1122
1123 /**
1124 * Returns a file system file, identical in content to the file at a storage path.
1125 * The file returned is either:
1126 * - a) A TempFSFile local copy of the file at a storage path in the backend.
1127 * The temporary copy will have the same extension as the source.
1128 * Temporary files may be purged when the file object falls out of scope.
1129 * - b) An FSFile pointing to the original file at a storage path in the backend.
1130 * This is applicable for backends layered directly on top of file systems.
1131 *
1132 * Never modify the returned file since it might be the original, it might be shared
1133 * among multiple callers of this method, or the backend might internally keep FSFile
1134 * references for deferred operations.
1135 *
1136 * @param array $params Parameters include:
1137 * - src : source storage path
1138 * - latest : use the latest available data
1139 * @return FSFile|null Local file copy or null (missing file or I/O error)
1140 */
1141 final public function getLocalReference( array $params ) {
1142 $fsFiles = $this->getLocalReferenceMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
1143
1144 return $fsFiles[$params['src']];
1145 }
1146
1147 /**
1148 * Like getLocalReference() except it takes an array of storage paths and
1149 * yields an order-preserved map of storage paths to temporary local file copies.
1150 *
1151 * Never modify the returned files since they might be originals, they might be shared
1152 * among multiple callers of this method, or the backend might internally keep FSFile
1153 * references for deferred operations.
1154 *
1155 * @see FileBackend::getLocalReference()
1156 *
1157 * @param array $params Parameters include:
1158 * - srcs : list of source storage paths
1159 * - latest : use the latest available data
1160 * - parallelize : try to do operations in parallel when possible
1161 * @return array Map of (path name => FSFile or null on failure)
1162 * @since 1.20
1163 */
1164 abstract public function getLocalReferenceMulti( array $params );
1165
1166 /**
1167 * Get a local copy on disk of the file at a storage path in the backend.
1168 * The temporary copy will have the same file extension as the source.
1169 * Temporary files may be purged when the file object falls out of scope.
1170 *
1171 * Multiple calls to this method for the same path will create new copies.
1172 *
1173 * @param array $params Parameters include:
1174 * - src : source storage path
1175 * - latest : use the latest available data
1176 * @return TempFSFile|null Temporary local file copy or null (missing file or I/O error)
1177 */
1178 final public function getLocalCopy( array $params ) {
1179 $tmpFiles = $this->getLocalCopyMulti( [ 'srcs' => [ $params['src'] ] ] + $params );
1180
1181 return $tmpFiles[$params['src']];
1182 }
1183
1184 /**
1185 * Like getLocalCopy() except it takes an array of storage paths and yields
1186 * an order preserved-map of storage paths to temporary local file copies.
1187 *
1188 * Multiple calls to this method for the same path will create new copies.
1189 *
1190 * @see FileBackend::getLocalCopy()
1191 *
1192 * @param array $params Parameters include:
1193 * - srcs : list of source storage paths
1194 * - latest : use the latest available data
1195 * - parallelize : try to do operations in parallel when possible
1196 * @return array Map of (path name => TempFSFile or null on failure)
1197 * @since 1.20
1198 */
1199 abstract public function getLocalCopyMulti( array $params );
1200
1201 /**
1202 * Return an HTTP URL to a given file that requires no authentication to use.
1203 * The URL may be pre-authenticated (via some token in the URL) and temporary.
1204 * This will return null if the backend cannot make an HTTP URL for the file.
1205 *
1206 * This is useful for key/value stores when using scripts that seek around
1207 * large files and those scripts (and the backend) support HTTP Range headers.
1208 * Otherwise, one would need to use getLocalReference(), which involves loading
1209 * the entire file on to local disk.
1210 *
1211 * @see FileBackend::TEMPURL_ERROR
1212 *
1213 * @param array $params Parameters include:
1214 * - src : source storage path
1215 * - ttl : lifetime (seconds) if pre-authenticated; default is 1 day
1216 * @return string|null URL or null (not supported or I/O error)
1217 * @since 1.21
1218 */
1219 abstract public function getFileHttpUrl( array $params );
1220
1221 /**
1222 * Check if a directory exists at a given storage path
1223 *
1224 * For backends using key/value stores, a directory is said to exist whenever
1225 * there exist any files with paths using the given directory path as a prefix
1226 * followed by a forward slash. For example, if there is a file called
1227 * "mwstore://backend/container/dir/path.svg" then directories are said to exist
1228 * at "mwstore://backend/container" and "mwstore://backend/container/dir". These
1229 * can be thought of as "virtual" directories.
1230 *
1231 * Backends that directly use a filesystem layer might enumerate empty directories.
1232 * The clean() method should always be used when files are deleted or moved if this
1233 * is a concern. This is a trade-off to avoid write amplication/contention on file
1234 * changes or read amplification when calling this method.
1235 *
1236 * Storage backends with eventual consistency might return stale data.
1237 *
1238 * @see FileBackend::EXISTENCE_ERROR
1239 * @see FileBackend::clean()
1240 *
1241 * @param array $params Parameters include:
1242 * - dir : storage directory
1243 * @return bool|null Whether a directory exists or null (I/O error)
1244 * @since 1.20
1245 */
1246 abstract public function directoryExists( array $params );
1247
1248 /**
1249 * Get an iterator to list *all* directories under a storage directory
1250 *
1251 * If the directory is of the form "mwstore://backend/container",
1252 * then all directories in the container will be listed.
1253 * If the directory is of form "mwstore://backend/container/dir",
1254 * then all directories directly under that directory will be listed.
1255 * Results will be storage directories relative to the given directory.
1256 *
1257 * Storage backends with eventual consistency might return stale data.
1258 *
1259 * Failures during iteration can result in FileBackendError exceptions (since 1.22).
1260 *
1261 * @see FileBackend::LIST_ERROR
1262 * @see FileBackend::directoryExists()
1263 *
1264 * @param array $params Parameters include:
1265 * - dir : storage directory
1266 * - topOnly : only return direct child dirs of the directory
1267 * @return Traversable|array|null Directory list enumerator or null (initial I/O error)
1268 * @since 1.20
1269 */
1270 abstract public function getDirectoryList( array $params );
1271
1272 /**
1273 * Same as FileBackend::getDirectoryList() except only lists
1274 * directories that are immediately under the given directory.
1275 *
1276 * Storage backends with eventual consistency might return stale data.
1277 *
1278 * Failures during iteration can result in FileBackendError exceptions (since 1.22).
1279 *
1280 * @see FileBackend::LIST_ERROR
1281 * @see FileBackend::directoryExists()
1282 *
1283 * @param array $params Parameters include:
1284 * - dir : storage directory
1285 * @return Traversable|array|null Directory list enumerator or null (initial I/O error)
1286 * @since 1.20
1287 */
1288 final public function getTopDirectoryList( array $params ) {
1289 return $this->getDirectoryList( [ 'topOnly' => true ] + $params );
1290 }
1291
1292 /**
1293 * Get an iterator to list *all* stored files under a storage directory
1294 *
1295 * If the directory is of the form "mwstore://backend/container", then all
1296 * files in the container will be listed. If the directory is of form
1297 * "mwstore://backend/container/dir", then all files under that directory will
1298 * be listed. Results will be storage paths relative to the given directory.
1299 *
1300 * Storage backends with eventual consistency might return stale data.
1301 *
1302 * Failures during iteration can result in FileBackendError exceptions (since 1.22).
1303 *
1304 * @see FileBackend::LIST_ERROR
1305 *
1306 * @param array $params Parameters include:
1307 * - dir : storage directory
1308 * - topOnly : only return direct child files of the directory (since 1.20)
1309 * - adviseStat : set to true if stat requests will be made on the files (since 1.22)
1310 * @return Traversable|array|null File list enumerator or null (initial I/O error)
1311 */
1312 abstract public function getFileList( array $params );
1313
1314 /**
1315 * Same as FileBackend::getFileList() except only lists
1316 * files that are immediately under the given directory.
1317 *
1318 * Storage backends with eventual consistency might return stale data.
1319 *
1320 * Failures during iteration can result in FileBackendError exceptions (since 1.22).
1321 *
1322 * @see FileBackend::LIST_ERROR
1323 *
1324 * @param array $params Parameters include:
1325 * - dir : storage directory
1326 * - adviseStat : set to true if stat requests will be made on the files (since 1.22)
1327 * @return Traversable|array|null File list enumerator or null on failure
1328 * @since 1.20
1329 */
1330 final public function getTopFileList( array $params ) {
1331 return $this->getFileList( [ 'topOnly' => true ] + $params );
1332 }
1333
1334 /**
1335 * Preload persistent file stat cache and property cache into in-process cache.
1336 * This should be used when stat calls will be made on a known list of a many files.
1337 *
1338 * @see FileBackend::getFileStat()
1339 *
1340 * @param array $paths Storage paths
1341 */
1342 abstract public function preloadCache( array $paths );
1343
1344 /**
1345 * Invalidate any in-process file stat and property cache.
1346 * If $paths is given, then only the cache for those files will be cleared.
1347 *
1348 * @see FileBackend::getFileStat()
1349 *
1350 * @param array|null $paths Storage paths (optional)
1351 */
1352 abstract public function clearCache( array $paths = null );
1353
1354 /**
1355 * Preload file stat information (concurrently if possible) into in-process cache.
1356 *
1357 * This should be used when stat calls will be made on a known list of a many files.
1358 * This does not make use of the persistent file stat cache.
1359 *
1360 * @see FileBackend::getFileStat()
1361 *
1362 * @param array $params Parameters include:
1363 * - srcs : list of source storage paths
1364 * - latest : use the latest available data
1365 * @return bool Whether all requests proceeded without I/O errors (since 1.24)
1366 * @since 1.23
1367 */
1368 abstract public function preloadFileStat( array $params );
1369
1370 /**
1371 * Lock the files at the given storage paths in the backend.
1372 * This will either lock all the files or none (on failure).
1373 *
1374 * Callers should consider using getScopedFileLocks() instead.
1375 *
1376 * @param array $paths Storage paths
1377 * @param int $type LockManager::LOCK_* constant
1378 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
1379 * @return StatusValue
1380 */
1381 final public function lockFiles( array $paths, $type, $timeout = 0 ) {
1382 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1383
1384 return $this->wrapStatus( $this->lockManager->lock( $paths, $type, $timeout ) );
1385 }
1386
1387 /**
1388 * Unlock the files at the given storage paths in the backend.
1389 *
1390 * @param array $paths Storage paths
1391 * @param int $type LockManager::LOCK_* constant
1392 * @return StatusValue
1393 */
1394 final public function unlockFiles( array $paths, $type ) {
1395 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1396
1397 return $this->wrapStatus( $this->lockManager->unlock( $paths, $type ) );
1398 }
1399
1400 /**
1401 * Lock the files at the given storage paths in the backend.
1402 * This will either lock all the files or none (on failure).
1403 * On failure, the StatusValue object will be updated with errors.
1404 *
1405 * Once the return value goes out scope, the locks will be released and
1406 * the StatusValue updated. Unlock fatals will not change the StatusValue "OK" value.
1407 *
1408 * @see ScopedLock::factory()
1409 *
1410 * @param array $paths List of storage paths or map of lock types to path lists
1411 * @param int|string $type LockManager::LOCK_* constant or "mixed"
1412 * @param StatusValue $status StatusValue to update on lock/unlock
1413 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.24)
1414 * @return ScopedLock|null RAII-style self-unlocking lock or null on failure
1415 */
1416 final public function getScopedFileLocks(
1417 array $paths, $type, StatusValue $status, $timeout = 0
1418 ) {
1419 if ( $type === 'mixed' ) {
1420 foreach ( $paths as &$typePaths ) {
1421 $typePaths = array_map( 'FileBackend::normalizeStoragePath', $typePaths );
1422 }
1423 } else {
1424 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1425 }
1426
1427 return ScopedLock::factory( $this->lockManager, $paths, $type, $status, $timeout );
1428 }
1429
1430 /**
1431 * Get an array of scoped locks needed for a batch of file operations.
1432 *
1433 * Normally, FileBackend::doOperations() handles locking, unless
1434 * the 'nonLocking' param is passed in. This function is useful if you
1435 * want the files to be locked for a broader scope than just when the
1436 * files are changing. For example, if you need to update DB metadata,
1437 * you may want to keep the files locked until finished.
1438 *
1439 * @see FileBackend::doOperations()
1440 *
1441 * @param array $ops List of file operations to FileBackend::doOperations()
1442 * @param StatusValue $status StatusValue to update on lock/unlock
1443 * @return ScopedLock|null RAII-style self-unlocking lock or null on failure
1444 * @since 1.20
1445 */
1446 abstract public function getScopedLocksForOps( array $ops, StatusValue $status );
1447
1448 /**
1449 * Get the root storage path of this backend.
1450 * All container paths are "subdirectories" of this path.
1451 *
1452 * @return string Storage path
1453 * @since 1.20
1454 */
1455 final public function getRootStoragePath() {
1456 return "mwstore://{$this->name}";
1457 }
1458
1459 /**
1460 * Get the storage path for the given container for this backend
1461 *
1462 * @param string $container Container name
1463 * @return string Storage path
1464 * @since 1.21
1465 */
1466 final public function getContainerStoragePath( $container ) {
1467 return $this->getRootStoragePath() . "/{$container}";
1468 }
1469
1470 /**
1471 * Get the file journal object for this backend
1472 *
1473 * @return FileJournal
1474 */
1475 final public function getJournal() {
1476 return $this->fileJournal;
1477 }
1478
1479 /**
1480 * Convert FSFile 'src' paths to string paths (with an 'srcRef' field set to the FSFile)
1481 *
1482 * The 'srcRef' field keeps any TempFSFile objects in scope for the backend to have it
1483 * around as long it needs (which may vary greatly depending on configuration)
1484 *
1485 * @param array $ops File operation batch for FileBaclend::doOperations()
1486 * @return array File operation batch
1487 */
1488 protected function resolveFSFileObjects( array $ops ) {
1489 foreach ( $ops as &$op ) {
1490 $src = $op['src'] ?? null;
1491 if ( $src instanceof FSFile ) {
1492 $op['srcRef'] = $src;
1493 $op['src'] = $src->getPath();
1494 }
1495 }
1496 unset( $op );
1497
1498 return $ops;
1499 }
1500
1501 /**
1502 * Check if a given path is a "mwstore://" path.
1503 * This does not do any further validation or any existence checks.
1504 *
1505 * @param string $path
1506 * @return bool
1507 */
1508 final public static function isStoragePath( $path ) {
1509 return ( strpos( $path, 'mwstore://' ) === 0 );
1510 }
1511
1512 /**
1513 * Split a storage path into a backend name, a container name,
1514 * and a relative file path. The relative path may be the empty string.
1515 * This does not do any path normalization or traversal checks.
1516 *
1517 * @param string $storagePath
1518 * @return array (backend, container, rel object) or (null, null, null)
1519 */
1520 final public static function splitStoragePath( $storagePath ) {
1521 if ( self::isStoragePath( $storagePath ) ) {
1522 // Remove the "mwstore://" prefix and split the path
1523 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
1524 if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
1525 if ( count( $parts ) == 3 ) {
1526 return $parts; // e.g. "backend/container/path"
1527 } else {
1528 return [ $parts[0], $parts[1], '' ]; // e.g. "backend/container"
1529 }
1530 }
1531 }
1532
1533 return [ null, null, null ];
1534 }
1535
1536 /**
1537 * Normalize a storage path by cleaning up directory separators.
1538 * Returns null if the path is not of the format of a valid storage path.
1539 *
1540 * @param string $storagePath
1541 * @return string|null Normalized storage path or null on failure
1542 */
1543 final public static function normalizeStoragePath( $storagePath ) {
1544 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
1545 if ( $relPath !== null ) { // must be for this backend
1546 $relPath = self::normalizeContainerPath( $relPath );
1547 if ( $relPath !== null ) {
1548 return ( $relPath != '' )
1549 ? "mwstore://{$backend}/{$container}/{$relPath}"
1550 : "mwstore://{$backend}/{$container}";
1551 }
1552 }
1553
1554 return null;
1555 }
1556
1557 /**
1558 * Get the parent storage directory of a storage path.
1559 * This returns a path like "mwstore://backend/container",
1560 * "mwstore://backend/container/...", or null if there is no parent.
1561 *
1562 * @param string $storagePath
1563 * @return string|null Parent storage path or null on failure
1564 */
1565 final public static function parentStoragePath( $storagePath ) {
1566 $storagePath = dirname( $storagePath );
1567 list( , , $rel ) = self::splitStoragePath( $storagePath );
1568
1569 return ( $rel === null ) ? null : $storagePath;
1570 }
1571
1572 /**
1573 * Get the final extension from a storage or FS path
1574 *
1575 * @param string $path
1576 * @param string $case One of (rawcase, uppercase, lowercase) (since 1.24)
1577 * @return string
1578 */
1579 final public static function extensionFromPath( $path, $case = 'lowercase' ) {
1580 $i = strrpos( $path, '.' );
1581 $ext = $i ? substr( $path, $i + 1 ) : '';
1582
1583 if ( $case === 'lowercase' ) {
1584 $ext = strtolower( $ext );
1585 } elseif ( $case === 'uppercase' ) {
1586 $ext = strtoupper( $ext );
1587 }
1588
1589 return $ext;
1590 }
1591
1592 /**
1593 * Check if a relative path has no directory traversals
1594 *
1595 * @param string $path
1596 * @return bool
1597 * @since 1.20
1598 */
1599 final public static function isPathTraversalFree( $path ) {
1600 return ( self::normalizeContainerPath( $path ) !== null );
1601 }
1602
1603 /**
1604 * Build a Content-Disposition header value per RFC 6266.
1605 *
1606 * @param string $type One of (attachment, inline)
1607 * @param string $filename Suggested file name (should not contain slashes)
1608 * @throws InvalidArgumentException
1609 * @return string
1610 * @since 1.20
1611 */
1612 final public static function makeContentDisposition( $type, $filename = '' ) {
1613 $parts = [];
1614
1615 $type = strtolower( $type );
1616 if ( !in_array( $type, [ 'inline', 'attachment' ] ) ) {
1617 throw new InvalidArgumentException( "Invalid Content-Disposition type '$type'." );
1618 }
1619 $parts[] = $type;
1620
1621 if ( strlen( $filename ) ) {
1622 $parts[] = "filename*=UTF-8''" . rawurlencode( basename( $filename ) );
1623 }
1624
1625 return implode( ';', $parts );
1626 }
1627
1628 /**
1629 * Validate and normalize a relative storage path.
1630 * Null is returned if the path involves directory traversal.
1631 * Traversal is insecure for FS backends and broken for others.
1632 *
1633 * This uses the same traversal protection as Title::secureAndSplit().
1634 *
1635 * @param string $path Storage path relative to a container
1636 * @return string|null Normalized container path or null on failure
1637 */
1638 final protected static function normalizeContainerPath( $path ) {
1639 // Normalize directory separators
1640 $path = strtr( $path, '\\', '/' );
1641 // Collapse any consecutive directory separators
1642 $path = preg_replace( '![/]{2,}!', '/', $path );
1643 // Remove any leading directory separator
1644 $path = ltrim( $path, '/' );
1645 // Use the same traversal protection as Title::secureAndSplit()
1646 if ( strpos( $path, '.' ) !== false ) {
1647 if (
1648 $path === '.' ||
1649 $path === '..' ||
1650 strpos( $path, './' ) === 0 ||
1651 strpos( $path, '../' ) === 0 ||
1652 strpos( $path, '/./' ) !== false ||
1653 strpos( $path, '/../' ) !== false
1654 ) {
1655 return null;
1656 }
1657 }
1658
1659 return $path;
1660 }
1661
1662 /**
1663 * Yields the result of the status wrapper callback on either:
1664 * - StatusValue::newGood() if this method is called without parameters
1665 * - StatusValue::newFatal() with all parameters to this method if passed in
1666 *
1667 * @param string ...$args
1668 * @return StatusValue
1669 */
1670 final protected function newStatus( ...$args ) {
1671 if ( count( $args ) ) {
1672 $sv = StatusValue::newFatal( ...$args );
1673 } else {
1674 $sv = StatusValue::newGood();
1675 }
1676
1677 return $this->wrapStatus( $sv );
1678 }
1679
1680 /**
1681 * @param StatusValue $sv
1682 * @return StatusValue Modified status or StatusValue subclass
1683 */
1684 final protected function wrapStatus( StatusValue $sv ) {
1685 return $this->statusWrapper ? call_user_func( $this->statusWrapper, $sv ) : $sv;
1686 }
1687
1688 /**
1689 * @param string $section
1690 * @return ScopedCallback|null
1691 */
1692 protected function scopedProfileSection( $section ) {
1693 return $this->profiler ? ( $this->profiler )( $section ) : null;
1694 }
1695
1696 protected function resetOutputBuffer() {
1697 while ( ob_get_status() ) {
1698 if ( !ob_end_clean() ) {
1699 // Could not remove output buffer handler; abort now
1700 // to avoid getting in some kind of infinite loop.
1701 break;
1702 }
1703 }
1704 }
1705 }