Merge "Update formatting of file backend classes"
[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 /**
44 * @return FileBackendGroup
45 */
46 public static function singleton() {
47 if ( self::$instance == null ) {
48 self::$instance = new self();
49 self::$instance->initFromGlobals();
50 }
51
52 return self::$instance;
53 }
54
55 /**
56 * Destroy the singleton instance
57 *
58 * @return void
59 */
60 public static function destroySingleton() {
61 self::$instance = null;
62 }
63
64 /**
65 * Register file backends from the global variables
66 *
67 * @return void
68 */
69 protected function initFromGlobals() {
70 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
71
72 // Register explicitly defined backends
73 $this->register( $wgFileBackends );
74
75 $autoBackends = array();
76 // Automatically create b/c backends for file repos...
77 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
78 foreach ( $repos as $info ) {
79 $backendName = $info['backend'];
80 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
81 continue; // already defined (or set to the object for some reason)
82 }
83 $repoName = $info['name'];
84 // Local vars that used to be FSRepo members...
85 $directory = $info['directory'];
86 $deletedDir = isset( $info['deletedDir'] )
87 ? $info['deletedDir']
88 : false; // deletion disabled
89 $thumbDir = isset( $info['thumbDir'] )
90 ? $info['thumbDir']
91 : "{$directory}/thumb";
92 $transcodedDir = isset( $info['transcodedDir'] )
93 ? $info['transcodedDir']
94 : "{$directory}/transcoded";
95 $fileMode = isset( $info['fileMode'] )
96 ? $info['fileMode']
97 : 0644;
98 // Get the FS backend configuration
99 $autoBackends[] = array(
100 'name' => $backendName,
101 'class' => 'FSFileBackend',
102 'lockManager' => 'fsLockManager',
103 'containerPaths' => array(
104 "{$repoName}-public" => "{$directory}",
105 "{$repoName}-thumb" => $thumbDir,
106 "{$repoName}-transcoded" => $transcodedDir,
107 "{$repoName}-deleted" => $deletedDir,
108 "{$repoName}-temp" => "{$directory}/temp"
109 ),
110 'fileMode' => $fileMode,
111 );
112 }
113
114 // Register implicitly defined backends
115 $this->register( $autoBackends );
116 }
117
118 /**
119 * Register an array of file backend configurations
120 *
121 * @param Array $configs
122 * @return void
123 * @throws MWException
124 */
125 protected function register( array $configs ) {
126 foreach ( $configs as $config ) {
127 if ( !isset( $config['name'] ) ) {
128 throw new MWException( "Cannot register a backend with no name." );
129 }
130 $name = $config['name'];
131 if ( isset( $this->backends[$name] ) ) {
132 throw new MWException( "Backend with name `{$name}` already registered." );
133 } elseif ( !isset( $config['class'] ) ) {
134 throw new MWException( "Cannot register backend `{$name}` with no class." );
135 }
136 $class = $config['class'];
137
138 unset( $config['class'] ); // backend won't need this
139 $this->backends[$name] = array(
140 'class' => $class,
141 'config' => $config,
142 'instance' => null
143 );
144 }
145 }
146
147 /**
148 * Get the backend object with a given name
149 *
150 * @param string $name
151 * @return FileBackend
152 * @throws MWException
153 */
154 public function get( $name ) {
155 if ( !isset( $this->backends[$name] ) ) {
156 throw new MWException( "No backend defined with the name `$name`." );
157 }
158 // Lazy-load the actual backend instance
159 if ( !isset( $this->backends[$name]['instance'] ) ) {
160 $class = $this->backends[$name]['class'];
161 $config = $this->backends[$name]['config'];
162 $this->backends[$name]['instance'] = new $class( $config );
163 }
164
165 return $this->backends[$name]['instance'];
166 }
167
168 /**
169 * Get the config array for a backend object with a given name
170 *
171 * @param string $name
172 * @return Array
173 * @throws MWException
174 */
175 public function config( $name ) {
176 if ( !isset( $this->backends[$name] ) ) {
177 throw new MWException( "No backend defined with the name `$name`." );
178 }
179 $class = $this->backends[$name]['class'];
180
181 return array( 'class' => $class ) + $this->backends[$name]['config'];
182 }
183
184 /**
185 * Get an appropriate backend object from a storage path
186 *
187 * @param string $storagePath
188 * @return FileBackend|null Backend or null on failure
189 */
190 public function backendFromPath( $storagePath ) {
191 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
192 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
193 return $this->get( $backend );
194 }
195
196 return null;
197 }
198 }