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
24 use \MediaWiki\Logger\LoggerFactory
;
27 * Class to handle file backend registration
29 * @ingroup FileBackend
32 class FileBackendGroup
{
33 /** @var FileBackendGroup */
34 protected static $instance = null;
36 /** @var array (name => ('class' => string, 'config' => array, 'instance' => object)) */
37 protected $backends = [];
39 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();
51 return self
::$instance;
55 * Destroy the singleton instance
57 public static function destroySingleton() {
58 self
::$instance = null;
62 * Register file backends from the global variables
64 protected function initFromGlobals() {
65 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
67 // Register explicitly defined backends
68 $this->register( $wgFileBackends, wfConfiguredReadOnlyReason() );
71 // Automatically create b/c backends for file repos...
72 $repos = array_merge( $wgForeignFileRepos, [ $wgLocalFileRepo ] );
73 foreach ( $repos as $info ) {
74 $backendName = $info['backend'];
75 if ( is_object( $backendName ) ||
isset( $this->backends
[$backendName] ) ) {
76 continue; // already defined (or set to the object for some reason)
78 $repoName = $info['name'];
79 // Local vars that used to be FSRepo members...
80 $directory = $info['directory'];
81 $deletedDir = isset( $info['deletedDir'] )
83 : false; // deletion disabled
84 $thumbDir = isset( $info['thumbDir'] )
86 : "{$directory}/thumb";
87 $transcodedDir = isset( $info['transcodedDir'] )
88 ?
$info['transcodedDir']
89 : "{$directory}/transcoded";
90 $fileMode = isset( $info['fileMode'] )
93 // Get the FS backend configuration
95 'name' => $backendName,
96 'class' => 'FSFileBackend',
97 'lockManager' => 'fsLockManager',
99 "{$repoName}-public" => "{$directory}",
100 "{$repoName}-thumb" => $thumbDir,
101 "{$repoName}-transcoded" => $transcodedDir,
102 "{$repoName}-deleted" => $deletedDir,
103 "{$repoName}-temp" => "{$directory}/temp"
105 'fileMode' => $fileMode,
109 // Register implicitly defined backends
110 $this->register( $autoBackends, wfConfiguredReadOnlyReason() );
114 * Register an array of file backend configurations
116 * @param array $configs
117 * @param string|null $readOnlyReason
118 * @throws InvalidArgumentException
120 protected function register( array $configs, $readOnlyReason = null ) {
121 foreach ( $configs as $config ) {
122 if ( !isset( $config['name'] ) ) {
123 throw new InvalidArgumentException( "Cannot register a backend with no name." );
125 $name = $config['name'];
126 if ( isset( $this->backends
[$name] ) ) {
127 throw new LogicException( "Backend with name `{$name}` already registered." );
128 } elseif ( !isset( $config['class'] ) ) {
129 throw new InvalidArgumentException( "Backend with name `{$name}` has no class." );
131 $class = $config['class'];
133 $config['readOnly'] = !empty( $config['readOnly'] )
134 ?
$config['readOnly']
137 unset( $config['class'] ); // backend won't need this
138 $this->backends
[$name] = [
147 * Get the backend object with a given name
149 * @param string $name
150 * @return FileBackend
151 * @throws InvalidArgumentException
153 public function get( $name ) {
154 if ( !isset( $this->backends
[$name] ) ) {
155 throw new InvalidArgumentException( "No backend defined with the name `$name`." );
157 // Lazy-load the actual backend instance
158 if ( !isset( $this->backends
[$name]['instance'] ) ) {
159 $class = $this->backends
[$name]['class'];
160 $config = $this->backends
[$name]['config'];
162 'wikiId' => wfWikiID(), // e.g. "my_wiki-en_"
163 'mimeCallback' => [ $this, 'guessMimeInternal' ],
164 'obResetFunc' => 'wfResetOutputBuffers',
165 'streamMimeFunc' => [ 'StreamFile', 'contentTypeFromPath' ]
167 $config['lockManager'] =
168 LockManagerGroup
::singleton( $config['wikiId'] )->get( $config['lockManager'] );
169 $config['fileJournal'] = isset( $config['fileJournal'] )
170 ? FileJournal
::factory( $config['fileJournal'], $name )
171 : FileJournal
::factory( [ 'class' => 'NullFileJournal' ], $name );
172 $config['wanCache'] = ObjectCache
::getMainWANInstance();
173 $config['statusWrapper'] = [ 'Status', 'wrap' ];
174 $config['tmpDirectory'] = wfTempDir();
175 $config['logger'] = LoggerFactory
::getInstance( 'FileOperation' );
176 $config['profiler'] = Profiler
::instance();
178 $this->backends
[$name]['instance'] = new $class( $config );
181 return $this->backends
[$name]['instance'];
185 * Get the config array for a backend object with a given name
187 * @param string $name
189 * @throws InvalidArgumentException
191 public function config( $name ) {
192 if ( !isset( $this->backends
[$name] ) ) {
193 throw new InvalidArgumentException( "No backend defined with the name `$name`." );
195 $class = $this->backends
[$name]['class'];
197 return [ 'class' => $class ] +
$this->backends
[$name]['config'];
201 * Get an appropriate backend object from a storage path
203 * @param string $storagePath
204 * @return FileBackend|null Backend or null on failure
206 public function backendFromPath( $storagePath ) {
207 list( $backend, , ) = FileBackend
::splitStoragePath( $storagePath );
208 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
209 return $this->get( $backend );
216 * @param string $storagePath
217 * @param string|null $content
218 * @param string|null $fsPath
222 public function guessMimeInternal( $storagePath, $content, $fsPath ) {
223 $magic = MimeMagic
::singleton();
224 // Trust the extension of the storage path (caller must validate)
225 $ext = FileBackend
::extensionFromPath( $storagePath );
226 $type = $magic->guessTypesForExtension( $ext );
227 // For files without a valid extension (or one at all), inspect the contents
228 if ( !$type && $fsPath ) {
229 $type = $magic->guessMimeType( $fsPath, false );
230 } elseif ( !$type && strlen( $content ) ) {
231 $tmpFile = TempFSFile
::factory( 'mime_', '', wfTempDir() );
232 file_put_contents( $tmpFile->getPath(), $content );
233 $type = $magic->guessMimeType( $tmpFile->getPath(), false );
235 return $type ?
: 'unknown/unknown';