Split filebackend files to class per file
[lhc/web/wiklou.git] / includes / libs / filebackend / SwiftFileBackendList.php
1 <?php
2 /**
3 * OpenStack Swift based file backend.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Russ Nelson
23 */
24
25 /**
26 * SwiftFileBackend helper class to page through listings.
27 * Swift also has a listing limit of 10,000 objects for sanity.
28 * Do not use this class from places outside SwiftFileBackend.
29 *
30 * @ingroup FileBackend
31 */
32 abstract class SwiftFileBackendList implements Iterator {
33 /** @var array List of path or (path,stat array) entries */
34 protected $bufferIter = [];
35
36 /** @var string List items *after* this path */
37 protected $bufferAfter = null;
38
39 /** @var int */
40 protected $pos = 0;
41
42 /** @var array */
43 protected $params = [];
44
45 /** @var SwiftFileBackend */
46 protected $backend;
47
48 /** @var string Container name */
49 protected $container;
50
51 /** @var string Storage directory */
52 protected $dir;
53
54 /** @var int */
55 protected $suffixStart;
56
57 const PAGE_SIZE = 9000; // file listing buffer size
58
59 /**
60 * @param SwiftFileBackend $backend
61 * @param string $fullCont Resolved container name
62 * @param string $dir Resolved directory relative to container
63 * @param array $params
64 */
65 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
66 $this->backend = $backend;
67 $this->container = $fullCont;
68 $this->dir = $dir;
69 if ( substr( $this->dir, -1 ) === '/' ) {
70 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
71 }
72 if ( $this->dir == '' ) { // whole container
73 $this->suffixStart = 0;
74 } else { // dir within container
75 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
76 }
77 $this->params = $params;
78 }
79
80 /**
81 * @see Iterator::key()
82 * @return int
83 */
84 public function key() {
85 return $this->pos;
86 }
87
88 /**
89 * @see Iterator::next()
90 */
91 public function next() {
92 // Advance to the next file in the page
93 next( $this->bufferIter );
94 ++$this->pos;
95 // Check if there are no files left in this page and
96 // advance to the next page if this page was not empty.
97 if ( !$this->valid() && count( $this->bufferIter ) ) {
98 $this->bufferIter = $this->pageFromList(
99 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
100 ); // updates $this->bufferAfter
101 }
102 }
103
104 /**
105 * @see Iterator::rewind()
106 */
107 public function rewind() {
108 $this->pos = 0;
109 $this->bufferAfter = null;
110 $this->bufferIter = $this->pageFromList(
111 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
112 ); // updates $this->bufferAfter
113 }
114
115 /**
116 * @see Iterator::valid()
117 * @return bool
118 */
119 public function valid() {
120 if ( $this->bufferIter === null ) {
121 return false; // some failure?
122 } else {
123 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
124 }
125 }
126
127 /**
128 * Get the given list portion (page)
129 *
130 * @param string $container Resolved container name
131 * @param string $dir Resolved path relative to container
132 * @param string &$after
133 * @param int $limit
134 * @param array $params
135 * @return Traversable|array
136 */
137 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
138 }