Merge "Add CollationFa"
[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 * @author Aaron Schulz
23 */
24
25 /**
26 * Copy a file from one storage path to another in the backend.
27 * Parameters for this operation are outlined in FileBackend::doOperations().
28 */
29 class CopyFileOp extends FileOp {
30 protected function allowedParams() {
31 return [
32 [ 'src', 'dst' ],
33 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
34 [ 'src', 'dst' ]
35 ];
36 }
37
38 protected function doPrecheck( array &$predicates ) {
39 $status = StatusValue::newGood();
40 // Check if the source file exists
41 if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
42 if ( $this->getParam( 'ignoreMissingSource' ) ) {
43 $this->doOperation = false; // no-op
44 // Update file existence predicates (cache 404s)
45 $predicates['exists'][$this->params['src']] = false;
46 $predicates['sha1'][$this->params['src']] = false;
47
48 return $status; // nothing to do
49 } else {
50 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
51
52 return $status;
53 }
54 // Check if a file can be placed/changed at the destination
55 } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
56 $status->fatal( 'backend-fail-usable', $this->params['dst'] );
57 $status->fatal( 'backend-fail-copy', $this->params['src'], $this->params['dst'] );
58
59 return $status;
60 }
61 // Check if destination file exists
62 $status->merge( $this->precheckDestExistence( $predicates ) );
63 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
64 if ( $status->isOK() ) {
65 // Update file existence predicates
66 $predicates['exists'][$this->params['dst']] = true;
67 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
68 }
69
70 return $status; // safe to call attempt()
71 }
72
73 protected function doAttempt() {
74 if ( $this->overwriteSameCase ) {
75 $status = StatusValue::newGood(); // nothing to do
76 } elseif ( $this->params['src'] === $this->params['dst'] ) {
77 // Just update the destination file headers
78 $headers = $this->getParam( 'headers' ) ?: [];
79 $status = $this->backend->describeInternal( $this->setFlags( [
80 'src' => $this->params['dst'], 'headers' => $headers
81 ] ) );
82 } else {
83 // Copy the file to the destination
84 $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
85 }
86
87 return $status;
88 }
89
90 public function storagePathsRead() {
91 return [ $this->params['src'] ];
92 }
93
94 public function storagePathsChanged() {
95 return [ $this->params['dst'] ];
96 }
97 }