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() {
44 * @return FileBackendGroup
46 public static function singleton() {
47 if ( self
::$instance == null ) {
48 self
::$instance = new self();
49 self
::$instance->initFromGlobals();
52 return self
::$instance;
56 * Destroy the singleton instance
60 public static function destroySingleton() {
61 self
::$instance = null;
65 * Register file backends from the global variables
69 protected function initFromGlobals() {
70 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
72 // Register explicitly defined backends
73 $this->register( $wgFileBackends );
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)
83 $repoName = $info['name'];
84 // Local vars that used to be FSRepo members...
85 $directory = $info['directory'];
86 $deletedDir = isset( $info['deletedDir'] )
88 : false; // deletion disabled
89 $thumbDir = isset( $info['thumbDir'] )
91 : "{$directory}/thumb";
92 $transcodedDir = isset( $info['transcodedDir'] )
93 ?
$info['transcodedDir']
94 : "{$directory}/transcoded";
95 $fileMode = isset( $info['fileMode'] )
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"
110 'fileMode' => $fileMode,
114 // Register implicitly defined backends
115 $this->register( $autoBackends );
119 * Register an array of file backend configurations
121 * @param Array $configs
123 * @throws MWException
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." );
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." );
136 $class = $config['class'];
138 unset( $config['class'] ); // backend won't need this
139 $this->backends
[$name] = array(
148 * Get the backend object with a given name
150 * @param string $name
151 * @return FileBackend
152 * @throws MWException
154 public function get( $name ) {
155 if ( !isset( $this->backends
[$name] ) ) {
156 throw new MWException( "No backend defined with the name `$name`." );
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 );
165 return $this->backends
[$name]['instance'];
169 * Get the config array for a backend object with a given name
171 * @param string $name
173 * @throws MWException
175 public function config( $name ) {
176 if ( !isset( $this->backends
[$name] ) ) {
177 throw new MWException( "No backend defined with the name `$name`." );
179 $class = $this->backends
[$name]['class'];
181 return array( 'class' => $class ) +
$this->backends
[$name]['config'];
185 * Get an appropriate backend object from a storage path
187 * @param string $storagePath
188 * @return FileBackend|null Backend or null on failure
190 public function backendFromPath( $storagePath ) {
191 list( $backend, , ) = FileBackend
::splitStoragePath( $storagePath );
192 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
193 return $this->get( $backend );