MediaWiki master
CopyFileOp.php
Go to the documentation of this file.
1<?php
11
12use StatusValue;
14
19class CopyFileOp extends FileOp {
21 protected function allowedParams() {
22 return [
23 [ 'src', 'dst' ],
24 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
25 [ 'src', 'dst' ]
26 ];
27 }
28
30 protected function doPrecheck(
31 FileStatePredicates $opPredicates,
32 FileStatePredicates $batchPredicates
33 ) {
34 $status = StatusValue::newGood();
35
36 // Check source file existence
37 $srcExists = $this->resolveFileExistence( $this->params['src'], $opPredicates );
38 if ( $srcExists === false ) {
39 if ( $this->getParam( 'ignoreMissingSource' ) ) {
40 $this->noOp = true; // no-op
41 // Update file existence predicates (cache 404s)
42 $batchPredicates->assumeFileDoesNotExist( $this->params['src'] );
43
44 return $status; // nothing to do
45 } else {
46 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
47
48 return $status;
49 }
50 } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
51 $status->fatal( 'backend-fail-stat', $this->params['src'] );
52
53 return $status;
54 }
55 // Check if an incompatible destination file exists
56 $srcSize = function () use ( $opPredicates ) {
57 static $size = null;
58 $size ??= $this->resolveFileSize( $this->params['src'], $opPredicates );
59 return $size;
60 };
61 $srcSha1 = function () use ( $opPredicates ) {
62 static $sha1 = null;
63 $sha1 ??= $this->resolveFileSha1Base36( $this->params['src'], $opPredicates );
64 return $sha1;
65 };
66 $status->merge( $this->precheckDestExistence( $opPredicates, $srcSize, $srcSha1 ) );
67 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
68
69 // Update file existence predicates if the operation is expected to be allowed to run
70 if ( $status->isOK() ) {
71 $batchPredicates->assumeFileExists( $this->params['dst'], $srcSize, $srcSha1 );
72 }
73
74 return $status; // safe to call attempt()
75 }
76
78 protected function doAttempt() {
79 if ( $this->overwriteSameCase ) {
80 $status = StatusValue::newGood(); // nothing to do
81 } elseif ( $this->params['src'] === $this->params['dst'] ) {
82 // Just update the destination file headers
83 $headers = $this->getParam( 'headers' ) ?: [];
84 $status = $this->backend->describeInternal( $this->setFlags( [
85 'src' => $this->params['dst'], 'headers' => $headers
86 ] ) );
87 } else {
88 // Copy the file to the destination
89 $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
90 }
91
92 return $status;
93 }
94
96 public function storagePathsRead() {
97 return [ $this->params['src'] ];
98 }
99
101 public function storagePathsChanged() {
102 return [ $this->params['dst'] ];
103 }
104}
105
107class_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.array (required params list, optional params list,...
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.Updates the batch predica...
storagePathsRead()
Get a list of storage paths read from for this operation.array
storagePathsChanged()
Get a list of storage paths written to for this operation.array
FileBackend helper class for representing operations.
Definition FileOp.php:32
resolveFileSize( $source, FileStatePredicates $opPredicates)
Get the size a file in storage will have when this operation is attempted.
Definition FileOp.php:418
getParam( $name)
Get the value of the parameter with the given name.
Definition FileOp.php:117
resolveFileSha1Base36( $source, FileStatePredicates $opPredicates)
Get the SHA-1 of a file in storage when this operation is attempted.
Definition FileOp.php:434
resolveFileExistence( $source, FileStatePredicates $opPredicates)
Check if a file will exist in storage when this operation is attempted.
Definition FileOp.php:398
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:348
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:304
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.