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
24 namespace Wikimedia\FileBackend\FileOps
;
27 use Wikimedia\FileBackend\FileBackend
;
30 * Copy a file from one storage path to another in the backend.
31 * Parameters for this operation are outlined in FileBackend::doOperations().
33 class CopyFileOp
extends FileOp
{
34 protected function allowedParams() {
37 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
42 protected function doPrecheck(
43 FileStatePredicates
$opPredicates,
44 FileStatePredicates
$batchPredicates
46 $status = StatusValue
::newGood();
48 // Check source file existence
49 $srcExists = $this->resolveFileExistence( $this->params
['src'], $opPredicates );
50 if ( $srcExists === false ) {
51 if ( $this->getParam( 'ignoreMissingSource' ) ) {
52 $this->noOp
= true; // no-op
53 // Update file existence predicates (cache 404s)
54 $batchPredicates->assumeFileDoesNotExist( $this->params
['src'] );
56 return $status; // nothing to do
58 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
62 } elseif ( $srcExists === FileBackend
::EXISTENCE_ERROR
) {
63 $status->fatal( 'backend-fail-stat', $this->params
['src'] );
67 // Check if an incompatible destination file exists
68 $srcSize = function () use ( $opPredicates ) {
70 $size ??
= $this->resolveFileSize( $this->params
['src'], $opPredicates );
73 $srcSha1 = function () use ( $opPredicates ) {
75 $sha1 ??
= $this->resolveFileSha1Base36( $this->params
['src'], $opPredicates );
78 $status->merge( $this->precheckDestExistence( $opPredicates, $srcSize, $srcSha1 ) );
79 $this->params
['dstExists'] = $this->destExists
; // see FileBackendStore::setFileCache()
81 // Update file existence predicates if the operation is expected to be allowed to run
82 if ( $status->isOK() ) {
83 $batchPredicates->assumeFileExists( $this->params
['dst'], $srcSize, $srcSha1 );
86 return $status; // safe to call attempt()
89 protected function doAttempt() {
90 if ( $this->overwriteSameCase
) {
91 $status = StatusValue
::newGood(); // nothing to do
92 } elseif ( $this->params
['src'] === $this->params
['dst'] ) {
93 // Just update the destination file headers
94 $headers = $this->getParam( 'headers' ) ?
: [];
95 $status = $this->backend
->describeInternal( $this->setFlags( [
96 'src' => $this->params
['dst'], 'headers' => $headers
99 // Copy the file to the destination
100 $status = $this->backend
->copyInternal( $this->setFlags( $this->params
) );
106 public function storagePathsRead() {
107 return [ $this->params
['src'] ];
110 public function storagePathsChanged() {
111 return [ $this->params
['dst'] ];
115 /** @deprecated class alias since 1.43 */
116 class_alias( CopyFileOp
::class, 'CopyFileOp' );