Merge "Add HTTPFileStreamer class"
[lhc/web/wiklou.git] / includes / filebackend / FileBackendGroup.php
index c043106..c8a68d2 100644 (file)
@@ -21,6 +21,7 @@
  * @ingroup FileBackend
  * @author Aaron Schulz
  */
+use \MediaWiki\Logger\LoggerFactory;
 
 /**
  * Class to handle file backend registration
@@ -33,7 +34,7 @@ class FileBackendGroup {
        protected static $instance = null;
 
        /** @var array (name => ('class' => string, 'config' => array, 'instance' => object)) */
-       protected $backends = array();
+       protected $backends = [];
 
        protected function __construct() {
        }
@@ -66,9 +67,9 @@ class FileBackendGroup {
                // Register explicitly defined backends
                $this->register( $wgFileBackends, wfConfiguredReadOnlyReason() );
 
-               $autoBackends = array();
+               $autoBackends = [];
                // Automatically create b/c backends for file repos...
-               $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
+               $repos = array_merge( $wgForeignFileRepos, [ $wgLocalFileRepo ] );
                foreach ( $repos as $info ) {
                        $backendName = $info['backend'];
                        if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
@@ -90,19 +91,19 @@ class FileBackendGroup {
                                ? $info['fileMode']
                                : 0644;
                        // Get the FS backend configuration
-                       $autoBackends[] = array(
+                       $autoBackends[] = [
                                'name' => $backendName,
                                'class' => 'FSFileBackend',
                                'lockManager' => 'fsLockManager',
-                               'containerPaths' => array(
+                               'containerPaths' => [
                                        "{$repoName}-public" => "{$directory}",
                                        "{$repoName}-thumb" => $thumbDir,
                                        "{$repoName}-transcoded" => $transcodedDir,
                                        "{$repoName}-deleted" => $deletedDir,
                                        "{$repoName}-temp" => "{$directory}/temp"
-                               ),
+                               ],
                                'fileMode' => $fileMode,
-                       );
+                       ];
                }
 
                // Register implicitly defined backends
@@ -114,18 +115,18 @@ class FileBackendGroup {
         *
         * @param array $configs
         * @param string|null $readOnlyReason
-        * @throws FileBackendException
+        * @throws InvalidArgumentException
         */
        protected function register( array $configs, $readOnlyReason = null ) {
                foreach ( $configs as $config ) {
                        if ( !isset( $config['name'] ) ) {
-                               throw new FileBackendException( "Cannot register a backend with no name." );
+                               throw new InvalidArgumentException( "Cannot register a backend with no name." );
                        }
                        $name = $config['name'];
                        if ( isset( $this->backends[$name] ) ) {
-                               throw new FileBackendException( "Backend with name `{$name}` already registered." );
+                               throw new LogicException( "Backend with name `{$name}` already registered." );
                        } elseif ( !isset( $config['class'] ) ) {
-                               throw new FileBackendException( "Backend with name `{$name}` has no class." );
+                               throw new InvalidArgumentException( "Backend with name `{$name}` has no class." );
                        }
                        $class = $config['class'];
 
@@ -134,11 +135,11 @@ class FileBackendGroup {
                                : $readOnlyReason;
 
                        unset( $config['class'] ); // backend won't need this
-                       $this->backends[$name] = array(
+                       $this->backends[$name] = [
                                'class' => $class,
                                'config' => $config,
                                'instance' => null
-                       );
+                       ];
                }
        }
 
@@ -147,26 +148,41 @@ class FileBackendGroup {
         *
         * @param string $name
         * @return FileBackend
-        * @throws FileBackendException
+        * @throws InvalidArgumentException
         */
        public function get( $name ) {
                if ( !isset( $this->backends[$name] ) ) {
-                       throw new FileBackendException( "No backend defined with the name `$name`." );
+                       throw new InvalidArgumentException( "No backend defined with the name `$name`." );
                }
                // Lazy-load the actual backend instance
                if ( !isset( $this->backends[$name]['instance'] ) ) {
                        $class = $this->backends[$name]['class'];
                        $config = $this->backends[$name]['config'];
-                       $config['wikiId'] = isset( $config['wikiId'] )
-                               ? $config['wikiId']
-                               : wfWikiID(); // e.g. "my_wiki-en_"
+                       $config += [
+                               'wikiId' => wfWikiID(), // e.g. "my_wiki-en_"
+                               'mimeCallback' => [ $this, 'guessMimeInternal' ],
+                               'obResetFunc' => 'wfResetOutputBuffers',
+                               'streamMimeFunc' => [ 'StreamFile', 'contentTypeFromPath' ]
+                       ];
                        $config['lockManager'] =
                                LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
                        $config['fileJournal'] = isset( $config['fileJournal'] )
                                ? FileJournal::factory( $config['fileJournal'], $name )
-                               : FileJournal::factory( array( 'class' => 'NullFileJournal' ), $name );
+                               : FileJournal::factory( [ 'class' => 'NullFileJournal' ], $name );
                        $config['wanCache'] = ObjectCache::getMainWANInstance();
-                       $config['mimeCallback'] = array( $this, 'guessMimeInternal' );
+                       $config['statusWrapper'] = [ 'Status', 'wrap' ];
+                       $config['tmpDirectory'] = wfTempDir();
+                       $config['logger'] = LoggerFactory::getInstance( 'FileOperation' );
+                       $config['profiler'] = Profiler::instance();
+                       if ( $class === 'FileBackendMultiWrite' ) {
+                               foreach ( $config['backends'] as $index => $beConfig ) {
+                                       if ( isset( $beConfig['template'] ) ) {
+                                               // Config is just a modified version of a registered backend's.
+                                               // This should only be used when that config is used only by this backend.
+                                               $config['backends'][$index] += $this->config( $beConfig['template'] );
+                                       }
+                               }
+                       }
 
                        $this->backends[$name]['instance'] = new $class( $config );
                }
@@ -179,15 +195,15 @@ class FileBackendGroup {
         *
         * @param string $name
         * @return array
-        * @throws FileBackendException
+        * @throws InvalidArgumentException
         */
        public function config( $name ) {
                if ( !isset( $this->backends[$name] ) ) {
-                       throw new FileBackendException( "No backend defined with the name `$name`." );
+                       throw new InvalidArgumentException( "No backend defined with the name `$name`." );
                }
                $class = $this->backends[$name]['class'];
 
-               return array( 'class' => $class ) + $this->backends[$name]['config'];
+               return [ 'class' => $class ] + $this->backends[$name]['config'];
        }
 
        /**
@@ -221,7 +237,7 @@ class FileBackendGroup {
                if ( !$type && $fsPath ) {
                        $type = $magic->guessMimeType( $fsPath, false );
                } elseif ( !$type && strlen( $content ) ) {
-                       $tmpFile = TempFSFile::factory( 'mime_' );
+                       $tmpFile = TempFSFile::factory( 'mime_', '', wfTempDir() );
                        file_put_contents( $tmpFile->getPath(), $content );
                        $type = $magic->guessMimeType( $tmpFile->getPath(), false );
                }