| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace Wikimedia\FileBackend; |
| 25 |
|
| 26 |
use Wikimedia\AtEase\AtEase; |
| 27 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class MemoryFileBackend extends FileBackendStore { |
| 39 |
|
| 40 |
protected $files = []; |
| 41 |
|
| 42 |
public function getFeatures() { |
| 43 |
return self::ATTR_UNICODE_PATHS; |
| 44 |
} |
| 45 |
|
| 46 |
public function isPathUsableInternal( $storagePath ) { |
| 47 |
return ( $this->resolveHashKey( $storagePath ) !== null ); |
| 48 |
} |
| 49 |
|
| 50 |
protected function doCreateInternal( array $params ) { |
| 51 |
$status = $this->newStatus(); |
| 52 |
|
| 53 |
$dst = $this->resolveHashKey( $params['dst'] ); |
| 54 |
if ( $dst === null ) { |
| 55 |
$status->fatal( 'backend-fail-invalidpath', $params['dst'] ); |
| 56 |
|
| 57 |
return $status; |
| 58 |
} |
| 59 |
|
| 60 |
$this->files[$dst] = [ |
| 61 |
'data' => $params['content'], |
| 62 |
'mtime' => ConvertibleTimestamp::convert( TS_MW, time() ) |
| 63 |
]; |
| 64 |
|
| 65 |
return $status; |
| 66 |
} |
| 67 |
|
| 68 |
protected function doStoreInternal( array $params ) { |
| 69 |
$status = $this->newStatus(); |
| 70 |
|
| 71 |
$dst = $this->resolveHashKey( $params['dst'] ); |
| 72 |
if ( $dst === null ) { |
| 73 |
$status->fatal( 'backend-fail-invalidpath', $params['dst'] ); |
| 74 |
|
| 75 |
return $status; |
| 76 |
} |
| 77 |
|
| 78 |
AtEase::suppressWarnings(); |
| 79 |
$data = file_get_contents( $params['src'] ); |
| 80 |
AtEase::restoreWarnings(); |
| 81 |
if ( $data === false ) { |
| 82 |
$status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); |
| 83 |
|
| 84 |
return $status; |
| 85 |
} |
| 86 |
|
| 87 |
$this->files[$dst] = [ |
| 88 |
'data' => $data, |
| 89 |
'mtime' => ConvertibleTimestamp::convert( TS_MW, time() ) |
| 90 |
]; |
| 91 |
|
| 92 |
return $status; |
| 93 |
} |
| 94 |
|
| 95 |
protected function doCopyInternal( array $params ) { |
| 96 |
$status = $this->newStatus(); |
| 97 |
|
| 98 |
$src = $this->resolveHashKey( $params['src'] ); |
| 99 |
if ( $src === null ) { |
| 100 |
$status->fatal( 'backend-fail-invalidpath', $params['src'] ); |
| 101 |
|
| 102 |
return $status; |
| 103 |
} |
| 104 |
|
| 105 |
$dst = $this->resolveHashKey( $params['dst'] ); |
| 106 |
if ( $dst === null ) { |
| 107 |
$status->fatal( 'backend-fail-invalidpath', $params['dst'] ); |
| 108 |
|
| 109 |
return $status; |
| 110 |
} |
| 111 |
|
| 112 |
if ( !isset( $this->files[$src] ) ) { |
| 113 |
if ( empty( $params['ignoreMissingSource'] ) ) { |
| 114 |
$status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); |
| 115 |
} |
| 116 |
|
| 117 |
return $status; |
| 118 |
} |
| 119 |
|
| 120 |
$this->files[$dst] = [ |
| 121 |
'data' => $this->files[$src]['data'], |
| 122 |
'mtime' => ConvertibleTimestamp::convert( TS_MW, time() ) |
| 123 |
]; |
| 124 |
|
| 125 |
return $status; |
| 126 |
} |
| 127 |
|
| 128 |
protected function doMoveInternal( array $params ) { |
| 129 |
$status = $this->newStatus(); |
| 130 |
|
| 131 |
$src = $this->resolveHashKey( $params['src'] ); |
| 132 |
if ( $src === null ) { |
| 133 |
$status->fatal( 'backend-fail-invalidpath', $params['src'] ); |
| 134 |
|
| 135 |
return $status; |
| 136 |
} |
| 137 |
|
| 138 |
$dst = $this->resolveHashKey( $params['dst'] ); |
| 139 |
if ( $dst === null ) { |
| 140 |
$status->fatal( 'backend-fail-invalidpath', $params['dst'] ); |
| 141 |
|
| 142 |
return $status; |
| 143 |
} |
| 144 |
|
| 145 |
if ( !isset( $this->files[$src] ) ) { |
| 146 |
if ( empty( $params['ignoreMissingSource'] ) ) { |
| 147 |
$status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); |
| 148 |
} |
| 149 |
|
| 150 |
return $status; |
| 151 |
} |
| 152 |
|
| 153 |
$this->files[$dst] = $this->files[$src]; |
| 154 |
unset( $this->files[$src] ); |
| 155 |
$this->files[$dst]['mtime'] = ConvertibleTimestamp::convert( TS_MW, time() ); |
| 156 |
|
| 157 |
return $status; |
| 158 |
} |
| 159 |
|
| 160 |
protected function doDeleteInternal( array $params ) { |
| 161 |
$status = $this->newStatus(); |
| 162 |
|
| 163 |
$src = $this->resolveHashKey( $params['src'] ); |
| 164 |
if ( $src === null ) { |
| 165 |
$status->fatal( 'backend-fail-invalidpath', $params['src'] ); |
| 166 |
|
| 167 |
return $status; |
| 168 |
} |
| 169 |
|
| 170 |
if ( !isset( $this->files[$src] ) ) { |
| 171 |
if ( empty( $params['ignoreMissingSource'] ) ) { |
| 172 |
$status->fatal( 'backend-fail-delete', $params['src'] ); |
| 173 |
} |
| 174 |
|
| 175 |
return $status; |
| 176 |
} |
| 177 |
|
| 178 |
unset( $this->files[$src] ); |
| 179 |
|
| 180 |
return $status; |
| 181 |
} |
| 182 |
|
| 183 |
protected function doGetFileStat( array $params ) { |
| 184 |
$src = $this->resolveHashKey( $params['src'] ); |
| 185 |
if ( $src === null ) { |
| 186 |
return self::RES_ERROR; |
| 187 |
} |
| 188 |
|
| 189 |
if ( isset( $this->files[$src] ) ) { |
| 190 |
return [ |
| 191 |
'mtime' => $this->files[$src]['mtime'], |
| 192 |
'size' => strlen( $this->files[$src]['data'] ), |
| 193 |
]; |
| 194 |
} |
| 195 |
|
| 196 |
return self::RES_ABSENT; |
| 197 |
} |
| 198 |
|
| 199 |
protected function doGetLocalCopyMulti( array $params ) { |
| 200 |
$tmpFiles = []; |
| 201 |
foreach ( $params['srcs'] as $srcPath ) { |
| 202 |
$src = $this->resolveHashKey( $srcPath ); |
| 203 |
if ( $src === null ) { |
| 204 |
$fsFile = self::RES_ERROR; |
| 205 |
} elseif ( !isset( $this->files[$src] ) ) { |
| 206 |
$fsFile = self::RES_ABSENT; |
| 207 |
} else { |
| 208 |
|
| 209 |
$ext = FileBackend::extensionFromPath( $src ); |
| 210 |
$fsFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext ); |
| 211 |
if ( $fsFile ) { |
| 212 |
$bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] ); |
| 213 |
if ( $bytes !== strlen( $this->files[$src]['data'] ) ) { |
| 214 |
$fsFile = self::RES_ERROR; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
$tmpFiles[$srcPath] = $fsFile; |
| 219 |
} |
| 220 |
|
| 221 |
return $tmpFiles; |
| 222 |
} |
| 223 |
|
| 224 |
protected function doDirectoryExists( $container, $dir, array $params ) { |
| 225 |
$prefix = rtrim( "$container/$dir", '/' ) . '/'; |
| 226 |
foreach ( $this->files as $path => $data ) { |
| 227 |
if ( strpos( $path, $prefix ) === 0 ) { |
| 228 |
return true; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
public function getDirectoryListInternal( $container, $dir, array $params ) { |
| 236 |
$dirs = []; |
| 237 |
$prefix = rtrim( "$container/$dir", '/' ) . '/'; |
| 238 |
$prefixLen = strlen( $prefix ); |
| 239 |
foreach ( $this->files as $path => $data ) { |
| 240 |
if ( strpos( $path, $prefix ) === 0 ) { |
| 241 |
$relPath = substr( $path, $prefixLen ); |
| 242 |
if ( strpos( $relPath, '/' ) === false ) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
$parts = array_slice( explode( '/', $relPath ), 0, -1 ); |
| 246 |
if ( !empty( $params['topOnly'] ) ) { |
| 247 |
$dirs[$parts[0]] = 1; |
| 248 |
} else { |
| 249 |
$current = ''; |
| 250 |
foreach ( $parts as $part ) { |
| 251 |
$dir = ( $current === '' ) ? $part : "$current/$part"; |
| 252 |
$dirs[$dir] = 1; |
| 253 |
$current = $dir; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return array_keys( $dirs ); |
| 260 |
} |
| 261 |
|
| 262 |
public function getFileListInternal( $container, $dir, array $params ) { |
| 263 |
$files = []; |
| 264 |
$prefix = rtrim( "$container/$dir", '/' ) . '/'; |
| 265 |
$prefixLen = strlen( $prefix ); |
| 266 |
foreach ( $this->files as $path => $data ) { |
| 267 |
if ( strpos( $path, $prefix ) === 0 ) { |
| 268 |
$relPath = substr( $path, $prefixLen ); |
| 269 |
if ( |
| 270 |
$relPath === '' || |
| 271 |
( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) |
| 272 |
) { |
| 273 |
continue; |
| 274 |
} |
| 275 |
$files[] = $relPath; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $files; |
| 280 |
} |
| 281 |
|
| 282 |
protected function directoriesAreVirtual() { |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
protected function resolveHashKey( $storagePath ) { |
| 293 |
[ $fullCont, $relPath ] = $this->resolveStoragePathReal( $storagePath ); |
| 294 |
if ( $relPath === null ) { |
| 295 |
return null; |
| 296 |
} |
| 297 |
|
| 298 |
return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
class_alias( MemoryFileBackend::class, 'MemoryFileBackend' ); |