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