MediaWiki  master
MoveFileOp.php
Go to the documentation of this file.
1 <?php
28 class MoveFileOp extends FileOp {
29  protected function allowedParams() {
30  return [
31  [ 'src', 'dst' ],
32  [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
33  [ 'src', 'dst' ]
34  ];
35  }
36 
37  protected function doPrecheck( array &$predicates ) {
38  $status = StatusValue::newGood();
39 
40  // Check source file existence
41  $srcExists = $this->fileExists( $this->params['src'], $predicates );
42  if ( $srcExists === false ) {
43  if ( $this->getParam( 'ignoreMissingSource' ) ) {
44  $this->cancelled = true; // no-op
45  // Update file existence predicates (cache 404s)
46  $predicates[self::ASSUMED_EXISTS][$this->params['src']] = false;
47  $predicates[self::ASSUMED_SIZE][$this->params['src']] = false;
48  $predicates[self::ASSUMED_SHA1][$this->params['src']] = false;
49 
50  return $status; // nothing to do
51  } else {
52  $status->fatal( 'backend-fail-notexists', $this->params['src'] );
53 
54  return $status;
55  }
56  } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
57  $status->fatal( 'backend-fail-stat', $this->params['src'] );
58 
59  return $status;
60  }
61  // Check if an incompatible destination file exists
62  $status->merge( $this->precheckDestExistence( $predicates ) );
63  $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
64 
65  // Update file existence predicates if the operation is expected to be allowed to run
66  if ( $status->isOK() ) {
67  $predicates[self::ASSUMED_EXISTS][$this->params['src']] = false;
68  $predicates[self::ASSUMED_SIZE][$this->params['src']] = false;
69  $predicates[self::ASSUMED_SHA1][$this->params['src']] = false;
70  $predicates[self::ASSUMED_EXISTS][$this->params['dst']] = true;
71  $predicates[self::ASSUMED_SIZE][$this->params['dst']] = $this->sourceSize;
72  $predicates[self::ASSUMED_SHA1][$this->params['dst']] = $this->sourceSha1;
73  }
74 
75  return $status; // safe to call attempt()
76  }
77 
78  protected function doAttempt() {
79  if ( $this->overwriteSameCase ) {
80  if ( $this->params['src'] === $this->params['dst'] ) {
81  // Do nothing to the destination (which is also the source)
82  $status = StatusValue::newGood();
83  } else {
84  // Just delete the source as the destination file needs no changes
85  $status = $this->backend->deleteInternal( $this->setFlags(
86  [ 'src' => $this->params['src'] ]
87  ) );
88  }
89  } elseif ( $this->params['src'] === $this->params['dst'] ) {
90  // Just update the destination file headers
91  $headers = $this->getParam( 'headers' ) ?: [];
92  $status = $this->backend->describeInternal( $this->setFlags(
93  [ 'src' => $this->params['dst'], 'headers' => $headers ]
94  ) );
95  } else {
96  // Move the file to the destination
97  $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
98  }
99 
100  return $status;
101  }
102 
103  public function storagePathsRead() {
104  return [ $this->params['src'] ];
105  }
106 
107  public function storagePathsChanged() {
108  return [ $this->params['src'], $this->params['dst'] ];
109  }
110 }
MoveFileOp\storagePathsRead
storagePathsRead()
Get a list of storage paths read from for this operation.
Definition: MoveFileOp.php:103
FileOp
FileBackend helper class for representing operations.
Definition: FileOp.php:36
FileOp\$destExists
bool $destExists
Definition: FileOp.php:65
MoveFileOp\allowedParams
allowedParams()
Get the file operation parameters.
Definition: MoveFileOp.php:29
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
MoveFileOp\doPrecheck
doPrecheck(array &$predicates)
Definition: MoveFileOp.php:37
FileOp\getParam
getParam( $name)
Get the value of the parameter with the given name.
Definition: FileOp.php:141
FileOp\$sourceSha1
string bool $sourceSha1
Definition: FileOp.php:59
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MoveFileOp\doAttempt
doAttempt()
Definition: MoveFileOp.php:78
MoveFileOp
Move a file from one storage path to another in the backend.
Definition: MoveFileOp.php:28
FileOp\ASSUMED_SIZE
const ASSUMED_SIZE
Definition: FileOp.php:74
FileOp\$sourceSize
int bool $sourceSize
Definition: FileOp.php:57
MoveFileOp\storagePathsChanged
storagePathsChanged()
Get a list of storage paths written to for this operation.
Definition: MoveFileOp.php:107
FileOp\fileExists
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
Definition: FileOp.php:483
FileOp\precheckDestExistence
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition: FileOp.php:409