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;
88 list( $required, $optional, $paths ) = $this->
allowedParams();
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] ) ) {
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();
363 if ( $this->sourceSize ===
null ) {
364 $this->sourceSize = $this->
fileSize( $this->params[
'src'], $predicates );
368 if ( $this->sourceSha1 ===
null ) {
369 $this->sourceSha1 = $this->
fileSha1( $this->params[
'src'], $predicates );
372 $this->destExists = $this->
fileExists( $this->params[
'dst'], $predicates );
374 $this->overwriteSameCase =
false;
375 if ( $this->destExists ) {
376 if ( $this->
getParam(
'overwrite' ) ) {
378 } elseif ( $this->
getParam(
'overwriteSame' ) ) {
380 $dhash = $this->
fileSha1( $this->params[
'dst'], $predicates );
381 $dsize = $this->
fileSize( $this->params[
'dst'], $predicates );
383 if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
384 $status->fatal(
'backend-fail-hashes' );
385 } elseif ( !is_int( $this->sourceSize ) || !is_int( $dsize ) ) {
386 $status->fatal(
'backend-fail-sizes' );
387 } elseif ( $this->sourceSha1 !== $dhash || $this->sourceSize !== $dsize ) {
389 $status->fatal(
'backend-fail-notsame', $this->params[
'dst'] );
391 $this->overwriteSameCase =
true;
394 $status->fatal(
'backend-fail-alreadyexists', $this->params[
'dst'] );
396 } elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) {
397 $status->fatal(
'backend-fail-stat', $this->params[
'dst'] );
434 if ( isset( $predicates[self::ASSUMED_EXISTS][
$source] ) ) {
439 return $this->backend->fileExists(
$params );
455 if ( isset( $predicates[self::ASSUMED_SIZE][
$source] ) ) {
458 isset( $predicates[self::ASSUMED_EXISTS][
$source] ) &&
459 !$predicates[self::ASSUMED_EXISTS][
$source]
465 return $this->backend->getFileSize(
$params );
477 if ( isset( $predicates[self::ASSUMED_SHA1][
$source] ) ) {
480 isset( $predicates[self::ASSUMED_EXISTS][
$source] ) &&
481 !$predicates[self::ASSUMED_EXISTS][
$source]
487 return $this->backend->getFileSha1Base36(
$params );
507 $params[
'failedAction'] = $action;
509 $this->logger->error( static::class .
510 " failed: " . FormatJson::encode(
$params ) );
511 }
catch ( TimeoutException $e ) {
513 }
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.