MediaWiki master
CreateFileOp.php
Go to the documentation of this file.
1<?php
26class CreateFileOp extends FileOp {
27 protected function allowedParams() {
28 return [
29 [ 'content', 'dst' ],
30 [ 'overwrite', 'overwriteSame', 'headers' ],
31 [ 'dst' ]
32 ];
33 }
34
35 protected function doPrecheck(
36 FileStatePredicates $opPredicates,
37 FileStatePredicates $batchPredicates
38 ) {
39 $status = StatusValue::newGood();
40
41 // Check if the source data is too big
42 $sourceSize = $this->getSourceSize();
43 $maxFileSize = $this->backend->maxFileSizeInternal();
44 if ( $sourceSize > $maxFileSize ) {
45 $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxFileSize );
46
47 return $status;
48 }
49 // Check if an incompatible destination file exists
50 $sourceSha1 = $this->getSourceSha1Base36();
51 $status->merge( $this->precheckDestExistence( $opPredicates, $sourceSize, $sourceSha1 ) );
52 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
53
54 // Update file existence predicates if the operation is expected to be allowed to run
55 if ( $status->isOK() ) {
56 $batchPredicates->assumeFileExists( $this->params['dst'], $sourceSize, $sourceSha1 );
57 }
58
59 return $status; // safe to call attempt()
60 }
61
62 protected function doAttempt() {
63 if ( $this->overwriteSameCase ) {
64 $status = StatusValue::newGood(); // nothing to do
65 } else {
66 // Create the file at the destination
67 $status = $this->backend->createInternal( $this->setFlags( $this->params ) );
68 }
69
70 return $status;
71 }
72
73 protected function getSourceSize() {
74 return strlen( $this->params['content'] );
75 }
76
77 protected function getSourceSha1Base36() {
78 return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
79 }
80
81 public function storagePathsChanged() {
82 return [ $this->params['dst'] ];
83 }
84}
Create a file in the backend with the given content.
allowedParams()
Get the file operation parameters.
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.
storagePathsChanged()
Get a list of storage paths written to for this operation.
FileBackend helper class for representing operations.
Definition FileOp.php:37
bool null $destExists
Definition FileOp.php:58
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:354
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:310
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.