| 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\FileOps; |
| 25 |
|
| 26 |
use Closure; |
| 27 |
use Exception; |
| 28 |
use InvalidArgumentException; |
| 29 |
use MediaWiki\Json\FormatJson; |
| 30 |
use Psr\Log\LoggerInterface; |
| 31 |
use StatusValue; |
| 32 |
use Wikimedia\FileBackend\FileBackend; |
| 33 |
use Wikimedia\FileBackend\FileBackendStore; |
| 34 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
abstract class FileOp { |
| 48 |
|
| 49 |
protected $backend; |
| 50 |
|
| 51 |
protected $logger; |
| 52 |
|
| 53 |
|
| 54 |
protected $params = []; |
| 55 |
|
| 56 |
|
| 57 |
protected $state = self::STATE_NEW; |
| 58 |
|
| 59 |
protected $failed = false; |
| 60 |
|
| 61 |
protected $async = false; |
| 62 |
|
| 63 |
protected $noOp = false; |
| 64 |
|
| 65 |
|
| 66 |
protected $overwriteSameCase; |
| 67 |
|
| 68 |
protected $destExists; |
| 69 |
|
| 70 |
|
| 71 |
private const STATE_NEW = 1; |
| 72 |
|
| 73 |
private const STATE_CHECKED = 2; |
| 74 |
|
| 75 |
private const STATE_ATTEMPTED = 3; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
final public function __construct( |
| 85 |
FileBackendStore $backend, array $params, LoggerInterface $logger |
| 86 |
) { |
| 87 |
$this->backend = $backend; |
| 88 |
$this->logger = $logger; |
| 89 |
[ $required, $optional, $paths ] = $this->allowedParams(); |
| 90 |
foreach ( $required as $name ) { |
| 91 |
if ( isset( $params[$name] ) ) { |
| 92 |
$this->params[$name] = $params[$name]; |
| 93 |
} else { |
| 94 |
throw new InvalidArgumentException( "File operation missing parameter '$name'." ); |
| 95 |
} |
| 96 |
} |
| 97 |
foreach ( $optional as $name ) { |
| 98 |
if ( isset( $params[$name] ) ) { |
| 99 |
$this->params[$name] = $params[$name]; |
| 100 |
} |
| 101 |
} |
| 102 |
foreach ( $paths as $name ) { |
| 103 |
if ( isset( $this->params[$name] ) ) { |
| 104 |
|
| 105 |
$this->params[$name] = self::normalizeIfValidStoragePath( $this->params[$name] ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected static function normalizeIfValidStoragePath( $path ) { |
| 117 |
if ( FileBackend::isStoragePath( $path ) ) { |
| 118 |
$res = FileBackend::normalizeStoragePath( $path ); |
| 119 |
|
| 120 |
return $res ?? $path; |
| 121 |
} |
| 122 |
|
| 123 |
return $path; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
final public function getParam( $name ) { |
| 133 |
return $this->params[$name] ?? null; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
final public function failed() { |
| 142 |
return $this->failed; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
final public static function newDependencies() { |
| 151 |
return [ 'read' => [], 'write' => [] ]; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
final public function applyDependencies( array $deps ) { |
| 161 |
$deps['read'] += array_fill_keys( $this->storagePathsRead(), 1 ); |
| 162 |
$deps['write'] += array_fill_keys( $this->storagePathsChanged(), 1 ); |
| 163 |
|
| 164 |
return $deps; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
final public function dependsOn( array $deps ) { |
| 174 |
foreach ( $this->storagePathsChanged() as $path ) { |
| 175 |
if ( isset( $deps['read'][$path] ) || isset( $deps['write'][$path] ) ) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
} |
| 179 |
foreach ( $this->storagePathsRead() as $path ) { |
| 180 |
if ( isset( $deps['write'][$path] ) ) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
final public function precheck( FileStatePredicates $predicates ) { |
| 197 |
if ( $this->state !== self::STATE_NEW ) { |
| 198 |
return StatusValue::newFatal( 'fileop-fail-state', self::STATE_NEW, $this->state ); |
| 199 |
} |
| 200 |
$this->state = self::STATE_CHECKED; |
| 201 |
|
| 202 |
$status = StatusValue::newGood(); |
| 203 |
foreach ( $this->storagePathsReadOrChanged() as $path ) { |
| 204 |
if ( !$this->backend->isPathUsableInternal( $path ) ) { |
| 205 |
$status->fatal( 'backend-fail-usable', $path ); |
| 206 |
} |
| 207 |
} |
| 208 |
if ( !$status->isOK() ) { |
| 209 |
return $status; |
| 210 |
} |
| 211 |
|
| 212 |
$opPredicates = $predicates->snapshot( $this->storagePathsReadOrChanged() ); |
| 213 |
$status = $this->doPrecheck( $opPredicates, $predicates ); |
| 214 |
if ( !$status->isOK() ) { |
| 215 |
$this->failed = true; |
| 216 |
} |
| 217 |
|
| 218 |
return $status; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected function doPrecheck( |
| 231 |
FileStatePredicates $opPredicates, |
| 232 |
FileStatePredicates $batchPredicates |
| 233 |
) { |
| 234 |
return StatusValue::newGood(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
final public function attempt() { |
| 243 |
if ( $this->state !== self::STATE_CHECKED ) { |
| 244 |
return StatusValue::newFatal( 'fileop-fail-state', self::STATE_CHECKED, $this->state ); |
| 245 |
} elseif ( $this->failed ) { |
| 246 |
return StatusValue::newFatal( 'fileop-fail-attempt-precheck' ); |
| 247 |
} |
| 248 |
$this->state = self::STATE_ATTEMPTED; |
| 249 |
if ( $this->noOp ) { |
| 250 |
$status = StatusValue::newGood(); |
| 251 |
} else { |
| 252 |
$status = $this->doAttempt(); |
| 253 |
if ( !$status->isOK() ) { |
| 254 |
$this->failed = true; |
| 255 |
$this->logFailure( 'attempt' ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return $status; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
protected function doAttempt() { |
| 266 |
return StatusValue::newGood(); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
final public function attemptAsync() { |
| 275 |
$this->async = true; |
| 276 |
$result = $this->attempt(); |
| 277 |
$this->async = false; |
| 278 |
|
| 279 |
return $result; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
final public function attemptQuick() { |
| 288 |
$this->state = self::STATE_CHECKED; |
| 289 |
|
| 290 |
return $this->attempt(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
final public function attemptAsyncQuick() { |
| 299 |
$this->state = self::STATE_CHECKED; |
| 300 |
|
| 301 |
return $this->attemptAsync(); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
protected function allowedParams() { |
| 310 |
return [ [], [], [] ]; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
protected function setFlags( array $params ) { |
| 320 |
return [ 'async' => $this->async ] + $params; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function storagePathsRead() { |
| 329 |
return []; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function storagePathsChanged() { |
| 338 |
return []; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
final public function storagePathsReadOrChanged() { |
| 347 |
return array_values( array_unique( |
| 348 |
array_merge( $this->storagePathsRead(), $this->storagePathsChanged() ) |
| 349 |
) ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
protected function precheckDestExistence( |
| 364 |
FileStatePredicates $opPredicates, |
| 365 |
$sourceSize, |
| 366 |
$sourceSha1 |
| 367 |
) { |
| 368 |
$status = StatusValue::newGood(); |
| 369 |
|
| 370 |
$this->destExists = $this->resolveFileExistence( $this->params['dst'], $opPredicates ); |
| 371 |
|
| 372 |
$this->overwriteSameCase = false; |
| 373 |
if ( $this->destExists ) { |
| 374 |
if ( $this->getParam( 'overwrite' ) ) { |
| 375 |
return $status; |
| 376 |
} elseif ( $this->getParam( 'overwriteSame' ) ) { |
| 377 |
|
| 378 |
$sourceSize = ( $sourceSize instanceof Closure ) ? $sourceSize() : $sourceSize; |
| 379 |
$sourceSha1 = ( $sourceSha1 instanceof Closure ) ? $sourceSha1() : $sourceSha1; |
| 380 |
$dstSha1 = $this->resolveFileSha1Base36( $this->params['dst'], $opPredicates ); |
| 381 |
$dstSize = $this->resolveFileSize( $this->params['dst'], $opPredicates ); |
| 382 |
|
| 383 |
if ( !strlen( $sourceSha1 ) || !strlen( $dstSha1 ) ) { |
| 384 |
$status->fatal( 'backend-fail-hashes' ); |
| 385 |
} elseif ( !is_int( $sourceSize ) || !is_int( $dstSize ) ) { |
| 386 |
$status->fatal( 'backend-fail-sizes' ); |
| 387 |
} elseif ( $sourceSha1 !== $dstSha1 || $sourceSize !== $dstSize ) { |
| 388 |
|
| 389 |
$status->fatal( 'backend-fail-notsame', $this->params['dst'] ); |
| 390 |
} else { |
| 391 |
$this->overwriteSameCase = true; |
| 392 |
} |
| 393 |
} else { |
| 394 |
$status->fatal( 'backend-fail-alreadyexists', $this->params['dst'] ); |
| 395 |
} |
| 396 |
} elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) { |
| 397 |
$status->fatal( 'backend-fail-stat', $this->params['dst'] ); |
| 398 |
} |
| 399 |
|
| 400 |
return $status; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
final protected function resolveFileExistence( $source, FileStatePredicates $opPredicates ) { |
| 414 |
return $opPredicates->resolveFileExistence( |
| 415 |
$source, |
| 416 |
function ( $path ) { |
| 417 |
return $this->backend->fileExists( [ 'src' => $path, 'latest' => true ] ); |
| 418 |
} |
| 419 |
); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
final protected function resolveFileSize( $source, FileStatePredicates $opPredicates ) { |
| 434 |
return $opPredicates->resolveFileSize( |
| 435 |
$source, |
| 436 |
function ( $path ) { |
| 437 |
return $this->backend->getFileSize( [ 'src' => $path, 'latest' => true ] ); |
| 438 |
} |
| 439 |
); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
final protected function resolveFileSha1Base36( $source, FileStatePredicates $opPredicates ) { |
| 450 |
return $opPredicates->resolveFileSha1Base36( |
| 451 |
$source, |
| 452 |
function ( $path ) { |
| 453 |
return $this->backend->getFileSha1Base36( [ 'src' => $path, 'latest' => true ] ); |
| 454 |
} |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function getBackend() { |
| 464 |
return $this->backend; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
final public function logFailure( $action ) { |
| 473 |
$params = $this->params; |
| 474 |
$params['failedAction'] = $action; |
| 475 |
try { |
| 476 |
$this->logger->error( static::class . |
| 477 |
" failed: " . FormatJson::encode( $params ) ); |
| 478 |
} catch ( TimeoutException $e ) { |
| 479 |
throw $e; |
| 480 |
} catch ( Exception $e ) { |
| 481 |
|
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
class_alias( FileOp::class, 'FileOp' ); |