From: Aaron Schulz Date: Thu, 8 Nov 2012 17:50:00 +0000 (-0800) Subject: Moved "create" function definitions before "store" for consistency. X-Git-Tag: 1.31.0-rc.0~21634 X-Git-Url: http://git.cyclocoop.org/%22.%24image2.%22?a=commitdiff_plain;h=3e2bb97cc73ac80e1be15b8bc48590ee46c1898e;p=lhc%2Fweb%2Fwiklou.git Moved "create" function definitions before "store" for consistency. Change-Id: Ic44470d7a178cb8dcd8f6a9343f961c26fb92988 --- diff --git a/includes/filebackend/FSFileBackend.php b/includes/filebackend/FSFileBackend.php index 3a03d4d12b..3c23e4c999 100644 --- a/includes/filebackend/FSFileBackend.php +++ b/includes/filebackend/FSFileBackend.php @@ -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 diff --git a/includes/filebackend/FileOp.php b/includes/filebackend/FileOp.php index ff1b604337..49111d9b3b 100644 --- a/includes/filebackend/FileOp.php +++ b/includes/filebackend/FileOp.php @@ -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'] ); }