Merge "[FileBackend] Added preloadCache() so callers can trigger cache getMulti()."
[lhc/web/wiklou.git] / includes / filebackend / FileBackendGroup.php
1 <?php
2 /**
3 * File backend registration handling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Class to handle file backend registration
27 *
28 * @ingroup FileBackend
29 * @since 1.19
30 */
31 class FileBackendGroup {
32 /**
33 * @var FileBackendGroup
34 */
35 protected static $instance = null;
36
37 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
38 protected $backends = array();
39
40 protected function __construct() {}
41
42 /**
43 * @return FileBackendGroup
44 */
45 public static function singleton() {
46 if ( self::$instance == null ) {
47 self::$instance = new self();
48 self::$instance->initFromGlobals();
49 }
50 return self::$instance;
51 }
52
53 /**
54 * Destroy the singleton instance
55 *
56 * @return void
57 */
58 public static function destroySingleton() {
59 self::$instance = null;
60 }
61
62 /**
63 * Register file backends from the global variables
64 *
65 * @return void
66 */
67 protected function initFromGlobals() {
68 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
69
70 // Register explicitly defined backends
71 $this->register( $wgFileBackends );
72
73 $autoBackends = array();
74 // Automatically create b/c backends for file repos...
75 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
76 foreach ( $repos as $info ) {
77 $backendName = $info['backend'];
78 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
79 continue; // already defined (or set to the object for some reason)
80 }
81 $repoName = $info['name'];
82 // Local vars that used to be FSRepo members...
83 $directory = $info['directory'];
84 $deletedDir = isset( $info['deletedDir'] )
85 ? $info['deletedDir']
86 : false; // deletion disabled
87 $thumbDir = isset( $info['thumbDir'] )
88 ? $info['thumbDir']
89 : "{$directory}/thumb";
90 $fileMode = isset( $info['fileMode'] )
91 ? $info['fileMode']
92 : 0644;
93 // Get the FS backend configuration
94 $autoBackends[] = array(
95 'name' => $backendName,
96 'class' => 'FSFileBackend',
97 'lockManager' => 'fsLockManager',
98 'containerPaths' => array(
99 "{$repoName}-public" => "{$directory}",
100 "{$repoName}-thumb" => $thumbDir,
101 "{$repoName}-deleted" => $deletedDir,
102 "{$repoName}-temp" => "{$directory}/temp"
103 ),
104 'fileMode' => $fileMode,
105 );
106 }
107
108 // Register implicitly defined backends
109 $this->register( $autoBackends );
110 }
111
112 /**
113 * Register an array of file backend configurations
114 *
115 * @param $configs Array
116 * @return void
117 * @throws MWException
118 */
119 protected function register( array $configs ) {
120 foreach ( $configs as $config ) {
121 if ( !isset( $config['name'] ) ) {
122 throw new MWException( "Cannot register a backend with no name." );
123 }
124 $name = $config['name'];
125 if ( !isset( $config['class'] ) ) {
126 throw new MWException( "Cannot register backend `{$name}` with no class." );
127 }
128 $class = $config['class'];
129
130 unset( $config['class'] ); // backend won't need this
131 $this->backends[$name] = array(
132 'class' => $class,
133 'config' => $config,
134 'instance' => null
135 );
136 }
137 }
138
139 /**
140 * Get the backend object with a given name
141 *
142 * @param $name string
143 * @return FileBackend
144 * @throws MWException
145 */
146 public function get( $name ) {
147 if ( !isset( $this->backends[$name] ) ) {
148 throw new MWException( "No backend defined with the name `$name`." );
149 }
150 // Lazy-load the actual backend instance
151 if ( !isset( $this->backends[$name]['instance'] ) ) {
152 $class = $this->backends[$name]['class'];
153 $config = $this->backends[$name]['config'];
154 $this->backends[$name]['instance'] = new $class( $config );
155 }
156 return $this->backends[$name]['instance'];
157 }
158
159 /**
160 * Get the config array for a backend object with a given name
161 *
162 * @param $name string
163 * @return Array
164 * @throws MWException
165 */
166 public function config( $name ) {
167 if ( !isset( $this->backends[$name] ) ) {
168 throw new MWException( "No backend defined with the name `$name`." );
169 }
170 $class = $this->backends[$name]['class'];
171 return array( 'class' => $class ) + $this->backends[$name]['config'];
172 }
173
174 /**
175 * Get an appropriate backend object from a storage path
176 *
177 * @param $storagePath string
178 * @return FileBackend|null Backend or null on failure
179 */
180 public function backendFromPath( $storagePath ) {
181 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
182 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
183 return $this->get( $backend );
184 }
185 return null;
186 }
187 }