28use InvalidArgumentException;
42use Wikimedia\ObjectFactory\ObjectFactory;
68 private $mimeAnalyzer;
74 private $tmpFileFactory;
77 private $objectFactory;
105 MimeAnalyzer $mimeAnalyzer,
108 ObjectFactory $objectFactory
110 $this->options = $options;
111 $this->srvCache = $srvCache;
112 $this->wanCache = $wanCache;
113 $this->mimeAnalyzer = $mimeAnalyzer;
114 $this->lmgFactory = $lmgFactory;
115 $this->tmpFileFactory = $tmpFileFactory;
116 $this->objectFactory = $objectFactory;
123 $repos = array_merge(
125 foreach ( $repos as $info ) {
126 $backendName = $info[
'backend'];
127 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
130 $repoName = $info[
'name'];
132 $directory = $info[
'directory'];
133 $deletedDir = $info[
'deletedDir'] ??
false;
134 $thumbDir = $info[
'thumbDir'] ??
"{$directory}/thumb";
135 $transcodedDir = $info[
'transcodedDir'] ??
"{$directory}/transcoded";
136 $lockManager = $info[
'lockManager'] ??
'fsLockManager';
139 'name' => $backendName,
140 'class' => FSFileBackend::class,
141 'lockManager' => $lockManager,
142 'containerPaths' => [
143 "{$repoName}-public" =>
"{$directory}",
144 "{$repoName}-thumb" => $thumbDir,
145 "{$repoName}-transcoded" => $transcodedDir,
146 "{$repoName}-deleted" => $deletedDir,
147 "{$repoName}-temp" =>
"{$directory}/temp"
149 'fileMode' => $info[
'fileMode'] ?? 0644,
165 protected function register( array $configs, $readOnlyReason = null ) {
166 foreach ( $configs as $config ) {
167 if ( !isset( $config[
'name'] ) ) {
168 throw new InvalidArgumentException(
"Cannot register a backend with no name." );
170 $name = $config[
'name'];
171 if ( isset( $this->backends[$name] ) ) {
172 throw new LogicException(
"Backend with name '$name' already registered." );
173 } elseif ( !isset( $config[
'class'] ) ) {
174 throw new InvalidArgumentException(
"Backend with name '$name' has no class." );
176 $class = $config[
'class'];
178 $config[
'domainId'] ??= $config[
'wikiId'] ?? $this->options->get(
'fallbackWikiId' );
179 $config[
'readOnly'] ??= $readOnlyReason;
181 unset( $config[
'class'] );
182 $this->backends[$name] = [
197 public function get( $name ) {
199 if ( !isset( $this->backends[$name][
'instance'] ) ) {
200 $config = $this->
config( $name );
202 $class = $config[
'class'];
203 if ( $class === FileBackendMultiWrite::class ) {
205 foreach ( $config[
'backends'] as $index => $beConfig ) {
206 if ( isset( $beConfig[
'template'] ) ) {
209 $config[
'backends'][$index] += $this->
config( $beConfig[
'template'] );
214 $this->backends[$name][
'instance'] =
new $class( $config );
217 return $this->backends[$name][
'instance'];
228 if ( !isset( $this->backends[$name] ) ) {
229 throw new InvalidArgumentException(
"No backend defined with the name '$name'." );
232 $config = $this->backends[$name][
'config'];
237 'mimeCallback' => [ $this,
'guessMimeInternal' ],
238 'obResetFunc' =>
'wfResetOutputBuffers',
239 'streamMimeFunc' => [ StreamFile::class,
'contentTypeFromPath' ],
240 'tmpFileFactory' => $this->tmpFileFactory,
241 'statusWrapper' => [ Status::class,
'wrap' ],
242 'wanCache' => $this->wanCache,
243 'srvCache' => $this->srvCache,
244 'logger' => LoggerFactory::getInstance(
'FileOperation' ),
245 'profiler' =>
static function ( $section ) {
253 'class' => $this->backends[$name][
'class'],
255 $this->lmgFactory->getLockManagerGroup( $config[
'domainId'] )
256 ->get( $config[
'lockManager'] ),
268 [ $backend, , ] = FileBackend::splitStoragePath( $storagePath );
269 if ( $backend !==
null && isset( $this->backends[$backend] ) ) {
270 return $this->
get( $backend );
285 $ext = FileBackend::extensionFromPath( $storagePath );
286 $type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext );
288 if ( !$type && $fsPath ) {
289 $type = $this->mimeAnalyzer->guessMimeType( $fsPath,
false );
290 } elseif ( !$type && $content !==
null && $content !==
'' ) {
291 $tmpFile = $this->tmpFileFactory->newTempFSFile(
'mime_',
'' );
292 file_put_contents( $tmpFile->getPath(), $content );
293 $type = $this->mimeAnalyzer->guessMimeType( $tmpFile->getPath(),
false );
295 return $type ?:
'unknown/unknown';
299class_alias( FileBackendGroup::class,
'FileBackendGroup' );
Class for a file system (FS) based file backend.
Proxy backend that mirrors writes to several internal backends.
A class containing constants representing the names of configuration variables.
const FileBackends
Name constant for the FileBackends setting, for use with Config::get()
const LocalFileRepo
Name constant for the LocalFileRepo setting, for use with Config::get()
const ForeignFileRepos
Name constant for the ForeignFileRepos setting, for use with Config::get()
const DirectoryMode
Name constant for the DirectoryMode setting, for use with Config::get()
Profiler base class that defines the interface and some shared functionality.
Multi-datacenter aware caching interface.