Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / includes / libs / filebackend / fileop / StoreFileOp.php
1 <?php
2 /**
3 * Helper class for representing operations with transaction support.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Store a file into the backend from a file on the file system.
27 * Parameters for this operation are outlined in FileBackend::doOperations().
28 */
29 class StoreFileOp extends FileOp {
30 protected function allowedParams() {
31 return [
32 [ 'src', 'dst' ],
33 [ 'overwrite', 'overwriteSame', 'headers' ],
34 [ 'src', 'dst' ]
35 ];
36 }
37
38 protected function doPrecheck( array &$predicates ) {
39 $status = StatusValue::newGood();
40 // Check if the source file exists on the file system
41 if ( !is_file( $this->params['src'] ) ) {
42 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
43
44 return $status;
45 // Check if the source file is too big
46 } elseif ( filesize( $this->params['src'] ) > $this->backend->maxFileSizeInternal() ) {
47 $status->fatal( 'backend-fail-maxsize',
48 $this->params['dst'], $this->backend->maxFileSizeInternal() );
49 $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
50
51 return $status;
52 // Check if a file can be placed/changed at the destination
53 } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
54 $status->fatal( 'backend-fail-usable', $this->params['dst'] );
55 $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
56
57 return $status;
58 }
59 // Check if destination file exists
60 $status->merge( $this->precheckDestExistence( $predicates ) );
61 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
62 if ( $status->isOK() ) {
63 // Update file existence predicates
64 $predicates['exists'][$this->params['dst']] = true;
65 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
66 }
67
68 return $status; // safe to call attempt()
69 }
70
71 protected function doAttempt() {
72 if ( !$this->overwriteSameCase ) {
73 // Store the file at the destination
74 return $this->backend->storeInternal( $this->setFlags( $this->params ) );
75 }
76
77 return StatusValue::newGood();
78 }
79
80 protected function getSourceSha1Base36() {
81 MediaWiki\suppressWarnings();
82 $hash = sha1_file( $this->params['src'] );
83 MediaWiki\restoreWarnings();
84 if ( $hash !== false ) {
85 $hash = Wikimedia\base_convert( $hash, 16, 36, 31 );
86 }
87
88 return $hash;
89 }
90
91 public function storagePathsChanged() {
92 return [ $this->params['dst'] ];
93 }
94 }