MediaWiki  master
CreateFileOp.php
Go to the documentation of this file.
1 <?php
26 class 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( array &$predicates ) {
36  $status = StatusValue::newGood();
37 
38  // Check if the source data is too big
39  $maxBytes = $this->backend->maxFileSizeInternal();
40  if ( strlen( $this->getParam( 'content' ) ) > $maxBytes ) {
41  $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxBytes );
42 
43  return $status;
44  }
45  // Check if an incompatible destination file exists
46  $status->merge( $this->precheckDestExistence( $predicates ) );
47  $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
48 
49  // Update file existence predicates if the operation is expected to be allowed to run
50  if ( $status->isOK() ) {
51  $predicates[self::ASSUMED_EXISTS][$this->params['dst']] = true;
52  $predicates[self::ASSUMED_SIZE][$this->params['dst']] = $this->sourceSize;
53  $predicates[self::ASSUMED_SHA1][$this->params['dst']] = $this->sourceSha1;
54  }
55 
56  return $status; // safe to call attempt()
57  }
58 
59  protected function doAttempt() {
60  if ( $this->overwriteSameCase ) {
61  $status = StatusValue::newGood(); // nothing to do
62  } else {
63  // Create the file at the destination
64  $status = $this->backend->createInternal( $this->setFlags( $this->params ) );
65  }
66 
67  return $status;
68  }
69 
70  protected function getSourceSize() {
71  return strlen( $this->params['content'] );
72  }
73 
74  protected function getSourceSha1Base36() {
75  return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
76  }
77 
78  public function storagePathsChanged() {
79  return [ $this->params['dst'] ];
80  }
81 }
CreateFileOp\doAttempt
doAttempt()
Definition: CreateFileOp.php:59
FileOp
FileBackend helper class for representing operations.
Definition: FileOp.php:36
FileOp\$destExists
bool $destExists
Definition: FileOp.php:65
CreateFileOp\getSourceSize
getSourceSize()
precheckDestExistence() helper function to get the source file size.
Definition: CreateFileOp.php:70
FileOp\ASSUMED_EXISTS
const ASSUMED_EXISTS
Definition: FileOp.php:73
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
CreateFileOp\getSourceSha1Base36
getSourceSha1Base36()
precheckDestExistence() helper function to get the source file SHA-1.
Definition: CreateFileOp.php:74
FileOp\getParam
getParam( $name)
Get the value of the parameter with the given name.
Definition: FileOp.php:141
CreateFileOp\doPrecheck
doPrecheck(array &$predicates)
Definition: CreateFileOp.php:35
CreateFileOp\allowedParams
allowedParams()
Get the file operation parameters.
Definition: CreateFileOp.php:27
FileOp\$sourceSha1
string bool $sourceSha1
Definition: FileOp.php:59
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
FileOp\ASSUMED_SIZE
const ASSUMED_SIZE
Definition: FileOp.php:74
FileOp\$sourceSize
int bool $sourceSize
Definition: FileOp.php:57
CreateFileOp\storagePathsChanged
storagePathsChanged()
Get a list of storage paths written to for this operation.
Definition: CreateFileOp.php:78
FileOp\precheckDestExistence
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition: FileOp.php:409
CreateFileOp
Create a file in the backend with the given content.
Definition: CreateFileOp.php:26