fix some spacing
[lhc/web/wiklou.git] / includes / filebackend / FileOp.php
index ff1b604..28f5b73 100644 (file)
@@ -42,12 +42,12 @@ abstract class FileOp {
        protected $state = self::STATE_NEW; // integer
        protected $failed = false; // boolean
        protected $async = false; // boolean
-       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;
@@ -123,16 +123,6 @@ abstract class FileOp {
                $this->batchId = $batchId;
        }
 
-       /**
-        * Whether to allow stale data for file reads and stat checks
-        *
-        * @param $allowStale bool
-        * @return void
-        */
-       final public function allowStaleReads( $allowStale ) {
-               $this->useLatest = !$allowStale;
-       }
-
        /**
         * Get the value of the parameter with the given name
         *
@@ -177,7 +167,7 @@ abstract class FileOp {
         * @return Array
         */
        final public function applyDependencies( array $deps ) {
-               $deps['read']  += array_fill_keys( $this->storagePathsRead(), 1 );
+               $deps['read'] += array_fill_keys( $this->storagePathsRead(), 1 );
                $deps['write'] += array_fill_keys( $this->storagePathsChanged(), 1 );
                return $deps;
        }
@@ -351,7 +341,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 +355,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' ) ) {
@@ -409,7 +400,7 @@ abstract class FileOp {
                if ( isset( $predicates['exists'][$source] ) ) {
                        return $predicates['exists'][$source]; // previous op assures this
                } else {
-                       $params = array( 'src' => $source, 'latest' => $this->useLatest );
+                       $params = array( 'src' => $source, 'latest' => true );
                        return $this->backend->fileExists( $params );
                }
        }
@@ -427,7 +418,7 @@ abstract class FileOp {
                } elseif ( isset( $predicates['exists'][$source] ) && !$predicates['exists'][$source] ) {
                        return false; // previous op assures this
                } else {
-                       $params = array( 'src' => $source, 'latest' => $this->useLatest );
+                       $params = array( 'src' => $source, 'latest' => true );
                        return $this->backend->getFileSha1Base36( $params );
                }
        }
@@ -460,42 +451,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 +489,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 +561,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 +625,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 +702,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 +804,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
  */