Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / filerepo / backend / FileBackendGroup.php
blob73815cfbddb766ddded7e7d0bada853fa1295578
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
8 /**
9 * Class to handle file backend registration
11 * @ingroup FileBackend
12 * @since 1.19
14 class FileBackendGroup {
15 /**
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() {}
26 /**
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;
37 /**
38 * Destroy the singleton instance
40 * @return void
42 public static function destroySingleton() {
43 self::$instance = null;
46 /**
47 * Register file backends from the global variables
49 * @return void
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'] )
69 ? $info['deletedDir']
70 : false; // deletion disabled
71 $thumbDir = isset( $info['thumbDir'] )
72 ? $info['thumbDir']
73 : "{$directory}/thumb";
74 $fileMode = isset( $info['fileMode'] )
75 ? $info['fileMode']
76 : 0644;
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 );
96 /**
97 * Register an array of file backend configurations
99 * @param $configs Array
100 * @return void
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(
116 'class' => $class,
117 'config' => $config,
118 'instance' => null
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 );
154 return null;