MediaWiki master
CopyFileOp.php
Go to the documentation of this file.
1<?php
25
26use StatusValue;
28
33class CopyFileOp 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 }
85
86 return $status; // safe to call attempt()
87 }
88
89 protected function doAttempt() {
90 if ( $this->overwriteSameCase ) {
91 $status = StatusValue::newGood(); // nothing to do
92 } elseif ( $this->params['src'] === $this->params['dst'] ) {
93 // Just update the destination file headers
94 $headers = $this->getParam( 'headers' ) ?: [];
95 $status = $this->backend->describeInternal( $this->setFlags( [
96 'src' => $this->params['dst'], 'headers' => $headers
97 ] ) );
98 } else {
99 // Copy the file to the destination
100 $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
101 }
102
103 return $status;
104 }
105
106 public function storagePathsRead() {
107 return [ $this->params['src'] ];
108 }
109
110 public function storagePathsChanged() {
111 return [ $this->params['dst'] ];
112 }
113}
114
116class_alias( CopyFileOp::class, 'CopyFileOp' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Base class for all file backend classes (including multi-write backends).
Copy a file from one storage path to another in the backend.
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.
storagePathsRead()
Get a list of storage paths read from for this operation.
storagePathsChanged()
Get a list of storage paths written to for this operation.
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.