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() {}
24 protected function __clone() {}
27 * @return FileBackendGroup
29 public static function singleton() {
30 if ( self
::$instance == null ) {
31 self
::$instance = new self();
32 self
::$instance->initFromGlobals();
34 return self
::$instance;
38 * Destroy the singleton instance
42 public static function destroySingleton() {
43 self
::$instance = null;
47 * Register file backends from the global variables
51 protected function initFromGlobals() {
52 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
54 // Register explicitly defined backends
55 $this->register( $wgFileBackends );
57 $autoBackends = array();
58 // Automatically create b/c backends for file repos...
59 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
60 foreach ( $repos as $info ) {
61 $backendName = $info['backend'];
62 if ( is_object( $backendName ) ||
isset( $this->backends
[$backendName] ) ) {
63 continue; // already defined (or set to the object for some reason)
65 $repoName = $info['name'];
66 // Local vars that used to be FSRepo members...
67 $directory = $info['directory'];
68 $deletedDir = isset( $info['deletedDir'] )
70 : false; // deletion disabled
71 $thumbDir = isset( $info['thumbDir'] )
73 : "{$directory}/thumb";
74 $fileMode = isset( $info['fileMode'] )
77 // Get the FS backend configuration
78 $autoBackends[] = array(
79 'name' => $backendName,
80 'class' => 'FSFileBackend',
81 'lockManager' => 'fsLockManager',
82 'containerPaths' => array(
83 "{$repoName}-public" => "{$directory}",
84 "{$repoName}-thumb" => $thumbDir,
85 "{$repoName}-deleted" => $deletedDir,
86 "{$repoName}-temp" => "{$directory}/temp"
88 'fileMode' => $fileMode,
92 // Register implicitly defined backends
93 $this->register( $autoBackends );
97 * Register an array of file backend configurations
99 * @param $configs Array
101 * @throws MWException
103 protected function register( array $configs ) {
104 foreach ( $configs as $config ) {
105 if ( !isset( $config['name'] ) ) {
106 throw new MWException( "Cannot register a backend with no name." );
108 $name = $config['name'];
109 if ( !isset( $config['class'] ) ) {
110 throw new MWException( "Cannot register backend `{$name}` with no class." );
112 $class = $config['class'];
114 unset( $config['class'] ); // backend won't need this
115 $this->backends
[$name] = array(
124 * Get the backend object with a given name
126 * @param $name string
127 * @return FileBackend
128 * @throws MWException
130 public function get( $name ) {
131 if ( !isset( $this->backends
[$name] ) ) {
132 throw new MWException( "No backend defined with the name `$name`." );
134 // Lazy-load the actual backend instance
135 if ( !isset( $this->backends
[$name]['instance'] ) ) {
136 $class = $this->backends
[$name]['class'];
137 $config = $this->backends
[$name]['config'];
138 $this->backends
[$name]['instance'] = new $class( $config );
140 return $this->backends
[$name]['instance'];
144 * Get an appropriate backend object from a storage path
146 * @param $storagePath string
147 * @return FileBackend|null Backend or null on failure
149 public function backendFromPath( $storagePath ) {
150 list( $backend, $c, $p ) = FileBackend
::splitStoragePath( $storagePath );
151 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
152 return $this->get( $backend );