Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.64% |
39 / 44 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
CopyFileOp | |
90.70% |
39 / 43 |
|
80.00% |
4 / 5 |
12.12 | |
0.00% |
0 / 1 |
allowedParams | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
doPrecheck | |
85.19% |
23 / 27 |
|
0.00% |
0 / 1 |
5.08 | |||
doAttempt | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
storagePathsRead | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
storagePathsChanged | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Helper class for representing operations with transaction support. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup FileBackend |
22 | */ |
23 | |
24 | namespace Wikimedia\FileBackend\FileOps; |
25 | |
26 | use StatusValue; |
27 | use Wikimedia\FileBackend\FileBackend; |
28 | |
29 | /** |
30 | * Copy a file from one storage path to another in the backend. |
31 | * Parameters for this operation are outlined in FileBackend::doOperations(). |
32 | */ |
33 | class 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 | |
115 | /** @deprecated class alias since 1.43 */ |
116 | class_alias( CopyFileOp::class, 'CopyFileOp' ); |