26use InvalidArgumentException;
40use Wikimedia\Mime\MimeAnalyzer;
43use Wikimedia\ObjectFactory\ObjectFactory;
69 private $mimeAnalyzer;
75 private $tmpFileFactory;
78 private $objectFactory;
106 MimeAnalyzer $mimeAnalyzer,
109 ObjectFactory $objectFactory
111 $this->options = $options;
112 $this->srvCache = $srvCache;
113 $this->wanCache = $wanCache;
114 $this->mimeAnalyzer = $mimeAnalyzer;
115 $this->lmgFactory = $lmgFactory;
116 $this->tmpFileFactory = $tmpFileFactory;
117 $this->objectFactory = $objectFactory;
124 $repos = array_merge(
126 foreach ( $repos as $info ) {
127 $backendName = $info[
'backend'];
128 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
131 $repoName = $info[
'name'];
133 $directory = $info[
'directory'];
134 $deletedDir = $info[
'deletedDir'] ??
false;
135 $thumbDir = $info[
'thumbDir'] ??
"{$directory}/thumb";
136 $transcodedDir = $info[
'transcodedDir'] ??
"{$directory}/transcoded";
137 $lockManager = $info[
'lockManager'] ??
'fsLockManager';
140 'name' => $backendName,
141 'class' => FSFileBackend::class,
142 'lockManager' => $lockManager,
143 'containerPaths' => [
144 "{$repoName}-public" =>
"{$directory}",
145 "{$repoName}-thumb" => $thumbDir,
146 "{$repoName}-transcoded" => $transcodedDir,
147 "{$repoName}-deleted" => $deletedDir,
148 "{$repoName}-temp" =>
"{$directory}/temp"
150 '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] = [
196 public function get( $name ) {
198 if ( !isset( $this->backends[$name][
'instance'] ) ) {
199 $config = $this->
config( $name );
201 $class = $config[
'class'];
203 if ( $class === FileBackendMultiWrite::class || $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'];
227 if ( !isset( $this->backends[$name] ) ) {
228 throw new InvalidArgumentException(
"No backend defined with the name '$name'." );
231 $config = $this->backends[$name][
'config'];
236 'mimeCallback' => [ $this,
'guessMimeInternal' ],
237 'obResetFunc' =>
'wfResetOutputBuffers',
238 'asyncHandler' => [ DeferredUpdates::class,
'addCallableUpdate' ],
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'] ),
269 if ( $backend !==
null && isset( $this->backends[$backend] ) ) {
270 return $this->
get( $backend );
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' );
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.