Merge "SpecialBlock: Add confirmation dialog when removing blocks"
[mediawiki.git] / includes / libs / filebackend / fileop / CreateFileOp.php
blobd3fe10b26679d771407aed5bd8ec54acfc6db313
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup FileBackend
22 namespace Wikimedia\FileBackend\FileOps;
24 use StatusValue;
26 /**
27 * Create a file in the backend with the given content.
28 * Parameters for this operation are outlined in FileBackend::doOperations().
30 class CreateFileOp extends FileOp {
31 protected function allowedParams() {
32 return [
33 [ 'content', 'dst' ],
34 [ 'overwrite', 'overwriteSame', 'headers' ],
35 [ 'dst' ]
39 protected function doPrecheck(
40 FileStatePredicates $opPredicates,
41 FileStatePredicates $batchPredicates
42 ) {
43 $status = StatusValue::newGood();
45 // Check if the source data is too big
46 $sourceSize = $this->getSourceSize();
47 $maxFileSize = $this->backend->maxFileSizeInternal();
48 if ( $sourceSize > $maxFileSize ) {
49 $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize );
51 return $status;
53 // Check if an incompatible destination file exists
54 $sourceSha1 = $this->getSourceSha1Base36();
55 $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) );
56 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
58 // Update file existence predicates if the operation is expected to be allowed to run
59 if ( $status->isOK() ) {
60 $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 );
63 return $status; // safe to call attempt()
66 protected function doAttempt() {
67 if ( $this->overwriteSameCase ) {
68 $status = StatusValue::newGood(); // nothing to do
69 } else {
70 // Create the file at the destination
71 $status = $this->backend->createInternal( $this->setFlags( $this->params ) );
74 return $status;
77 protected function getSourceSize() {
78 return strlen( $this->params['content'] );
81 protected function getSourceSha1Base36() {
82 return \Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
85 public function storagePathsChanged() {
86 return [ $this->params['dst'] ];
90 /** @deprecated class alias since 1.43 */
91 class_alias( CreateFileOp::class, 'CreateFileOp' );