Merge "Include delayed job count in showJobs.php"
[lhc/web/wiklou.git] / includes / filebackend / MemoryFileBackend.php
1 <?php
2 /**
3 * Simulation of a backend storage in memory.
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 Aaron Schulz
23 */
24
25 /**
26 * Simulation of a backend storage in memory.
27 *
28 * All data in the backend is automatically deleted at the end of PHP execution.
29 * Since the data stored here is volatile, this is only useful for staging or testing.
30 *
31 * @ingroup FileBackend
32 * @since 1.23
33 */
34 class MemoryFileBackend extends FileBackendStore {
35 /** @var array Map of (file path => (data,mtime) */
36 protected $files = array();
37
38 public function isPathUsableInternal( $storagePath ) {
39 return true;
40 }
41
42 protected function doCreateInternal( array $params ) {
43 $status = Status::newGood();
44
45 $dst = $this->resolveHashKey( $params['dst'] );
46 if ( $dst === null ) {
47 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
48 return $status;
49 }
50
51 $this->files[$dst] = array(
52 'data' => $params['content'],
53 'mtime' => wfTimestamp( TS_MW, time() )
54 );
55
56 return $status;
57 }
58
59 protected function doStoreInternal( array $params ) {
60 $status = Status::newGood();
61
62 $dst = $this->resolveHashKey( $params['dst'] );
63 if ( $dst === null ) {
64 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
65 return $status;
66 }
67
68 wfSuppressWarnings();
69 $data = file_get_contents( $params['src'] );
70 wfRestoreWarnings();
71 if ( $data === false ) { // source doesn't exist?
72 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
73 return $status;
74 }
75
76 $this->files[$dst] = array(
77 'data' => $data,
78 'mtime' => wfTimestamp( TS_MW, time() )
79 );
80
81 return $status;
82 }
83
84 protected function doCopyInternal( array $params ) {
85 $status = Status::newGood();
86
87 $src = $this->resolveHashKey( $params['src'] );
88 if ( $src === null ) {
89 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
90 return $status;
91 }
92
93 $dst = $this->resolveHashKey( $params['dst'] );
94 if ( $dst === null ) {
95 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
96 return $status;
97 }
98
99 if ( !isset( $this->files[$src] ) ) {
100 if ( empty( $params['ignoreMissingSource'] ) ) {
101 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
102 }
103 return $status;
104 }
105
106 $this->files[$dst] = array(
107 'data' => $this->files[$src]['data'],
108 'mtime' => wfTimestamp( TS_MW, time() )
109 );
110
111 return $status;
112 }
113
114 protected function doDeleteInternal( array $params ) {
115 $status = Status::newGood();
116
117 $src = $this->resolveHashKey( $params['src'] );
118 if ( $src === null ) {
119 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
120 return $status;
121 }
122
123 if ( !isset( $this->files[$src] ) ) {
124 if ( empty( $params['ignoreMissingSource'] ) ) {
125 $status->fatal( 'backend-fail-delete', $params['src'] );
126 }
127 return $status;
128 }
129
130 unset( $this->files[$src] );
131
132 return $status;
133 }
134
135 protected function doGetFileStat( array $params ) {
136 $src = $this->resolveHashKey( $params['src'] );
137 if ( $src === null ) {
138 return null;
139 }
140
141 if ( isset( $this->files[$src] ) ) {
142 return array(
143 'mtime' => $this->files[$src]['mtime'],
144 'size' => strlen( $this->files[$src]['data'] ),
145 );
146 }
147
148 return false;
149 }
150
151 protected function doGetLocalCopyMulti( array $params ) {
152 $tmpFiles = array(); // (path => TempFSFile)
153 foreach ( $params['srcs'] as $srcPath ) {
154 $src = $this->resolveHashKey( $srcPath );
155 if ( $src === null || !isset( $this->files[$src] ) ) {
156 $fsFile = null;
157 } else {
158 $fsFile = TempFSFile::factory( 'localcopy_' );
159 if ( $fsFile ) {
160 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
161 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
162 $fsFile = null;
163 }
164 }
165 }
166 $tmpFiles[$srcPath] = $fsFile;
167 }
168 return $tmpFiles;
169 }
170
171 protected function doStreamFile( array $params ) {
172 $status = Status::newGood();
173
174 $src = $this->resolveHashKey( $params['src'] );
175 if ( $src === null || !isset( $this->files[$src] ) ) {
176 $status->fatal( 'backend-fail-stream', $params['src'] );
177 return $status;
178 }
179
180 print $this->files[$src]['data'];
181
182 return $status;
183 }
184
185 protected function doDirectoryExists( $container, $dir, array $params ) {
186 $prefix = rtrim( "$container/$dir", '/' ) . '/';
187 foreach ( $this->files as $path => $data ) {
188 if ( strpos( $path, $prefix ) === 0 ) {
189 return true;
190 }
191 }
192 return false;
193 }
194
195 public function getDirectoryListInternal( $container, $dir, array $params ) {
196 $dirs = array();
197 $prefix = rtrim( "$container/$dir", '/' ) . '/';
198 $prefixLen = strlen( $prefix );
199 foreach ( $this->files as $path => $data ) {
200 if ( strpos( $path, $prefix ) === 0 ) {
201 $relPath = substr( $path, $prefixLen );
202 if ( $relPath === false ) {
203 continue;
204 } elseif ( strpos( $relPath, '/' ) === false ) {
205 continue; // just a file
206 }
207 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
208 if ( !empty( $params['topOnly'] ) ) {
209 $dirs[$parts[0]] = 1; // top directory
210 } else {
211 $current = '';
212 foreach ( $parts as $part ) { // all directories
213 $dir = ( $current === '' ) ? $part : "$current/$part";
214 $dirs[$dir] = 1;
215 $current = $dir;
216 }
217 }
218 }
219 }
220 return array_keys( $dirs );
221 }
222
223 public function getFileListInternal( $container, $dir, array $params ) {
224 $files = array();
225 $prefix = rtrim( "$container/$dir", '/' ) . '/';
226 $prefixLen = strlen( $prefix );
227 foreach ( $this->files as $path => $data ) {
228 if ( strpos( $path, $prefix ) === 0 ) {
229 $relPath = substr( $path, $prefixLen );
230 if ( $relPath === false ) {
231 continue;
232 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
233 continue;
234 }
235 $files[] = $relPath;
236 }
237 }
238 return $files;
239 }
240
241 protected function directoriesAreVirtual() {
242 return true;
243 }
244
245 /**
246 * Get the absolute file system path for a storage path
247 *
248 * @param string $storagePath Storage path
249 * @return string|null
250 */
251 protected function resolveHashKey( $storagePath ) {
252 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
253 if ( $relPath === null ) {
254 return null; // invalid
255 }
256 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
257 }
258 }