Merge "ObjectCacheFactory: use Tracer telemetry"
[mediawiki.git] / includes / libs / filebackend / fileop / StoreFileOp.php
blobfffe41704dd089570cb07280f82d07ad51b4242a
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
24 namespace Wikimedia\FileBackend\FileOps;
26 use StatusValue;
27 use Wikimedia\AtEase\AtEase;
29 /**
30 * Store a file into the backend from a file on the file system.
31 * Parameters for this operation are outlined in FileBackend::doOperations().
33 class StoreFileOp extends FileOp {
34 protected function allowedParams() {
35 return [
36 [ 'src', 'dst' ],
37 [ 'overwrite', 'overwriteSame', 'headers' ],
38 [ 'src', 'dst' ]
42 protected function doPrecheck(
43 FileStatePredicates $opPredicates,
44 FileStatePredicates $batchPredicates
45 ) {
46 $status = StatusValue::newGood();
48 // Check if the source file exists in the file system
49 if ( !is_file( $this->params['src'] ) ) {
50 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
52 return $status;
54 // Check if the source file is too big
55 $sourceSize = $this->getSourceSize();
56 $maxFileSize = $this->backend->maxFileSizeInternal();
57 if ( $sourceSize > $maxFileSize ) {
58 $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize );
60 return $status;
62 // Check if an incompatible destination file exists
63 $sourceSha1 = function () {
64 static $sha1 = null;
65 $sha1 ??= $this->getSourceSha1Base36();
66 return $sha1;
68 $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) );
69 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
71 // Update file existence predicates if the operation is expected to be allowed to run
72 if ( $status->isOK() ) {
73 $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 );
76 return $status; // safe to call attempt()
79 protected function doAttempt() {
80 if ( $this->overwriteSameCase ) {
81 $status = StatusValue::newGood(); // nothing to do
82 } else {
83 // Store the file at the destination
84 $status = $this->backend->storeInternal( $this->setFlags( $this->params ) );
87 return $status;
90 protected function getSourceSize() {
91 AtEase::suppressWarnings();
92 $size = filesize( $this->params['src'] );
93 AtEase::restoreWarnings();
95 return $size;
98 protected function getSourceSha1Base36() {
99 AtEase::suppressWarnings();
100 $hash = sha1_file( $this->params['src'] );
101 AtEase::restoreWarnings();
102 if ( $hash !== false ) {
103 $hash = \Wikimedia\base_convert( $hash, 16, 36, 31 );
106 return $hash;
109 public function storagePathsChanged() {
110 return [ $this->params['dst'] ];
114 /** @deprecated class alias since 1.43 */
115 class_alias( StoreFileOp::class, 'StoreFileOp' );