Improve the shell cgroup feature
[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( $this->backends[$name] ) ) {
126 throw new MWException( "Backend with name `{$name}` already registered." );
127 } elseif ( !isset( $config['class'] ) ) {
128 throw new MWException( "Cannot register backend `{$name}` with no class." );
129 }
130 $class = $config['class'];
131
132 unset( $config['class'] ); // backend won't need this
133 $this->backends[$name] = array(
134 'class' => $class,
135 'config' => $config,
136 'instance' => null
137 );
138 }
139 }
140
141 /**
142 * Get the backend object with a given name
143 *
144 * @param $name string
145 * @return FileBackend
146 * @throws MWException
147 */
148 public function get( $name ) {
149 if ( !isset( $this->backends[$name] ) ) {
150 throw new MWException( "No backend defined with the name `$name`." );
151 }
152 // Lazy-load the actual backend instance
153 if ( !isset( $this->backends[$name]['instance'] ) ) {
154 $class = $this->backends[$name]['class'];
155 $config = $this->backends[$name]['config'];
156 $this->backends[$name]['instance'] = new $class( $config );
157 }
158 return $this->backends[$name]['instance'];
159 }
160
161 /**
162 * Get the config array for a backend object with a given name
163 *
164 * @param $name string
165 * @return Array
166 * @throws MWException
167 */
168 public function config( $name ) {
169 if ( !isset( $this->backends[$name] ) ) {
170 throw new MWException( "No backend defined with the name `$name`." );
171 }
172 $class = $this->backends[$name]['class'];
173 return array( 'class' => $class ) + $this->backends[$name]['config'];
174 }
175
176 /**
177 * Get an appropriate backend object from a storage path
178 *
179 * @param $storagePath string
180 * @return FileBackend|null Backend or null on failure
181 */
182 public function backendFromPath( $storagePath ) {
183 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
184 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
185 return $this->get( $backend );
186 }
187 return null;
188 }
189 }