]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/filebackend/fileop/CopyFileOp.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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  */
23
24 /**
25  * Copy a file from one storage path to another in the backend.
26  * Parameters for this operation are outlined in FileBackend::doOperations().
27  */
28 class CopyFileOp extends FileOp {
29         protected function allowedParams() {
30                 return [
31                         [ 'src', 'dst' ],
32                         [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
33                         [ 'src', 'dst' ]
34                 ];
35         }
36
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;
46
47                                 return $status; // nothing to do
48                         } else {
49                                 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
50
51                                 return $status;
52                         }
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-copy', $this->params['src'], $this->params['dst'] );
57
58                         return $status;
59                 }
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['dst']] = true;
66                         $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
67                 }
68
69                 return $status; // safe to call attempt()
70         }
71
72         protected function doAttempt() {
73                 if ( $this->overwriteSameCase ) {
74                         $status = StatusValue::newGood(); // nothing to do
75                 } elseif ( $this->params['src'] === $this->params['dst'] ) {
76                         // Just update the destination file headers
77                         $headers = $this->getParam( 'headers' ) ?: [];
78                         $status = $this->backend->describeInternal( $this->setFlags( [
79                                 'src' => $this->params['dst'], 'headers' => $headers
80                         ] ) );
81                 } else {
82                         // Copy the file to the destination
83                         $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
84                 }
85
86                 return $status;
87         }
88
89         public function storagePathsRead() {
90                 return [ $this->params['src'] ];
91         }
92
93         public function storagePathsChanged() {
94                 return [ $this->params['dst'] ];
95         }
96 }