Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.84% |
45 / 49 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
MoveFileOp | |
91.84% |
45 / 49 |
|
80.00% |
4 / 5 |
14.11 | |
0.00% |
0 / 1 |
allowedParams | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
doPrecheck | |
86.21% |
25 / 29 |
|
0.00% |
0 / 1 |
6.09 | |||
doAttempt | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
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 | /** |
25 | * Move a file from one storage path to another in the backend. |
26 | * Parameters for this operation are outlined in FileBackend::doOperations(). |
27 | */ |
28 | class MoveFileOp extends FileOp { |
29 | protected function allowedParams() { |
30 | return [ |
31 | [ 'src', 'dst' ], |
32 | [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ], |
33 | [ 'src', 'dst' ] |
34 | ]; |
35 | } |
36 | |
37 | protected function doPrecheck( |
38 | FileStatePredicates $opPredicates, |
39 | FileStatePredicates $batchPredicates |
40 | ) { |
41 | $status = StatusValue::newGood(); |
42 | |
43 | // Check source file existence |
44 | $srcExists = $this->resolveFileExistence( $this->params['src'], $opPredicates ); |
45 | if ( $srcExists === false ) { |
46 | if ( $this->getParam( 'ignoreMissingSource' ) ) { |
47 | $this->noOp = true; // no-op |
48 | // Update file existence predicates (cache 404s) |
49 | $batchPredicates->assumeFileDoesNotExist( $this->params['src'] ); |
50 | |
51 | return $status; // nothing to do |
52 | } else { |
53 | $status->fatal( 'backend-fail-notexists', $this->params['src'] ); |
54 | |
55 | return $status; |
56 | } |
57 | } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) { |
58 | $status->fatal( 'backend-fail-stat', $this->params['src'] ); |
59 | |
60 | return $status; |
61 | } |
62 | // Check if an incompatible destination file exists |
63 | $srcSize = function () use ( $opPredicates ) { |
64 | static $size = null; |
65 | $size ??= $this->resolveFileSize( $this->params['src'], $opPredicates ); |
66 | return $size; |
67 | }; |
68 | $srcSha1 = function () use ( $opPredicates ) { |
69 | static $sha1 = null; |
70 | $sha1 ??= $this->resolveFileSha1Base36( $this->params['src'], $opPredicates ); |
71 | return $sha1; |
72 | }; |
73 | $status->merge( $this->precheckDestExistence( $opPredicates, $srcSize, $srcSha1 ) ); |
74 | $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache() |
75 | |
76 | // Update file existence predicates if the operation is expected to be allowed to run |
77 | if ( $status->isOK() ) { |
78 | $batchPredicates->assumeFileExists( $this->params['dst'], $srcSize, $srcSha1 ); |
79 | if ( $this->params['src'] !== $this->params['dst'] ) { |
80 | $batchPredicates->assumeFileDoesNotExist( $this->params['src'] ); |
81 | } |
82 | } |
83 | |
84 | return $status; // safe to call attempt() |
85 | } |
86 | |
87 | protected function doAttempt() { |
88 | if ( $this->overwriteSameCase ) { |
89 | if ( $this->params['src'] === $this->params['dst'] ) { |
90 | // Do nothing to the destination (which is also the source) |
91 | $status = StatusValue::newGood(); |
92 | } else { |
93 | // Just delete the source as the destination file needs no changes |
94 | $status = $this->backend->deleteInternal( $this->setFlags( |
95 | [ 'src' => $this->params['src'] ] |
96 | ) ); |
97 | } |
98 | } elseif ( $this->params['src'] === $this->params['dst'] ) { |
99 | // Just update the destination file headers |
100 | $headers = $this->getParam( 'headers' ) ?: []; |
101 | $status = $this->backend->describeInternal( $this->setFlags( |
102 | [ 'src' => $this->params['dst'], 'headers' => $headers ] |
103 | ) ); |
104 | } else { |
105 | // Move the file to the destination |
106 | $status = $this->backend->moveInternal( $this->setFlags( $this->params ) ); |
107 | } |
108 | |
109 | return $status; |
110 | } |
111 | |
112 | public function storagePathsRead() { |
113 | return [ $this->params['src'] ]; |
114 | } |
115 | |
116 | public function storagePathsChanged() { |
117 | return [ $this->params['src'], $this->params['dst'] ]; |
118 | } |
119 | } |