MediaWiki REL1_40
CopyFileOp.php
Go to the documentation of this file.
1<?php
28class CopyFileOp 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 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['dst']] = true;
68 $predicates[self::ASSUMED_SIZE][$this->params['dst']] = $this->sourceSize;
69 $predicates[self::ASSUMED_SHA1][$this->params['dst']] = $this->sourceSha1;
70 }
71
72 return $status; // safe to call attempt()
73 }
74
75 protected function doAttempt() {
76 if ( $this->overwriteSameCase ) {
77 $status = StatusValue::newGood(); // nothing to do
78 } elseif ( $this->params['src'] === $this->params['dst'] ) {
79 // Just update the destination file headers
80 $headers = $this->getParam( 'headers' ) ?: [];
81 $status = $this->backend->describeInternal( $this->setFlags( [
82 'src' => $this->params['dst'], 'headers' => $headers
83 ] ) );
84 } else {
85 // Copy the file to the destination
86 $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
87 }
88
89 return $status;
90 }
91
92 public function storagePathsRead() {
93 return [ $this->params['src'] ];
94 }
95
96 public function storagePathsChanged() {
97 return [ $this->params['dst'] ];
98 }
99}
Copy a file from one storage path to another in the backend.
doPrecheck(array &$predicates)
allowedParams()
Get the file operation parameters.
storagePathsChanged()
Get a list of storage paths written to for this operation.
storagePathsRead()
Get a list of storage paths read from for this operation.
FileBackend helper class for representing operations.
Definition FileOp.php:37
fileExists( $source, array $predicates)
Check if a file will exist in storage when this operation is attempted.
Definition FileOp.php:431
const ASSUMED_EXISTS
Definition FileOp.php:72
string bool $sourceSha1
Definition FileOp.php:58
getParam( $name)
Get the value of the parameter with the given name.
Definition FileOp.php:131
const ASSUMED_SIZE
Definition FileOp.php:73
const ASSUMED_SHA1
Definition FileOp.php:71
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
Definition FileOp.php:318
bool $destExists
Definition FileOp.php:64
int bool $sourceSize
Definition FileOp.php:56
precheckDestExistence(array $predicates)
Check for errors with regards to the destination file already existing.
Definition FileOp.php:359