Merge "Fix uselang parameter in ApiWatch"
[lhc/web/wiklou.git] / includes / filebackend / FileOp.php
index ff1b604..0d64a44 100644 (file)
@@ -48,6 +48,7 @@ abstract class FileOp {
        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;
@@ -351,7 +352,7 @@ abstract class FileOp {
 
        /**
         * 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
@@ -365,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' ) ) {
@@ -460,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/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;
@@ -508,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 );
        }
 
+       /**
+        * @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/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;
@@ -571,23 +572,26 @@ 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
-        */
        public function storagePathsChanged() {
                return array( $this->params['dst'] );
        }
@@ -632,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;
@@ -708,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;
@@ -809,6 +815,58 @@ class DeleteFileOp extends FileOp {
        }
 }
 
+/**
+ * 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;
+               }
+               // 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
+        */
+       public function storagePathsChanged() {
+               return array( $this->params['src'] );
+       }
+}
+
 /**
  * Placeholder operation that has no params and does nothing
  */