MediaWiki  1.34.0
FSFileBackendList.php
Go to the documentation of this file.
1 <?php
29 abstract 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 }
FSFileBackendList\initIterator
initIterator( $dir)
Return an appropriate iterator object to wrap.
Definition: FSFileBackendList.php:67
FSFileBackendList\current
current()
Definition: FSFileBackendList.php:96
FileBackendError
File backend exception for checked exceptions (e.g.
Definition: FileBackendError.php:8
FSFileBackendList\getRelPath
getRelPath( $dir)
Return only the relative path and normalize slashes to FileBackend-style.
Definition: FSFileBackendList.php:149
FSFileBackendList
Wrapper around RecursiveDirectoryIterator/DirectoryIterator that catches exception or does any custom...
Definition: FSFileBackendList.php:29
FSFileBackendList\$iter
Iterator $iter
Definition: FSFileBackendList.php:31
FSFileBackendList\valid
valid()
Definition: FSFileBackendList.php:132
FSFileBackendList\$pos
int $pos
Definition: FSFileBackendList.php:37
FSFileBackendList\next
next()
Definition: FSFileBackendList.php:104
FSFileBackendList\key
key()
Definition: FSFileBackendList.php:88
$path
$path
Definition: NoLocalSettings.php:25
FSFileBackendList\$suffixStart
int $suffixStart
Definition: FSFileBackendList.php:34
FSFileBackendList\rewind
rewind()
Definition: FSFileBackendList.php:118
FSFileBackendList\$params
array $params
Definition: FSFileBackendList.php:40
FSFileBackendList\__construct
__construct( $dir, array $params)
Definition: FSFileBackendList.php:46
FSFileBackendList\filterViaNext
filterViaNext()
Filter out items by advancing to the next ones.
Definition: FSFileBackendList.php:139