MediaWiki  REL1_20
FileBackendGroup.php
Go to the documentation of this file.
00001 <?php
00031 class FileBackendGroup {
00035         protected static $instance = null;
00036 
00038         protected $backends = array();
00039 
00040         protected function __construct() {}
00041 
00045         public static function singleton() {
00046                 if ( self::$instance == null ) {
00047                         self::$instance = new self();
00048                         self::$instance->initFromGlobals();
00049                 }
00050                 return self::$instance;
00051         }
00052 
00058         public static function destroySingleton() {
00059                 self::$instance = null;
00060         }
00061 
00067         protected function initFromGlobals() {
00068                 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
00069 
00070                 // Register explicitly defined backends
00071                 $this->register( $wgFileBackends );
00072 
00073                 $autoBackends = array();
00074                 // Automatically create b/c backends for file repos...
00075                 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
00076                 foreach ( $repos as $info ) {
00077                         $backendName = $info['backend'];
00078                         if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
00079                                 continue; // already defined (or set to the object for some reason)
00080                         }
00081                         $repoName = $info['name'];
00082                         // Local vars that used to be FSRepo members...
00083                         $directory = $info['directory'];
00084                         $deletedDir = isset( $info['deletedDir'] )
00085                                 ? $info['deletedDir']
00086                                 : false; // deletion disabled
00087                         $thumbDir = isset( $info['thumbDir'] )
00088                                 ? $info['thumbDir']
00089                                 : "{$directory}/thumb";
00090                         $fileMode = isset( $info['fileMode'] )
00091                                 ? $info['fileMode']
00092                                 : 0644;
00093                         // Get the FS backend configuration
00094                         $autoBackends[] = array(
00095                                 'name'           => $backendName,
00096                                 'class'          => 'FSFileBackend',
00097                                 'lockManager'    => 'fsLockManager',
00098                                 'containerPaths' => array(
00099                                         "{$repoName}-public"  => "{$directory}",
00100                                         "{$repoName}-thumb"   => $thumbDir,
00101                                         "{$repoName}-deleted" => $deletedDir,
00102                                         "{$repoName}-temp"    => "{$directory}/temp"
00103                                 ),
00104                                 'fileMode'       => $fileMode,
00105                         );
00106                 }
00107 
00108                 // Register implicitly defined backends
00109                 $this->register( $autoBackends );
00110         }
00111 
00119         protected function register( array $configs ) {
00120                 foreach ( $configs as $config ) {
00121                         if ( !isset( $config['name'] ) ) {
00122                                 throw new MWException( "Cannot register a backend with no name." );
00123                         }
00124                         $name = $config['name'];
00125                         if ( !isset( $config['class'] ) ) {
00126                                 throw new MWException( "Cannot register backend `{$name}` with no class." );
00127                         }
00128                         $class = $config['class'];
00129 
00130                         unset( $config['class'] ); // backend won't need this
00131                         $this->backends[$name] = array(
00132                                 'class'    => $class,
00133                                 'config'   => $config,
00134                                 'instance' => null
00135                         );
00136                 }
00137         }
00138 
00146         public function get( $name ) {
00147                 if ( !isset( $this->backends[$name] ) ) {
00148                         throw new MWException( "No backend defined with the name `$name`." );
00149                 }
00150                 // Lazy-load the actual backend instance
00151                 if ( !isset( $this->backends[$name]['instance'] ) ) {
00152                         $class = $this->backends[$name]['class'];
00153                         $config = $this->backends[$name]['config'];
00154                         $this->backends[$name]['instance'] = new $class( $config );
00155                 }
00156                 return $this->backends[$name]['instance'];
00157         }
00158 
00166         public function config( $name ) {
00167                 if ( !isset( $this->backends[$name] ) ) {
00168                         throw new MWException( "No backend defined with the name `$name`." );
00169                 }
00170                 $class = $this->backends[$name]['class'];
00171                 return array( 'class' => $class ) + $this->backends[$name]['config'];
00172         }
00173 
00180         public function backendFromPath( $storagePath ) {
00181                 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
00182                 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
00183                         return $this->get( $backend );
00184                 }
00185                 return null;
00186         }
00187 }