Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / libs / filebackend / fileop / CreateFileOp.php
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 */
26 class 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( array &$predicates ) {
36 $status = StatusValue::newGood();
37 // Check if the source data is too big
38 if ( strlen( $this->getParam( 'content' ) ) > $this->backend->maxFileSizeInternal() ) {
39 $status->fatal( 'backend-fail-maxsize',
40 $this->params['dst'], $this->backend->maxFileSizeInternal() );
41 $status->fatal( 'backend-fail-create', $this->params['dst'] );
42
43 return $status;
44 // Check if a file can be placed/changed at the destination
45 } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
46 $status->fatal( 'backend-fail-usable', $this->params['dst'] );
47 $status->fatal( 'backend-fail-create', $this->params['dst'] );
48
49 return $status;
50 }
51 // Check if destination file exists
52 $status->merge( $this->precheckDestExistence( $predicates ) );
53 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
54 if ( $status->isOK() ) {
55 // Update file existence predicates
56 $predicates['exists'][$this->params['dst']] = true;
57 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
58 }
59
60 return $status; // safe to call attempt()
61 }
62
63 protected function doAttempt() {
64 if ( !$this->overwriteSameCase ) {
65 // Create the file at the destination
66 return $this->backend->createInternal( $this->setFlags( $this->params ) );
67 }
68
69 return StatusValue::newGood();
70 }
71
72 protected function getSourceSha1Base36() {
73 return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
74 }
75
76 public function storagePathsChanged() {
77 return [ $this->params['dst'] ];
78 }
79 }