MediaWiki  REL1_21
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                         $transcodedDir = isset( $info['transcodedDir'] )
00091                                 ? $info['transcodedDir']
00092                                 : "{$directory}/transcoded";
00093                         $fileMode = isset( $info['fileMode'] )
00094                                 ? $info['fileMode']
00095                                 : 0644;
00096                         // Get the FS backend configuration
00097                         $autoBackends[] = array(
00098                                 'name'           => $backendName,
00099                                 'class'          => 'FSFileBackend',
00100                                 'lockManager'    => 'fsLockManager',
00101                                 'containerPaths' => array(
00102                                         "{$repoName}-public"  => "{$directory}",
00103                                         "{$repoName}-thumb"   => $thumbDir,
00104                                         "{$repoName}-transcoded"   => $transcodedDir,
00105                                         "{$repoName}-deleted" => $deletedDir,
00106                                         "{$repoName}-temp"    => "{$directory}/temp"
00107                                 ),
00108                                 'fileMode'       => $fileMode,
00109                         );
00110                 }
00111 
00112                 // Register implicitly defined backends
00113                 $this->register( $autoBackends );
00114         }
00115 
00123         protected function register( array $configs ) {
00124                 foreach ( $configs as $config ) {
00125                         if ( !isset( $config['name'] ) ) {
00126                                 throw new MWException( "Cannot register a backend with no name." );
00127                         }
00128                         $name = $config['name'];
00129                         if ( isset( $this->backends[$name] ) ) {
00130                                 throw new MWException( "Backend with name `{$name}` already registered." );
00131                         } elseif ( !isset( $config['class'] ) ) {
00132                                 throw new MWException( "Cannot register backend `{$name}` with no class." );
00133                         }
00134                         $class = $config['class'];
00135 
00136                         unset( $config['class'] ); // backend won't need this
00137                         $this->backends[$name] = array(
00138                                 'class'    => $class,
00139                                 'config'   => $config,
00140                                 'instance' => null
00141                         );
00142                 }
00143         }
00144 
00152         public function get( $name ) {
00153                 if ( !isset( $this->backends[$name] ) ) {
00154                         throw new MWException( "No backend defined with the name `$name`." );
00155                 }
00156                 // Lazy-load the actual backend instance
00157                 if ( !isset( $this->backends[$name]['instance'] ) ) {
00158                         $class = $this->backends[$name]['class'];
00159                         $config = $this->backends[$name]['config'];
00160                         $this->backends[$name]['instance'] = new $class( $config );
00161                 }
00162                 return $this->backends[$name]['instance'];
00163         }
00164 
00172         public function config( $name ) {
00173                 if ( !isset( $this->backends[$name] ) ) {
00174                         throw new MWException( "No backend defined with the name `$name`." );
00175                 }
00176                 $class = $this->backends[$name]['class'];
00177                 return array( 'class' => $class ) + $this->backends[$name]['config'];
00178         }
00179 
00186         public function backendFromPath( $storagePath ) {
00187                 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
00188                 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
00189                         return $this->get( $backend );
00190                 }
00191                 return null;
00192         }
00193 }