Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / libs / filebackend / fileop / MoveFileOp.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 * Move a file from one storage path to another in the backend.
27 * Parameters for this operation are outlined in FileBackend::doOperations().
28 */
29 class MoveFileOp 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-move', $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['src']] = false;
67 $predicates['sha1'][$this->params['src']] = false;
68 $predicates['exists'][$this->params['dst']] = true;
69 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
70 }
71
72 return $status; // safe to call attempt()
73 }
74
75 protected function doAttempt() {
76 if ( $this->overwriteSameCase ) {
77 if ( $this->params['src'] === $this->params['dst'] ) {
78 // Do nothing to the destination (which is also the source)
79 $status = StatusValue::newGood();
80 } else {
81 // Just delete the source as the destination file needs no changes
82 $status = $this->backend->deleteInternal( $this->setFlags(
83 [ 'src' => $this->params['src'] ]
84 ) );
85 }
86 } elseif ( $this->params['src'] === $this->params['dst'] ) {
87 // Just update the destination file headers
88 $headers = $this->getParam( 'headers' ) ?: [];
89 $status = $this->backend->describeInternal( $this->setFlags(
90 [ 'src' => $this->params['dst'], 'headers' => $headers ]
91 ) );
92 } else {
93 // Move the file to the destination
94 $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
95 }
96
97 return $status;
98 }
99
100 public function storagePathsRead() {
101 return [ $this->params['src'] ];
102 }
103
104 public function storagePathsChanged() {
105 return [ $this->params['src'], $this->params['dst'] ];
106 }
107 }