3 * Helper class for representing operations with transaction support.
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.
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.
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
21 * @ingroup FileBackend
25 * Move a file from one storage path to another in the backend.
26 * Parameters for this operation are outlined in FileBackend::doOperations().
28 class MoveFileOp
extends FileOp
{
29 protected function allowedParams() {
32 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
37 protected function doPrecheck( array &$predicates ) {
38 $status = StatusValue
::newGood();
39 // Check if the source file exists
40 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
41 if ( $this->getParam( 'ignoreMissingSource' ) ) {
42 $this->doOperation
= false; // no-op
43 // Update file existence predicates (cache 404s)
44 $predicates['exists'][$this->params
['src']] = false;
45 $predicates['sha1'][$this->params
['src']] = false;
47 return $status; // nothing to do
49 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
53 // Check if a file can be placed/changed at the destination
54 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
55 $status->fatal( 'backend-fail-usable', $this->params
['dst'] );
56 $status->fatal( 'backend-fail-move', $this->params
['src'], $this->params
['dst'] );
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
['src']] = false;
66 $predicates['sha1'][$this->params
['src']] = false;
67 $predicates['exists'][$this->params
['dst']] = true;
68 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
71 return $status; // safe to call attempt()
74 protected function doAttempt() {
75 if ( $this->overwriteSameCase
) {
76 if ( $this->params
['src'] === $this->params
['dst'] ) {
77 // Do nothing to the destination (which is also the source)
78 $status = StatusValue
::newGood();
80 // Just delete the source as the destination file needs no changes
81 $status = $this->backend
->deleteInternal( $this->setFlags(
82 [ 'src' => $this->params
['src'] ]
85 } elseif ( $this->params
['src'] === $this->params
['dst'] ) {
86 // Just update the destination file headers
87 $headers = $this->getParam( 'headers' ) ?
: [];
88 $status = $this->backend
->describeInternal( $this->setFlags(
89 [ 'src' => $this->params
['dst'], 'headers' => $headers ]
92 // Move the file to the destination
93 $status = $this->backend
->moveInternal( $this->setFlags( $this->params
) );
99 public function storagePathsRead() {
100 return [ $this->params
['src'] ];
103 public function storagePathsChanged() {
104 return [ $this->params
['src'], $this->params
['dst'] ];