MediaWiki
master
Toggle main menu visibility
CopyFileOp.php
Go to the documentation of this file.
1
<?php
9
10
namespace
Wikimedia\FileBackend\FileOps
;
11
12
use StatusValue;
13
use
Wikimedia\FileBackend\FileBackend
;
14
19
class
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
if
( !$this->backend->isPathUsableInternal( $this->params[
'dst'
] ) ) {
37
$status->fatal(
'backend-fail-usable'
, $this->params[
'dst'
] );
38
39
return
$status;
40
}
41
42
if
(
43
!$this->
getParam
(
'ignoreMissingSource'
) &&
44
!$this->backend->isPathUsableInternal( $this->params[
'src'
] )
45
) {
46
$status->fatal(
'backend-fail-usable'
, $this->params[
'src'
] );
47
48
return
$status;
49
}
50
51
// Check source file existence
52
$srcExists = $this->
resolveFileExistence
( $this->params[
'src'
], $opPredicates );
53
if
( $srcExists ===
false
) {
54
if
( $this->
getParam
(
'ignoreMissingSource'
) ) {
55
$this->noOp =
true
;
// no-op
56
// Update file existence predicates (cache 404s)
57
$batchPredicates->
assumeFileDoesNotExist
( $this->params[
'src'
] );
58
59
return
$status;
// nothing to do
60
}
else
{
61
$status->fatal(
'backend-fail-notexists'
, $this->params[
'src'
] );
62
63
return
$status;
64
}
65
} elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
66
$status->fatal(
'backend-fail-stat'
, $this->params[
'src'
] );
67
68
return
$status;
69
}
70
// Check if an incompatible destination file exists
71
$srcSize =
function
() use ( $opPredicates ) {
72
static
$size =
null
;
73
$size ??= $this->
resolveFileSize
( $this->params[
'src'
], $opPredicates );
74
return
$size;
75
};
76
$srcSha1 =
function
() use ( $opPredicates ) {
77
static
$sha1 =
null
;
78
$sha1 ??= $this->
resolveFileSha1Base36
( $this->params[
'src'
], $opPredicates );
79
return
$sha1;
80
};
81
$status->merge( $this->
precheckDestExistence
( $opPredicates, $srcSize, $srcSha1 ) );
82
$this->params[
'dstExists'
] =
$this->destExists
;
// see FileBackendStore::setFileCache()
83
84
// Update file existence predicates if the operation is expected to be allowed to run
85
if
( $status->isOK() ) {
86
$batchPredicates->
assumeFileExists
( $this->params[
'dst'
], $srcSize, $srcSha1 );
87
}
88
89
return
$status;
// safe to call attempt()
90
}
91
93
protected
function
doAttempt
() {
94
if
( $this->overwriteSameCase ) {
95
$status = StatusValue::newGood();
// nothing to do
96
} elseif ( $this->params[
'src'
] === $this->params[
'dst'
] ) {
97
// Just update the destination file headers
98
$headers = $this->
getParam
(
'headers'
) ?: [];
99
$status = $this->backend->describeInternal( $this->
setFlags
( [
100
'src'
=> $this->params[
'dst'
],
'headers'
=> $headers
101
] ) );
102
}
else
{
103
// Copy the file to the destination
104
$status = $this->backend->copyInternal( $this->
setFlags
( $this->params ) );
105
}
106
107
return
$status;
108
}
109
111
public
function
storagePathsRead
() {
112
return
[ $this->params[
'src'
] ];
113
}
114
116
public
function
storagePathsChanged
() {
117
return
[ $this->params[
'dst'
] ];
118
}
119
}
120
122
class_alias( CopyFileOp::class,
'CopyFileOp'
);
Wikimedia\FileBackend\FileBackend
Base class for all file backend classes (including multi-write backends).
Definition
FileBackend.php:99
Wikimedia\FileBackend\FileOps\CopyFileOp
Copy a file from one storage path to another in the backend.
Definition
CopyFileOp.php:19
Wikimedia\FileBackend\FileOps\CopyFileOp\doAttempt
doAttempt()
StatusValue
Definition
CopyFileOp.php:93
Wikimedia\FileBackend\FileOps\CopyFileOp\allowedParams
allowedParams()
Get the file operation parameters.array (required params list, optional params list,...
Definition
CopyFileOp.php:21
Wikimedia\FileBackend\FileOps\CopyFileOp\doPrecheck
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.Updates the batch predica...
Definition
CopyFileOp.php:30
Wikimedia\FileBackend\FileOps\CopyFileOp\storagePathsRead
storagePathsRead()
Get a list of storage paths read from for this operation.array
Definition
CopyFileOp.php:111
Wikimedia\FileBackend\FileOps\CopyFileOp\storagePathsChanged
storagePathsChanged()
Get a list of storage paths written to for this operation.array
Definition
CopyFileOp.php:116
Wikimedia\FileBackend\FileOps\FileOp
FileBackend helper class for representing operations.
Definition
FileOp.php:32
Wikimedia\FileBackend\FileOps\FileOp\resolveFileSize
resolveFileSize( $source, FileStatePredicates $opPredicates)
Get the size a file in storage will have when this operation is attempted.
Definition
FileOp.php:408
Wikimedia\FileBackend\FileOps\FileOp\getParam
getParam( $name)
Get the value of the parameter with the given name.
Definition
FileOp.php:117
Wikimedia\FileBackend\FileOps\FileOp\resolveFileSha1Base36
resolveFileSha1Base36( $source, FileStatePredicates $opPredicates)
Get the SHA-1 of a file in storage when this operation is attempted.
Definition
FileOp.php:424
Wikimedia\FileBackend\FileOps\FileOp\resolveFileExistence
resolveFileExistence( $source, FileStatePredicates $opPredicates)
Check if a file will exist in storage when this operation is attempted.
Definition
FileOp.php:388
Wikimedia\FileBackend\FileOps\FileOp\$destExists
bool null $destExists
Definition
FileOp.php:53
Wikimedia\FileBackend\FileOps\FileOp\precheckDestExistence
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
Definition
FileOp.php:338
Wikimedia\FileBackend\FileOps\FileOp\setFlags
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition
FileOp.php:294
Wikimedia\FileBackend\FileOps\FileStatePredicates
Helper class for tracking counterfactual file states when pre-checking file operation batches.
Definition
FileStatePredicates.php:23
Wikimedia\FileBackend\FileOps\FileStatePredicates\assumeFileDoesNotExist
assumeFileDoesNotExist(string $path)
Predicate that no file exists at the path.
Definition
FileStatePredicates.php:55
Wikimedia\FileBackend\FileOps\FileStatePredicates\assumeFileExists
assumeFileExists(string $path, $size, $sha1Base36)
Predicate that a file exists at the path.
Definition
FileStatePredicates.php:42
Wikimedia\FileBackend\FileOps
Definition
CopyFileOp.php:10
includes
libs
FileBackend
FileOps
CopyFileOp.php
Generated on
for MediaWiki by
1.18.0