MediaWiki REL1_37
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->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}
FileBackend helper class for representing operations.
Definition FileOp.php:36
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
Definition FileOp.php:483
const ASSUMED_EXISTS
Definition FileOp.php:73
string bool $sourceSha1
Definition FileOp.php:59
getParam( $name)
Get the value of the parameter with the given name.
Definition FileOp.php:141
const ASSUMED_SIZE
Definition FileOp.php:74
const ASSUMED_SHA1
Definition FileOp.php:72
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:368
bool $destExists
Definition FileOp.php:65
int bool $sourceSize
Definition FileOp.php:57
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:409
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.