Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / filebackend / MemoryFileBackend.php
blobbf30e7faa59b03dd255565be082794a0c9e5b426
1 <?php
2 /**
3 * Simulation of a backend storage in memory.
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.
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.
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
20 * @file
21 * @ingroup FileBackend
24 namespace Wikimedia\FileBackend;
26 use Wikimedia\AtEase\AtEase;
27 use Wikimedia\Timestamp\ConvertibleTimestamp;
29 /**
30 * Simulation of a backend storage in memory.
32 * All data in the backend is automatically deleted at the end of PHP execution.
33 * Since the data stored here is volatile, this is only useful for staging or testing.
35 * @ingroup FileBackend
36 * @since 1.23
38 class MemoryFileBackend extends FileBackendStore {
39 /** @var array Map of (file path => (data,mtime) */
40 protected $files = [];
42 public function getFeatures() {
43 return self::ATTR_UNICODE_PATHS;
46 public function isPathUsableInternal( $storagePath ) {
47 return ( $this->resolveHashKey( $storagePath ) !== null );
50 protected function doCreateInternal( array $params ) {
51 $status = $this->newStatus();
53 $dst = $this->resolveHashKey( $params['dst'] );
54 if ( $dst === null ) {
55 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
57 return $status;
60 $this->files[$dst] = [
61 'data' => $params['content'],
62 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
65 return $status;
68 protected function doStoreInternal( array $params ) {
69 $status = $this->newStatus();
71 $dst = $this->resolveHashKey( $params['dst'] );
72 if ( $dst === null ) {
73 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
75 return $status;
78 AtEase::suppressWarnings();
79 $data = file_get_contents( $params['src'] );
80 AtEase::restoreWarnings();
81 if ( $data === false ) { // source doesn't exist?
82 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
84 return $status;
87 $this->files[$dst] = [
88 'data' => $data,
89 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
92 return $status;
95 protected function doCopyInternal( array $params ) {
96 $status = $this->newStatus();
98 $src = $this->resolveHashKey( $params['src'] );
99 if ( $src === null ) {
100 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
102 return $status;
105 $dst = $this->resolveHashKey( $params['dst'] );
106 if ( $dst === null ) {
107 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
109 return $status;
112 if ( !isset( $this->files[$src] ) ) {
113 if ( empty( $params['ignoreMissingSource'] ) ) {
114 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
117 return $status;
120 $this->files[$dst] = [
121 'data' => $this->files[$src]['data'],
122 'mtime' => ConvertibleTimestamp::convert( TS_MW, time() )
125 return $status;
128 protected function doMoveInternal( array $params ) {
129 $status = $this->newStatus();
131 $src = $this->resolveHashKey( $params['src'] );
132 if ( $src === null ) {
133 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
135 return $status;
138 $dst = $this->resolveHashKey( $params['dst'] );
139 if ( $dst === null ) {
140 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
142 return $status;
145 if ( !isset( $this->files[$src] ) ) {
146 if ( empty( $params['ignoreMissingSource'] ) ) {
147 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
150 return $status;
153 $this->files[$dst] = $this->files[$src];
154 unset( $this->files[$src] );
155 $this->files[$dst]['mtime'] = ConvertibleTimestamp::convert( TS_MW, time() );
157 return $status;
160 protected function doDeleteInternal( array $params ) {
161 $status = $this->newStatus();
163 $src = $this->resolveHashKey( $params['src'] );
164 if ( $src === null ) {
165 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
167 return $status;
170 if ( !isset( $this->files[$src] ) ) {
171 if ( empty( $params['ignoreMissingSource'] ) ) {
172 $status->fatal( 'backend-fail-delete', $params['src'] );
175 return $status;
178 unset( $this->files[$src] );
180 return $status;
183 protected function doGetFileStat( array $params ) {
184 $src = $this->resolveHashKey( $params['src'] );
185 if ( $src === null ) {
186 return self::RES_ERROR; // invalid path
189 if ( isset( $this->files[$src] ) ) {
190 return [
191 'mtime' => $this->files[$src]['mtime'],
192 'size' => strlen( $this->files[$src]['data'] ),
196 return self::RES_ABSENT;
199 protected function doGetLocalCopyMulti( array $params ) {
200 $tmpFiles = []; // (path => TempFSFile)
201 foreach ( $params['srcs'] as $srcPath ) {
202 $src = $this->resolveHashKey( $srcPath );
203 if ( $src === null ) {
204 $fsFile = self::RES_ERROR;
205 } elseif ( !isset( $this->files[$src] ) ) {
206 $fsFile = self::RES_ABSENT;
207 } else {
208 // Create a new temporary file with the same extension...
209 $ext = FileBackend::extensionFromPath( $src );
210 $fsFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
211 if ( $fsFile ) {
212 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
213 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
214 $fsFile = self::RES_ERROR;
218 $tmpFiles[$srcPath] = $fsFile;
221 return $tmpFiles;
224 protected function doDirectoryExists( $container, $dir, array $params ) {
225 $prefix = rtrim( "$container/$dir", '/' ) . '/';
226 foreach ( $this->files as $path => $data ) {
227 if ( strpos( $path, $prefix ) === 0 ) {
228 return true;
232 return false;
235 public function getDirectoryListInternal( $container, $dir, array $params ) {
236 $dirs = [];
237 $prefix = rtrim( "$container/$dir", '/' ) . '/';
238 $prefixLen = strlen( $prefix );
239 foreach ( $this->files as $path => $data ) {
240 if ( strpos( $path, $prefix ) === 0 ) {
241 $relPath = substr( $path, $prefixLen );
242 if ( $relPath === false ) {
243 continue;
244 } elseif ( strpos( $relPath, '/' ) === false ) {
245 continue; // just a file
247 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
248 if ( !empty( $params['topOnly'] ) ) {
249 $dirs[$parts[0]] = 1; // top directory
250 } else {
251 $current = '';
252 foreach ( $parts as $part ) { // all directories
253 $dir = ( $current === '' ) ? $part : "$current/$part";
254 $dirs[$dir] = 1;
255 $current = $dir;
261 return array_keys( $dirs );
264 public function getFileListInternal( $container, $dir, array $params ) {
265 $files = [];
266 $prefix = rtrim( "$container/$dir", '/' ) . '/';
267 $prefixLen = strlen( $prefix );
268 foreach ( $this->files as $path => $data ) {
269 if ( strpos( $path, $prefix ) === 0 ) {
270 $relPath = substr( $path, $prefixLen );
271 if ( $relPath === false ) {
272 continue;
273 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
274 continue;
276 $files[] = $relPath;
280 return $files;
283 protected function directoriesAreVirtual() {
284 return true;
288 * Get the absolute file system path for a storage path
290 * @param string $storagePath
291 * @return string|null
293 protected function resolveHashKey( $storagePath ) {
294 [ $fullCont, $relPath ] = $this->resolveStoragePathReal( $storagePath );
295 if ( $relPath === null ) {
296 return null; // invalid
299 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
303 /** @deprecated class alias since 1.43 */
304 class_alias( MemoryFileBackend::class, 'MemoryFileBackend' );