Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / filebackend / FileBackendGroup.php
blob1b88db7ec0b008903ec2aeb71987eac7f4df977f
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
25 /**
26 * Class to handle file backend registration
28 * @ingroup FileBackend
29 * @since 1.19
31 class FileBackendGroup {
32 /** @var FileBackendGroup */
33 protected static $instance = null;
35 /** @var array (name => ('class' => string, 'config' => array, 'instance' => object)) */
36 protected $backends = array();
38 protected function __construct() {
41 /**
42 * @return FileBackendGroup
44 public static function singleton() {
45 if ( self::$instance == null ) {
46 self::$instance = new self();
47 self::$instance->initFromGlobals();
50 return self::$instance;
53 /**
54 * Destroy the singleton instance
56 public static function destroySingleton() {
57 self::$instance = null;
60 /**
61 * Register file backends from the global variables
63 protected function initFromGlobals() {
64 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
66 // Register explicitly defined backends
67 $this->register( $wgFileBackends );
69 $autoBackends = array();
70 // Automatically create b/c backends for file repos...
71 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
72 foreach ( $repos as $info ) {
73 $backendName = $info['backend'];
74 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
75 continue; // already defined (or set to the object for some reason)
77 $repoName = $info['name'];
78 // Local vars that used to be FSRepo members...
79 $directory = $info['directory'];
80 $deletedDir = isset( $info['deletedDir'] )
81 ? $info['deletedDir']
82 : false; // deletion disabled
83 $thumbDir = isset( $info['thumbDir'] )
84 ? $info['thumbDir']
85 : "{$directory}/thumb";
86 $transcodedDir = isset( $info['transcodedDir'] )
87 ? $info['transcodedDir']
88 : "{$directory}/transcoded";
89 $fileMode = isset( $info['fileMode'] )
90 ? $info['fileMode']
91 : 0644;
92 // Get the FS backend configuration
93 $autoBackends[] = array(
94 'name' => $backendName,
95 'class' => 'FSFileBackend',
96 'lockManager' => 'fsLockManager',
97 'containerPaths' => array(
98 "{$repoName}-public" => "{$directory}",
99 "{$repoName}-thumb" => $thumbDir,
100 "{$repoName}-transcoded" => $transcodedDir,
101 "{$repoName}-deleted" => $deletedDir,
102 "{$repoName}-temp" => "{$directory}/temp"
104 'fileMode' => $fileMode,
108 // Register implicitly defined backends
109 $this->register( $autoBackends );
113 * Register an array of file backend configurations
115 * @param array $configs
116 * @throws FileBackendException
118 protected function register( array $configs ) {
119 foreach ( $configs as $config ) {
120 if ( !isset( $config['name'] ) ) {
121 throw new FileBackendException( "Cannot register a backend with no name." );
123 $name = $config['name'];
124 if ( isset( $this->backends[$name] ) ) {
125 throw new FileBackendException( "Backend with name `{$name}` already registered." );
126 } elseif ( !isset( $config['class'] ) ) {
127 throw new FileBackendException( "Backend with name `{$name}` has no class." );
129 $class = $config['class'];
131 unset( $config['class'] ); // backend won't need this
132 $this->backends[$name] = array(
133 'class' => $class,
134 'config' => $config,
135 'instance' => null
141 * Get the backend object with a given name
143 * @param string $name
144 * @return FileBackend
145 * @throws FileBackendException
147 public function get( $name ) {
148 if ( !isset( $this->backends[$name] ) ) {
149 throw new FileBackendException( "No backend defined with the name `$name`." );
151 // Lazy-load the actual backend instance
152 if ( !isset( $this->backends[$name]['instance'] ) ) {
153 $class = $this->backends[$name]['class'];
154 $config = $this->backends[$name]['config'];
155 $config['wikiId'] = isset( $config['wikiId'] )
156 ? $config['wikiId']
157 : wfWikiID(); // e.g. "my_wiki-en_"
158 $config['lockManager'] =
159 LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
160 $config['fileJournal'] = isset( $config['fileJournal'] )
161 ? FileJournal::factory( $config['fileJournal'], $name )
162 : FileJournal::factory( array( 'class' => 'NullFileJournal' ), $name );
163 $this->backends[$name]['instance'] = new $class( $config );
166 return $this->backends[$name]['instance'];
170 * Get the config array for a backend object with a given name
172 * @param string $name
173 * @return array
174 * @throws FileBackendException
176 public function config( $name ) {
177 if ( !isset( $this->backends[$name] ) ) {
178 throw new FileBackendException( "No backend defined with the name `$name`." );
180 $class = $this->backends[$name]['class'];
182 return array( 'class' => $class ) + $this->backends[$name]['config'];
186 * Get an appropriate backend object from a storage path
188 * @param string $storagePath
189 * @return FileBackend|null Backend or null on failure
191 public function backendFromPath( $storagePath ) {
192 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
193 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
194 return $this->get( $backend );
197 return null;