23use Psr\Log\LoggerInterface;
24use Wikimedia\RequestTimeout\TimeoutException;
67 private const STATE_NEW = 1;
68 private const STATE_CHECKED = 2;
69 private const STATE_ATTEMPTED = 3;
89 foreach ( $required as $name ) {
90 if ( isset(
$params[$name] ) ) {
91 $this->params[$name] =
$params[$name];
93 throw new InvalidArgumentException(
"File operation missing parameter '$name'." );
96 foreach ( $optional as $name ) {
97 if ( isset(
$params[$name] ) ) {
98 $this->params[$name] =
$params[$name];
101 foreach ( $paths as $name ) {
102 if ( isset( $this->params[$name] ) ) {
119 return $res ??
$path;
132 return $this->params[$name] ??
null;
150 return [ self::ASSUMED_EXISTS => [], self::ASSUMED_SHA1 => [], self::ASSUMED_SIZE => [] ];
159 return [
'read' => [],
'write' => [] ];
183 if ( isset( $deps[
'read'][
$path] ) || isset( $deps[
'write'][
$path] ) ) {
188 if ( isset( $deps[
'write'][
$path] ) ) {
204 final public function precheck( array &$predicates ) {
205 if ( $this->state !== self::STATE_NEW ) {
206 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_NEW, $this->state );
208 $this->state = self::STATE_CHECKED;
210 $status = StatusValue::newGood();
212 if ( !$this->backend->isPathUsableInternal(
$path ) ) {
213 $status->fatal(
'backend-fail-usable',
$path );
216 if ( !$status->isOK() ) {
221 if ( !$status->isOK() ) {
233 return StatusValue::newGood();
242 if ( $this->state !== self::STATE_CHECKED ) {
243 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_CHECKED, $this->state );
244 } elseif ( $this->
failed ) {
245 return StatusValue::newFatal(
'fileop-fail-attempt-precheck' );
247 $this->state = self::STATE_ATTEMPTED;
248 if ( $this->cancelled ) {
249 $status = StatusValue::newGood();
252 if ( !$status->isOK() ) {
265 return StatusValue::newGood();
276 $this->async =
false;
287 $this->state = self::STATE_CHECKED;
298 $this->state = self::STATE_CHECKED;
309 return [ [], [], [] ];
346 return array_values( array_unique(
360 $status = StatusValue::newGood();
364 $this->sourceSize ??= $this->
fileSize( $this->params[
'src'], $predicates );
368 $this->sourceSha1 ??= $this->
fileSha1( $this->params[
'src'], $predicates );
370 $this->destExists = $this->
fileExists( $this->params[
'dst'], $predicates );
372 $this->overwriteSameCase =
false;
373 if ( $this->destExists ) {
374 if ( $this->
getParam(
'overwrite' ) ) {
376 } elseif ( $this->
getParam(
'overwriteSame' ) ) {
378 $dhash = $this->
fileSha1( $this->params[
'dst'], $predicates );
379 $dsize = $this->
fileSize( $this->params[
'dst'], $predicates );
381 if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
382 $status->fatal(
'backend-fail-hashes' );
383 } elseif ( !is_int( $this->sourceSize ) || !is_int( $dsize ) ) {
384 $status->fatal(
'backend-fail-sizes' );
385 } elseif ( $this->sourceSha1 !== $dhash || $this->sourceSize !== $dsize ) {
387 $status->fatal(
'backend-fail-notsame', $this->params[
'dst'] );
389 $this->overwriteSameCase =
true;
392 $status->fatal(
'backend-fail-alreadyexists', $this->params[
'dst'] );
394 } elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) {
395 $status->fatal(
'backend-fail-stat', $this->params[
'dst'] );
432 if ( isset( $predicates[self::ASSUMED_EXISTS][
$source] ) ) {
437 return $this->backend->fileExists(
$params );
453 if ( isset( $predicates[self::ASSUMED_SIZE][
$source] ) ) {
456 isset( $predicates[self::ASSUMED_EXISTS][
$source] ) &&
457 !$predicates[self::ASSUMED_EXISTS][
$source]
463 return $this->backend->getFileSize(
$params );
475 if ( isset( $predicates[self::ASSUMED_SHA1][
$source] ) ) {
478 isset( $predicates[self::ASSUMED_EXISTS][
$source] ) &&
479 !$predicates[self::ASSUMED_EXISTS][
$source]
485 return $this->backend->getFileSha1Base36(
$params );
505 $params[
'failedAction'] = $action;
507 $this->logger->error( static::class .
508 " failed: " . FormatJson::encode(
$params ) );
509 }
catch ( TimeoutException $e ) {
511 }
catch ( Exception $e ) {
Base class for all backends using particular storage medium.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
static normalizeStoragePath( $storagePath)
Normalize a storage path by cleaning up directory separators.
FileBackend helper class for representing operations.
static normalizeIfValidStoragePath( $path)
Normalize a string if it is a valid storage path.
static newDependencies()
Get a new empty dependency tracking array for paths read/written to.
fileSha1( $source, array $predicates)
Get the SHA-1 of a file in storage when this operation is attempted.
static newPredicates()
Get a new empty predicates array for precheck()
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
allowedParams()
Get the file operation parameters.
attemptAsync()
Attempt the operation in the background.
doPrecheck(array &$predicates)
getParam( $name)
Get the value of the parameter with the given name.
storagePathsReadOrChanged()
Get a list of storage paths read from or written to for this operation.
getSourceSize()
precheckDestExistence() helper function to get the source file size.
storagePathsChanged()
Get a list of storage paths written to for this operation.
applyDependencies(array $deps)
Update a dependency tracking array to account for this operation.
attempt()
Attempt the operation.
precheck(array &$predicates)
Check preconditions of the operation without writing anything.
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
fileSize( $source, array $predicates)
Get the size a file in storage will have when this operation is attempted.
logFailure( $action)
Log a file operation failure and preserve any temp files.
storagePathsRead()
Get a list of storage paths read from for this operation.
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
attemptQuick()
Attempt the operation without regards to prechecks.
FileBackendStore $backend
dependsOn(array $deps)
Check if this operation changes files listed in $paths.
getBackend()
Get the backend this operation is for.
attemptAsyncQuick()
Attempt the operation in the background without regards to prechecks.
failed()
Check if this operation failed precheck() or attempt()
__construct(FileBackendStore $backend, array $params, LoggerInterface $logger)
Build a new batch file operation transaction.
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.