3 * Helper class for representing operations with transaction support.
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.
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.
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
21 * @ingroup FileBackend
25 * Store a file into the backend from a file on the file system.
26 * Parameters for this operation are outlined in FileBackend::doOperations().
28 class StoreFileOp
extends FileOp
{
29 protected function allowedParams() {
32 [ 'overwrite', 'overwriteSame', 'headers' ],
37 protected function doPrecheck( array &$predicates ) {
38 $status = StatusValue
::newGood();
39 // Check if the source file exists on the file system
40 if ( !is_file( $this->params
['src'] ) ) {
41 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
44 // Check if the source file is too big
45 } elseif ( filesize( $this->params
['src'] ) > $this->backend
->maxFileSizeInternal() ) {
46 $status->fatal( 'backend-fail-maxsize',
47 $this->params
['dst'], $this->backend
->maxFileSizeInternal() );
48 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
51 // Check if a file can be placed/changed at the destination
52 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
53 $status->fatal( 'backend-fail-usable', $this->params
['dst'] );
54 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
58 // Check if destination file exists
59 $status->merge( $this->precheckDestExistence( $predicates ) );
60 $this->params
['dstExists'] = $this->destExists
; // see FileBackendStore::setFileCache()
61 if ( $status->isOK() ) {
62 // Update file existence predicates
63 $predicates['exists'][$this->params
['dst']] = true;
64 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
67 return $status; // safe to call attempt()
70 protected function doAttempt() {
71 if ( !$this->overwriteSameCase
) {
72 // Store the file at the destination
73 return $this->backend
->storeInternal( $this->setFlags( $this->params
) );
76 return StatusValue
::newGood();
79 protected function getSourceSha1Base36() {
80 MediaWiki\
suppressWarnings();
81 $hash = sha1_file( $this->params
['src'] );
82 MediaWiki\restoreWarnings
();
83 if ( $hash !== false ) {
84 $hash = Wikimedia\base_convert
( $hash, 16, 36, 31 );
90 public function storagePathsChanged() {
91 return [ $this->params
['dst'] ];