Moved "create" function definitions before "store" for consistency.
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 8 Nov 2012 17:50:00 +0000 (09:50 -0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 15 Nov 2012 09:22:43 +0000 (09:22 +0000)
Change-Id: Ic44470d7a178cb8dcd8f6a9343f961c26fb92988

includes/filebackend/FSFileBackend.php
includes/filebackend/FileOp.php

index 3a03d4d..3c23e4c 100644 (file)
@@ -177,6 +177,59 @@ class FSFileBackend extends FileBackendStore {
                return $ok;
        }
 
+       /**
+        * @see FileBackendStore::doCreateInternal()
+        * @return Status
+        */
+       protected function doCreateInternal( array $params ) {
+               $status = Status::newGood();
+
+               $dest = $this->resolveToFSPath( $params['dst'] );
+               if ( $dest === null ) {
+                       $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
+                       return $status;
+               }
+
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $tempFile = TempFSFile::factory( 'create_', 'tmp' );
+                       if ( !$tempFile ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
+                       if ( $bytes === false ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $cmd = implode( ' ', array(
+                               wfIsWindows() ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
+                               wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
+                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
+                       $tempFile->bind( $status->value );
+               } else { // immediate write
+                       $bytes = file_put_contents( $dest, $params['content'] );
+                       if ( $bytes === false ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $this->chmod( $dest );
+               }
+
+               return $status;
+       }
+
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseCreate( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-create', $params['dst'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doStoreInternal()
         * @return Status
@@ -386,59 +439,6 @@ class FSFileBackend extends FileBackendStore {
                }
        }
 
-       /**
-        * @see FileBackendStore::doCreateInternal()
-        * @return Status
-        */
-       protected function doCreateInternal( array $params ) {
-               $status = Status::newGood();
-
-               $dest = $this->resolveToFSPath( $params['dst'] );
-               if ( $dest === null ) {
-                       $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
-                       return $status;
-               }
-
-               if ( !empty( $params['async'] ) ) { // deferred
-                       $tempFile = TempFSFile::factory( 'create_', 'tmp' );
-                       if ( !$tempFile ) {
-                               $status->fatal( 'backend-fail-create', $params['dst'] );
-                               return $status;
-                       }
-                       $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
-                       if ( $bytes === false ) {
-                               $status->fatal( 'backend-fail-create', $params['dst'] );
-                               return $status;
-                       }
-                       $cmd = implode( ' ', array(
-                               wfIsWindows() ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
-                               wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
-                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
-                       ) );
-                       $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
-                       $tempFile->bind( $status->value );
-               } else { // immediate write
-                       $bytes = file_put_contents( $dest, $params['content'] );
-                       if ( $bytes === false ) {
-                               $status->fatal( 'backend-fail-create', $params['dst'] );
-                               return $status;
-                       }
-                       $this->chmod( $dest );
-               }
-
-               return $status;
-       }
-
-       /**
-        * @see FSFileBackend::doExecuteOpHandlesInternal()
-        */
-       protected function _getResponseCreate( $errors, Status $status, array $params, $cmd ) {
-               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
-                       $status->fatal( 'backend-fail-create', $params['dst'] );
-                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
-               }
-       }
-
        /**
         * @see FileBackendStore::doPrepareInternal()
         * @return Status
index ff1b604..49111d9 100644 (file)
@@ -460,38 +460,27 @@ 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' ),
+               return array( array( 'content', 'dst' ),
                        array( 'overwrite', 'overwriteSame', 'disposition' ) );
        }
 
-       /**
-        * @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
@@ -508,53 +497,61 @@ 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' ),
+               return array( array( 'src', 'dst' ),
                        array( 'overwrite', 'overwriteSame', 'disposition' ) );
        }
 
+       /**
+        * @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
@@ -571,23 +568,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'] );
        }