MediaWiki REL1_35
StoreFileOp.php
Go to the documentation of this file.
1<?php
24use Wikimedia\AtEase\AtEase;
25
30class 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
42 // Check if the source file exists in the file system and is not too big
43 if ( !is_file( $this->params['src'] ) ) {
44 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
45
46 return $status;
47 }
48 // Check if the source file is too big
49 $maxBytes = $this->backend->maxFileSizeInternal();
50 if ( filesize( $this->params['src'] ) > $maxBytes ) {
51 $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxBytes );
52
53 return $status;
54 }
55 // Check if an incompatible destination file exists
56 $status->merge( $this->precheckDestExistence( $predicates ) );
57 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
58
59 // Update file existence predicates if the operation is expected to be allowed to run
60 if ( $status->isOK() ) {
61 $predicates[self::ASSUMED_EXISTS][$this->params['dst']] = true;
62 $predicates[self::ASSUMED_SIZE][$this->params['dst']] = $this->sourceSize;
63 $predicates[self::ASSUMED_SHA1][$this->params['dst']] = $this->sourceSha1;
64 }
65
66 return $status; // safe to call attempt()
67 }
68
69 protected function doAttempt() {
70 if ( $this->overwriteSameCase ) {
71 $status = StatusValue::newGood(); // nothing to do
72 } else {
73 // Store the file at the destination
74 $status = $this->backend->storeInternal( $this->setFlags( $this->params ) );
75 }
76
77 return $status;
78 }
79
80 protected function getSourceSize() {
81 AtEase::suppressWarnings();
82 $size = filesize( $this->params['src'] );
83 AtEase::restoreWarnings();
84
85 return $size;
86 }
87
88 protected function getSourceSha1Base36() {
89 AtEase::suppressWarnings();
90 $hash = sha1_file( $this->params['src'] );
91 AtEase::restoreWarnings();
92 if ( $hash !== false ) {
93 $hash = Wikimedia\base_convert( $hash, 16, 36, 31 );
94 }
95
96 return $hash;
97 }
98
99 public function storagePathsChanged() {
100 return [ $this->params['dst'] ];
101 }
102}
FileBackend helper class for representing operations.
Definition FileOp.php:36
const ASSUMED_EXISTS
Definition FileOp.php:73
string bool $sourceSha1
Definition FileOp.php:59
const ASSUMED_SIZE
Definition FileOp.php:74
const ASSUMED_SHA1
Definition FileOp.php:72
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:368
bool $destExists
Definition FileOp.php:65
int bool $sourceSize
Definition FileOp.php:57
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:409
Store a file into the backend from a file on the file system.
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
doPrecheck(array &$predicates)
allowedParams()
Get the file operation parameters.
getSourceSize()
precheckDestExistence() helper function to get the source file size.
storagePathsChanged()
Get a list of storage paths written to for this operation.