MediaWiki  master
StoreFileOp.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\AtEase\AtEase;
25 
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 
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 }
StoreFileOp\doPrecheck
doPrecheck(array &$predicates)
Definition: StoreFileOp.php:39
FileOp
FileBackend helper class for representing operations.
Definition: FileOp.php:36
FileOp\$destExists
bool $destExists
Definition: FileOp.php:65
StoreFileOp\getSourceSize
getSourceSize()
precheckDestExistence() helper function to get the source file size.
Definition: StoreFileOp.php:80
FileOp\ASSUMED_EXISTS
const ASSUMED_EXISTS
Definition: FileOp.php:73
StoreFileOp\getSourceSha1Base36
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
Definition: StoreFileOp.php:88
FileOp\ASSUMED_SHA1
const ASSUMED_SHA1
Definition: FileOp.php:72
FileOp\setFlags
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition: FileOp.php:368
FileOp\$sourceSha1
string bool $sourceSha1
Definition: FileOp.php:59
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
StoreFileOp\doAttempt
doAttempt()
Definition: StoreFileOp.php:69
FileOp\ASSUMED_SIZE
const ASSUMED_SIZE
Definition: FileOp.php:74
StoreFileOp
Store a file into the backend from a file on the file system.
Definition: StoreFileOp.php:30
FileOp\$sourceSize
int bool $sourceSize
Definition: FileOp.php:57
StoreFileOp\allowedParams
allowedParams()
Get the file operation parameters.
Definition: StoreFileOp.php:31
FileOp\precheckDestExistence
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition: FileOp.php:409
StoreFileOp\storagePathsChanged
storagePathsChanged()
Get a list of storage paths written to for this operation.
Definition: StoreFileOp.php:99