]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/filebackend/MemoryFileBackend.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / includes / libs / filebackend / MemoryFileBackend.php
1 <?php
2 /**
3  * Simulation of a backend storage in memory.
4  *
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.
9  *
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.
14  *
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
19  *
20  * @file
21  * @ingroup FileBackend
22  */
23
24 /**
25  * Simulation of a backend storage in memory.
26  *
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.
29  *
30  * @ingroup FileBackend
31  * @since 1.23
32  */
33 class MemoryFileBackend extends FileBackendStore {
34         /** @var array Map of (file path => (data,mtime) */
35         protected $files = [];
36
37         public function getFeatures() {
38                 return self::ATTR_UNICODE_PATHS;
39         }
40
41         public function isPathUsableInternal( $storagePath ) {
42                 return true;
43         }
44
45         protected function doCreateInternal( array $params ) {
46                 $status = $this->newStatus();
47
48                 $dst = $this->resolveHashKey( $params['dst'] );
49                 if ( $dst === null ) {
50                         $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
51
52                         return $status;
53                 }
54
55                 $this->files[$dst] = [
56                         'data' => $params['content'],
57                         'mtime' => wfTimestamp( TS_MW, time() )
58                 ];
59
60                 return $status;
61         }
62
63         protected function doStoreInternal( array $params ) {
64                 $status = $this->newStatus();
65
66                 $dst = $this->resolveHashKey( $params['dst'] );
67                 if ( $dst === null ) {
68                         $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
69
70                         return $status;
71                 }
72
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'] );
78
79                         return $status;
80                 }
81
82                 $this->files[$dst] = [
83                         'data' => $data,
84                         'mtime' => wfTimestamp( TS_MW, time() )
85                 ];
86
87                 return $status;
88         }
89
90         protected function doCopyInternal( array $params ) {
91                 $status = $this->newStatus();
92
93                 $src = $this->resolveHashKey( $params['src'] );
94                 if ( $src === null ) {
95                         $status->fatal( 'backend-fail-invalidpath', $params['src'] );
96
97                         return $status;
98                 }
99
100                 $dst = $this->resolveHashKey( $params['dst'] );
101                 if ( $dst === null ) {
102                         $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
103
104                         return $status;
105                 }
106
107                 if ( !isset( $this->files[$src] ) ) {
108                         if ( empty( $params['ignoreMissingSource'] ) ) {
109                                 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
110                         }
111
112                         return $status;
113                 }
114
115                 $this->files[$dst] = [
116                         'data' => $this->files[$src]['data'],
117                         'mtime' => wfTimestamp( TS_MW, time() )
118                 ];
119
120                 return $status;
121         }
122
123         protected function doDeleteInternal( array $params ) {
124                 $status = $this->newStatus();
125
126                 $src = $this->resolveHashKey( $params['src'] );
127                 if ( $src === null ) {
128                         $status->fatal( 'backend-fail-invalidpath', $params['src'] );
129
130                         return $status;
131                 }
132
133                 if ( !isset( $this->files[$src] ) ) {
134                         if ( empty( $params['ignoreMissingSource'] ) ) {
135                                 $status->fatal( 'backend-fail-delete', $params['src'] );
136                         }
137
138                         return $status;
139                 }
140
141                 unset( $this->files[$src] );
142
143                 return $status;
144         }
145
146         protected function doGetFileStat( array $params ) {
147                 $src = $this->resolveHashKey( $params['src'] );
148                 if ( $src === null ) {
149                         return null;
150                 }
151
152                 if ( isset( $this->files[$src] ) ) {
153                         return [
154                                 'mtime' => $this->files[$src]['mtime'],
155                                 'size' => strlen( $this->files[$src]['data'] ),
156                         ];
157                 }
158
159                 return false;
160         }
161
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;
176                                         }
177                                 }
178                         }
179                         $tmpFiles[$srcPath] = $fsFile;
180                 }
181
182                 return $tmpFiles;
183         }
184
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;
190                         }
191                 }
192
193                 return false;
194         }
195
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
207                                 }
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;
217                                         }
218                                 }
219                         }
220                 }
221
222                 return array_keys( $dirs );
223         }
224
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;
236                                 }
237                                 $files[] = $relPath;
238                         }
239                 }
240
241                 return $files;
242         }
243
244         protected function directoriesAreVirtual() {
245                 return true;
246         }
247
248         /**
249          * Get the absolute file system path for a storage path
250          *
251          * @param string $storagePath Storage path
252          * @return string|null
253          */
254         protected function resolveHashKey( $storagePath ) {
255                 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
256                 if ( $relPath === null ) {
257                         return null; // invalid
258                 }
259
260                 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
261         }
262 }