MediaWiki master
MoveFileOp.php
Go to the documentation of this file.
1<?php
28class MoveFileOp extends FileOp {
29 protected function allowedParams() {
30 return [
31 [ 'src', 'dst' ],
32 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
33 [ 'src', 'dst' ]
34 ];
35 }
36
37 protected function doPrecheck(
38 FileStatePredicates $opPredicates,
39 FileStatePredicates $batchPredicates
40 ) {
41 $status = StatusValue::newGood();
42
43 // Check source file existence
44 $srcExists = $this->resolveFileExistence( $this->params['src'], $opPredicates );
45 if ( $srcExists === false ) {
46 if ( $this->getParam( 'ignoreMissingSource' ) ) {
47 $this->noOp = true; // no-op
48 // Update file existence predicates (cache 404s)
49 $batchPredicates->assumeFileDoesNotExist( $this->params['src'] );
50
51 return $status; // nothing to do
52 } else {
53 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
54
55 return $status;
56 }
57 } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
58 $status->fatal( 'backend-fail-stat', $this->params['src'] );
59
60 return $status;
61 }
62 // Check if an incompatible destination file exists
63 $srcSize = function () use ( $opPredicates ) {
64 static $size = null;
65 $size ??= $this->resolveFileSize( $this->params['src'], $opPredicates );
66 return $size;
67 };
68 $srcSha1 = function () use ( $opPredicates ) {
69 static $sha1 = null;
70 $sha1 ??= $this->resolveFileSha1Base36( $this->params['src'], $opPredicates );
71 return $sha1;
72 };
73 $status->merge( $this->precheckDestExistence( $opPredicates, $srcSize, $srcSha1 ) );
74 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
75
76 // Update file existence predicates if the operation is expected to be allowed to run
77 if ( $status->isOK() ) {
78 $batchPredicates->assumeFileExists( $this->params['dst'], $srcSize, $srcSha1 );
79 if ( $this->params['src'] !== $this->params['dst'] ) {
80 $batchPredicates->assumeFileDoesNotExist( $this->params['src'] );
81 }
82 }
83
84 return $status; // safe to call attempt()
85 }
86
87 protected function doAttempt() {
88 if ( $this->overwriteSameCase ) {
89 if ( $this->params['src'] === $this->params['dst'] ) {
90 // Do nothing to the destination (which is also the source)
91 $status = StatusValue::newGood();
92 } else {
93 // Just delete the source as the destination file needs no changes
94 $status = $this->backend->deleteInternal( $this->setFlags(
95 [ 'src' => $this->params['src'] ]
96 ) );
97 }
98 } elseif ( $this->params['src'] === $this->params['dst'] ) {
99 // Just update the destination file headers
100 $headers = $this->getParam( 'headers' ) ?: [];
101 $status = $this->backend->describeInternal( $this->setFlags(
102 [ 'src' => $this->params['dst'], 'headers' => $headers ]
103 ) );
104 } else {
105 // Move the file to the destination
106 $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
107 }
108
109 return $status;
110 }
111
112 public function storagePathsRead() {
113 return [ $this->params['src'] ];
114 }
115
116 public function storagePathsChanged() {
117 return [ $this->params['src'], $this->params['dst'] ];
118 }
119}
FileBackend helper class for representing operations.
Definition FileOp.php:37
bool null $destExists
Definition FileOp.php:58
resolveFileExistence( $source, FileStatePredicates $opPredicates)
Check if a file will exist in storage when this operation is attempted.
Definition FileOp.php:404
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:354
getParam( $name)
Get the value of the parameter with the given name.
Definition FileOp.php:123
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:310
resolveFileSha1Base36( $source, FileStatePredicates $opPredicates)
Get the SHA-1 of a file in storage when this operation is attempted.
Definition FileOp.php:440
resolveFileSize( $source, FileStatePredicates $opPredicates)
Get the size a file in storage will have when this operation is attempted.
Definition FileOp.php:424
Helper class for tracking counterfactual file states when pre-checking file operation batches.
assumeFileExists(string $path, $size, $sha1Base36)
Predicate that a file exists at the path.
assumeFileDoesNotExist(string $path)
Predicate that no file exists at the path.
Move a file from one storage path to another in the backend.
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.
storagePathsRead()
Get a list of storage paths read from for this operation.
allowedParams()
Get the file operation parameters.
storagePathsChanged()
Get a list of storage paths written to for this operation.