Merge "Enable users to watch category membership changes"
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
index ef402ea..1852912 100644 (file)
@@ -29,6 +29,9 @@
  * @ingroup FileRepo
  */
 class LocalRepo extends FileRepo {
+       /** @var bool */
+       protected $hasSha1Storage = false;
+
        /** @var array */
        protected $fileFactory = array( 'LocalFile', 'newFromTitle' );
 
@@ -47,6 +50,20 @@ class LocalRepo extends FileRepo {
        /** @var array */
        protected $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
 
+       function __construct( array $info = null ) {
+               parent::__construct( $info );
+
+               $this->hasSha1Storage = isset( $info['storageLayout'] ) && $info['storageLayout'] === 'sha1';
+
+               if ( $this->hasSha1Storage() ) {
+                       $this->backend = new FileBackendDBRepoWrapper( array(
+                               'backend'         => $this->backend,
+                               'repoName'        => $this->name,
+                               'dbHandleFactory' => $this->getDBFactory()
+                       ) );
+               }
+       }
+
        /**
         * @throws MWException
         * @param stdClass $row
@@ -82,6 +99,11 @@ class LocalRepo extends FileRepo {
         * @return FileRepoStatus
         */
        function cleanupDeletedBatch( array $storageKeys ) {
+               if ( $this->hasSha1Storage() ) {
+                       wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths\n" );
+                       return Status::newGood();
+               }
+
                $backend = $this->backend; // convenience
                $root = $this->getZonePath( 'deleted' );
                $dbw = $this->getMasterDB();
@@ -285,7 +307,7 @@ class LocalRepo extends FileRepo {
                                $file = $that->newFileFromRow( $row );
                                // There must have been a search for this DB key, but this has to handle the
                                // cases were title capitalization is different on the client and repo wikis.
-                               $dbKeysLook = array( str_replace( ' ', '_', $file->getName() ) );
+                               $dbKeysLook = array( strtr( $file->getName(), ' ', '_' ) );
                                if ( !empty( $info['initialCapital'] ) ) {
                                        // Search keys for "hi.png" and "Hi.png" should use the "Hi.png file"
                                        $dbKeysLook[] = $wgContLang->lcfirst( $file->getName() );
@@ -469,6 +491,16 @@ class LocalRepo extends FileRepo {
                return wfGetDB( DB_MASTER );
        }
 
+       /**
+        * Get a callback to get a DB handle given an index (DB_SLAVE/DB_MASTER)
+        * @return Closure
+        */
+       protected function getDBFactory() {
+               return function( $index ) {
+                       return wfGetDB( $index );
+               };
+       }
+
        /**
         * Get a key on the primary cache for this repository.
         * Returns false if the repository's cache is not accessible at this site.
@@ -514,4 +546,56 @@ class LocalRepo extends FileRepo {
                        'favicon' => wfExpandUrl( $wgFavicon ),
                ) );
        }
+
+       public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function storeBatch( array $triplets, $flags = 0 ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function cleanupBatch( array $files, $flags = 0 ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function publish(
+               $srcPath,
+               $dstRel,
+               $archiveRel,
+               $flags = 0,
+               array $options = array()
+       ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function publishBatch( array $ntuples, $flags = 0 ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function delete( $srcRel, $archiveRel ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       public function deleteBatch( array $sourceDestPairs ) {
+               return $this->skipWriteOperationIfSha1( __FUNCTION__, func_get_args() );
+       }
+
+       /**
+        * Skips the write operation if storage is sha1-based, executes it normally otherwise
+        *
+        * @param string $function
+        * @param array $args
+        * @return FileRepoStatus
+        */
+       protected function skipWriteOperationIfSha1( $function, array $args ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               if ( $this->hasSha1Storage() ) {
+                       wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths\n" );
+                       return Status::newGood();
+               } else {
+                       return call_user_func_array('parent::' . $function, $args );
+               }
+       }
 }