Merge "Convert Special:DeletedContributions to use OOUI."
[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 * @author Aaron Schulz
21 */
22
23 /**
24 * Create a file in the backend with the given content.
25 * Parameters for this operation are outlined in FileBackend::doOperations().
26 */
27 class CreateFileOp extends FileOp {
28 protected function allowedParams() {
29 return [
30 [ 'content', 'dst' ],
31 [ 'overwrite', 'overwriteSame', 'headers' ],
32 [ 'dst' ]
33 ];
34 }
35
36 protected function doPrecheck( array &$predicates ) {
37 $status = StatusValue::newGood();
38 // Check if the source data is too big
39 if ( strlen( $this->getParam( 'content' ) ) > $this->backend->maxFileSizeInternal() ) {
40 $status->fatal( 'backend-fail-maxsize',
41 $this->params['dst'], $this->backend->maxFileSizeInternal() );
42 $status->fatal( 'backend-fail-create', $this->params['dst'] );
43
44 return $status;
45 // Check if a file can be placed/changed at the destination
46 } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
47 $status->fatal( 'backend-fail-usable', $this->params['dst'] );
48 $status->fatal( 'backend-fail-create', $this->params['dst'] );
49
50 return $status;
51 }
52 // Check if destination file exists
53 $status->merge( $this->precheckDestExistence( $predicates ) );
54 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
55 if ( $status->isOK() ) {
56 // Update file existence predicates
57 $predicates['exists'][$this->params['dst']] = true;
58 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
59 }
60
61 return $status; // safe to call attempt()
62 }
63
64 protected function doAttempt() {
65 if ( !$this->overwriteSameCase ) {
66 // Create the file at the destination
67 return $this->backend->createInternal( $this->setFlags( $this->params ) );
68 }
69
70 return StatusValue::newGood();
71 }
72
73 protected function getSourceSha1Base36() {
74 return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
75 }
76
77 public function storagePathsChanged() {
78 return [ $this->params['dst'] ];
79 }
80 }