MediaWiki  REL1_19
FileBackendGroup.php
Go to the documentation of this file.
00001 <?php
00014 class FileBackendGroup {
00018         protected static $instance = null;
00019 
00021         protected $backends = array();
00022 
00023         protected function __construct() {}
00024         protected function __clone() {}
00025 
00029         public static function singleton() {
00030                 if ( self::$instance == null ) {
00031                         self::$instance = new self();
00032                         self::$instance->initFromGlobals();
00033                 }
00034                 return self::$instance;
00035         }
00036 
00042         public static function destroySingleton() {
00043                 self::$instance = null;
00044         }
00045 
00051         protected function initFromGlobals() {
00052                 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
00053 
00054                 // Register explicitly defined backends
00055                 $this->register( $wgFileBackends );
00056 
00057                 $autoBackends = array();
00058                 // Automatically create b/c backends for file repos...
00059                 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
00060                 foreach ( $repos as $info ) {
00061                         $backendName = $info['backend'];
00062                         if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
00063                                 continue; // already defined (or set to the object for some reason)
00064                         }
00065                         $repoName = $info['name'];
00066                         // Local vars that used to be FSRepo members...
00067                         $directory = $info['directory'];
00068                         $deletedDir = isset( $info['deletedDir'] )
00069                                 ? $info['deletedDir']
00070                                 : false; // deletion disabled
00071                         $thumbDir = isset( $info['thumbDir'] )
00072                                 ? $info['thumbDir']
00073                                 : "{$directory}/thumb";
00074                         $fileMode = isset( $info['fileMode'] )
00075                                 ? $info['fileMode']
00076                                 : 0644;
00077                         // Get the FS backend configuration
00078                         $autoBackends[] = array(
00079                                 'name'           => $backendName,
00080                                 'class'          => 'FSFileBackend',
00081                                 'lockManager'    => 'fsLockManager',
00082                                 'containerPaths' => array(
00083                                         "{$repoName}-public"  => "{$directory}",
00084                                         "{$repoName}-thumb"   => $thumbDir,
00085                                         "{$repoName}-deleted" => $deletedDir,
00086                                         "{$repoName}-temp"    => "{$directory}/temp"
00087                                 ),
00088                                 'fileMode'       => $fileMode,
00089                         );
00090                 }
00091 
00092                 // Register implicitly defined backends
00093                 $this->register( $autoBackends );
00094         }
00095 
00103         protected function register( array $configs ) {
00104                 foreach ( $configs as $config ) {
00105                         if ( !isset( $config['name'] ) ) {
00106                                 throw new MWException( "Cannot register a backend with no name." );
00107                         }
00108                         $name = $config['name'];
00109                         if ( !isset( $config['class'] ) ) {
00110                                 throw new MWException( "Cannot register backend `{$name}` with no class." );
00111                         }
00112                         $class = $config['class'];
00113 
00114                         unset( $config['class'] ); // backend won't need this
00115                         $this->backends[$name] = array(
00116                                 'class'    => $class,
00117                                 'config'   => $config,
00118                                 'instance' => null
00119                         );
00120                 }
00121         }
00122 
00130         public function get( $name ) {
00131                 if ( !isset( $this->backends[$name] ) ) {
00132                         throw new MWException( "No backend defined with the name `$name`." );
00133                 }
00134                 // Lazy-load the actual backend instance
00135                 if ( !isset( $this->backends[$name]['instance'] ) ) {
00136                         $class = $this->backends[$name]['class'];
00137                         $config = $this->backends[$name]['config'];
00138                         $this->backends[$name]['instance'] = new $class( $config );
00139                 }
00140                 return $this->backends[$name]['instance'];
00141         }
00142 
00149         public function backendFromPath( $storagePath ) {
00150                 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
00151                 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
00152                         return $this->get( $backend );
00153                 }
00154                 return null;
00155         }
00156 }