Merge "(Bug 41352) Provide tests for edit conflicts."
[lhc/web/wiklou.git] / includes / filebackend / FileOp.php
index 7c43c48..ff1b604 100644 (file)
@@ -45,6 +45,7 @@ abstract class FileOp {
        protected $useLatest = true; // boolean
        protected $batchId; // string
 
+       protected $doOperation = true; // boolean; operation is not a no-op
        protected $sourceSha1; // string
        protected $destSameAsSource; // boolean
 
@@ -65,19 +66,53 @@ abstract class FileOp {
                list( $required, $optional ) = $this->allowedParams();
                foreach ( $required as $name ) {
                        if ( isset( $params[$name] ) ) {
-                               $this->params[$name] = $params[$name];
+                               $this->params[$name] = self::normalizeAnyStoragePaths( $params[$name] );
                        } else {
                                throw new MWException( "File operation missing parameter '$name'." );
                        }
                }
                foreach ( $optional as $name ) {
                        if ( isset( $params[$name] ) ) {
-                               $this->params[$name] = $params[$name];
+                               $this->params[$name] = self::normalizeAnyStoragePaths( $params[$name] );
                        }
                }
                $this->params = $params;
        }
 
+       /**
+        * Normalize $item or anything in $item that is a valid storage path
+        *
+        * @param $item string|array
+        * @return string|Array
+        */
+       protected function normalizeAnyStoragePaths( $item ) {
+               if ( is_array( $item ) ) {
+                       $res = array();
+                       foreach ( $item as $k => $v ) {
+                               $k = self::normalizeIfValidStoragePath( $k );
+                               $v = self::normalizeIfValidStoragePath( $v );
+                               $res[$k] = $v;
+                       }
+                       return $res;
+               } else {
+                       return self::normalizeIfValidStoragePath( $item );
+               }
+       }
+
+       /**
+        * Normalize a string if it is a valid storage path
+        *
+        * @param $path string
+        * @return string
+        */
+       protected static function normalizeIfValidStoragePath( $path ) {
+               if ( FileBackend::isStoragePath( $path ) ) {
+                       $res = FileBackend::normalizeStoragePath( $path );
+                       return ( $res !== null ) ? $res : $path;
+               }
+               return $path;
+       }
+
        /**
         * Set the batch UUID this operation belongs to
         *
@@ -175,11 +210,14 @@ abstract class FileOp {
         * @return Array
         */
        final public function getJournalEntries( array $oPredicates, array $nPredicates ) {
+               if ( !$this->doOperation ) {
+                       return array(); // this is a no-op
+               }
                $nullEntries = array();
                $updateEntries = array();
                $deleteEntries = array();
                $pathsUsed = array_merge( $this->storagePathsRead(), $this->storagePathsChanged() );
-               foreach ( $pathsUsed as $path ) {
+               foreach ( array_unique( $pathsUsed ) as $path ) {
                        $nullEntries[] = array( // assertion for recovery
                                'op'      => 'null',
                                'path'    => $path,
@@ -205,7 +243,9 @@ abstract class FileOp {
        }
 
        /**
-        * Check preconditions of the operation without writing anything
+        * Check preconditions of the operation without writing anything.
+        * This must update $predicates for each path that the op can change
+        * except when a failing status object is returned.
         *
         * @param $predicates Array
         * @return Status
@@ -241,10 +281,14 @@ abstract class FileOp {
                        return Status::newFatal( 'fileop-fail-attempt-precheck' );
                }
                $this->state = self::STATE_ATTEMPTED;
-               $status = $this->doAttempt();
-               if ( !$status->isOK() ) {
-                       $this->failed = true;
-                       $this->logFailure( 'attempt' );
+               if ( $this->doOperation ) {
+                       $status = $this->doAttempt();
+                       if ( !$status->isOK() ) {
+                               $this->failed = true;
+                               $this->logFailure( 'attempt' );
+                       }
+               } else { // no-op
+                       $status = Status::newGood();
                }
                return $status;
        }
@@ -292,15 +336,7 @@ abstract class FileOp {
         *
         * @return Array
         */
-       final public function storagePathsRead() {
-               return array_map( 'FileBackend::normalizeStoragePath', $this->doStoragePathsRead() );
-       }
-
-       /**
-        * @see FileOp::storagePathsRead()
-        * @return Array
-        */
-       protected function doStoragePathsRead() {
+       public function storagePathsRead() {
                return array();
        }
 
@@ -309,15 +345,7 @@ abstract class FileOp {
         *
         * @return Array
         */
-       final public function storagePathsChanged() {
-               return array_map( 'FileBackend::normalizeStoragePath', $this->doStoragePathsChanged() );
-       }
-
-       /**
-        * @see FileOp::storagePathsChanged()
-        * @return Array
-        */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array();
        }
 
@@ -396,6 +424,8 @@ abstract class FileOp {
        final protected function fileSha1( $source, array $predicates ) {
                if ( isset( $predicates['sha1'][$source] ) ) {
                        return $predicates['sha1'][$source]; // previous op assures this
+               } elseif ( isset( $predicates['exists'][$source] ) && !$predicates['exists'][$source] ) {
+                       return false; // previous op assures this
                } else {
                        $params = array( 'src' => $source, 'latest' => $this->useLatest );
                        return $this->backend->getFileSha1Base36( $params );
@@ -458,7 +488,7 @@ class StoreFileOp extends FileOp {
                                $this->params['dst'], $this->backend->maxFileSizeInternal() );
                        $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
                        return $status;
-               // Check if a file can be placed at the destination
+               // Check if a file can be placed/changed at the destination
                } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
                        $status->fatal( 'backend-fail-usable', $this->params['dst'] );
                        $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
@@ -498,7 +528,7 @@ class StoreFileOp extends FileOp {
                return $hash;
        }
 
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
 }
@@ -521,7 +551,7 @@ class CreateFileOp extends FileOp {
                                $this->params['dst'], $this->backend->maxFileSizeInternal() );
                        $status->fatal( 'backend-fail-create', $this->params['dst'] );
                        return $status;
-               // Check if a file can be placed at the destination
+               // Check if a file can be placed/changed at the destination
                } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
                        $status->fatal( 'backend-fail-usable', $this->params['dst'] );
                        $status->fatal( 'backend-fail-create', $this->params['dst'] );
@@ -558,7 +588,7 @@ class CreateFileOp extends FileOp {
        /**
         * @return array
         */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
 }
@@ -573,7 +603,7 @@ class CopyFileOp extends FileOp {
         */
        protected function allowedParams() {
                return array( array( 'src', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+                       array( 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'disposition' ) );
        }
 
        /**
@@ -584,9 +614,17 @@ class CopyFileOp extends FileOp {
                $status = Status::newGood();
                // Check if the source file exists
                if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
-                       $status->fatal( 'backend-fail-notexists', $this->params['src'] );
-                       return $status;
-               // Check if a file can be placed at the destination
+                       if ( $this->getParam( 'ignoreMissingSource' ) ) {
+                               $this->doOperation = false; // no-op
+                               // Update file existence predicates (cache 404s)
+                               $predicates['exists'][$this->params['src']] = false;
+                               $predicates['sha1'][$this->params['src']] = false;
+                               return $status; // nothing to do
+                       } else {
+                               $status->fatal( 'backend-fail-notexists', $this->params['src'] );
+                               return $status;
+                       }
+               // Check if a file can be placed/changed at the destination
                } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
                        $status->fatal( 'backend-fail-usable', $this->params['dst'] );
                        $status->fatal( 'backend-fail-copy', $this->params['src'], $this->params['dst'] );
@@ -619,14 +657,14 @@ class CopyFileOp extends FileOp {
        /**
         * @return array
         */
-       protected function doStoragePathsRead() {
+       public function storagePathsRead() {
                return array( $this->params['src'] );
        }
 
        /**
         * @return array
         */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
 }
@@ -641,7 +679,7 @@ class MoveFileOp extends FileOp {
         */
        protected function allowedParams() {
                return array( array( 'src', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+                       array( 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'disposition' ) );
        }
 
        /**
@@ -652,9 +690,17 @@ class MoveFileOp extends FileOp {
                $status = Status::newGood();
                // Check if the source file exists
                if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
-                       $status->fatal( 'backend-fail-notexists', $this->params['src'] );
-                       return $status;
-               // Check if a file can be placed at the destination
+                       if ( $this->getParam( 'ignoreMissingSource' ) ) {
+                               $this->doOperation = false; // no-op
+                               // Update file existence predicates (cache 404s)
+                               $predicates['exists'][$this->params['src']] = false;
+                               $predicates['sha1'][$this->params['src']] = false;
+                               return $status; // nothing to do
+                       } else {
+                               $status->fatal( 'backend-fail-notexists', $this->params['src'] );
+                               return $status;
+                       }
+               // Check if a file can be placed/changed at the destination
                } elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
                        $status->fatal( 'backend-fail-usable', $this->params['dst'] );
                        $status->fatal( 'backend-fail-move', $this->params['src'], $this->params['dst'] );
@@ -693,14 +739,14 @@ class MoveFileOp extends FileOp {
        /**
         * @return array
         */
-       protected function doStoragePathsRead() {
+       public function storagePathsRead() {
                return array( $this->params['src'] );
        }
 
        /**
         * @return array
         */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['src'], $this->params['dst'] );
        }
 }
@@ -717,21 +763,29 @@ class DeleteFileOp extends FileOp {
                return array( array( 'src' ), array( 'ignoreMissingSource' ) );
        }
 
-       protected $needsDelete = true;
-
        /**
-        * @param array $predicates
+        * @param $predicates array
         * @return Status
         */
        protected function doPrecheck( array &$predicates ) {
                $status = Status::newGood();
                // Check if the source file exists
                if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
-                       if ( !$this->getParam( 'ignoreMissingSource' ) ) {
+                       if ( $this->getParam( 'ignoreMissingSource' ) ) {
+                               $this->doOperation = false; // no-op
+                               // Update file existence predicates (cache 404s)
+                               $predicates['exists'][$this->params['src']] = false;
+                               $predicates['sha1'][$this->params['src']] = false;
+                               return $status; // nothing to do
+                       } else {
                                $status->fatal( 'backend-fail-notexists', $this->params['src'] );
                                return $status;
                        }
-                       $this->needsDelete = false;
+               // Check if a file can be placed/changed at the source
+               } elseif ( !$this->backend->isPathUsableInternal( $this->params['src'] ) ) {
+                       $status->fatal( 'backend-fail-usable', $this->params['src'] );
+                       $status->fatal( 'backend-fail-delete', $this->params['src'] );
+                       return $status;
                }
                // Update file existence predicates
                $predicates['exists'][$this->params['src']] = false;
@@ -743,17 +797,14 @@ class DeleteFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               if ( $this->needsDelete ) {
-                       // Delete the source file
-                       return $this->backend->deleteInternal( $this->setFlags( $this->params ) );
-               }
-               return Status::newGood();
+               // Delete the source file
+               return $this->backend->deleteInternal( $this->setFlags( $this->params ) );
        }
 
        /**
         * @return array
         */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['src'] );
        }
 }