Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateFileOp
87.50% covered (warning)
87.50%
21 / 24
66.67% covered (warning)
66.67%
4 / 6
9.16
0.00% covered (danger)
0.00%
0 / 1
 allowedParams
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 doPrecheck
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
3.04
 doAttempt
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getSourceSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceSha1Base36
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 storagePathsChanged
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup FileBackend
20 */
21
22/**
23 * Create a file in the backend with the given content.
24 * Parameters for this operation are outlined in FileBackend::doOperations().
25 */
26class CreateFileOp extends FileOp {
27    protected function allowedParams() {
28        return [
29            [ 'content', 'dst' ],
30            [ 'overwrite', 'overwriteSame', 'headers' ],
31            [ 'dst' ]
32        ];
33    }
34
35    protected function doPrecheck(
36        FileStatePredicates $opPredicates,
37        FileStatePredicates $batchPredicates
38     ) {
39        $status = StatusValue::newGood();
40
41        // Check if the source data is too big
42        $sourceSize = $this->getSourceSize();
43        $maxFileSize = $this->backend->maxFileSizeInternal();
44        if ( $sourceSize > $maxFileSize ) {
45            $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize );
46
47            return $status;
48        }
49        // Check if an incompatible destination file exists
50        $sourceSha1 = $this->getSourceSha1Base36();
51        $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) );
52        $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
53
54        // Update file existence predicates if the operation is expected to be allowed to run
55        if ( $status->isOK() ) {
56            $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 );
57        }
58
59        return $status; // safe to call attempt()
60    }
61
62    protected function doAttempt() {
63        if ( $this->overwriteSameCase ) {
64            $status = StatusValue::newGood(); // nothing to do
65        } else {
66            // Create the file at the destination
67            $status = $this->backend->createInternal( $this->setFlags( $this->params ) );
68        }
69
70        return $status;
71    }
72
73    protected function getSourceSize() {
74        return strlen( $this->params['content'] );
75    }
76
77    protected function getSourceSha1Base36() {
78        return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
79    }
80
81    public function storagePathsChanged() {
82        return [ $this->params['dst'] ];
83    }
84}