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