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
21 * @ingroup FileBackend
24 use Wikimedia\AtEase\AtEase
;
25 use Wikimedia\Timestamp\ConvertibleTimestamp
;
28 * Simulation of a backend storage in memory.
30 * All data in the backend is automatically deleted at the end of PHP execution.
31 * Since the data stored here is volatile, this is only useful for staging or testing.
33 * @ingroup FileBackend
36 class MemoryFileBackend
extends FileBackendStore
{
37 /** @var array Map of (file path => (data,mtime) */
38 protected $files = [];
40 public function getFeatures() {
41 return self
::ATTR_UNICODE_PATHS
;
44 public function isPathUsableInternal( $storagePath ) {
45 return ( $this->resolveHashKey( $storagePath ) !== null );
48 protected function doCreateInternal( array $params ) {
49 $status = $this->newStatus();
51 $dst = $this->resolveHashKey( $params['dst'] );
52 if ( $dst === null ) {
53 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
58 $this->files
[$dst] = [
59 'data' => $params['content'],
60 'mtime' => ConvertibleTimestamp
::convert( TS_MW
, time() )
66 protected function doStoreInternal( array $params ) {
67 $status = $this->newStatus();
69 $dst = $this->resolveHashKey( $params['dst'] );
70 if ( $dst === null ) {
71 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
76 AtEase
::suppressWarnings();
77 $data = file_get_contents( $params['src'] );
78 AtEase
::restoreWarnings();
79 if ( $data === false ) { // source doesn't exist?
80 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
85 $this->files
[$dst] = [
87 'mtime' => ConvertibleTimestamp
::convert( TS_MW
, time() )
93 protected function doCopyInternal( array $params ) {
94 $status = $this->newStatus();
96 $src = $this->resolveHashKey( $params['src'] );
97 if ( $src === null ) {
98 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
103 $dst = $this->resolveHashKey( $params['dst'] );
104 if ( $dst === null ) {
105 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
110 if ( !isset( $this->files
[$src] ) ) {
111 if ( empty( $params['ignoreMissingSource'] ) ) {
112 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
118 $this->files
[$dst] = [
119 'data' => $this->files
[$src]['data'],
120 'mtime' => ConvertibleTimestamp
::convert( TS_MW
, time() )
126 protected function doDeleteInternal( array $params ) {
127 $status = $this->newStatus();
129 $src = $this->resolveHashKey( $params['src'] );
130 if ( $src === null ) {
131 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
136 if ( !isset( $this->files
[$src] ) ) {
137 if ( empty( $params['ignoreMissingSource'] ) ) {
138 $status->fatal( 'backend-fail-delete', $params['src'] );
144 unset( $this->files
[$src] );
149 protected function doGetFileStat( array $params ) {
150 $src = $this->resolveHashKey( $params['src'] );
151 if ( $src === null ) {
152 return self
::$RES_ERROR; // invalid path
155 if ( isset( $this->files
[$src] ) ) {
157 'mtime' => $this->files
[$src]['mtime'],
158 'size' => strlen( $this->files
[$src]['data'] ),
162 return self
::$RES_ABSENT;
165 protected function doGetLocalCopyMulti( array $params ) {
166 $tmpFiles = []; // (path => TempFSFile)
167 foreach ( $params['srcs'] as $srcPath ) {
168 $src = $this->resolveHashKey( $srcPath );
169 if ( $src === null ) {
170 $fsFile = self
::$RES_ERROR;
171 } elseif ( !isset( $this->files
[$src] ) ) {
172 $fsFile = self
::$RES_ABSENT;
174 // Create a new temporary file with the same extension...
175 $ext = FileBackend
::extensionFromPath( $src );
176 $fsFile = $this->tmpFileFactory
->newTempFSFile( 'localcopy_', $ext );
178 $bytes = file_put_contents( $fsFile->getPath(), $this->files
[$src]['data'] );
179 if ( $bytes !== strlen( $this->files
[$src]['data'] ) ) {
180 $fsFile = self
::$RES_ERROR;
184 $tmpFiles[$srcPath] = $fsFile;
190 protected function doDirectoryExists( $container, $dir, array $params ) {
191 $prefix = rtrim( "$container/$dir", '/' ) . '/';
192 foreach ( $this->files
as $path => $data ) {
193 if ( strpos( $path, $prefix ) === 0 ) {
201 public function getDirectoryListInternal( $container, $dir, array $params ) {
203 $prefix = rtrim( "$container/$dir", '/' ) . '/';
204 $prefixLen = strlen( $prefix );
205 foreach ( $this->files
as $path => $data ) {
206 if ( strpos( $path, $prefix ) === 0 ) {
207 $relPath = substr( $path, $prefixLen );
208 if ( $relPath === false ) {
210 } elseif ( strpos( $relPath, '/' ) === false ) {
211 continue; // just a file
213 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
214 if ( !empty( $params['topOnly'] ) ) {
215 $dirs[$parts[0]] = 1; // top directory
218 foreach ( $parts as $part ) { // all directories
219 $dir = ( $current === '' ) ?
$part : "$current/$part";
227 return array_keys( $dirs );
230 public function getFileListInternal( $container, $dir, array $params ) {
232 $prefix = rtrim( "$container/$dir", '/' ) . '/';
233 $prefixLen = strlen( $prefix );
234 foreach ( $this->files
as $path => $data ) {
235 if ( strpos( $path, $prefix ) === 0 ) {
236 $relPath = substr( $path, $prefixLen );
237 if ( $relPath === false ) {
239 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
249 protected function directoriesAreVirtual() {
254 * Get the absolute file system path for a storage path
256 * @param string $storagePath Storage path
257 * @return string|null
259 protected function resolveHashKey( $storagePath ) {
260 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
261 if ( $relPath === null ) {
262 return null; // invalid
265 return ( $relPath !== '' ) ?
"$fullCont/$relPath" : $fullCont;