Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.70% covered (success)
90.70%
39 / 43
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CopyFileOp
90.70% covered (success)
90.70%
39 / 43
80.00% covered (warning)
80.00%
4 / 5
12.12
0.00% covered (danger)
0.00%
0 / 1
 allowedParams
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 doPrecheck
85.19% covered (warning)
85.19%
23 / 27
0.00% covered (danger)
0.00%
0 / 1
5.08
 doAttempt
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 storagePathsRead
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 storagePathsChanged
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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 * Copy a file from one storage path to another in the backend.
26 * Parameters for this operation are outlined in FileBackend::doOperations().
27 */
28class CopyFileOp 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        }
80
81        return $status; // safe to call attempt()
82    }
83
84    protected function doAttempt() {
85        if ( $this->overwriteSameCase ) {
86            $status = StatusValue::newGood(); // nothing to do
87        } elseif ( $this->params['src'] === $this->params['dst'] ) {
88            // Just update the destination file headers
89            $headers = $this->getParam( 'headers' ) ?: [];
90            $status = $this->backend->describeInternal( $this->setFlags( [
91                'src' => $this->params['dst'], 'headers' => $headers
92            ] ) );
93        } else {
94            // Copy the file to the destination
95            $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
96        }
97
98        return $status;
99    }
100
101    public function storagePathsRead() {
102        return [ $this->params['src'] ];
103    }
104
105    public function storagePathsChanged() {
106        return [ $this->params['dst'] ];
107    }
108}