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 isPathUsableInternal( $storagePath ) {
42 protected function doCreateInternal( array $params ) {
43 $status = Status
::newGood();
45 $dst = $this->resolveHashKey( $params['dst'] );
46 if ( $dst === null ) {
47 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
51 $this->files
[$dst] = array(
52 'data' => $params['content'],
53 'mtime' => wfTimestamp( TS_MW
, time() )
59 protected function doStoreInternal( array $params ) {
60 $status = Status
::newGood();
62 $dst = $this->resolveHashKey( $params['dst'] );
63 if ( $dst === null ) {
64 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
69 $data = file_get_contents( $params['src'] );
71 if ( $data === false ) { // source doesn't exist?
72 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
76 $this->files
[$dst] = array(
78 'mtime' => wfTimestamp( TS_MW
, time() )
84 protected function doCopyInternal( array $params ) {
85 $status = Status
::newGood();
87 $src = $this->resolveHashKey( $params['src'] );
88 if ( $src === null ) {
89 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
93 $dst = $this->resolveHashKey( $params['dst'] );
94 if ( $dst === null ) {
95 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
99 if ( !isset( $this->files
[$src] ) ) {
100 if ( empty( $params['ignoreMissingSource'] ) ) {
101 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
106 $this->files
[$dst] = array(
107 'data' => $this->files
[$src]['data'],
108 'mtime' => wfTimestamp( TS_MW
, time() )
114 protected function doDeleteInternal( array $params ) {
115 $status = Status
::newGood();
117 $src = $this->resolveHashKey( $params['src'] );
118 if ( $src === null ) {
119 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
123 if ( !isset( $this->files
[$src] ) ) {
124 if ( empty( $params['ignoreMissingSource'] ) ) {
125 $status->fatal( 'backend-fail-delete', $params['src'] );
130 unset( $this->files
[$src] );
135 protected function doGetFileStat( array $params ) {
136 $src = $this->resolveHashKey( $params['src'] );
137 if ( $src === null ) {
141 if ( isset( $this->files
[$src] ) ) {
143 'mtime' => $this->files
[$src]['mtime'],
144 'size' => strlen( $this->files
[$src]['data'] ),
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] ) ) {
158 // Create a new temporary file with the same extension...
159 $ext = FileBackend
::extensionFromPath( $src );
160 $fsFile = TempFSFile
::factory( 'localcopy_', $ext );
162 $bytes = file_put_contents( $fsFile->getPath(), $this->files
[$src]['data'] );
163 if ( $bytes !== strlen( $this->files
[$src]['data'] ) ) {
168 $tmpFiles[$srcPath] = $fsFile;
173 protected function doStreamFile( array $params ) {
174 $status = Status
::newGood();
176 $src = $this->resolveHashKey( $params['src'] );
177 if ( $src === null ||
!isset( $this->files
[$src] ) ) {
178 $status->fatal( 'backend-fail-stream', $params['src'] );
182 print $this->files
[$src]['data'];
187 protected function doDirectoryExists( $container, $dir, array $params ) {
188 $prefix = rtrim( "$container/$dir", '/' ) . '/';
189 foreach ( $this->files
as $path => $data ) {
190 if ( strpos( $path, $prefix ) === 0 ) {
197 public function getDirectoryListInternal( $container, $dir, array $params ) {
199 $prefix = rtrim( "$container/$dir", '/' ) . '/';
200 $prefixLen = strlen( $prefix );
201 foreach ( $this->files
as $path => $data ) {
202 if ( strpos( $path, $prefix ) === 0 ) {
203 $relPath = substr( $path, $prefixLen );
204 if ( $relPath === false ) {
206 } elseif ( strpos( $relPath, '/' ) === false ) {
207 continue; // just a file
209 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
210 if ( !empty( $params['topOnly'] ) ) {
211 $dirs[$parts[0]] = 1; // top directory
214 foreach ( $parts as $part ) { // all directories
215 $dir = ( $current === '' ) ?
$part : "$current/$part";
222 return array_keys( $dirs );
225 public function getFileListInternal( $container, $dir, array $params ) {
227 $prefix = rtrim( "$container/$dir", '/' ) . '/';
228 $prefixLen = strlen( $prefix );
229 foreach ( $this->files
as $path => $data ) {
230 if ( strpos( $path, $prefix ) === 0 ) {
231 $relPath = substr( $path, $prefixLen );
232 if ( $relPath === false ) {
234 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
243 protected function directoriesAreVirtual() {
248 * Get the absolute file system path for a storage path
250 * @param string $storagePath Storage path
251 * @return string|null
253 protected function resolveHashKey( $storagePath ) {
254 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
255 if ( $relPath === null ) {
256 return null; // invalid
258 return ( $relPath !== '' ) ?
"$fullCont/$relPath" : $fullCont;