[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 * @author Aaron Schulz 23 */ 24 25 /** 26 * Simulation of a backend storage in memory. 27 * 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. 30 * 31 * @ingroup FileBackend 32 * @since 1.23 33 */ 34 class MemoryFileBackend extends FileBackendStore { 35 /** @var array Map of (file path => (data,mtime) */ 36 protected $files = array(); 37 38 public function isPathUsableInternal( $storagePath ) { 39 return true; 40 } 41 42 protected function doCreateInternal( array $params ) { 43 $status = Status::newGood(); 44 45 $dst = $this->resolveHashKey( $params['dst'] ); 46 if ( $dst === null ) { 47 $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); 48 49 return $status; 50 } 51 52 $this->files[$dst] = array( 53 'data' => $params['content'], 54 'mtime' => wfTimestamp( TS_MW, time() ) 55 ); 56 57 return $status; 58 } 59 60 protected function doStoreInternal( array $params ) { 61 $status = Status::newGood(); 62 63 $dst = $this->resolveHashKey( $params['dst'] ); 64 if ( $dst === null ) { 65 $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); 66 67 return $status; 68 } 69 70 wfSuppressWarnings(); 71 $data = file_get_contents( $params['src'] ); 72 wfRestoreWarnings(); 73 if ( $data === false ) { // source doesn't exist? 74 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); 75 76 return $status; 77 } 78 79 $this->files[$dst] = array( 80 'data' => $data, 81 'mtime' => wfTimestamp( TS_MW, time() ) 82 ); 83 84 return $status; 85 } 86 87 protected function doCopyInternal( array $params ) { 88 $status = Status::newGood(); 89 90 $src = $this->resolveHashKey( $params['src'] ); 91 if ( $src === null ) { 92 $status->fatal( 'backend-fail-invalidpath', $params['src'] ); 93 94 return $status; 95 } 96 97 $dst = $this->resolveHashKey( $params['dst'] ); 98 if ( $dst === null ) { 99 $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); 100 101 return $status; 102 } 103 104 if ( !isset( $this->files[$src] ) ) { 105 if ( empty( $params['ignoreMissingSource'] ) ) { 106 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); 107 } 108 109 return $status; 110 } 111 112 $this->files[$dst] = array( 113 'data' => $this->files[$src]['data'], 114 'mtime' => wfTimestamp( TS_MW, time() ) 115 ); 116 117 return $status; 118 } 119 120 protected function doDeleteInternal( array $params ) { 121 $status = Status::newGood(); 122 123 $src = $this->resolveHashKey( $params['src'] ); 124 if ( $src === null ) { 125 $status->fatal( 'backend-fail-invalidpath', $params['src'] ); 126 127 return $status; 128 } 129 130 if ( !isset( $this->files[$src] ) ) { 131 if ( empty( $params['ignoreMissingSource'] ) ) { 132 $status->fatal( 'backend-fail-delete', $params['src'] ); 133 } 134 135 return $status; 136 } 137 138 unset( $this->files[$src] ); 139 140 return $status; 141 } 142 143 protected function doGetFileStat( array $params ) { 144 $src = $this->resolveHashKey( $params['src'] ); 145 if ( $src === null ) { 146 return null; 147 } 148 149 if ( isset( $this->files[$src] ) ) { 150 return array( 151 'mtime' => $this->files[$src]['mtime'], 152 'size' => strlen( $this->files[$src]['data'] ), 153 ); 154 } 155 156 return false; 157 } 158 159 protected function doGetLocalCopyMulti( array $params ) { 160 $tmpFiles = array(); // (path => TempFSFile) 161 foreach ( $params['srcs'] as $srcPath ) { 162 $src = $this->resolveHashKey( $srcPath ); 163 if ( $src === null || !isset( $this->files[$src] ) ) { 164 $fsFile = null; 165 } else { 166 // Create a new temporary file with the same extension... 167 $ext = FileBackend::extensionFromPath( $src ); 168 $fsFile = TempFSFile::factory( 'localcopy_', $ext ); 169 if ( $fsFile ) { 170 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] ); 171 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) { 172 $fsFile = null; 173 } 174 } 175 } 176 $tmpFiles[$srcPath] = $fsFile; 177 } 178 179 return $tmpFiles; 180 } 181 182 protected function doStreamFile( array $params ) { 183 $status = Status::newGood(); 184 185 $src = $this->resolveHashKey( $params['src'] ); 186 if ( $src === null || !isset( $this->files[$src] ) ) { 187 $status->fatal( 'backend-fail-stream', $params['src'] ); 188 189 return $status; 190 } 191 192 print $this->files[$src]['data']; 193 194 return $status; 195 } 196 197 protected function doDirectoryExists( $container, $dir, array $params ) { 198 $prefix = rtrim( "$container/$dir", '/' ) . '/'; 199 foreach ( $this->files as $path => $data ) { 200 if ( strpos( $path, $prefix ) === 0 ) { 201 return true; 202 } 203 } 204 205 return false; 206 } 207 208 public function getDirectoryListInternal( $container, $dir, array $params ) { 209 $dirs = array(); 210 $prefix = rtrim( "$container/$dir", '/' ) . '/'; 211 $prefixLen = strlen( $prefix ); 212 foreach ( $this->files as $path => $data ) { 213 if ( strpos( $path, $prefix ) === 0 ) { 214 $relPath = substr( $path, $prefixLen ); 215 if ( $relPath === false ) { 216 continue; 217 } elseif ( strpos( $relPath, '/' ) === false ) { 218 continue; // just a file 219 } 220 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name 221 if ( !empty( $params['topOnly'] ) ) { 222 $dirs[$parts[0]] = 1; // top directory 223 } else { 224 $current = ''; 225 foreach ( $parts as $part ) { // all directories 226 $dir = ( $current === '' ) ? $part : "$current/$part"; 227 $dirs[$dir] = 1; 228 $current = $dir; 229 } 230 } 231 } 232 } 233 234 return array_keys( $dirs ); 235 } 236 237 public function getFileListInternal( $container, $dir, array $params ) { 238 $files = array(); 239 $prefix = rtrim( "$container/$dir", '/' ) . '/'; 240 $prefixLen = strlen( $prefix ); 241 foreach ( $this->files as $path => $data ) { 242 if ( strpos( $path, $prefix ) === 0 ) { 243 $relPath = substr( $path, $prefixLen ); 244 if ( $relPath === false ) { 245 continue; 246 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) { 247 continue; 248 } 249 $files[] = $relPath; 250 } 251 } 252 253 return $files; 254 } 255 256 protected function directoriesAreVirtual() { 257 return true; 258 } 259 260 /** 261 * Get the absolute file system path for a storage path 262 * 263 * @param string $storagePath Storage path 264 * @return string|null 265 */ 266 protected function resolveHashKey( $storagePath ) { 267 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath ); 268 if ( $relPath === null ) { 269 return null; // invalid 270 } 271 272 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont; 273 } 274 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |