Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[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 $transcodedDir = isset( $info['transcodedDir'] )
91 ? $info['transcodedDir']
92 : "{$directory}/transcoded";
93 $fileMode = isset( $info['fileMode'] )
94 ? $info['fileMode']
95 : 0644;
96 // Get the FS backend configuration
97 $autoBackends[] = array(
98 'name' => $backendName,
99 'class' => 'FSFileBackend',
100 'lockManager' => 'fsLockManager',
101 'containerPaths' => array(
102 "{$repoName}-public" => "{$directory}",
103 "{$repoName}-thumb" => $thumbDir,
104 "{$repoName}-transcoded" => $transcodedDir,
105 "{$repoName}-deleted" => $deletedDir,
106 "{$repoName}-temp" => "{$directory}/temp"
107 ),
108 'fileMode' => $fileMode,
109 );
110 }
111
112 // Register implicitly defined backends
113 $this->register( $autoBackends );
114 }
115
116 /**
117 * Register an array of file backend configurations
118 *
119 * @param $configs Array
120 * @return void
121 * @throws MWException
122 */
123 protected function register( array $configs ) {
124 foreach ( $configs as $config ) {
125 if ( !isset( $config['name'] ) ) {
126 throw new MWException( "Cannot register a backend with no name." );
127 }
128 $name = $config['name'];
129 if ( isset( $this->backends[$name] ) ) {
130 throw new MWException( "Backend with name `{$name}` already registered." );
131 } elseif ( !isset( $config['class'] ) ) {
132 throw new MWException( "Cannot register backend `{$name}` with no class." );
133 }
134 $class = $config['class'];
135
136 unset( $config['class'] ); // backend won't need this
137 $this->backends[$name] = array(
138 'class' => $class,
139 'config' => $config,
140 'instance' => null
141 );
142 }
143 }
144
145 /**
146 * Get the backend object with a given name
147 *
148 * @param $name string
149 * @return FileBackend
150 * @throws MWException
151 */
152 public function get( $name ) {
153 if ( !isset( $this->backends[$name] ) ) {
154 throw new MWException( "No backend defined with the name `$name`." );
155 }
156 // Lazy-load the actual backend instance
157 if ( !isset( $this->backends[$name]['instance'] ) ) {
158 $class = $this->backends[$name]['class'];
159 $config = $this->backends[$name]['config'];
160 $this->backends[$name]['instance'] = new $class( $config );
161 }
162 return $this->backends[$name]['instance'];
163 }
164
165 /**
166 * Get the config array for a backend object with a given name
167 *
168 * @param $name string
169 * @return Array
170 * @throws MWException
171 */
172 public function config( $name ) {
173 if ( !isset( $this->backends[$name] ) ) {
174 throw new MWException( "No backend defined with the name `$name`." );
175 }
176 $class = $this->backends[$name]['class'];
177 return array( 'class' => $class ) + $this->backends[$name]['config'];
178 }
179
180 /**
181 * Get an appropriate backend object from a storage path
182 *
183 * @param $storagePath string
184 * @return FileBackend|null Backend or null on failure
185 */
186 public function backendFromPath( $storagePath ) {
187 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
188 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
189 return $this->get( $backend );
190 }
191 return null;
192 }
193 }