79da6e17d7ca80c161e3536ad891381e6616d1b1
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackend.php
1 <?php
2 /**
3 * @defgroup FileBackend File backend
4 * @ingroup FileRepo
5 *
6 * This module regroup classes meant for MediaWiki to interacts with
7 */
8
9 /**
10 * @file
11 * @ingroup FileBackend
12 * @author Aaron Schulz
13 */
14
15 /**
16 * Base class for all file backend classes (including multi-write backends).
17 *
18 * This class defines the methods as abstract that subclasses must implement.
19 * Outside callers can assume that all backends will have these functions.
20 *
21 * All "storage paths" are of the format "mwstore://backend/container/path".
22 * The paths use UNIX file system (FS) notation, though any particular backend may
23 * not actually be using a local filesystem. Therefore, the paths are only virtual.
24 *
25 * Backend contents are stored under wiki-specific container names by default.
26 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
27 * segregation must be done by setting the container paths appropriately.
28 *
29 * FS-based backends are somewhat more restrictive due to the existence of real
30 * directory files; a regular file cannot have the same name as a directory. Other
31 * backends with virtual directories may not have this limitation. Callers should
32 * store files in such a way that no files and directories are under the same path.
33 *
34 * Methods should avoid throwing exceptions at all costs.
35 * As a corollary, external dependencies should be kept to a minimum.
36 *
37 * @ingroup FileBackend
38 * @since 1.19
39 */
40 abstract class FileBackend {
41 protected $name; // string; unique backend name
42 protected $wikiId; // string; unique wiki name
43 protected $readOnly; // string; read-only explanation message
44 /** @var LockManager */
45 protected $lockManager;
46
47 /**
48 * Create a new backend instance from configuration.
49 * This should only be called from within FileBackendGroup.
50 *
51 * $config includes:
52 * 'name' : The unique name of this backend.
53 * This should consist of alphanumberic, '-', and '_' characters.
54 * 'wikiId' : Prefix to container names that is unique to this wiki.
55 * This should consist of alphanumberic, '-', and '_' characters.
56 * 'lockManager' : Registered name of a file lock manager to use.
57 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
58 * It should be an explanation for the backend being read-only.
59 *
60 * @param $config Array
61 */
62 public function __construct( array $config ) {
63 $this->name = $config['name'];
64 $this->wikiId = isset( $config['wikiId'] )
65 ? $config['wikiId']
66 : wfWikiID(); // e.g. "my_wiki-en_"
67 $this->lockManager = ( $config['lockManager'] instanceof LockManager )
68 ? $config['lockManager']
69 : LockManagerGroup::singleton()->get( $config['lockManager'] );
70 $this->readOnly = isset( $config['readOnly'] )
71 ? (string)$config['readOnly']
72 : '';
73 }
74
75 /**
76 * Get the unique backend name.
77 * We may have multiple different backends of the same type.
78 * For example, we can have two Swift backends using different proxies.
79 *
80 * @return string
81 */
82 final public function getName() {
83 return $this->name;
84 }
85
86 /**
87 * Check if this backend is read-only
88 *
89 * @return bool
90 */
91 final public function isReadOnly() {
92 return ( $this->readOnly != '' );
93 }
94
95 /**
96 * Get an explanatory message if this backend is read-only
97 *
98 * @return string|false Returns falls if the backend is not read-only
99 */
100 final public function getReadOnlyReason() {
101 return ( $this->readOnly != '' ) ? $this->readOnly : false;
102 }
103
104 /**
105 * This is the main entry point into the backend for write operations.
106 * Callers supply an ordered list of operations to perform as a transaction.
107 * Files will be locked, the stat cache cleared, and then the operations attempted.
108 * If any serious errors occur, all attempted operations will be rolled back.
109 *
110 * $ops is an array of arrays. The outer array holds a list of operations.
111 * Each inner array is a set of key value pairs that specify an operation.
112 *
113 * Supported operations and their parameters:
114 * a) Create a new file in storage with the contents of a string
115 * array(
116 * 'op' => 'create',
117 * 'dst' => <storage path>,
118 * 'content' => <string of new file contents>,
119 * 'overwrite' => <boolean>,
120 * 'overwriteSame' => <boolean>
121 * )
122 * b) Copy a file system file into storage
123 * array(
124 * 'op' => 'store',
125 * 'src' => <file system path>,
126 * 'dst' => <storage path>,
127 * 'overwrite' => <boolean>,
128 * 'overwriteSame' => <boolean>
129 * )
130 * c) Copy a file within storage
131 * array(
132 * 'op' => 'copy',
133 * 'src' => <storage path>,
134 * 'dst' => <storage path>,
135 * 'overwrite' => <boolean>,
136 * 'overwriteSame' => <boolean>
137 * )
138 * d) Move a file within storage
139 * array(
140 * 'op' => 'move',
141 * 'src' => <storage path>,
142 * 'dst' => <storage path>,
143 * 'overwrite' => <boolean>,
144 * 'overwriteSame' => <boolean>
145 * )
146 * e) Delete a file within storage
147 * array(
148 * 'op' => 'delete',
149 * 'src' => <storage path>,
150 * 'ignoreMissingSource' => <boolean>
151 * )
152 * f) Do nothing (no-op)
153 * array(
154 * 'op' => 'null',
155 * )
156 *
157 * Boolean flags for operations (operation-specific):
158 * 'ignoreMissingSource' : The operation will simply succeed and do
159 * nothing if the source file does not exist.
160 * 'overwrite' : Any destination file will be overwritten.
161 * 'overwriteSame' : An error will not be given if a file already
162 * exists at the destination that has the same
163 * contents as the new contents to be written there.
164 *
165 * $opts is an associative of boolean flags, including:
166 * 'force' : Errors that would normally cause a rollback do not.
167 * The remaining operations are still attempted if any fail.
168 * 'nonLocking' : No locks are acquired for the operations.
169 * This can increase performance for non-critical writes.
170 * This has no effect unless the 'force' flag is set.
171 * 'allowStale' : Don't require the latest available data.
172 * This can increase performance for non-critical writes.
173 * This has no effect unless the 'force' flag is set.
174 *
175 * Remarks on locking:
176 * File system paths given to operations should refer to files that are
177 * already locked or otherwise safe from modification from other processes.
178 * Normally these files will be new temp files, which should be adequate.
179 *
180 * Return value:
181 * This returns a Status, which contains all warnings and fatals that occured
182 * during the operation. The 'failCount', 'successCount', and 'success' members
183 * will reflect each operation attempted. The status will be "OK" unless any
184 * of the operations failed and the 'force' parameter was not set.
185 *
186 * @param $ops Array List of operations to execute in order
187 * @param $opts Array Batch operation options
188 * @return Status
189 */
190 final public function doOperations( array $ops, array $opts = array() ) {
191 if ( $this->isReadOnly() ) {
192 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
193 }
194 if ( empty( $opts['force'] ) ) { // sanity
195 unset( $opts['nonLocking'] );
196 unset( $opts['allowStale'] );
197 }
198 return $this->doOperationsInternal( $ops, $opts );
199 }
200
201 /**
202 * @see FileBackend::doOperations()
203 */
204 abstract protected function doOperationsInternal( array $ops, array $opts );
205
206 /**
207 * Same as doOperations() except it takes a single operation.
208 * If you are doing a batch of operations that should either
209 * all succeed or all fail, then use that function instead.
210 *
211 * @see FileBackend::doOperations()
212 *
213 * @param $op Array Operation
214 * @param $opts Array Operation options
215 * @return Status
216 */
217 final public function doOperation( array $op, array $opts = array() ) {
218 return $this->doOperations( array( $op ), $opts );
219 }
220
221 /**
222 * Performs a single create operation.
223 * This sets $params['op'] to 'create' and passes it to doOperation().
224 *
225 * @see FileBackend::doOperation()
226 *
227 * @param $params Array Operation parameters
228 * @param $opts Array Operation options
229 * @return Status
230 */
231 final public function create( array $params, array $opts = array() ) {
232 $params['op'] = 'create';
233 return $this->doOperation( $params, $opts );
234 }
235
236 /**
237 * Performs a single store operation.
238 * This sets $params['op'] to 'store' and passes it to doOperation().
239 *
240 * @see FileBackend::doOperation()
241 *
242 * @param $params Array Operation parameters
243 * @param $opts Array Operation options
244 * @return Status
245 */
246 final public function store( array $params, array $opts = array() ) {
247 $params['op'] = 'store';
248 return $this->doOperation( $params, $opts );
249 }
250
251 /**
252 * Performs a single copy operation.
253 * This sets $params['op'] to 'copy' and passes it to doOperation().
254 *
255 * @see FileBackend::doOperation()
256 *
257 * @param $params Array Operation parameters
258 * @param $opts Array Operation options
259 * @return Status
260 */
261 final public function copy( array $params, array $opts = array() ) {
262 $params['op'] = 'copy';
263 return $this->doOperation( $params, $opts );
264 }
265
266 /**
267 * Performs a single move operation.
268 * This sets $params['op'] to 'move' and passes it to doOperation().
269 *
270 * @see FileBackend::doOperation()
271 *
272 * @param $params Array Operation parameters
273 * @param $opts Array Operation options
274 * @return Status
275 */
276 final public function move( array $params, array $opts = array() ) {
277 $params['op'] = 'move';
278 return $this->doOperation( $params, $opts );
279 }
280
281 /**
282 * Performs a single delete operation.
283 * This sets $params['op'] to 'delete' and passes it to doOperation().
284 *
285 * @see FileBackend::doOperation()
286 *
287 * @param $params Array Operation parameters
288 * @param $opts Array Operation options
289 * @return Status
290 */
291 final public function delete( array $params, array $opts = array() ) {
292 $params['op'] = 'delete';
293 return $this->doOperation( $params, $opts );
294 }
295
296 /**
297 * Concatenate a list of storage files into a single file system file.
298 * The target path should refer to a file that is already locked or
299 * otherwise safe from modification from other processes. Normally,
300 * the file will be a new temp file, which should be adequate.
301 * $params include:
302 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
303 * dst : file system path to 0-byte temp file
304 *
305 * @param $params Array Operation parameters
306 * @return Status
307 */
308 abstract public function concatenate( array $params );
309
310 /**
311 * Prepare a storage directory for usage.
312 * This will create any required containers and parent directories.
313 * Backends using key/value stores only need to create the container.
314 *
315 * $params include:
316 * dir : storage directory
317 *
318 * @param $params Array
319 * @return Status
320 */
321 final public function prepare( array $params ) {
322 if ( $this->isReadOnly() ) {
323 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
324 }
325 return $this->doPrepare( $params );
326 }
327
328 /**
329 * @see FileBackend::prepare()
330 */
331 abstract protected function doPrepare( array $params );
332
333 /**
334 * Take measures to block web access to a storage directory and
335 * the container it belongs to. FS backends might add .htaccess
336 * files whereas key/value store backends might restrict container
337 * access to the auth user that represents end-users in web request.
338 * This is not guaranteed to actually do anything.
339 *
340 * $params include:
341 * dir : storage directory
342 * noAccess : try to deny file access
343 * noListing : try to deny file listing
344 *
345 * @param $params Array
346 * @return Status
347 */
348 final public function secure( array $params ) {
349 if ( $this->isReadOnly() ) {
350 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
351 }
352 $status = $this->doPrepare( $params ); // dir must exist to restrict it
353 if ( $status->isOK() ) {
354 $status->merge( $this->doSecure( $params ) );
355 }
356 return $status;
357 }
358
359 /**
360 * @see FileBackend::secure()
361 */
362 abstract protected function doSecure( array $params );
363
364 /**
365 * Delete a storage directory if it is empty.
366 * Backends using key/value stores may do nothing unless the directory
367 * is that of an empty container, in which case it should be deleted.
368 *
369 * $params include:
370 * dir : storage directory
371 *
372 * @param $params Array
373 * @return Status
374 */
375 final public function clean( array $params ) {
376 if ( $this->isReadOnly() ) {
377 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
378 }
379 return $this->doClean( $params );
380 }
381
382 /**
383 * @see FileBackend::clean()
384 */
385 abstract protected function doClean( array $params );
386
387 /**
388 * Check if a file exists at a storage path in the backend.
389 * This returns false if only a directory exists at the path.
390 *
391 * $params include:
392 * src : source storage path
393 * latest : use the latest available data
394 *
395 * @param $params Array
396 * @return bool|null Returns null on failure
397 */
398 abstract public function fileExists( array $params );
399
400 /**
401 * Get the last-modified timestamp of the file at a storage path.
402 *
403 * $params include:
404 * src : source storage path
405 * latest : use the latest available data
406 *
407 * @param $params Array
408 * @return string|false TS_MW timestamp or false on failure
409 */
410 abstract public function getFileTimestamp( array $params );
411
412 /**
413 * Get the contents of a file at a storage path in the backend.
414 * This should be avoided for potentially large files.
415 *
416 * $params include:
417 * src : source storage path
418 * latest : use the latest available data
419 *
420 * @param $params Array
421 * @return string|false Returns false on failure
422 */
423 abstract public function getFileContents( array $params );
424
425 /**
426 * Get the size (bytes) of a file at a storage path in the backend.
427 *
428 * $params include:
429 * src : source storage path
430 * latest : use the latest available data
431 *
432 * @param $params Array
433 * @return integer|false Returns false on failure
434 */
435 abstract public function getFileSize( array $params );
436
437 /**
438 * Get quick information about a file at a storage path in the backend.
439 * If the file does not exist, then this returns false.
440 * Otherwise, the result is an associative array that includes:
441 * mtime : the last-modified timestamp (TS_MW)
442 * size : the file size (bytes)
443 * Additional values may be included for internal use only.
444 *
445 * $params include:
446 * src : source storage path
447 * latest : use the latest available data
448 *
449 * @param $params Array
450 * @return Array|false|null Returns null on failure
451 */
452 abstract public function getFileStat( array $params );
453
454 /**
455 * Get a SHA-1 hash of the file at a storage path in the backend.
456 *
457 * $params include:
458 * src : source storage path
459 * latest : use the latest available data
460 *
461 * @param $params Array
462 * @return string|false Hash string or false on failure
463 */
464 abstract public function getFileSha1Base36( array $params );
465
466 /**
467 * Get the properties of the file at a storage path in the backend.
468 * Returns FSFile::placeholderProps() on failure.
469 *
470 * $params include:
471 * src : source storage path
472 * latest : use the latest available data
473 *
474 * @param $params Array
475 * @return Array
476 */
477 abstract public function getFileProps( array $params );
478
479 /**
480 * Stream the file at a storage path in the backend.
481 * If the file does not exists, a 404 error will be given.
482 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
483 * must be sent if streaming began, while none should be sent otherwise.
484 * Implementations should flush the output buffer before sending data.
485 *
486 * $params include:
487 * src : source storage path
488 * headers : additional HTTP headers to send on success
489 * latest : use the latest available data
490 *
491 * @param $params Array
492 * @return Status
493 */
494 abstract public function streamFile( array $params );
495
496 /**
497 * Returns a file system file, identical to the file at a storage path.
498 * The file returned is either:
499 * a) A local copy of the file at a storage path in the backend.
500 * The temporary copy will have the same extension as the source.
501 * b) An original of the file at a storage path in the backend.
502 * Temporary files may be purged when the file object falls out of scope.
503 *
504 * Write operations should *never* be done on this file as some backends
505 * may do internal tracking or may be instances of FileBackendMultiWrite.
506 * In that later case, there are copies of the file that must stay in sync.
507 * Additionally, further calls to this function may return the same file.
508 *
509 * $params include:
510 * src : source storage path
511 * latest : use the latest available data
512 *
513 * @param $params Array
514 * @return FSFile|null Returns null on failure
515 */
516 abstract public function getLocalReference( array $params );
517
518 /**
519 * Get a local copy on disk of the file at a storage path in the backend.
520 * The temporary copy will have the same file extension as the source.
521 * Temporary files may be purged when the file object falls out of scope.
522 *
523 * $params include:
524 * src : source storage path
525 * latest : use the latest available data
526 *
527 * @param $params Array
528 * @return TempFSFile|null Returns null on failure
529 */
530 abstract public function getLocalCopy( array $params );
531
532 /**
533 * Get an iterator to list out all stored files under a storage directory.
534 * If the directory is of the form "mwstore://backend/container",
535 * then all files in the container should be listed.
536 * If the directory is of form "mwstore://backend/container/dir",
537 * then all files under that container directory should be listed.
538 * Results should be storage paths relative to the given directory.
539 *
540 * Storage backends with eventual consistency might return stale data.
541 *
542 * $params include:
543 * dir : storage path directory
544 *
545 * @return Traversable|Array|null Returns null on failure
546 */
547 abstract public function getFileList( array $params );
548
549 /**
550 * Invalidate any in-process file existence and property cache.
551 * If $paths is given, then only the cache for those files will be cleared.
552 *
553 * @param $paths Array Storage paths (optional)
554 * @return void
555 */
556 public function clearCache( array $paths = null ) {}
557
558 /**
559 * Lock the files at the given storage paths in the backend.
560 * This will either lock all the files or none (on failure).
561 *
562 * Callers should consider using getScopedFileLocks() instead.
563 *
564 * @param $paths Array Storage paths
565 * @param $type integer LockManager::LOCK_* constant
566 * @return Status
567 */
568 final public function lockFiles( array $paths, $type ) {
569 return $this->lockManager->lock( $paths, $type );
570 }
571
572 /**
573 * Unlock the files at the given storage paths in the backend.
574 *
575 * @param $paths Array Storage paths
576 * @param $type integer LockManager::LOCK_* constant
577 * @return Status
578 */
579 final public function unlockFiles( array $paths, $type ) {
580 return $this->lockManager->unlock( $paths, $type );
581 }
582
583 /**
584 * Lock the files at the given storage paths in the backend.
585 * This will either lock all the files or none (on failure).
586 * On failure, the status object will be updated with errors.
587 *
588 * Once the return value goes out scope, the locks will be released and
589 * the status updated. Unlock fatals will not change the status "OK" value.
590 *
591 * @param $paths Array Storage paths
592 * @param $type integer LockManager::LOCK_* constant
593 * @param $status Status Status to update on lock/unlock
594 * @return ScopedLock|null Returns null on failure
595 */
596 final public function getScopedFileLocks( array $paths, $type, Status $status ) {
597 return ScopedLock::factory( $this->lockManager, $paths, $type, $status );
598 }
599
600 /**
601 * Check if a given path is a "mwstore://" path.
602 * This does not do any further validation or any existence checks.
603 *
604 * @param $path string
605 * @return bool
606 */
607 final public static function isStoragePath( $path ) {
608 return ( strpos( $path, 'mwstore://' ) === 0 );
609 }
610
611 /**
612 * Split a storage path into a backend name, a container name,
613 * and a relative file path. The relative path may be the empty string.
614 * This does not do any path normalization or traversal checks.
615 *
616 * @param $storagePath string
617 * @return Array (backend, container, rel object) or (null, null, null)
618 */
619 final public static function splitStoragePath( $storagePath ) {
620 if ( self::isStoragePath( $storagePath ) ) {
621 // Remove the "mwstore://" prefix and split the path
622 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
623 if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
624 if ( count( $parts ) == 3 ) {
625 return $parts; // e.g. "backend/container/path"
626 } else {
627 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
628 }
629 }
630 }
631 return array( null, null, null );
632 }
633
634 /**
635 * Normalize a storage path by cleaning up directory separators.
636 * Returns null if the path is not of the format of a valid storage path.
637 *
638 * @param $storagePath string
639 * @return string|null
640 */
641 final public static function normalizeStoragePath( $storagePath ) {
642 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
643 if ( $relPath !== null ) { // must be for this backend
644 $relPath = self::normalizeContainerPath( $relPath );
645 if ( $relPath !== null ) {
646 return ( $relPath != '' )
647 ? "mwstore://{$backend}/{$container}/{$relPath}"
648 : "mwstore://{$backend}/{$container}";
649 }
650 }
651 return null;
652 }
653
654 /**
655 * Validate and normalize a relative storage path.
656 * Null is returned if the path involves directory traversal.
657 * Traversal is insecure for FS backends and broken for others.
658 *
659 * @param $path string Storage path relative to a container
660 * @return string|null
661 */
662 final protected static function normalizeContainerPath( $path ) {
663 // Normalize directory separators
664 $path = strtr( $path, '\\', '/' );
665 // Collapse any consecutive directory separators
666 $path = preg_replace( '![/]{2,}!', '/', $path );
667 // Remove any leading directory separator
668 $path = ltrim( $path, '/' );
669 // Use the same traversal protection as Title::secureAndSplit()
670 if ( strpos( $path, '.' ) !== false ) {
671 if (
672 $path === '.' ||
673 $path === '..' ||
674 strpos( $path, './' ) === 0 ||
675 strpos( $path, '../' ) === 0 ||
676 strpos( $path, '/./' ) !== false ||
677 strpos( $path, '/../' ) !== false
678 ) {
679 return null;
680 }
681 }
682 return $path;
683 }
684
685 /**
686 * Get the parent storage directory of a storage path.
687 * This returns a path like "mwstore://backend/container",
688 * "mwstore://backend/container/...", or null if there is no parent.
689 *
690 * @param $storagePath string
691 * @return string|null
692 */
693 final public static function parentStoragePath( $storagePath ) {
694 $storagePath = dirname( $storagePath );
695 list( $b, $cont, $rel ) = self::splitStoragePath( $storagePath );
696 return ( $rel === null ) ? null : $storagePath;
697 }
698
699 /**
700 * Get the final extension from a storage or FS path
701 *
702 * @param $path string
703 * @return string
704 */
705 final public static function extensionFromPath( $path ) {
706 $i = strrpos( $path, '.' );
707 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
708 }
709 }
710
711 /**
712 * @brief Base class for all backends associated with a particular storage medium.
713 *
714 * This class defines the methods as abstract that subclasses must implement.
715 * Outside callers should *not* use functions with "Internal" in the name.
716 *
717 * The FileBackend operations are implemented using basic functions
718 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
719 * This class is also responsible for path resolution and sanitization.
720 *
721 * @ingroup FileBackend
722 * @since 1.19
723 */
724 abstract class FileBackendStore extends FileBackend {
725 /** @var Array Map of paths to small (RAM/disk) cache items */
726 protected $cache = array(); // (storage path => key => value)
727 protected $maxCacheSize = 100; // integer; max paths with entries
728 /** @var Array Map of paths to large (RAM/disk) cache items */
729 protected $expensiveCache = array(); // (storage path => key => value)
730 protected $maxExpensiveCacheSize = 10; // integer; max paths with entries
731
732 /** @var Array Map of container names to sharding settings */
733 protected $shardViaHashLevels = array(); // (container name => config array)
734
735 protected $maxFileSize = 1000000000; // integer bytes (1GB)
736
737 /**
738 * Get the maximum allowable file size given backend
739 * medium restrictions and basic performance constraints.
740 * Do not call this function from places outside FileBackend and FileOp.
741 *
742 * @return integer Bytes
743 */
744 final public function maxFileSizeInternal() {
745 return $this->maxFileSize;
746 }
747
748 /**
749 * Check if a file can be created at a given storage path.
750 * FS backends should check if the parent directory exists and the file is writable.
751 * Backends using key/value stores should check if the container exists.
752 *
753 * @param $storagePath string
754 * @return bool
755 */
756 abstract public function isPathUsableInternal( $storagePath );
757
758 /**
759 * Create a file in the backend with the given contents.
760 * Do not call this function from places outside FileBackend and FileOp.
761 *
762 * $params include:
763 * content : the raw file contents
764 * dst : destination storage path
765 * overwrite : overwrite any file that exists at the destination
766 *
767 * @param $params Array
768 * @return Status
769 */
770 final public function createInternal( array $params ) {
771 wfProfileIn( __METHOD__ );
772 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
773 $status = Status::newFatal( 'backend-fail-create', $params['dst'] );
774 } else {
775 $status = $this->doCreateInternal( $params );
776 $this->clearCache( array( $params['dst'] ) );
777 }
778 wfProfileOut( __METHOD__ );
779 return $status;
780 }
781
782 /**
783 * @see FileBackendStore::createInternal()
784 */
785 abstract protected function doCreateInternal( array $params );
786
787 /**
788 * Store a file into the backend from a file on disk.
789 * Do not call this function from places outside FileBackend and FileOp.
790 *
791 * $params include:
792 * src : source path on disk
793 * dst : destination storage path
794 * overwrite : overwrite any file that exists at the destination
795 *
796 * @param $params Array
797 * @return Status
798 */
799 final public function storeInternal( array $params ) {
800 wfProfileIn( __METHOD__ );
801 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
802 $status = Status::newFatal( 'backend-fail-store', $params['dst'] );
803 } else {
804 $status = $this->doStoreInternal( $params );
805 $this->clearCache( array( $params['dst'] ) );
806 }
807 wfProfileOut( __METHOD__ );
808 return $status;
809 }
810
811 /**
812 * @see FileBackendStore::storeInternal()
813 */
814 abstract protected function doStoreInternal( array $params );
815
816 /**
817 * Copy a file from one storage path to another in the backend.
818 * Do not call this function from places outside FileBackend and FileOp.
819 *
820 * $params include:
821 * src : source storage path
822 * dst : destination storage path
823 * overwrite : overwrite any file that exists at the destination
824 *
825 * @param $params Array
826 * @return Status
827 */
828 final public function copyInternal( array $params ) {
829 wfProfileIn( __METHOD__ );
830 $status = $this->doCopyInternal( $params );
831 $this->clearCache( array( $params['dst'] ) );
832 wfProfileOut( __METHOD__ );
833 return $status;
834 }
835
836 /**
837 * @see FileBackendStore::copyInternal()
838 */
839 abstract protected function doCopyInternal( array $params );
840
841 /**
842 * Delete a file at the storage path.
843 * Do not call this function from places outside FileBackend and FileOp.
844 *
845 * $params include:
846 * src : source storage path
847 * ignoreMissingSource : do nothing if the source file does not exist
848 *
849 * @param $params Array
850 * @return Status
851 */
852 final public function deleteInternal( array $params ) {
853 wfProfileIn( __METHOD__ );
854 $status = $this->doDeleteInternal( $params );
855 $this->clearCache( array( $params['src'] ) );
856 wfProfileOut( __METHOD__ );
857 return $status;
858 }
859
860 /**
861 * @see FileBackendStore::deleteInternal()
862 */
863 abstract protected function doDeleteInternal( array $params );
864
865 /**
866 * Move a file from one storage path to another in the backend.
867 * Do not call this function from places outside FileBackend and FileOp.
868 *
869 * $params include:
870 * src : source storage path
871 * dst : destination storage path
872 * overwrite : overwrite any file that exists at the destination
873 *
874 * @param $params Array
875 * @return Status
876 */
877 final public function moveInternal( array $params ) {
878 wfProfileIn( __METHOD__ );
879 $status = $this->doMoveInternal( $params );
880 $this->clearCache( array( $params['src'], $params['dst'] ) );
881 wfProfileOut( __METHOD__ );
882 return $status;
883 }
884
885 /**
886 * @see FileBackendStore::moveInternal()
887 */
888 protected function doMoveInternal( array $params ) {
889 // Copy source to dest
890 $status = $this->copyInternal( $params );
891 if ( $status->isOK() ) {
892 // Delete source (only fails due to races or medium going down)
893 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
894 $status->setResult( true, $status->value ); // ignore delete() errors
895 }
896 return $status;
897 }
898
899 /**
900 * @see FileBackend::concatenate()
901 */
902 final public function concatenate( array $params ) {
903 wfProfileIn( __METHOD__ );
904 $status = Status::newGood();
905
906 // Try to lock the source files for the scope of this function
907 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
908 if ( $status->isOK() ) {
909 // Actually do the concatenation
910 $status->merge( $this->doConcatenate( $params ) );
911 }
912
913 wfProfileOut( __METHOD__ );
914 return $status;
915 }
916
917 /**
918 * @see FileBackendStore::concatenate()
919 */
920 protected function doConcatenate( array $params ) {
921 $status = Status::newGood();
922 $tmpPath = $params['dst']; // convenience
923
924 // Check that the specified temp file is valid...
925 wfSuppressWarnings();
926 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
927 wfRestoreWarnings();
928 if ( !$ok ) { // not present or not empty
929 $status->fatal( 'backend-fail-opentemp', $tmpPath );
930 return $status;
931 }
932
933 // Build up the temp file using the source chunks (in order)...
934 $tmpHandle = fopen( $tmpPath, 'ab' );
935 if ( $tmpHandle === false ) {
936 $status->fatal( 'backend-fail-opentemp', $tmpPath );
937 return $status;
938 }
939 foreach ( $params['srcs'] as $virtualSource ) {
940 // Get a local FS version of the chunk
941 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
942 if ( !$tmpFile ) {
943 $status->fatal( 'backend-fail-read', $virtualSource );
944 return $status;
945 }
946 // Get a handle to the local FS version
947 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
948 if ( $sourceHandle === false ) {
949 fclose( $tmpHandle );
950 $status->fatal( 'backend-fail-read', $virtualSource );
951 return $status;
952 }
953 // Append chunk to file (pass chunk size to avoid magic quotes)
954 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
955 fclose( $sourceHandle );
956 fclose( $tmpHandle );
957 $status->fatal( 'backend-fail-writetemp', $tmpPath );
958 return $status;
959 }
960 fclose( $sourceHandle );
961 }
962 if ( !fclose( $tmpHandle ) ) {
963 $status->fatal( 'backend-fail-closetemp', $tmpPath );
964 return $status;
965 }
966
967 clearstatcache(); // temp file changed
968
969 return $status;
970 }
971
972 /**
973 * @see FileBackend::doPrepare()
974 */
975 final protected function doPrepare( array $params ) {
976 wfProfileIn( __METHOD__ );
977
978 $status = Status::newGood();
979 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
980 if ( $dir === null ) {
981 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
982 wfProfileOut( __METHOD__ );
983 return $status; // invalid storage path
984 }
985
986 if ( $shard !== null ) { // confined to a single container/shard
987 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
988 } else { // directory is on several shards
989 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
990 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
991 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
992 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
993 }
994 }
995
996 wfProfileOut( __METHOD__ );
997 return $status;
998 }
999
1000 /**
1001 * @see FileBackendStore::doPrepare()
1002 */
1003 protected function doPrepareInternal( $container, $dir, array $params ) {
1004 return Status::newGood();
1005 }
1006
1007 /**
1008 * @see FileBackend::doSecure()
1009 */
1010 final protected function doSecure( array $params ) {
1011 wfProfileIn( __METHOD__ );
1012 $status = Status::newGood();
1013
1014 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1015 if ( $dir === null ) {
1016 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1017 wfProfileOut( __METHOD__ );
1018 return $status; // invalid storage path
1019 }
1020
1021 if ( $shard !== null ) { // confined to a single container/shard
1022 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
1023 } else { // directory is on several shards
1024 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
1025 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
1026 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1027 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1028 }
1029 }
1030
1031 wfProfileOut( __METHOD__ );
1032 return $status;
1033 }
1034
1035 /**
1036 * @see FileBackendStore::doSecure()
1037 */
1038 protected function doSecureInternal( $container, $dir, array $params ) {
1039 return Status::newGood();
1040 }
1041
1042 /**
1043 * @see FileBackend::doClean()
1044 */
1045 final protected function doClean( array $params ) {
1046 wfProfileIn( __METHOD__ );
1047 $status = Status::newGood();
1048
1049 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1050 if ( $dir === null ) {
1051 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1052 wfProfileOut( __METHOD__ );
1053 return $status; // invalid storage path
1054 }
1055
1056 // Attempt to lock this directory...
1057 $filesLockEx = array( $params['dir'] );
1058 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
1059 if ( !$status->isOK() ) {
1060 wfProfileOut( __METHOD__ );
1061 return $status; // abort
1062 }
1063
1064 if ( $shard !== null ) { // confined to a single container/shard
1065 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
1066 } else { // directory is on several shards
1067 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
1068 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
1069 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1070 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1071 }
1072 }
1073
1074 wfProfileOut( __METHOD__ );
1075 return $status;
1076 }
1077
1078 /**
1079 * @see FileBackendStore::doClean()
1080 */
1081 protected function doCleanInternal( $container, $dir, array $params ) {
1082 return Status::newGood();
1083 }
1084
1085 /**
1086 * @see FileBackend::fileExists()
1087 */
1088 final public function fileExists( array $params ) {
1089 wfProfileIn( __METHOD__ );
1090 $stat = $this->getFileStat( $params );
1091 wfProfileOut( __METHOD__ );
1092 return ( $stat === null ) ? null : (bool)$stat; // null => failure
1093 }
1094
1095 /**
1096 * @see FileBackend::getFileTimestamp()
1097 */
1098 final public function getFileTimestamp( array $params ) {
1099 wfProfileIn( __METHOD__ );
1100 $stat = $this->getFileStat( $params );
1101 wfProfileOut( __METHOD__ );
1102 return $stat ? $stat['mtime'] : false;
1103 }
1104
1105 /**
1106 * @see FileBackend::getFileSize()
1107 */
1108 final public function getFileSize( array $params ) {
1109 wfProfileIn( __METHOD__ );
1110 $stat = $this->getFileStat( $params );
1111 wfProfileOut( __METHOD__ );
1112 return $stat ? $stat['size'] : false;
1113 }
1114
1115 /**
1116 * @see FileBackend::getFileStat()
1117 */
1118 final public function getFileStat( array $params ) {
1119 wfProfileIn( __METHOD__ );
1120 $path = self::normalizeStoragePath( $params['src'] );
1121 if ( $path === null ) {
1122 return false; // invalid storage path
1123 }
1124 $latest = !empty( $params['latest'] );
1125 if ( isset( $this->cache[$path]['stat'] ) ) {
1126 // If we want the latest data, check that this cached
1127 // value was in fact fetched with the latest available data.
1128 if ( !$latest || $this->cache[$path]['stat']['latest'] ) {
1129 wfProfileOut( __METHOD__ );
1130 return $this->cache[$path]['stat'];
1131 }
1132 }
1133 $stat = $this->doGetFileStat( $params );
1134 if ( is_array( $stat ) ) { // don't cache negatives
1135 $this->trimCache(); // limit memory
1136 $this->cache[$path]['stat'] = $stat;
1137 $this->cache[$path]['stat']['latest'] = $latest;
1138 }
1139 wfProfileOut( __METHOD__ );
1140 return $stat;
1141 }
1142
1143 /**
1144 * @see FileBackendStore::getFileStat()
1145 */
1146 abstract protected function doGetFileStat( array $params );
1147
1148 /**
1149 * @see FileBackend::getFileContents()
1150 */
1151 public function getFileContents( array $params ) {
1152 wfProfileIn( __METHOD__ );
1153 $tmpFile = $this->getLocalReference( $params );
1154 if ( !$tmpFile ) {
1155 wfProfileOut( __METHOD__ );
1156 return false;
1157 }
1158 wfSuppressWarnings();
1159 $data = file_get_contents( $tmpFile->getPath() );
1160 wfRestoreWarnings();
1161 wfProfileOut( __METHOD__ );
1162 return $data;
1163 }
1164
1165 /**
1166 * @see FileBackend::getFileSha1Base36()
1167 */
1168 final public function getFileSha1Base36( array $params ) {
1169 wfProfileIn( __METHOD__ );
1170 $path = $params['src'];
1171 if ( isset( $this->cache[$path]['sha1'] ) ) {
1172 wfProfileOut( __METHOD__ );
1173 return $this->cache[$path]['sha1'];
1174 }
1175 $hash = $this->doGetFileSha1Base36( $params );
1176 if ( $hash ) { // don't cache negatives
1177 $this->trimCache(); // limit memory
1178 $this->cache[$path]['sha1'] = $hash;
1179 }
1180 wfProfileOut( __METHOD__ );
1181 return $hash;
1182 }
1183
1184 /**
1185 * @see FileBackendStore::getFileSha1Base36()
1186 */
1187 protected function doGetFileSha1Base36( array $params ) {
1188 $fsFile = $this->getLocalReference( $params );
1189 if ( !$fsFile ) {
1190 return false;
1191 } else {
1192 return $fsFile->getSha1Base36();
1193 }
1194 }
1195
1196 /**
1197 * @see FileBackend::getFileProps()
1198 */
1199 final public function getFileProps( array $params ) {
1200 wfProfileIn( __METHOD__ );
1201 $fsFile = $this->getLocalReference( $params );
1202 $props = $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
1203 wfProfileOut( __METHOD__ );
1204 return $props;
1205 }
1206
1207 /**
1208 * @see FileBackend::getLocalReference()
1209 */
1210 public function getLocalReference( array $params ) {
1211 wfProfileIn( __METHOD__ );
1212 $path = $params['src'];
1213 if ( isset( $this->expensiveCache[$path]['localRef'] ) ) {
1214 wfProfileOut( __METHOD__ );
1215 return $this->expensiveCache[$path]['localRef'];
1216 }
1217 $tmpFile = $this->getLocalCopy( $params );
1218 if ( $tmpFile ) { // don't cache negatives
1219 $this->trimExpensiveCache(); // limit memory
1220 $this->expensiveCache[$path]['localRef'] = $tmpFile;
1221 }
1222 wfProfileOut( __METHOD__ );
1223 return $tmpFile;
1224 }
1225
1226 /**
1227 * @see FileBackend::streamFile()
1228 */
1229 final public function streamFile( array $params ) {
1230 wfProfileIn( __METHOD__ );
1231 $status = Status::newGood();
1232
1233 $info = $this->getFileStat( $params );
1234 if ( !$info ) { // let StreamFile handle the 404
1235 $status->fatal( 'backend-fail-notexists', $params['src'] );
1236 }
1237
1238 // Set output buffer and HTTP headers for stream
1239 $extraHeaders = $params['headers'] ? $params['headers'] : array();
1240 $res = StreamFile::prepareForStream( $params['src'], $info, $extraHeaders );
1241 if ( $res == StreamFile::NOT_MODIFIED ) {
1242 // do nothing; client cache is up to date
1243 } elseif ( $res == StreamFile::READY_STREAM ) {
1244 $status = $this->doStreamFile( $params );
1245 } else {
1246 $status->fatal( 'backend-fail-stream', $params['src'] );
1247 }
1248
1249 wfProfileOut( __METHOD__ );
1250 return $status;
1251 }
1252
1253 /**
1254 * @see FileBackendStore::streamFile()
1255 */
1256 protected function doStreamFile( array $params ) {
1257 $status = Status::newGood();
1258
1259 $fsFile = $this->getLocalReference( $params );
1260 if ( !$fsFile ) {
1261 $status->fatal( 'backend-fail-stream', $params['src'] );
1262 } elseif ( !readfile( $fsFile->getPath() ) ) {
1263 $status->fatal( 'backend-fail-stream', $params['src'] );
1264 }
1265
1266 return $status;
1267 }
1268
1269 /**
1270 * @copydoc FileBackend::getFileList()
1271 */
1272 final public function getFileList( array $params ) {
1273 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1274 if ( $dir === null ) { // invalid storage path
1275 return null;
1276 }
1277 if ( $shard !== null ) {
1278 // File listing is confined to a single container/shard
1279 return $this->getFileListInternal( $fullCont, $dir, $params );
1280 } else {
1281 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
1282 // File listing spans multiple containers/shards
1283 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
1284 return new FileBackendStoreShardListIterator( $this,
1285 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
1286 }
1287 }
1288
1289 /**
1290 * Do not call this function from places outside FileBackend
1291 *
1292 * @see FileBackendStore::getFileList()
1293 *
1294 * @param $container string Resolved container name
1295 * @param $dir string Resolved path relative to container
1296 * @param $params Array
1297 * @return Traversable|Array|null
1298 */
1299 abstract public function getFileListInternal( $container, $dir, array $params );
1300
1301 /**
1302 * Get the list of supported operations and their corresponding FileOp classes.
1303 *
1304 * @return Array
1305 */
1306 protected function supportedOperations() {
1307 return array(
1308 'store' => 'StoreFileOp',
1309 'copy' => 'CopyFileOp',
1310 'move' => 'MoveFileOp',
1311 'delete' => 'DeleteFileOp',
1312 'create' => 'CreateFileOp',
1313 'null' => 'NullFileOp'
1314 );
1315 }
1316
1317 /**
1318 * Return a list of FileOp objects from a list of operations.
1319 * Do not call this function from places outside FileBackend.
1320 *
1321 * The result must have the same number of items as the input.
1322 * An exception is thrown if an unsupported operation is requested.
1323 *
1324 * @param $ops Array Same format as doOperations()
1325 * @return Array List of FileOp objects
1326 * @throws MWException
1327 */
1328 final public function getOperations( array $ops ) {
1329 $supportedOps = $this->supportedOperations();
1330
1331 $performOps = array(); // array of FileOp objects
1332 // Build up ordered array of FileOps...
1333 foreach ( $ops as $operation ) {
1334 $opName = $operation['op'];
1335 if ( isset( $supportedOps[$opName] ) ) {
1336 $class = $supportedOps[$opName];
1337 // Get params for this operation
1338 $params = $operation;
1339 // Append the FileOp class
1340 $performOps[] = new $class( $this, $params );
1341 } else {
1342 throw new MWException( "Operation `$opName` is not supported." );
1343 }
1344 }
1345
1346 return $performOps;
1347 }
1348
1349 /**
1350 * @see FileBackend::doOperationsInternal()
1351 */
1352 protected function doOperationsInternal( array $ops, array $opts ) {
1353 wfProfileIn( __METHOD__ );
1354 $status = Status::newGood();
1355
1356 // Build up a list of FileOps...
1357 $performOps = $this->getOperations( $ops );
1358
1359 // Acquire any locks as needed...
1360 if ( empty( $opts['nonLocking'] ) ) {
1361 // Build up a list of files to lock...
1362 $filesLockEx = $filesLockSh = array();
1363 foreach ( $performOps as $fileOp ) {
1364 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1365 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1366 }
1367 // Optimization: if doing an EX lock anyway, don't also set an SH one
1368 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1369 // Get a shared lock on the parent directory of each path changed
1370 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1371 // Try to lock those files for the scope of this function...
1372 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status );
1373 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
1374 if ( !$status->isOK() ) {
1375 wfProfileOut( __METHOD__ );
1376 return $status; // abort
1377 }
1378 }
1379
1380 // Clear any cache entries (after locks acquired)
1381 $this->clearCache();
1382
1383 // Actually attempt the operation batch...
1384 $subStatus = FileOp::attemptBatch( $performOps, $opts );
1385
1386 // Merge errors into status fields
1387 $status->merge( $subStatus );
1388 $status->success = $subStatus->success; // not done in merge()
1389
1390 wfProfileOut( __METHOD__ );
1391 return $status;
1392 }
1393
1394 /**
1395 * @see FileBackend::clearCache()
1396 */
1397 final public function clearCache( array $paths = null ) {
1398 if ( is_array( $paths ) ) {
1399 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1400 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1401 }
1402 if ( $paths === null ) {
1403 $this->cache = array();
1404 $this->expensiveCache = array();
1405 } else {
1406 foreach ( $paths as $path ) {
1407 unset( $this->cache[$path] );
1408 unset( $this->expensiveCache[$path] );
1409 }
1410 }
1411 $this->doClearCache( $paths );
1412 }
1413
1414 /**
1415 * Clears any additional stat caches for storage paths
1416 *
1417 * @see FileBackend::clearCache()
1418 *
1419 * @param $paths Array Storage paths (optional)
1420 * @return void
1421 */
1422 protected function doClearCache( array $paths = null ) {}
1423
1424 /**
1425 * Prune the inexpensive cache if it is too big to add an item
1426 *
1427 * @return void
1428 */
1429 protected function trimCache() {
1430 if ( count( $this->cache ) >= $this->maxCacheSize ) {
1431 reset( $this->cache );
1432 unset( $this->cache[key( $this->cache )] );
1433 }
1434 }
1435
1436 /**
1437 * Prune the expensive cache if it is too big to add an item
1438 *
1439 * @return void
1440 */
1441 protected function trimExpensiveCache() {
1442 if ( count( $this->expensiveCache ) >= $this->maxExpensiveCacheSize ) {
1443 reset( $this->expensiveCache );
1444 unset( $this->expensiveCache[key( $this->expensiveCache )] );
1445 }
1446 }
1447
1448 /**
1449 * Check if a container name is valid.
1450 * This checks for for length and illegal characters.
1451 *
1452 * @param $container string
1453 * @return bool
1454 */
1455 final protected static function isValidContainerName( $container ) {
1456 // This accounts for Swift and S3 restrictions while leaving room
1457 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1458 // This disallows directory separators or traversal characters.
1459 // Note that matching strings URL encode to the same string;
1460 // in Swift, the length restriction is *after* URL encoding.
1461 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1462 }
1463
1464 /**
1465 * Splits a storage path into an internal container name,
1466 * an internal relative file name, and a container shard suffix.
1467 * Any shard suffix is already appended to the internal container name.
1468 * This also checks that the storage path is valid and within this backend.
1469 *
1470 * If the container is sharded but a suffix could not be determined,
1471 * this means that the path can only refer to a directory and can only
1472 * be scanned by looking in all the container shards.
1473 *
1474 * @param $storagePath string
1475 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1476 */
1477 final protected function resolveStoragePath( $storagePath ) {
1478 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
1479 if ( $backend === $this->name ) { // must be for this backend
1480 $relPath = self::normalizeContainerPath( $relPath );
1481 if ( $relPath !== null ) {
1482 // Get shard for the normalized path if this container is sharded
1483 $cShard = $this->getContainerShard( $container, $relPath );
1484 // Validate and sanitize the relative path (backend-specific)
1485 $relPath = $this->resolveContainerPath( $container, $relPath );
1486 if ( $relPath !== null ) {
1487 // Prepend any wiki ID prefix to the container name
1488 $container = $this->fullContainerName( $container );
1489 if ( self::isValidContainerName( $container ) ) {
1490 // Validate and sanitize the container name (backend-specific)
1491 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1492 if ( $container !== null ) {
1493 return array( $container, $relPath, $cShard );
1494 }
1495 }
1496 }
1497 }
1498 }
1499 return array( null, null, null );
1500 }
1501
1502 /**
1503 * Like resolveStoragePath() except null values are returned if
1504 * the container is sharded and the shard could not be determined.
1505 *
1506 * @see FileBackendStore::resolveStoragePath()
1507 *
1508 * @param $storagePath string
1509 * @return Array (container, path) or (null, null) if invalid
1510 */
1511 final protected function resolveStoragePathReal( $storagePath ) {
1512 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1513 if ( $cShard !== null ) {
1514 return array( $container, $relPath );
1515 }
1516 return array( null, null );
1517 }
1518
1519 /**
1520 * Get the container name shard suffix for a given path.
1521 * Any empty suffix means the container is not sharded.
1522 *
1523 * @param $container string Container name
1524 * @param $relStoragePath string Storage path relative to the container
1525 * @return string|null Returns null if shard could not be determined
1526 */
1527 final protected function getContainerShard( $container, $relPath ) {
1528 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1529 if ( $levels == 1 || $levels == 2 ) {
1530 // Hash characters are either base 16 or 36
1531 $char = ( $base == 36 ) ? '[0-9a-z]' : '[0-9a-f]';
1532 // Get a regex that represents the shard portion of paths.
1533 // The concatenation of the captures gives us the shard.
1534 if ( $levels === 1 ) { // 16 or 36 shards per container
1535 $hashDirRegex = '(' . $char . ')';
1536 } else { // 256 or 1296 shards per container
1537 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1538 $hashDirRegex = $char . '/(' . $char . '{2})';
1539 } else { // short hash dir format (e.g. "a/b/c")
1540 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1541 }
1542 }
1543 // Allow certain directories to be above the hash dirs so as
1544 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1545 // They must be 2+ chars to avoid any hash directory ambiguity.
1546 $m = array();
1547 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1548 return '.' . implode( '', array_slice( $m, 1 ) );
1549 }
1550 return null; // failed to match
1551 }
1552 return ''; // no sharding
1553 }
1554
1555 /**
1556 * Get the sharding config for a container.
1557 * If greater than 0, then all file storage paths within
1558 * the container are required to be hashed accordingly.
1559 *
1560 * @param $container string
1561 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1562 */
1563 final protected function getContainerHashLevels( $container ) {
1564 if ( isset( $this->shardViaHashLevels[$container] ) ) {
1565 $config = $this->shardViaHashLevels[$container];
1566 $hashLevels = (int)$config['levels'];
1567 if ( $hashLevels == 1 || $hashLevels == 2 ) {
1568 $hashBase = (int)$config['base'];
1569 if ( $hashBase == 16 || $hashBase == 36 ) {
1570 return array( $hashLevels, $hashBase, $config['repeat'] );
1571 }
1572 }
1573 }
1574 return array( 0, 0, false ); // no sharding
1575 }
1576
1577 /**
1578 * Get a list of full container shard suffixes for a container
1579 *
1580 * @param $container string
1581 * @return Array
1582 */
1583 final protected function getContainerSuffixes( $container ) {
1584 $shards = array();
1585 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1586 if ( $digits > 0 ) {
1587 $numShards = pow( $base, $digits );
1588 for ( $index = 0; $index < $numShards; $index++ ) {
1589 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1590 }
1591 }
1592 return $shards;
1593 }
1594
1595 /**
1596 * Get the full container name, including the wiki ID prefix
1597 *
1598 * @param $container string
1599 * @return string
1600 */
1601 final protected function fullContainerName( $container ) {
1602 if ( $this->wikiId != '' ) {
1603 return "{$this->wikiId}-$container";
1604 } else {
1605 return $container;
1606 }
1607 }
1608
1609 /**
1610 * Resolve a container name, checking if it's allowed by the backend.
1611 * This is intended for internal use, such as encoding illegal chars.
1612 * Subclasses can override this to be more restrictive.
1613 *
1614 * @param $container string
1615 * @return string|null
1616 */
1617 protected function resolveContainerName( $container ) {
1618 return $container;
1619 }
1620
1621 /**
1622 * Resolve a relative storage path, checking if it's allowed by the backend.
1623 * This is intended for internal use, such as encoding illegal chars or perhaps
1624 * getting absolute paths (e.g. FS based backends). Note that the relative path
1625 * may be the empty string (e.g. the path is simply to the container).
1626 *
1627 * @param $container string Container name
1628 * @param $relStoragePath string Storage path relative to the container
1629 * @return string|null Path or null if not valid
1630 */
1631 protected function resolveContainerPath( $container, $relStoragePath ) {
1632 return $relStoragePath;
1633 }
1634 }
1635
1636 /**
1637 * FileBackendStore helper function to handle file listings that span container shards.
1638 * Do not use this class from places outside of FileBackendStore.
1639 *
1640 * @ingroup FileBackend
1641 */
1642 class FileBackendStoreShardListIterator implements Iterator {
1643 /* @var FileBackendStore */
1644 protected $backend;
1645 /* @var Array */
1646 protected $params;
1647 /* @var Array */
1648 protected $shardSuffixes;
1649 protected $container; // string
1650 protected $directory; // string
1651
1652 /* @var Traversable */
1653 protected $iter;
1654 protected $curShard = 0; // integer
1655 protected $pos = 0; // integer
1656
1657 /**
1658 * @param $backend FileBackendStore
1659 * @param $container string Full storage container name
1660 * @param $dir string Storage directory relative to container
1661 * @param $suffixes Array List of container shard suffixes
1662 * @param $params Array
1663 */
1664 public function __construct(
1665 FileBackendStore $backend, $container, $dir, array $suffixes, array $params
1666 ) {
1667 $this->backend = $backend;
1668 $this->container = $container;
1669 $this->directory = $dir;
1670 $this->shardSuffixes = $suffixes;
1671 $this->params = $params;
1672 }
1673
1674 public function current() {
1675 if ( is_array( $this->iter ) ) {
1676 return current( $this->iter );
1677 } else {
1678 return $this->iter->current();
1679 }
1680 }
1681
1682 public function key() {
1683 return $this->pos;
1684 }
1685
1686 public function next() {
1687 ++$this->pos;
1688 if ( is_array( $this->iter ) ) {
1689 next( $this->iter );
1690 } else {
1691 $this->iter->next();
1692 }
1693 // Find the next non-empty shard if no elements are left
1694 $this->nextShardIteratorIfNotValid();
1695 }
1696
1697 /**
1698 * If the iterator for this container shard is out of items,
1699 * then move on to the next container that has items.
1700 * If there are none, then it advances to the last container.
1701 */
1702 protected function nextShardIteratorIfNotValid() {
1703 while ( !$this->valid() ) {
1704 if ( ++$this->curShard >= count( $this->shardSuffixes ) ) {
1705 break; // no more container shards
1706 }
1707 $this->setIteratorFromCurrentShard();
1708 }
1709 }
1710
1711 protected function setIteratorFromCurrentShard() {
1712 $suffix = $this->shardSuffixes[$this->curShard];
1713 $this->iter = $this->backend->getFileListInternal(
1714 "{$this->container}{$suffix}", $this->directory, $this->params );
1715 }
1716
1717 public function rewind() {
1718 $this->pos = 0;
1719 $this->curShard = 0;
1720 $this->setIteratorFromCurrentShard();
1721 // Find the next non-empty shard if this one has no elements
1722 $this->nextShardIteratorIfNotValid();
1723 }
1724
1725 public function valid() {
1726 if ( $this->iter == null ) {
1727 return false; // some failure?
1728 } elseif ( is_array( $this->iter ) ) {
1729 return ( current( $this->iter ) !== false ); // no paths can have this value
1730 } else {
1731 return $this->iter->valid();
1732 }
1733 }
1734 }