Special case opus mime detction
[mediawiki.git] / includes / libs / filebackend / fileop / CopyFileOp.php
blobe3b8c51719b27f4440fcae11aec6ef4022e3c75f
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
25 /**
26 * Copy a file from one storage path to another in the backend.
27 * Parameters for this operation are outlined in FileBackend::doOperations().
29 class CopyFileOp extends FileOp {
30 protected function allowedParams() {
31 return [
32 [ 'src', 'dst' ],
33 [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
34 [ 'src', 'dst' ]
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;
48 return $status; // nothing to do
49 } else {
50 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
52 return $status;
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'] );
59 return $status;
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;
70 return $status; // safe to call attempt()
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 ) );
87 return $status;
90 public function storagePathsRead() {
91 return [ $this->params['src'] ];
94 public function storagePathsChanged() {
95 return [ $this->params['dst'] ];