1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?php
/**
* File backend registration handling.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup FileBackend
*/
namespace MediaWiki\FileBackend;
use InvalidArgumentException;
use LogicException;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Output\StreamFile;
use MediaWiki\Status\Status;
use Profiler;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\FileBackend\FileBackendMultiWrite;
use Wikimedia\FileBackend\FSFileBackend;
use Wikimedia\Mime\MimeAnalyzer;
use Wikimedia\ObjectCache\BagOStuff;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\ObjectFactory\ObjectFactory;
use Wikimedia\Rdbms\ReadOnlyMode;
/**
* Class to handle file backend registration
*
* @ingroup FileBackend
* @since 1.19
*/
class FileBackendGroup {
/**
* @var array[] (name => ('class' => string, 'config' => array, 'instance' => object))
* @phan-var array<string,array{class:class-string,config:array,instance:object}>
*/
protected $backends = [];
/** @var ServiceOptions */
private $options;
/** @var BagOStuff */
private $srvCache;
/** @var WANObjectCache */
private $wanCache;
/** @var MimeAnalyzer */
private $mimeAnalyzer;
/** @var LockManagerGroupFactory */
private $lmgFactory;
/** @var TempFSFileFactory */
private $tmpFileFactory;
/** @var ObjectFactory */
private $objectFactory;
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::DirectoryMode,
MainConfigNames::FileBackends,
MainConfigNames::ForeignFileRepos,
MainConfigNames::LocalFileRepo,
'fallbackWikiId',
];
/**
* @param ServiceOptions $options
* @param ReadOnlyMode $readOnlyMode
* @param BagOStuff $srvCache
* @param WANObjectCache $wanCache
* @param MimeAnalyzer $mimeAnalyzer
* @param LockManagerGroupFactory $lmgFactory
* @param TempFSFileFactory $tmpFileFactory
* @param ObjectFactory $objectFactory
*/
public function __construct(
ServiceOptions $options,
ReadOnlyMode $readOnlyMode,
BagOStuff $srvCache,
WANObjectCache $wanCache,
MimeAnalyzer $mimeAnalyzer,
LockManagerGroupFactory $lmgFactory,
TempFSFileFactory $tmpFileFactory,
ObjectFactory $objectFactory
) {
$this->options = $options;
$this->srvCache = $srvCache;
$this->wanCache = $wanCache;
$this->mimeAnalyzer = $mimeAnalyzer;
$this->lmgFactory = $lmgFactory;
$this->tmpFileFactory = $tmpFileFactory;
$this->objectFactory = $objectFactory;
// Register explicitly defined backends
$this->register( $options->get( MainConfigNames::FileBackends ), $readOnlyMode->getConfiguredReason() );
$autoBackends = [];
// Automatically create b/c backends for file repos...
$repos = array_merge(
$options->get( MainConfigNames::ForeignFileRepos ), [ $options->get( MainConfigNames::LocalFileRepo ) ] );
foreach ( $repos as $info ) {
$backendName = $info['backend'];
if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
continue; // already defined (or set to the object for some reason)
}
$repoName = $info['name'];
// Local vars that used to be FSRepo members...
$directory = $info['directory'];
$deletedDir = $info['deletedDir'] ?? false; // deletion disabled
$thumbDir = $info['thumbDir'] ?? "{$directory}/thumb";
$transcodedDir = $info['transcodedDir'] ?? "{$directory}/transcoded";
$lockManager = $info['lockManager'] ?? 'fsLockManager';
// Get the FS backend configuration
$autoBackends[] = [
'name' => $backendName,
'class' => FSFileBackend::class,
'lockManager' => $lockManager,
'containerPaths' => [
"{$repoName}-public" => "{$directory}",
"{$repoName}-thumb" => $thumbDir,
"{$repoName}-transcoded" => $transcodedDir,
"{$repoName}-deleted" => $deletedDir,
"{$repoName}-temp" => "{$directory}/temp"
],
'fileMode' => $info['fileMode'] ?? 0644,
'directoryMode' => $options->get( MainConfigNames::DirectoryMode ),
];
}
// Register implicitly defined backends
$this->register( $autoBackends, $readOnlyMode->getConfiguredReason() );
}
/**
* Register an array of file backend configurations
*
* @param array[] $configs
* @param string|null $readOnlyReason
*/
protected function register( array $configs, $readOnlyReason = null ) {
foreach ( $configs as $config ) {
if ( !isset( $config['name'] ) ) {
throw new InvalidArgumentException( "Cannot register a backend with no name." );
}
$name = $config['name'];
if ( isset( $this->backends[$name] ) ) {
throw new LogicException( "Backend with name '$name' already registered." );
} elseif ( !isset( $config['class'] ) ) {
throw new InvalidArgumentException( "Backend with name '$name' has no class." );
}
$class = $config['class'];
$config['domainId'] ??= $config['wikiId'] ?? $this->options->get( 'fallbackWikiId' );
$config['readOnly'] ??= $readOnlyReason;
unset( $config['class'] ); // backend won't need this
$this->backends[$name] = [
'class' => $class,
'config' => $config,
'instance' => null
];
}
}
/**
* Get the backend object with a given name
*
* @param string $name
* @return FileBackend
*/
public function get( $name ) {
// Lazy-load the actual backend instance
if ( !isset( $this->backends[$name]['instance'] ) ) {
$config = $this->config( $name );
$class = $config['class'];
// Checking old alias for compatibility with unchanged config
if ( $class === FileBackendMultiWrite::class || $class === \FileBackendMultiWrite::class ) {
// @todo How can we test this? What's the intended use-case?
foreach ( $config['backends'] as $index => $beConfig ) {
if ( isset( $beConfig['template'] ) ) {
// Config is just a modified version of a registered backend's.
// This should only be used when that config is used only by this backend.
$config['backends'][$index] += $this->config( $beConfig['template'] );
}
}
}
$this->backends[$name]['instance'] = new $class( $config );
}
return $this->backends[$name]['instance'];
}
/**
* Get the config array for a backend object with a given name
*
* @param string $name
* @return array Parameters to FileBackend::__construct()
*/
public function config( $name ) {
if ( !isset( $this->backends[$name] ) ) {
throw new InvalidArgumentException( "No backend defined with the name '$name'." );
}
$config = $this->backends[$name]['config'];
return array_merge(
// Default backend parameters
[
'mimeCallback' => [ $this, 'guessMimeInternal' ],
'obResetFunc' => 'wfResetOutputBuffers',
'streamMimeFunc' => [ StreamFile::class, 'contentTypeFromPath' ],
'tmpFileFactory' => $this->tmpFileFactory,
'statusWrapper' => [ Status::class, 'wrap' ],
'wanCache' => $this->wanCache,
'srvCache' => $this->srvCache,
'logger' => LoggerFactory::getInstance( 'FileOperation' ),
'profiler' => static function ( $section ) {
return Profiler::instance()->scopedProfileIn( $section );
}
],
// Configured backend parameters
$config,
// Resolved backend parameters
[
'class' => $this->backends[$name]['class'],
'lockManager' =>
$this->lmgFactory->getLockManagerGroup( $config['domainId'] )
->get( $config['lockManager'] ),
]
);
}
/**
* Get an appropriate backend object from a storage path
*
* @param string $storagePath
* @return FileBackend|null Backend or null on failure
*/
public function backendFromPath( $storagePath ) {
[ $backend, , ] = FileBackend::splitStoragePath( $storagePath );
if ( $backend !== null && isset( $this->backends[$backend] ) ) {
return $this->get( $backend );
}
return null;
}
/**
* @param string $storagePath
* @param string|null $content
* @param string|null $fsPath
* @return string
* @since 1.27
*/
public function guessMimeInternal( $storagePath, $content, $fsPath ) {
// Trust the extension of the storage path (caller must validate)
$ext = FileBackend::extensionFromPath( $storagePath );
$type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext );
// For files without a valid extension (or one at all), inspect the contents
if ( !$type && $fsPath ) {
$type = $this->mimeAnalyzer->guessMimeType( $fsPath, false );
} elseif ( !$type && $content !== null && $content !== '' ) {
$tmpFile = $this->tmpFileFactory->newTempFSFile( 'mime_', '' );
file_put_contents( $tmpFile->getPath(), $content );
$type = $this->mimeAnalyzer->guessMimeType( $tmpFile->getPath(), false );
}
return $type ?: 'unknown/unknown';
}
}
/** @deprecated class alias since 1.43 */
class_alias( FileBackendGroup::class, 'FileBackendGroup' );
|