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