23use Psr\Log\LoggerInterface;
87 list( $required, $optional, $paths ) = $this->
allowedParams();
88 foreach ( $required as $name ) {
89 if ( isset(
$params[$name] ) ) {
90 $this->params[$name] =
$params[$name];
92 throw new InvalidArgumentException(
"File operation missing parameter '$name'." );
95 foreach ( $optional as $name ) {
96 if ( isset(
$params[$name] ) ) {
97 $this->params[$name] =
$params[$name];
100 foreach ( $paths as $name ) {
101 if ( isset( $this->params[$name] ) ) {
140 return $this->params[$name] ??
null;
158 return [
'exists' => [],
'sha1' => [] ];
167 return [
'read' => [],
'write' => [] ];
191 if ( isset( $deps[
'read'][
$path] ) || isset( $deps[
'write'][
$path] ) ) {
196 if ( isset( $deps[
'write'][
$path] ) ) {
212 if ( !$this->doOperation ) {
219 foreach ( array_unique( $pathsUsed ) as
$path ) {
223 'newSha1' => $this->
fileSha1( $path, $oPredicates )
227 if ( $nPredicates[
'sha1'][
$path] ===
false ) {
235 'op' => $this->
fileExists( $path, $oPredicates ) ?
'update' :
'create',
237 'newSha1' => $nPredicates[
'sha1'][
$path]
242 return array_merge( $nullEntries, $updateEntries, $deleteEntries );
253 final public function precheck( array &$predicates ) {
254 if ( $this->state !== self::STATE_NEW ) {
255 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_NEW, $this->state );
259 $status = StatusValue::newGood();
261 foreach ( array_unique( $storagePaths ) as $storagePath ) {
262 if ( !$this->backend->isPathUsableInternal( $storagePath ) ) {
263 $status->fatal(
'backend-fail-usable', $storagePath );
266 if ( !$status->isOK() ) {
271 if ( !$status->isOK() ) {
283 return StatusValue::newGood();
292 if ( $this->state !== self::STATE_CHECKED ) {
293 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_CHECKED, $this->state );
294 } elseif ( $this->
failed ) {
295 return StatusValue::newFatal(
'fileop-fail-attempt-precheck' );
298 if ( $this->doOperation ) {
300 if ( !$status->isOK() ) {
305 $status = StatusValue::newGood();
315 return StatusValue::newGood();
326 $this->async =
false;
337 return [ [], [], [] ];
377 $status = StatusValue::newGood();
380 if ( $this->sourceSha1 ===
null ) {
381 $this->sourceSha1 = $this->
fileSha1( $this->params[
'src'], $predicates );
383 $this->overwriteSameCase =
false;
384 $this->destExists = $this->
fileExists( $this->params[
'dst'], $predicates );
385 if ( $this->destExists ) {
386 if ( $this->
getParam(
'overwrite' ) ) {
388 } elseif ( $this->
getParam(
'overwriteSame' ) ) {
389 $dhash = $this->
fileSha1( $this->params[
'dst'], $predicates );
391 if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
392 $status->fatal(
'backend-fail-hashes' );
393 } elseif ( $this->sourceSha1 !== $dhash ) {
395 $status->fatal(
'backend-fail-notsame', $this->params[
'dst'] );
397 $this->overwriteSameCase =
true;
402 $status->fatal(
'backend-fail-alreadyexists', $this->params[
'dst'] );
406 } elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) {
407 $status->fatal(
'backend-fail-stat', $this->params[
'dst'] );
434 if ( isset( $predicates[
'exists'][
$source] ) ) {
435 return $predicates[
'exists'][
$source];
439 return $this->backend->fileExists(
$params );
454 if ( isset( $predicates[
'sha1'][
$source] ) ) {
455 return $predicates[
'sha1'][
$source];
456 } elseif ( isset( $predicates[
'exists'][
$source] ) && !$predicates[
'exists'][
$source] ) {
461 return $this->backend->getFileSha1Base36(
$params );
481 $params[
'failedAction'] = $action;
483 $this->logger->error( static::class .
484 " failed (batch #{$this->batchId}): " . FormatJson::encode(
$params ) );
485 }
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.
setBatchId( $batchId)
Set the batch UUID this operation belongs to.
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 hash a file in storage will have 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.
bool $doOperation
Operation is not a no-op.
attemptAsync()
Attempt the operation in the background.
doPrecheck(array &$predicates)
getParam( $name)
Get the value of the parameter with the given name.
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.
getJournalEntries(array $oPredicates, array $nPredicates)
Get the file journal entries for this file operation.
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.
FileBackendStore $backend
dependsOn(array $deps)
Check if this operation changes files listed in $paths.
getBackend()
Get the backend this operation is for.
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.