]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/filebackend/fileop/StoreFileOp.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / libs / filebackend / fileop / StoreFileOp.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  * Store a file into the backend from a file on the file system.
26  * Parameters for this operation are outlined in FileBackend::doOperations().
27  */
28 class StoreFileOp extends FileOp {
29         protected function allowedParams() {
30                 return [
31                         [ 'src', 'dst' ],
32                         [ 'overwrite', 'overwriteSame', 'headers' ],
33                         [ 'src', 'dst' ]
34                 ];
35         }
36
37         protected function doPrecheck( array &$predicates ) {
38                 $status = StatusValue::newGood();
39                 // Check if the source file exists on the file system
40                 if ( !is_file( $this->params['src'] ) ) {
41                         $status->fatal( 'backend-fail-notexists', $this->params['src'] );
42
43                         return $status;
44                         // Check if the source file is too big
45                 } elseif ( filesize( $this->params['src'] ) > $this->backend->maxFileSizeInternal() ) {
46                         $status->fatal( 'backend-fail-maxsize',
47                                 $this->params['dst'], $this->backend->maxFileSizeInternal() );
48                         $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
49
50                         return $status;
51                         // Check if a file can be placed/changed at the destination
52                 } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
53                         $status->fatal( 'backend-fail-usable', $this->params['dst'] );
54                         $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
55
56                         return $status;
57                 }
58                 // Check if destination file exists
59                 $status->merge( $this->precheckDestExistence( $predicates ) );
60                 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
61                 if ( $status->isOK() ) {
62                         // Update file existence predicates
63                         $predicates['exists'][$this->params['dst']] = true;
64                         $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
65                 }
66
67                 return $status; // safe to call attempt()
68         }
69
70         protected function doAttempt() {
71                 if ( !$this->overwriteSameCase ) {
72                         // Store the file at the destination
73                         return $this->backend->storeInternal( $this->setFlags( $this->params ) );
74                 }
75
76                 return StatusValue::newGood();
77         }
78
79         protected function getSourceSha1Base36() {
80                 MediaWiki\suppressWarnings();
81                 $hash = sha1_file( $this->params['src'] );
82                 MediaWiki\restoreWarnings();
83                 if ( $hash !== false ) {
84                         $hash = Wikimedia\base_convert( $hash, 16, 36, 31 );
85                 }
86
87                 return $hash;
88         }
89
90         public function storagePathsChanged() {
91                 return [ $this->params['dst'] ];
92         }
93 }