Merge "Fix uselang parameter in ApiWatch"
[lhc/web/wiklou.git] / includes / filebackend / FileOp.php
index 7c43c48..0d64a44 100644 (file)
@@ -45,8 +45,10 @@ 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
+       protected $destExists; // boolean
 
        /* Object life-cycle */
        const STATE_NEW = 1;
@@ -65,19 +67,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 +211,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 +244,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 +282,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 +337,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,21 +346,13 @@ 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();
        }
 
        /**
         * Check for errors with regards to the destination file already existing.
-        * This also updates the destSameAsSource and sourceSha1 member variables.
+        * Also set the destExists, destSameAsSource and sourceSha1 member variables.
         * A bad status will be returned if there is no chance it can be overwritten.
         *
         * @param $predicates Array
@@ -337,7 +366,8 @@ abstract class FileOp {
                        $this->sourceSha1 = $this->fileSha1( $this->params['src'], $predicates );
                }
                $this->destSameAsSource = false;
-               if ( $this->fileExists( $this->params['dst'], $predicates ) ) {
+               $this->destExists = $this->fileExists( $this->params['dst'], $predicates );
+               if ( $this->destExists ) {
                        if ( $this->getParam( 'overwrite' ) ) {
                                return $status; // OK
                        } elseif ( $this->getParam( 'overwriteSame' ) ) {
@@ -396,6 +426,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 );
@@ -430,42 +462,32 @@ abstract class FileOp {
 }
 
 /**
- * Store a file into the backend from a file on the file system.
+ * Create a file in the backend with the given content.
  * Parameters for this operation are outlined in FileBackend::doOperations().
  */
-class StoreFileOp extends FileOp {
-       /**
-        * @return array
-        */
+class CreateFileOp extends FileOp {
        protected function allowedParams() {
-               return array( array( 'src', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+               return array( array( 'content', 'dst' ),
+                       array( 'overwrite', 'overwriteSame', 'disposition', 'headers' ) );
        }
 
-       /**
-        * @param $predicates array
-        * @return Status
-        */
        protected function doPrecheck( array &$predicates ) {
                $status = Status::newGood();
-               // Check if the source file exists on the file system
-               if ( !is_file( $this->params['src'] ) ) {
-                       $status->fatal( 'backend-fail-notexists', $this->params['src'] );
-                       return $status;
-               // Check if the source file is too big
-               } elseif ( filesize( $this->params['src'] ) > $this->backend->maxFileSizeInternal() ) {
+               // Check if the source data is too big
+               if ( strlen( $this->getParam( 'content' ) ) > $this->backend->maxFileSizeInternal() ) {
                        $status->fatal( 'backend-fail-maxsize',
                                $this->params['dst'], $this->backend->maxFileSizeInternal() );
-                       $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
+                       $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-store', $this->params['src'], $this->params['dst'] );
+                       $status->fatal( 'backend-fail-create', $this->params['dst'] );
                        return $status;
                }
                // Check if destination file exists
                $status->merge( $this->precheckDestExistence( $predicates ) );
+               $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
                if ( $status->isOK() ) {
                        // Update file existence predicates
                        $predicates['exists'][$this->params['dst']] = true;
@@ -478,57 +500,66 @@ class StoreFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               // Store the file at the destination
                if ( !$this->destSameAsSource ) {
-                       return $this->backend->storeInternal( $this->setFlags( $this->params ) );
+                       // Create the file at the destination
+                       return $this->backend->createInternal( $this->setFlags( $this->params ) );
                }
                return Status::newGood();
        }
 
        /**
-        * @return bool|string
+        * @return bool|String
         */
        protected function getSourceSha1Base36() {
-               wfSuppressWarnings();
-               $hash = sha1_file( $this->params['src'] );
-               wfRestoreWarnings();
-               if ( $hash !== false ) {
-                       $hash = wfBaseConvert( $hash, 16, 36, 31 );
-               }
-               return $hash;
+               return wfBaseConvert( sha1( $this->params['content'] ), 16, 36, 31 );
        }
 
-       protected function doStoragePathsChanged() {
+       /**
+        * @return array
+        */
+       public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
 }
 
 /**
- * Create a file in the backend with the given content.
+ * Store a file into the backend from a file on the file system.
  * Parameters for this operation are outlined in FileBackend::doOperations().
  */
-class CreateFileOp extends FileOp {
+class StoreFileOp extends FileOp {
+       /**
+        * @return array
+        */
        protected function allowedParams() {
-               return array( array( 'content', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+               return array( array( 'src', 'dst' ),
+                       array( 'overwrite', 'overwriteSame', 'disposition', 'headers' ) );
        }
 
+       /**
+        * @param $predicates array
+        * @return Status
+        */
        protected function doPrecheck( array &$predicates ) {
                $status = Status::newGood();
-               // Check if the source data is too big
-               if ( strlen( $this->getParam( 'content' ) ) > $this->backend->maxFileSizeInternal() ) {
+               // Check if the source file exists on the file system
+               if ( !is_file( $this->params['src'] ) ) {
+                       $status->fatal( 'backend-fail-notexists', $this->params['src'] );
+                       return $status;
+               // Check if the source file is too big
+               } elseif ( filesize( $this->params['src'] ) > $this->backend->maxFileSizeInternal() ) {
                        $status->fatal( 'backend-fail-maxsize',
                                $this->params['dst'], $this->backend->maxFileSizeInternal() );
-                       $status->fatal( 'backend-fail-create', $this->params['dst'] );
+                       $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-create', $this->params['dst'] );
+                       $status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
                        return $status;
                }
                // Check if destination file exists
                $status->merge( $this->precheckDestExistence( $predicates ) );
+               $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
                if ( $status->isOK() ) {
                        // Update file existence predicates
                        $predicates['exists'][$this->params['dst']] = true;
@@ -541,24 +572,27 @@ class CreateFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
+               // Store the file at the destination
                if ( !$this->destSameAsSource ) {
-                       // Create the file at the destination
-                       return $this->backend->createInternal( $this->setFlags( $this->params ) );
+                       return $this->backend->storeInternal( $this->setFlags( $this->params ) );
                }
                return Status::newGood();
        }
 
        /**
-        * @return bool|String
+        * @return bool|string
         */
        protected function getSourceSha1Base36() {
-               return wfBaseConvert( sha1( $this->params['content'] ), 16, 36, 31 );
+               wfSuppressWarnings();
+               $hash = sha1_file( $this->params['src'] );
+               wfRestoreWarnings();
+               if ( $hash !== false ) {
+                       $hash = wfBaseConvert( $hash, 16, 36, 31 );
+               }
+               return $hash;
        }
 
-       /**
-        * @return array
-        */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
 }
@@ -573,7 +607,7 @@ class CopyFileOp extends FileOp {
         */
        protected function allowedParams() {
                return array( array( 'src', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+                       array( 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'disposition' ) );
        }
 
        /**
@@ -584,9 +618,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'] );
@@ -594,6 +636,7 @@ class CopyFileOp extends FileOp {
                }
                // Check if destination file exists
                $status->merge( $this->precheckDestExistence( $predicates ) );
+               $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
                if ( $status->isOK() ) {
                        // Update file existence predicates
                        $predicates['exists'][$this->params['dst']] = true;
@@ -619,14 +662,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 +684,7 @@ class MoveFileOp extends FileOp {
         */
        protected function allowedParams() {
                return array( array( 'src', 'dst' ),
-                       array( 'overwrite', 'overwriteSame', 'disposition' ) );
+                       array( 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'disposition' ) );
        }
 
        /**
@@ -652,9 +695,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'] );
@@ -662,6 +713,7 @@ class MoveFileOp extends FileOp {
                }
                // Check if destination file exists
                $status->merge( $this->precheckDestExistence( $predicates ) );
+               $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
                if ( $status->isOK() ) {
                        // Update file existence predicates
                        $predicates['exists'][$this->params['src']] = false;
@@ -693,14 +745,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 +769,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 +803,66 @@ class DeleteFileOp extends FileOp {
         * @return Status
         */
        protected function doAttempt() {
-               if ( $this->needsDelete ) {
-                       // Delete the source file
-                       return $this->backend->deleteInternal( $this->setFlags( $this->params ) );
+               // Delete the source file
+               return $this->backend->deleteInternal( $this->setFlags( $this->params ) );
+       }
+
+       /**
+        * @return array
+        */
+       public function storagePathsChanged() {
+               return array( $this->params['src'] );
+       }
+}
+
+/**
+ * Change metadata for a file at the given storage path in the backend.
+ * Parameters for this operation are outlined in FileBackend::doOperations().
+ */
+class DescribeFileOp extends FileOp {
+       /**
+        * @return array
+        */
+       protected function allowedParams() {
+               return array( array( 'src' ), array( 'disposition', 'headers' ) );
+       }
+
+       /**
+        * @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 ) ) {
+                       $status->fatal( 'backend-fail-notexists', $this->params['src'] );
+                       return $status;
+               // 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-describe', $this->params['src'] );
+                       return $status;
                }
-               return Status::newGood();
+               // Update file existence predicates
+               $predicates['exists'][$this->params['src']] =
+                       $this->fileExists( $this->params['src'], $predicates );
+               $predicates['sha1'][$this->params['src']] =
+                       $this->fileSha1( $this->params['src'], $predicates );
+               return $status; // safe to call attempt()
+       }
+
+       /**
+        * @return Status
+        */
+       protected function doAttempt() {
+               // Update the source file's metadata
+               return $this->backend->describeInternal( $this->setFlags( $this->params ) );
        }
 
        /**
         * @return array
         */
-       protected function doStoragePathsChanged() {
+       public function storagePathsChanged() {
                return array( $this->params['src'] );
        }
 }