MediaWiki  1.34.0
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 ) {
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  if ( $status->isOK() ) {
59  // Update file existence predicates
60  $predicates['exists'][$this->params['dst']] = true;
61  $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
62  }
63 
64  return $status; // safe to call attempt()
65  }
66 
67  protected function doAttempt() {
68  if ( $this->overwriteSameCase ) {
69  $status = StatusValue::newGood(); // nothing to do
70  } else {
71  // Store the file at the destination
72  $status = $this->backend->storeInternal( $this->setFlags( $this->params ) );
73  }
74 
75  return $status;
76  }
77 
78  protected function getSourceSha1Base36() {
79  AtEase::suppressWarnings();
80  $hash = sha1_file( $this->params['src'] );
81  AtEase::restoreWarnings();
82  if ( $hash !== false ) {
83  $hash = Wikimedia\base_convert( $hash, 16, 36, 31 );
84  }
85 
86  return $hash;
87  }
88 
89  public function storagePathsChanged() {
90  return [ $this->params['dst'] ];
91  }
92 }
FileOp\$sourceSha1
string $sourceSha1
Definition: FileOp.php:61
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:67
StoreFileOp\getSourceSha1Base36
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
Definition: StoreFileOp.php:78
FileOp\setFlags
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition: FileOp.php:346
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
StoreFileOp\doAttempt
doAttempt()
Definition: StoreFileOp.php:67
$status
return $status
Definition: SyntaxHighlight.php:347
StoreFileOp
Store a file into the backend from a file on the file system.
Definition: StoreFileOp.php:30
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:376
StoreFileOp\storagePathsChanged
storagePathsChanged()
Get a list of storage paths written to for this operation.
Definition: StoreFileOp.php:89