Merge "else if -> elseif"
[lhc/web/wiklou.git] / includes / filebackend / FileOp.php
index e019324..62e9fba 100644 (file)
@@ -46,7 +46,7 @@ abstract class FileOp {
 
        protected $doOperation = true; // boolean; operation is not a no-op
        protected $sourceSha1; // string
-       protected $destSameAsSource; // boolean
+       protected $overwriteSameCase; // boolean
        protected $destExists; // boolean
 
        /* Object life-cycle */
@@ -55,7 +55,7 @@ abstract class FileOp {
        const STATE_ATTEMPTED = 3;
 
        /**
-        * Build a new file operation transaction
+        * Build a new batch file operation transaction
         *
         * @param $backend FileBackendStore
         * @param $params Array
@@ -64,8 +64,10 @@ abstract class FileOp {
        final public function __construct( FileBackendStore $backend, array $params ) {
                $this->backend = $backend;
                list( $required, $optional ) = $this->allowedParams();
+               // @TODO: normalizeAnyStoragePaths() calls are overzealous, use a parameter list
                foreach ( $required as $name ) {
                        if ( isset( $params[$name] ) ) {
+                               // Normalize paths so the paths to the same file have the same string
                                $this->params[$name] = self::normalizeAnyStoragePaths( $params[$name] );
                        } else {
                                throw new MWException( "File operation missing parameter '$name'." );
@@ -73,6 +75,7 @@ abstract class FileOp {
                }
                foreach ( $optional as $name ) {
                        if ( isset( $params[$name] ) ) {
+                               // Normalize paths so the paths to the same file have the same string
                                $this->params[$name] = self::normalizeAnyStoragePaths( $params[$name] );
                        }
                }
@@ -341,7 +344,7 @@ abstract class FileOp {
 
        /**
         * Check for errors with regards to the destination file already existing.
-        * Also set the destExists, destSameAsSource and sourceSha1 member variables.
+        * Also set the destExists, overwriteSameCase and sourceSha1 member variables.
         * A bad status will be returned if there is no chance it can be overwritten.
         *
         * @param $predicates Array
@@ -354,7 +357,7 @@ abstract class FileOp {
                if ( $this->sourceSha1 === null ) { // file in storage?
                        $this->sourceSha1 = $this->fileSha1( $this->params['src'], $predicates );
                }
-               $this->destSameAsSource = false;
+               $this->overwriteSameCase = false;
                $this->destExists = $this->fileExists( $this->params['dst'], $predicates );
                if ( $this->destExists ) {
                        if ( $this->getParam( 'overwrite' ) ) {
@@ -368,7 +371,7 @@ abstract class FileOp {
                                        // Give an error if the files are not identical
                                        $status->fatal( 'backend-fail-notsame', $this->params['dst'] );
                                } else {
-                                       $this->destSameAsSource = true; // OK
+                                       $this->overwriteSameCase = true; // OK
                                }
                                return $status; // do nothing; either OK or bad status
                        } else {
@@ -489,7 +492,7 @@ class CreateFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               if ( !$this->destSameAsSource ) {
+               if ( !$this->overwriteSameCase ) {
                        // Create the file at the destination
                        return $this->backend->createInternal( $this->setFlags( $this->params ) );
                }
@@ -561,8 +564,8 @@ class StoreFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               // Store the file at the destination
-               if ( !$this->destSameAsSource ) {
+               if ( !$this->overwriteSameCase ) {
+                       // Store the file at the destination
                        return $this->backend->storeInternal( $this->setFlags( $this->params ) );
                }
                return Status::newGood();
@@ -638,14 +641,19 @@ class CopyFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               // Do nothing if the src/dst paths are the same
-               if ( $this->params['src'] !== $this->params['dst'] ) {
-                       // Copy the file into the destination
-                       if ( !$this->destSameAsSource ) {
-                               return $this->backend->copyInternal( $this->setFlags( $this->params ) );
-                       }
+               if ( $this->overwriteSameCase ) {
+                       $status = Status::newGood(); // nothing to do
+               } elseif ( $this->params['src'] === $this->params['dst'] ) {
+                       // Just update the destination file headers
+                       $headers = $this->getParam( 'headers' ) ?: array();
+                       $status = $this->backend->describeInternal( $this->setFlags( array(
+                               'src' => $this->params['dst'], 'headers' => $headers
+                       ) ) );
+               } else {
+                       // Copy the file to the destination
+                       $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
                }
-               return Status::newGood();
+               return $status;
        }
 
        /**
@@ -717,18 +725,27 @@ class MoveFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               // Do nothing if the src/dst paths are the same
-               if ( $this->params['src'] !== $this->params['dst'] ) {
-                       if ( !$this->destSameAsSource ) {
-                               // Move the file into the destination
-                               return $this->backend->moveInternal( $this->setFlags( $this->params ) );
+               if ( $this->overwriteSameCase ) {
+                       if ( $this->params['src'] === $this->params['dst'] ) {
+                               // Do nothing to the destination (which is also the source)
+                               $status = Status::newGood();
                        } else {
-                               // Just delete source as the destination needs no changes
-                               $params = array( 'src' => $this->params['src'] );
-                               return $this->backend->deleteInternal( $this->setFlags( $params ) );
+                               // Just delete the source as the destination file needs no changes
+                               $status = $this->backend->deleteInternal( $this->setFlags(
+                                       array( 'src' => $this->params['src'] )
+                               ) );
                        }
+               } elseif ( $this->params['src'] === $this->params['dst'] ) {
+                       // Just update the destination file headers
+                       $headers = $this->getParam( 'headers' ) ?: array();
+                       $status = $this->backend->describeInternal( $this->setFlags(
+                               array( 'src' => $this->params['dst'], 'headers' => $headers )
+                       ) );
+               } else {
+                       // Move the file to the destination
+                       $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
                }
-               return Status::newGood();
+               return $status;
        }
 
        /**