Merge "Adding file stat caching to avoid RTTs to high latency backends."
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendGroup.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Class to handle file backend registration
10 *
11 * @ingroup FileBackend
12 * @since 1.19
13 */
14 class FileBackendGroup {
15 /**
16 * @var FileBackendGroup
17 */
18 protected static $instance = null;
19
20 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
21 protected $backends = array();
22
23 protected function __construct() {}
24 protected function __clone() {}
25
26 /**
27 * @return FileBackendGroup
28 */
29 public static function singleton() {
30 if ( self::$instance == null ) {
31 self::$instance = new self();
32 self::$instance->initFromGlobals();
33 }
34 return self::$instance;
35 }
36
37 /**
38 * Destroy the singleton instance
39 *
40 * @return void
41 */
42 public static function destroySingleton() {
43 self::$instance = null;
44 }
45
46 /**
47 * Register file backends from the global variables
48 *
49 * @return void
50 */
51 protected function initFromGlobals() {
52 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
53
54 // Register explicitly defined backends
55 $this->register( $wgFileBackends );
56
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)
64 }
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"
87 ),
88 'fileMode' => $fileMode,
89 );
90 }
91
92 // Register implicitly defined backends
93 $this->register( $autoBackends );
94 }
95
96 /**
97 * Register an array of file backend configurations
98 *
99 * @param $configs Array
100 * @return void
101 * @throws MWException
102 */
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." );
107 }
108 $name = $config['name'];
109 if ( !isset( $config['class'] ) ) {
110 throw new MWException( "Cannot register backend `{$name}` with no class." );
111 }
112 $class = $config['class'];
113
114 unset( $config['class'] ); // backend won't need this
115 $this->backends[$name] = array(
116 'class' => $class,
117 'config' => $config,
118 'instance' => null
119 );
120 }
121 }
122
123 /**
124 * Get the backend object with a given name
125 *
126 * @param $name string
127 * @return FileBackend
128 * @throws MWException
129 */
130 public function get( $name ) {
131 if ( !isset( $this->backends[$name] ) ) {
132 throw new MWException( "No backend defined with the name `$name`." );
133 }
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 );
139 }
140 return $this->backends[$name]['instance'];
141 }
142
143 /**
144 * Get an appropriate backend object from a storage path
145 *
146 * @param $storagePath string
147 * @return FileBackend|null Backend or null on failure
148 */
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 );
153 }
154 return null;
155 }
156 }