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