MediaWiki REL1_34
FSFileBackendList.php
Go to the documentation of this file.
1<?php
29abstract class FSFileBackendList implements Iterator {
31 protected $iter;
32
34 protected $suffixStart;
35
37 protected $pos = 0;
38
40 protected $params = [];
41
46 public function __construct( $dir, array $params ) {
47 $path = realpath( $dir ); // normalize
48 if ( $path === false ) {
49 $path = $dir;
50 }
51 $this->suffixStart = strlen( $path ) + 1; // size of "path/to/dir/"
52 $this->params = $params;
53
54 try {
55 $this->iter = $this->initIterator( $path );
56 } catch ( UnexpectedValueException $e ) {
57 $this->iter = null; // bad permissions? deleted?
58 }
59 }
60
67 protected function initIterator( $dir ) {
68 if ( !empty( $this->params['topOnly'] ) ) { // non-recursive
69 # Get an iterator that will get direct sub-nodes
70 return new DirectoryIterator( $dir );
71 } else { // recursive
72 # Get an iterator that will return leaf nodes (non-directories)
73 # RecursiveDirectoryIterator extends FilesystemIterator.
74 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
75 $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
76
77 return new RecursiveIteratorIterator(
78 new RecursiveDirectoryIterator( $dir, $flags ),
79 RecursiveIteratorIterator::CHILD_FIRST // include dirs
80 );
81 }
82 }
83
88 public function key() {
89 return $this->pos;
90 }
91
96 public function current() {
97 return $this->getRelPath( $this->iter->current()->getPathname() );
98 }
99
104 public function next() {
105 try {
106 $this->iter->next();
107 $this->filterViaNext();
108 } catch ( UnexpectedValueException $e ) { // bad permissions? deleted?
109 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
110 }
111 ++$this->pos;
112 }
113
118 public function rewind() {
119 $this->pos = 0;
120 try {
121 $this->iter->rewind();
122 $this->filterViaNext();
123 } catch ( UnexpectedValueException $e ) { // bad permissions? deleted?
124 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
125 }
126 }
127
132 public function valid() {
133 return $this->iter && $this->iter->valid();
134 }
135
139 protected function filterViaNext() {
140 }
141
149 protected function getRelPath( $dir ) {
150 $path = realpath( $dir );
151 if ( $path === false ) {
152 $path = $dir;
153 }
154
155 return strtr( substr( $path, $this->suffixStart ), '\\', '/' );
156 }
157}
Wrapper around RecursiveDirectoryIterator/DirectoryIterator that catches exception or does any custom...
getRelPath( $dir)
Return only the relative path and normalize slashes to FileBackend-style.
__construct( $dir, array $params)
filterViaNext()
Filter out items by advancing to the next ones.
initIterator( $dir)
Return an appropriate iterator object to wrap.
File backend exception for checked exceptions (e.g.