Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / libs / filebackend / MemoryFileBackend.php
blob0341a2af1d689b7f631dbc351f42557831de1222
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 /**
25 * Simulation of a backend storage in memory.
27 * All data in the backend is automatically deleted at the end of PHP execution.
28 * Since the data stored here is volatile, this is only useful for staging or testing.
30 * @ingroup FileBackend
31 * @since 1.23
33 class MemoryFileBackend extends FileBackendStore {
34 /** @var array Map of (file path => (data,mtime) */
35 protected $files = [];
37 public function getFeatures() {
38 return self::ATTR_UNICODE_PATHS;
41 public function isPathUsableInternal( $storagePath ) {
42 return true;
45 protected function doCreateInternal( array $params ) {
46 $status = $this->newStatus();
48 $dst = $this->resolveHashKey( $params['dst'] );
49 if ( $dst === null ) {
50 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
52 return $status;
55 $this->files[$dst] = [
56 'data' => $params['content'],
57 'mtime' => wfTimestamp( TS_MW, time() )
60 return $status;
63 protected function doStoreInternal( array $params ) {
64 $status = $this->newStatus();
66 $dst = $this->resolveHashKey( $params['dst'] );
67 if ( $dst === null ) {
68 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
70 return $status;
73 MediaWiki\suppressWarnings();
74 $data = file_get_contents( $params['src'] );
75 MediaWiki\restoreWarnings();
76 if ( $data === false ) { // source doesn't exist?
77 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
79 return $status;
82 $this->files[$dst] = [
83 'data' => $data,
84 'mtime' => wfTimestamp( TS_MW, time() )
87 return $status;
90 protected function doCopyInternal( array $params ) {
91 $status = $this->newStatus();
93 $src = $this->resolveHashKey( $params['src'] );
94 if ( $src === null ) {
95 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
97 return $status;
100 $dst = $this->resolveHashKey( $params['dst'] );
101 if ( $dst === null ) {
102 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
104 return $status;
107 if ( !isset( $this->files[$src] ) ) {
108 if ( empty( $params['ignoreMissingSource'] ) ) {
109 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
112 return $status;
115 $this->files[$dst] = [
116 'data' => $this->files[$src]['data'],
117 'mtime' => wfTimestamp( TS_MW, time() )
120 return $status;
123 protected function doDeleteInternal( array $params ) {
124 $status = $this->newStatus();
126 $src = $this->resolveHashKey( $params['src'] );
127 if ( $src === null ) {
128 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
130 return $status;
133 if ( !isset( $this->files[$src] ) ) {
134 if ( empty( $params['ignoreMissingSource'] ) ) {
135 $status->fatal( 'backend-fail-delete', $params['src'] );
138 return $status;
141 unset( $this->files[$src] );
143 return $status;
146 protected function doGetFileStat( array $params ) {
147 $src = $this->resolveHashKey( $params['src'] );
148 if ( $src === null ) {
149 return null;
152 if ( isset( $this->files[$src] ) ) {
153 return [
154 'mtime' => $this->files[$src]['mtime'],
155 'size' => strlen( $this->files[$src]['data'] ),
159 return false;
162 protected function doGetLocalCopyMulti( array $params ) {
163 $tmpFiles = []; // (path => TempFSFile)
164 foreach ( $params['srcs'] as $srcPath ) {
165 $src = $this->resolveHashKey( $srcPath );
166 if ( $src === null || !isset( $this->files[$src] ) ) {
167 $fsFile = null;
168 } else {
169 // Create a new temporary file with the same extension...
170 $ext = FileBackend::extensionFromPath( $src );
171 $fsFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory );
172 if ( $fsFile ) {
173 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
174 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
175 $fsFile = null;
179 $tmpFiles[$srcPath] = $fsFile;
182 return $tmpFiles;
185 protected function doDirectoryExists( $container, $dir, array $params ) {
186 $prefix = rtrim( "$container/$dir", '/' ) . '/';
187 foreach ( $this->files as $path => $data ) {
188 if ( strpos( $path, $prefix ) === 0 ) {
189 return true;
193 return false;
196 public function getDirectoryListInternal( $container, $dir, array $params ) {
197 $dirs = [];
198 $prefix = rtrim( "$container/$dir", '/' ) . '/';
199 $prefixLen = strlen( $prefix );
200 foreach ( $this->files as $path => $data ) {
201 if ( strpos( $path, $prefix ) === 0 ) {
202 $relPath = substr( $path, $prefixLen );
203 if ( $relPath === false ) {
204 continue;
205 } elseif ( strpos( $relPath, '/' ) === false ) {
206 continue; // just a file
208 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
209 if ( !empty( $params['topOnly'] ) ) {
210 $dirs[$parts[0]] = 1; // top directory
211 } else {
212 $current = '';
213 foreach ( $parts as $part ) { // all directories
214 $dir = ( $current === '' ) ? $part : "$current/$part";
215 $dirs[$dir] = 1;
216 $current = $dir;
222 return array_keys( $dirs );
225 public function getFileListInternal( $container, $dir, array $params ) {
226 $files = [];
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 ) {
233 continue;
234 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
235 continue;
237 $files[] = $relPath;
241 return $files;
244 protected function directoriesAreVirtual() {
245 return true;
249 * Get the absolute file system path for a storage path
251 * @param string $storagePath Storage path
252 * @return string|null
254 protected function resolveHashKey( $storagePath ) {
255 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
256 if ( $relPath === null ) {
257 return null; // invalid
260 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;