MediaWiki REL1_34
MoveFileOp.php
Go to the documentation of this file.
1<?php
28class 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->doOperation = false; // no-op
45 // Update file existence predicates (cache 404s)
46 $predicates['exists'][$this->params['src']] = false;
47 $predicates['sha1'][$this->params['src']] = false;
48
49 return $status; // nothing to do
50 } else {
51 $status->fatal( 'backend-fail-notexists', $this->params['src'] );
52
53 return $status;
54 }
55 } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
56 $status->fatal( 'backend-fail-stat', $this->params['src'] );
57
58 return $status;
59 }
60 // Check if an incompatible destination file exists
61 $status->merge( $this->precheckDestExistence( $predicates ) );
62 $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
63 if ( $status->isOK() ) {
64 // Update file existence predicates
65 $predicates['exists'][$this->params['src']] = false;
66 $predicates['sha1'][$this->params['src']] = false;
67 $predicates['exists'][$this->params['dst']] = true;
68 $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
69 }
70
71 return $status; // safe to call attempt()
72 }
73
74 protected function doAttempt() {
75 if ( $this->overwriteSameCase ) {
76 if ( $this->params['src'] === $this->params['dst'] ) {
77 // Do nothing to the destination (which is also the source)
78 $status = StatusValue::newGood();
79 } else {
80 // Just delete the source as the destination file needs no changes
81 $status = $this->backend->deleteInternal( $this->setFlags(
82 [ 'src' => $this->params['src'] ]
83 ) );
84 }
85 } elseif ( $this->params['src'] === $this->params['dst'] ) {
86 // Just update the destination file headers
87 $headers = $this->getParam( 'headers' ) ?: [];
88 $status = $this->backend->describeInternal( $this->setFlags(
89 [ 'src' => $this->params['dst'], 'headers' => $headers ]
90 ) );
91 } else {
92 // Move the file to the destination
93 $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
94 }
95
96 return $status;
97 }
98
99 public function storagePathsRead() {
100 return [ $this->params['src'] ];
101 }
102
103 public function storagePathsChanged() {
104 return [ $this->params['src'], $this->params['dst'] ];
105 }
106}
FileBackend helper class for representing operations.
Definition FileOp.php:36
string $sourceSha1
Definition FileOp.php:61
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
Definition FileOp.php:433
getParam( $name)
Get the value of the parameter with the given name.
Definition FileOp.php:139
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:346
bool $destExists
Definition FileOp.php:67
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:376
Move a file from one storage path to another in the backend.
doPrecheck(array &$predicates)
storagePathsRead()
Get a list of storage paths read from for this operation.
allowedParams()
Get the file operation parameters.
storagePathsChanged()
Get a list of storage paths written to for this operation.