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