In FileBackend:
authorAaron Schulz <aaron@users.mediawiki.org>
Mon, 6 Feb 2012 05:25:26 +0000 (05:25 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Mon, 6 Feb 2012 05:25:26 +0000 (05:25 +0000)
* Added simple getReadOnly()/getReadOnlyReason() functions.
* Allow directly passing a LockManager object into __construct(), useful for testing.
* Fixed bug in FSFileBackend were creating empty files would result in a failing status.
* Added more file stat unit tests.

includes/filerepo/backend/FSFileBackend.php
includes/filerepo/backend/FileBackend.php

index 3eabd29..4556eaa 100644 (file)
@@ -316,9 +316,9 @@ class FSFileBackend extends FileBackendStore {
                }
 
                wfSuppressWarnings();
-               $ok = file_put_contents( $dest, $params['content'] );
+               $bytes = file_put_contents( $dest, $params['content'] );
                wfRestoreWarnings();
-               if ( !$ok ) {
+               if ( $bytes === false ) {
                        $status->fatal( 'backend-fail-create', $params['dst'] );
                        return $status;
                }
@@ -357,9 +357,9 @@ class FSFileBackend extends FileBackendStore {
                // Seed new directories with a blank index.html, to prevent crawling...
                if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
                        wfSuppressWarnings();
-                       $ok = file_put_contents( "{$dir}/index.html", '' );
+                       $bytes = file_put_contents( "{$dir}/index.html", '' );
                        wfRestoreWarnings();
-                       if ( !$ok ) {
+                       if ( !$bytes ) {
                                $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
                                return $status;
                        }
@@ -368,9 +368,9 @@ class FSFileBackend extends FileBackendStore {
                if ( !empty( $params['noAccess'] ) ) {
                        if ( !file_exists( "{$contRoot}/.htaccess" ) ) {
                                wfSuppressWarnings();
-                               $ok = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
+                               $bytes = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
                                wfRestoreWarnings();
-                               if ( !$ok ) {
+                               if ( !$bytes ) {
                                        $storeDir = "mwstore://{$this->name}/{$shortCont}";
                                        $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
                                        return $status;
index 27e3b84..b4b9ea2 100644 (file)
@@ -56,7 +56,9 @@ abstract class FileBackend {
                $this->wikiId = isset( $config['wikiId'] )
                        ? $config['wikiId']
                        : wfWikiID(); // e.g. "my_wiki-en_"
-               $this->lockManager = LockManagerGroup::singleton()->get( $config['lockManager'] );
+               $this->lockManager = ( $config['lockManager'] instanceof LockManager )
+                       ? $config['lockManager']
+                       : LockManagerGroup::singleton()->get( $config['lockManager'] );
                $this->readOnly = isset( $config['readOnly'] )
                        ? (string)$config['readOnly']
                        : '';
@@ -73,6 +75,24 @@ abstract class FileBackend {
                return $this->name;
        }
 
+       /**
+        * Check if this backend is read-only
+        * 
+        * @return bool
+        */
+       final public function isReadOnly() {
+               return ( $this->readOnly != '' );
+       }
+
+       /**
+        * Get an explanatory message if this backend is read-only
+        * 
+        * @return string|false Returns falls if the backend is not read-only
+        */
+       final public function getReadOnlyReason() {
+               return ( $this->readOnly != '' ) ? $this->readOnly : false;
+       }
+
        /**
         * This is the main entry point into the backend for write operations.
         * Callers supply an ordered list of operations to perform as a transaction.
@@ -160,7 +180,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function doOperations( array $ops, array $opts = array() ) {
-               if ( $this->readOnly != '' ) {
+               if ( $this->isReadOnly() ) {
                        return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
                if ( empty( $opts['force'] ) ) { // sanity
@@ -291,7 +311,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function prepare( array $params ) {
-               if ( $this->readOnly != '' ) {
+               if ( $this->isReadOnly() ) {
                        return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
                return $this->doPrepare( $params );
@@ -318,7 +338,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function secure( array $params ) {
-               if ( $this->readOnly != '' ) {
+               if ( $this->isReadOnly() ) {
                        return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
                $status = $this->doPrepare( $params ); // dir must exist to restrict it
@@ -345,7 +365,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function clean( array $params ) {
-               if ( $this->readOnly != '' ) {
+               if ( $this->isReadOnly() ) {
                        return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
                return $this->doClean( $params );