9 * Class to handle file backend registration
11 * @ingroup FileBackend
13 class FileBackendGroup
{
14 protected static $instance = null;
16 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
17 protected $backends = array();
19 protected function __construct() {}
20 protected function __clone() {}
23 * @return FileBackendGroup
25 public static function singleton() {
26 if ( self
::$instance == null ) {
27 self
::$instance = new self();
28 self
::$instance->initFromGlobals();
30 return self
::$instance;
34 * Destroy the singleton instance
36 public static function destroySingleton() {
37 self
::$instance = null;
41 * Register file backends from the global variables
43 protected function initFromGlobals() {
44 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
46 // Register explicitly defined backends
47 $this->register( $wgFileBackends );
49 $autoBackends = array();
50 // Automatically create b/c backends for file repos...
51 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
52 foreach ( $repos as $info ) {
53 $backendName = $info['backend'];
54 if ( is_object( $backendName ) ||
isset( $this->backends
[$backendName] ) ) {
55 continue; // already defined (or set to the object for some reason)
57 $repoName = $info['name'];
58 // Local vars that used to be FSRepo members...
59 $directory = $info['directory'];
60 $deletedDir = isset( $info['deletedDir'] )
62 : false; // deletion disabled
63 $thumbDir = isset( $info['thumbDir'] )
65 : "{$directory}/thumb";
66 $fileMode = isset( $info['fileMode'] )
69 // Get the FS backend configuration
70 $autoBackends[] = array(
71 'name' => $backendName,
72 'class' => 'FSFileBackend',
73 'lockManager' => 'fsLockManager',
74 'containerPaths' => array(
75 "{$repoName}-public" => "{$directory}",
76 "{$repoName}-thumb" => $thumbDir,
77 "{$repoName}-deleted" => $deletedDir,
78 "{$repoName}-temp" => "{$directory}/temp"
80 'fileMode' => $fileMode,
84 // Register implicitly defined backends
85 $this->register( $autoBackends );
89 * Register an array of file backend configurations
91 * @param $configs Array
94 protected function register( array $configs ) {
95 foreach ( $configs as $config ) {
96 if ( !isset( $config['name'] ) ) {
97 throw new MWException( "Cannot register a backend with no name." );
99 $name = $config['name'];
100 if ( !isset( $config['class'] ) ) {
101 throw new MWException( "Cannot register backend `{$name}` with no class." );
103 $class = $config['class'];
105 unset( $config['class'] ); // backend won't need this
106 $this->backends
[$name] = array(
115 * Get the backend object with a given name
117 * @param $name string
118 * @return FileBackendBase
119 * @throws MWException
121 public function get( $name ) {
122 if ( !isset( $this->backends
[$name] ) ) {
123 throw new MWException( "No backend defined with the name `$name`." );
125 // Lazy-load the actual backend instance
126 if ( !isset( $this->backends
[$name]['instance'] ) ) {
127 $class = $this->backends
[$name]['class'];
128 $config = $this->backends
[$name]['config'];
129 $this->backends
[$name]['instance'] = new $class( $config );
131 return $this->backends
[$name]['instance'];
135 * Get an appropriate backend object from a storage path
137 * @param $storagePath string
138 * @return FileBackendBase|null Backend or null on failure
140 public function backendFromPath( $storagePath ) {
141 list( $backend, $c, $p ) = FileBackend
::splitStoragePath( $storagePath );
142 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
143 return $this->get( $backend );