MediaWiki  REL1_24
FileBackendGroup.php
Go to the documentation of this file.
00001 <?php
00031 class FileBackendGroup {
00033     protected static $instance = null;
00034 
00036     protected $backends = array();
00037 
00038     protected function __construct() {
00039     }
00040 
00044     public static function singleton() {
00045         if ( self::$instance == null ) {
00046             self::$instance = new self();
00047             self::$instance->initFromGlobals();
00048         }
00049 
00050         return self::$instance;
00051     }
00052 
00056     public static function destroySingleton() {
00057         self::$instance = null;
00058     }
00059 
00063     protected function initFromGlobals() {
00064         global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
00065 
00066         // Register explicitly defined backends
00067         $this->register( $wgFileBackends );
00068 
00069         $autoBackends = array();
00070         // Automatically create b/c backends for file repos...
00071         $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
00072         foreach ( $repos as $info ) {
00073             $backendName = $info['backend'];
00074             if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
00075                 continue; // already defined (or set to the object for some reason)
00076             }
00077             $repoName = $info['name'];
00078             // Local vars that used to be FSRepo members...
00079             $directory = $info['directory'];
00080             $deletedDir = isset( $info['deletedDir'] )
00081                 ? $info['deletedDir']
00082                 : false; // deletion disabled
00083             $thumbDir = isset( $info['thumbDir'] )
00084                 ? $info['thumbDir']
00085                 : "{$directory}/thumb";
00086             $transcodedDir = isset( $info['transcodedDir'] )
00087                 ? $info['transcodedDir']
00088                 : "{$directory}/transcoded";
00089             $fileMode = isset( $info['fileMode'] )
00090                 ? $info['fileMode']
00091                 : 0644;
00092             // Get the FS backend configuration
00093             $autoBackends[] = array(
00094                 'name' => $backendName,
00095                 'class' => 'FSFileBackend',
00096                 'lockManager' => 'fsLockManager',
00097                 'containerPaths' => array(
00098                     "{$repoName}-public" => "{$directory}",
00099                     "{$repoName}-thumb" => $thumbDir,
00100                     "{$repoName}-transcoded" => $transcodedDir,
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 
00118     protected function register( array $configs ) {
00119         foreach ( $configs as $config ) {
00120             if ( !isset( $config['name'] ) ) {
00121                 throw new FileBackendException( "Cannot register a backend with no name." );
00122             }
00123             $name = $config['name'];
00124             if ( isset( $this->backends[$name] ) ) {
00125                 throw new FileBackendException( "Backend with name `{$name}` already registered." );
00126             } elseif ( !isset( $config['class'] ) ) {
00127                 throw new FileBackendException( "Backend with name `{$name}` has no class." );
00128             }
00129             $class = $config['class'];
00130 
00131             unset( $config['class'] ); // backend won't need this
00132             $this->backends[$name] = array(
00133                 'class' => $class,
00134                 'config' => $config,
00135                 'instance' => null
00136             );
00137         }
00138     }
00139 
00147     public function get( $name ) {
00148         if ( !isset( $this->backends[$name] ) ) {
00149             throw new FileBackendException( "No backend defined with the name `$name`." );
00150         }
00151         // Lazy-load the actual backend instance
00152         if ( !isset( $this->backends[$name]['instance'] ) ) {
00153             $class = $this->backends[$name]['class'];
00154             $config = $this->backends[$name]['config'];
00155             $config['wikiId'] = isset( $config['wikiId'] )
00156                 ? $config['wikiId']
00157                 : wfWikiID(); // e.g. "my_wiki-en_"
00158             $config['lockManager'] =
00159                 LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
00160             $config['fileJournal'] = isset( $config['fileJournal'] )
00161                 ? FileJournal::factory( $config['fileJournal'], $name )
00162                 : FileJournal::factory( array( 'class' => 'NullFileJournal' ), $name );
00163             $this->backends[$name]['instance'] = new $class( $config );
00164         }
00165 
00166         return $this->backends[$name]['instance'];
00167     }
00168 
00176     public function config( $name ) {
00177         if ( !isset( $this->backends[$name] ) ) {
00178             throw new FileBackendException( "No backend defined with the name `$name`." );
00179         }
00180         $class = $this->backends[$name]['class'];
00181 
00182         return array( 'class' => $class ) + $this->backends[$name]['config'];
00183     }
00184 
00191     public function backendFromPath( $storagePath ) {
00192         list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
00193         if ( $backend !== null && isset( $this->backends[$backend] ) ) {
00194             return $this->get( $backend );
00195         }
00196 
00197         return null;
00198     }
00199 }