MediaWiki master
MemoryFileBackend.php
Go to the documentation of this file.
1<?php
24use Wikimedia\AtEase\AtEase;
26use Wikimedia\Timestamp\ConvertibleTimestamp;
27
39 protected $files = [];
40
41 public function getFeatures() {
43 }
44
45 public function isPathUsableInternal( $storagePath ) {
46 return ( $this->resolveHashKey( $storagePath ) !== null );
47 }
48
49 protected function doCreateInternal( array $params ) {
50 $status = $this->newStatus();
51
52 $dst = $this->resolveHashKey( $params['dst'] );
53 if ( $dst === null ) {
54 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
55
56 return $status;
57 }
58
59 $this->files[$dst] = [
60 'data' => $params['content'],
61 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
62 ];
63
64 return $status;
65 }
66
67 protected function doStoreInternal( array $params ) {
68 $status = $this->newStatus();
69
70 $dst = $this->resolveHashKey( $params['dst'] );
71 if ( $dst === null ) {
72 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
73
74 return $status;
75 }
76
77 AtEase::suppressWarnings();
78 $data = file_get_contents( $params['src'] );
79 AtEase::restoreWarnings();
80 if ( $data === false ) { // source doesn't exist?
81 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
82
83 return $status;
84 }
85
86 $this->files[$dst] = [
87 'data' => $data,
88 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
89 ];
90
91 return $status;
92 }
93
94 protected function doCopyInternal( array $params ) {
95 $status = $this->newStatus();
96
97 $src = $this->resolveHashKey( $params['src'] );
98 if ( $src === null ) {
99 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
100
101 return $status;
102 }
103
104 $dst = $this->resolveHashKey( $params['dst'] );
105 if ( $dst === null ) {
106 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
107
108 return $status;
109 }
110
111 if ( !isset( $this->files[$src] ) ) {
112 if ( empty( $params['ignoreMissingSource'] ) ) {
113 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
114 }
115
116 return $status;
117 }
118
119 $this->files[$dst] = [
120 'data' => $this->files[$src]['data'],
121 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
122 ];
123
124 return $status;
125 }
126
127 protected function doMoveInternal( array $params ) {
128 $status = $this->newStatus();
129
130 $src = $this->resolveHashKey( $params['src'] );
131 if ( $src === null ) {
132 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
133
134 return $status;
135 }
136
137 $dst = $this->resolveHashKey( $params['dst'] );
138 if ( $dst === null ) {
139 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
140
141 return $status;
142 }
143
144 if ( !isset( $this->files[$src] ) ) {
145 if ( empty( $params['ignoreMissingSource'] ) ) {
146 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
147 }
148
149 return $status;
150 }
151
152 $this->files[$dst] = $this->files[$src];
153 unset( $this->files[$src] );
154 $this->files[$dst]['mtime'] = ConvertibleTimestamp::convert( TS_MW, time() );
155
156 return $status;
157 }
158
159 protected function doDeleteInternal( array $params ) {
160 $status = $this->newStatus();
161
162 $src = $this->resolveHashKey( $params['src'] );
163 if ( $src === null ) {
164 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
165
166 return $status;
167 }
168
169 if ( !isset( $this->files[$src] ) ) {
170 if ( empty( $params['ignoreMissingSource'] ) ) {
171 $status->fatal( 'backend-fail-delete', $params['src'] );
172 }
173
174 return $status;
175 }
176
177 unset( $this->files[$src] );
178
179 return $status;
180 }
181
182 protected function doGetFileStat( array $params ) {
183 $src = $this->resolveHashKey( $params['src'] );
184 if ( $src === null ) {
185 return self::RES_ERROR; // invalid path
186 }
187
188 if ( isset( $this->files[$src] ) ) {
189 return [
190 'mtime' => $this->files[$src]['mtime'],
191 'size' => strlen( $this->files[$src]['data'] ),
192 ];
193 }
194
195 return self::RES_ABSENT;
196 }
197
198 protected function doGetLocalCopyMulti( array $params ) {
199 $tmpFiles = []; // (path => TempFSFile)
200 foreach ( $params['srcs'] as $srcPath ) {
201 $src = $this->resolveHashKey( $srcPath );
202 if ( $src === null ) {
203 $fsFile = self::RES_ERROR;
204 } elseif ( !isset( $this->files[$src] ) ) {
205 $fsFile = self::RES_ABSENT;
206 } else {
207 // Create a new temporary file with the same extension...
208 $ext = FileBackend::extensionFromPath( $src );
209 $fsFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
210 if ( $fsFile ) {
211 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
212 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
213 $fsFile = self::RES_ERROR;
214 }
215 }
216 }
217 $tmpFiles[$srcPath] = $fsFile;
218 }
219
220 return $tmpFiles;
221 }
222
223 protected function doDirectoryExists( $container, $dir, array $params ) {
224 $prefix = rtrim( "$container/$dir", '/' ) . '/';
225 foreach ( $this->files as $path => $data ) {
226 if ( strpos( $path, $prefix ) === 0 ) {
227 return true;
228 }
229 }
230
231 return false;
232 }
233
234 public function getDirectoryListInternal( $container, $dir, array $params ) {
235 $dirs = [];
236 $prefix = rtrim( "$container/$dir", '/' ) . '/';
237 $prefixLen = strlen( $prefix );
238 foreach ( $this->files as $path => $data ) {
239 if ( strpos( $path, $prefix ) === 0 ) {
240 $relPath = substr( $path, $prefixLen );
241 if ( $relPath === false ) {
242 continue;
243 } elseif ( strpos( $relPath, '/' ) === false ) {
244 continue; // just a file
245 }
246 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
247 if ( !empty( $params['topOnly'] ) ) {
248 $dirs[$parts[0]] = 1; // top directory
249 } else {
250 $current = '';
251 foreach ( $parts as $part ) { // all directories
252 $dir = ( $current === '' ) ? $part : "$current/$part";
253 $dirs[$dir] = 1;
254 $current = $dir;
255 }
256 }
257 }
258 }
259
260 return array_keys( $dirs );
261 }
262
263 public function getFileListInternal( $container, $dir, array $params ) {
264 $files = [];
265 $prefix = rtrim( "$container/$dir", '/' ) . '/';
266 $prefixLen = strlen( $prefix );
267 foreach ( $this->files as $path => $data ) {
268 if ( strpos( $path, $prefix ) === 0 ) {
269 $relPath = substr( $path, $prefixLen );
270 if ( $relPath === false ) {
271 continue;
272 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
273 continue;
274 }
275 $files[] = $relPath;
276 }
277 }
278
279 return $files;
280 }
281
282 protected function directoriesAreVirtual() {
283 return true;
284 }
285
292 protected function resolveHashKey( $storagePath ) {
293 [ $fullCont, $relPath ] = $this->resolveStoragePathReal( $storagePath );
294 if ( $relPath === null ) {
295 return null; // invalid
296 }
297
298 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
299 }
300}
array $params
The job parameters.
Base class for all backends using particular storage medium.
resolveStoragePathReal( $storagePath)
Like resolveStoragePath() except null values are returned if the container is sharded and the shard c...
Simulation of a backend storage in memory.
doGetLocalCopyMulti(array $params)
doDirectoryExists( $container, $dir, array $params)
doCreateInternal(array $params)
getFileListInternal( $container, $dir, array $params)
Do not call this function from places outside FileBackend.
directoriesAreVirtual()
Is this a key/value store where directories are just virtual? Virtual directories exists in so much a...
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
getDirectoryListInternal( $container, $dir, array $params)
Do not call this function from places outside FileBackend.
doStoreInternal(array $params)
doDeleteInternal(array $params)
doMoveInternal(array $params)
array $files
Map of (file path => (data,mtime)
doCopyInternal(array $params)
doGetFileStat(array $params)
isPathUsableInternal( $storagePath)
Check if a file can be created or changed at a given storage path in the backend.
resolveHashKey( $storagePath)
Get the absolute file system path for a storage path.
Base class for all file backend classes (including multi-write backends).
newStatus(... $args)
Yields the result of the status wrapper callback on either: