9 * Class to handle file backend registration
11 * @ingroup FileBackend
14 class FileBackendGroup
{
16 * @var FileBackendGroup
18 protected static $instance = null;
20 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
21 protected $backends = array();
23 protected function __construct() {}
26 * @return FileBackendGroup
28 public static function singleton() {
29 if ( self
::$instance == null ) {
30 self
::$instance = new self();
31 self
::$instance->initFromGlobals();
33 return self
::$instance;
37 * Destroy the singleton instance
41 public static function destroySingleton() {
42 self
::$instance = null;
46 * Register file backends from the global variables
50 protected function initFromGlobals() {
51 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
53 // Register explicitly defined backends
54 $this->register( $wgFileBackends );
56 $autoBackends = array();
57 // Automatically create b/c backends for file repos...
58 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
59 foreach ( $repos as $info ) {
60 $backendName = $info['backend'];
61 if ( is_object( $backendName ) ||
isset( $this->backends
[$backendName] ) ) {
62 continue; // already defined (or set to the object for some reason)
64 $repoName = $info['name'];
65 // Local vars that used to be FSRepo members...
66 $directory = $info['directory'];
67 $deletedDir = isset( $info['deletedDir'] )
69 : false; // deletion disabled
70 $thumbDir = isset( $info['thumbDir'] )
72 : "{$directory}/thumb";
73 $fileMode = isset( $info['fileMode'] )
76 // Get the FS backend configuration
77 $autoBackends[] = array(
78 'name' => $backendName,
79 'class' => 'FSFileBackend',
80 'lockManager' => 'fsLockManager',
81 'containerPaths' => array(
82 "{$repoName}-public" => "{$directory}",
83 "{$repoName}-thumb" => $thumbDir,
84 "{$repoName}-deleted" => $deletedDir,
85 "{$repoName}-temp" => "{$directory}/temp"
87 'fileMode' => $fileMode,
91 // Register implicitly defined backends
92 $this->register( $autoBackends );
96 * Register an array of file backend configurations
98 * @param $configs Array
100 * @throws MWException
102 protected function register( array $configs ) {
103 foreach ( $configs as $config ) {
104 if ( !isset( $config['name'] ) ) {
105 throw new MWException( "Cannot register a backend with no name." );
107 $name = $config['name'];
108 if ( !isset( $config['class'] ) ) {
109 throw new MWException( "Cannot register backend `{$name}` with no class." );
111 $class = $config['class'];
113 unset( $config['class'] ); // backend won't need this
114 $this->backends
[$name] = array(
123 * Get the backend object with a given name
125 * @param $name string
126 * @return FileBackend
127 * @throws MWException
129 public function get( $name ) {
130 if ( !isset( $this->backends
[$name] ) ) {
131 throw new MWException( "No backend defined with the name `$name`." );
133 // Lazy-load the actual backend instance
134 if ( !isset( $this->backends
[$name]['instance'] ) ) {
135 $class = $this->backends
[$name]['class'];
136 $config = $this->backends
[$name]['config'];
137 $this->backends
[$name]['instance'] = new $class( $config );
139 return $this->backends
[$name]['instance'];
143 * Get an appropriate backend object from a storage path
145 * @param $storagePath string
146 * @return FileBackend|null Backend or null on failure
148 public function backendFromPath( $storagePath ) {
149 list( $backend, $c, $p ) = FileBackend
::splitStoragePath( $storagePath );
150 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
151 return $this->get( $backend );