Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / libs / filebackend / fileop / CopyFileOp.php
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 */
28 class 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( array &$predicates ) {
38 $status = StatusValue::newGood();
39
40 // Check source file existence
41 $srcExists = $this->fileExists( $this->params['src'], $predicates );
42 if ( $srcExists === false ) {
43 if ( $this->getParam( 'ignoreMissingSource' ) ) {
44 $this->doOperation = false; // no-op
45 // Update file existence predicates (cache 404s)
46 $predicates['exists'][$this->params['src']] = false;
47 $predicates['sha1'][$this->params['src']] = false;
48
49 return $status; // nothing to do
50 } else {
51 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
52
53 return $status;
54 }
55 } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
56 $status->fatal( 'backend-fail-stat', $this->params['src'] );
57
58 return $status;
59 }
60 // Check if destination file exists
61 $status->merge( $this->precheckDestExistence( $predicates ) );
62 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
63 if ( $status->isOK() ) {
64 // Update file existence predicates
65 $predicates['exists'][$this->params['dst']] = true;
66 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
67 }
68
69 return $status; // safe to call attempt()
70 }
71
72 protected function doAttempt() {
73 if ( $this->overwriteSameCase ) {
74 $status = StatusValue::newGood(); // nothing to do
75 } elseif ( $this->params['src'] === $this->params['dst'] ) {
76 // Just update the destination file headers
77 $headers = $this->getParam( 'headers' ) ?: [];
78 $status = $this->backend->describeInternal( $this->setFlags( [
79 'src' => $this->params['dst'], 'headers' => $headers
80 ] ) );
81 } else {
82 // Copy the file to the destination
83 $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
84 }
85
86 return $status;
87 }
88
89 public function storagePathsRead() {
90 return [ $this->params['src'] ];
91 }
92
93 public function storagePathsChanged() {
94 return [ $this->params['dst'] ];
95 }
96 }