3 * File backend registration handling.
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.
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.
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
21 * @ingroup FileBackend
22 * @author Aaron Schulz
26 * Class to handle file backend registration
28 * @ingroup FileBackend
31 class FileBackendGroup
{
33 * @var FileBackendGroup
35 protected static $instance = null;
37 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
38 protected $backends = array();
40 protected function __construct() {}
43 * @return FileBackendGroup
45 public static function singleton() {
46 if ( self
::$instance == null ) {
47 self
::$instance = new self();
48 self
::$instance->initFromGlobals();
50 return self
::$instance;
54 * Destroy the singleton instance
58 public static function destroySingleton() {
59 self
::$instance = null;
63 * Register file backends from the global variables
67 protected function initFromGlobals() {
68 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
70 // Register explicitly defined backends
71 $this->register( $wgFileBackends );
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)
81 $repoName = $info['name'];
82 // Local vars that used to be FSRepo members...
83 $directory = $info['directory'];
84 $deletedDir = isset( $info['deletedDir'] )
86 : false; // deletion disabled
87 $thumbDir = isset( $info['thumbDir'] )
89 : "{$directory}/thumb";
90 $transcodedDir = isset( $info['transcodedDir'] )
91 ?
$info['transcodedDir']
92 : "{$directory}/transcoded";
93 $fileMode = isset( $info['fileMode'] )
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"
108 'fileMode' => $fileMode,
112 // Register implicitly defined backends
113 $this->register( $autoBackends );
117 * Register an array of file backend configurations
119 * @param Array $configs
121 * @throws MWException
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." );
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." );
134 $class = $config['class'];
136 unset( $config['class'] ); // backend won't need this
137 $this->backends
[$name] = array(
146 * Get the backend object with a given name
148 * @param string $name
149 * @return FileBackend
150 * @throws MWException
152 public function get( $name ) {
153 if ( !isset( $this->backends
[$name] ) ) {
154 throw new MWException( "No backend defined with the name `$name`." );
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 );
162 return $this->backends
[$name]['instance'];
166 * Get the config array for a backend object with a given name
168 * @param string $name
170 * @throws MWException
172 public function config( $name ) {
173 if ( !isset( $this->backends
[$name] ) ) {
174 throw new MWException( "No backend defined with the name `$name`." );
176 $class = $this->backends
[$name]['class'];
177 return array( 'class' => $class ) +
$this->backends
[$name]['config'];
181 * Get an appropriate backend object from a storage path
183 * @param string $storagePath
184 * @return FileBackend|null Backend or null on failure
186 public function backendFromPath( $storagePath ) {
187 list( $backend, , ) = FileBackend
::splitStoragePath( $storagePath );
188 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
189 return $this->get( $backend );