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