Merge "Allow maintenance/cdb.php to look up keys with spaces in them."
[lhc/web/wiklou.git] / includes / filebackend / FileBackendGroup.php
index 9bb1582..c043106 100644 (file)
@@ -64,7 +64,7 @@ class FileBackendGroup {
                global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
 
                // Register explicitly defined backends
-               $this->register( $wgFileBackends );
+               $this->register( $wgFileBackends, wfConfiguredReadOnlyReason() );
 
                $autoBackends = array();
                // Automatically create b/c backends for file repos...
@@ -106,16 +106,17 @@ class FileBackendGroup {
                }
 
                // Register implicitly defined backends
-               $this->register( $autoBackends );
+               $this->register( $autoBackends, wfConfiguredReadOnlyReason() );
        }
 
        /**
         * Register an array of file backend configurations
         *
         * @param array $configs
+        * @param string|null $readOnlyReason
         * @throws FileBackendException
         */
-       protected function register( array $configs ) {
+       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." );
@@ -128,6 +129,10 @@ class FileBackendGroup {
                        }
                        $class = $config['class'];
 
+                       $config['readOnly'] = !empty( $config['readOnly'] )
+                               ? $config['readOnly']
+                               : $readOnlyReason;
+
                        unset( $config['class'] ); // backend won't need this
                        $this->backends[$name] = array(
                                'class' => $class,
@@ -161,6 +166,7 @@ class FileBackendGroup {
                                ? FileJournal::factory( $config['fileJournal'], $name )
                                : FileJournal::factory( array( 'class' => 'NullFileJournal' ), $name );
                        $config['wanCache'] = ObjectCache::getMainWANInstance();
+                       $config['mimeCallback'] = array( $this, 'guessMimeInternal' );
 
                        $this->backends[$name]['instance'] = new $class( $config );
                }
@@ -198,4 +204,27 @@ class FileBackendGroup {
 
                return null;
        }
+
+       /**
+        * @param string $storagePath
+        * @param string|null $content
+        * @param string|null $fsPath
+        * @return string
+        * @since 1.27
+        */
+       public function guessMimeInternal( $storagePath, $content, $fsPath ) {
+               $magic = MimeMagic::singleton();
+               // Trust the extension of the storage path (caller must validate)
+               $ext = FileBackend::extensionFromPath( $storagePath );
+               $type = $magic->guessTypesForExtension( $ext );
+               // For files without a valid extension (or one at all), inspect the contents
+               if ( !$type && $fsPath ) {
+                       $type = $magic->guessMimeType( $fsPath, false );
+               } elseif ( !$type && strlen( $content ) ) {
+                       $tmpFile = TempFSFile::factory( 'mime_' );
+                       file_put_contents( $tmpFile->getPath(), $content );
+                       $type = $magic->guessMimeType( $tmpFile->getPath(), false );
+               }
+               return $type ?: 'unknown/unknown';
+       }
 }