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
22 * @author Aaron Schulz
26 * Simulation of a backend storage in memory.
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.
31 * @ingroup FileBackend
34 class MemoryFileBackend
extends FileBackendStore
{
35 /** @var array Map of (file path => (data,mtime) */
36 protected $files = array();
38 public function getFeatures() {
39 return self
::ATTR_UNICODE_PATHS
;
42 public function isPathUsableInternal( $storagePath ) {
46 protected function doCreateInternal( array $params ) {
47 $status = Status
::newGood();
49 $dst = $this->resolveHashKey( $params['dst'] );
50 if ( $dst === null ) {
51 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
56 $this->files
[$dst] = array(
57 'data' => $params['content'],
58 'mtime' => wfTimestamp( TS_MW
, time() )
64 protected function doStoreInternal( array $params ) {
65 $status = Status
::newGood();
67 $dst = $this->resolveHashKey( $params['dst'] );
68 if ( $dst === null ) {
69 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
74 MediaWiki\
suppressWarnings();
75 $data = file_get_contents( $params['src'] );
76 MediaWiki\restoreWarnings
();
77 if ( $data === false ) { // source doesn't exist?
78 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
83 $this->files
[$dst] = array(
85 'mtime' => wfTimestamp( TS_MW
, time() )
91 protected function doCopyInternal( array $params ) {
92 $status = Status
::newGood();
94 $src = $this->resolveHashKey( $params['src'] );
95 if ( $src === null ) {
96 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
101 $dst = $this->resolveHashKey( $params['dst'] );
102 if ( $dst === null ) {
103 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
108 if ( !isset( $this->files
[$src] ) ) {
109 if ( empty( $params['ignoreMissingSource'] ) ) {
110 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
116 $this->files
[$dst] = array(
117 'data' => $this->files
[$src]['data'],
118 'mtime' => wfTimestamp( TS_MW
, time() )
124 protected function doDeleteInternal( array $params ) {
125 $status = Status
::newGood();
127 $src = $this->resolveHashKey( $params['src'] );
128 if ( $src === null ) {
129 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
134 if ( !isset( $this->files
[$src] ) ) {
135 if ( empty( $params['ignoreMissingSource'] ) ) {
136 $status->fatal( 'backend-fail-delete', $params['src'] );
142 unset( $this->files
[$src] );
147 protected function doGetFileStat( array $params ) {
148 $src = $this->resolveHashKey( $params['src'] );
149 if ( $src === null ) {
153 if ( isset( $this->files
[$src] ) ) {
155 'mtime' => $this->files
[$src]['mtime'],
156 'size' => strlen( $this->files
[$src]['data'] ),
163 protected function doGetLocalCopyMulti( array $params ) {
164 $tmpFiles = array(); // (path => TempFSFile)
165 foreach ( $params['srcs'] as $srcPath ) {
166 $src = $this->resolveHashKey( $srcPath );
167 if ( $src === null ||
!isset( $this->files
[$src] ) ) {
170 // Create a new temporary file with the same extension...
171 $ext = FileBackend
::extensionFromPath( $src );
172 $fsFile = TempFSFile
::factory( 'localcopy_', $ext );
174 $bytes = file_put_contents( $fsFile->getPath(), $this->files
[$src]['data'] );
175 if ( $bytes !== strlen( $this->files
[$src]['data'] ) ) {
180 $tmpFiles[$srcPath] = $fsFile;
186 protected function doStreamFile( array $params ) {
187 $status = Status
::newGood();
189 $src = $this->resolveHashKey( $params['src'] );
190 if ( $src === null ||
!isset( $this->files
[$src] ) ) {
191 $status->fatal( 'backend-fail-stream', $params['src'] );
196 print $this->files
[$src]['data'];
201 protected function doDirectoryExists( $container, $dir, array $params ) {
202 $prefix = rtrim( "$container/$dir", '/' ) . '/';
203 foreach ( $this->files
as $path => $data ) {
204 if ( strpos( $path, $prefix ) === 0 ) {
212 public function getDirectoryListInternal( $container, $dir, array $params ) {
214 $prefix = rtrim( "$container/$dir", '/' ) . '/';
215 $prefixLen = strlen( $prefix );
216 foreach ( $this->files
as $path => $data ) {
217 if ( strpos( $path, $prefix ) === 0 ) {
218 $relPath = substr( $path, $prefixLen );
219 if ( $relPath === false ) {
221 } elseif ( strpos( $relPath, '/' ) === false ) {
222 continue; // just a file
224 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
225 if ( !empty( $params['topOnly'] ) ) {
226 $dirs[$parts[0]] = 1; // top directory
229 foreach ( $parts as $part ) { // all directories
230 $dir = ( $current === '' ) ?
$part : "$current/$part";
238 return array_keys( $dirs );
241 public function getFileListInternal( $container, $dir, array $params ) {
243 $prefix = rtrim( "$container/$dir", '/' ) . '/';
244 $prefixLen = strlen( $prefix );
245 foreach ( $this->files
as $path => $data ) {
246 if ( strpos( $path, $prefix ) === 0 ) {
247 $relPath = substr( $path, $prefixLen );
248 if ( $relPath === false ) {
250 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
260 protected function directoriesAreVirtual() {
265 * Get the absolute file system path for a storage path
267 * @param string $storagePath Storage path
268 * @return string|null
270 protected function resolveHashKey( $storagePath ) {
271 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
272 if ( $relPath === null ) {
273 return null; // invalid
276 return ( $relPath !== '' ) ?
"$fullCont/$relPath" : $fullCont;