MediaWiki master
StoreFileOp.php
Go to the documentation of this file.
1<?php
11
12use StatusValue;
13use Wikimedia\AtEase\AtEase;
14
19class StoreFileOp extends FileOp {
21 protected function allowedParams() {
22 return [
23 [ 'src', 'dst' ],
24 [ 'overwrite', 'overwriteSame', 'headers' ],
25 [ 'src', 'dst' ]
26 ];
27 }
28
30 protected function doPrecheck(
31 FileStatePredicates $opPredicates,
32 FileStatePredicates $batchPredicates
33 ) {
34 $status = StatusValue::newGood();
35
36 // Check if the source file exists in the file system
37 if ( !is_file( $this->params['src'] ) ) {
38 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
39
40 return $status;
41 }
42 // Check if the source file is too big
43 $sourceSize = $this->getSourceSize();
44 $maxFileSize = $this->backend->maxFileSizeInternal();
45 if ( $sourceSize > $maxFileSize ) {
46 $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize );
47
48 return $status;
49 }
50 // Check if an incompatible destination file exists
51 $sourceSha1 = function () {
52 static $sha1 = null;
53 $sha1 ??= $this->getSourceSha1Base36();
54 return $sha1;
55 };
56 $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) );
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 $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 );
62 }
63
64 return $status; // safe to call attempt()
65 }
66
68 protected function doAttempt() {
69 if ( $this->overwriteSameCase ) {
70 $status = StatusValue::newGood(); // nothing to do
71 } else {
72 // Store the file at the destination
73 $status = $this->backend->storeInternal( $this->setFlags( $this->params ) );
74 }
75
76 return $status;
77 }
78
79 protected function getSourceSize(): int {
80 AtEase::suppressWarnings();
81 $size = filesize( $this->params['src'] );
82 AtEase::restoreWarnings();
83
84 return $size;
85 }
86
87 protected function getSourceSha1Base36(): string {
88 AtEase::suppressWarnings();
89 $hash = sha1_file( $this->params['src'] );
90 AtEase::restoreWarnings();
91 if ( $hash !== false ) {
92 $hash = \Wikimedia\base_convert( $hash, 16, 36, 31 );
93 }
94
95 return $hash;
96 }
97
99 public function storagePathsChanged() {
100 return [ $this->params['dst'] ];
101 }
102}
103
105class_alias( StoreFileOp::class, 'StoreFileOp' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.
FileBackend helper class for representing operations.
Definition FileOp.php:32
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:348
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:304
Helper class for tracking counterfactual file states when pre-checking file operation batches.
assumeFileExists(string $path, $size, $sha1Base36)
Predicate that a file exists at the path.
Store a file into the backend from a file on the file system.
storagePathsChanged()
Get a list of storage paths written to for this operation.array
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.Updates the batch predica...
allowedParams()
Get the file operation parameters.array (required params list, optional params list,...