12use InvalidArgumentException;
26use Wikimedia\Mime\MimeAnalyzer;
29use Wikimedia\ObjectFactory\ObjectFactory;
55 private $mimeAnalyzer;
61 private $tmpFileFactory;
64 private $objectFactory;
92 MimeAnalyzer $mimeAnalyzer,
95 ObjectFactory $objectFactory
97 $this->options = $options;
98 $this->srvCache = $srvCache;
99 $this->wanCache = $wanCache;
100 $this->mimeAnalyzer = $mimeAnalyzer;
101 $this->lmgFactory = $lmgFactory;
102 $this->tmpFileFactory = $tmpFileFactory;
103 $this->objectFactory = $objectFactory;
110 $repos = array_merge(
112 foreach ( $repos as $info ) {
113 $backendName = $info[
'backend'];
114 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
117 $repoName = $info[
'name'];
119 $directory = $info[
'directory'];
121 $deletedDir = $info[
'deletedDir'] ??
false;
122 $thumbDir = $info[
'thumbDir'] ??
"{$directory}/thumb";
123 $transcodedDir = $info[
'transcodedDir'] ??
"{$directory}/transcoded";
124 $lockManager = $info[
'lockManager'] ??
'fsLockManager';
127 'name' => $backendName,
128 'class' => FSFileBackend::class,
129 'lockManager' => $lockManager,
130 'containerPaths' => [
131 "{$repoName}-public" =>
"{$directory}",
132 "{$repoName}-thumb" => $thumbDir,
133 "{$repoName}-transcoded" => $transcodedDir,
134 "{$repoName}-deleted" => $deletedDir,
135 "{$repoName}-temp" =>
"{$directory}/temp"
137 'fileMode' => $info[
'fileMode'] ?? 0644,
152 protected function register( array $configs, $readOnlyReason = null ) {
153 foreach ( $configs as $config ) {
154 if ( !isset( $config[
'name'] ) ) {
155 throw new InvalidArgumentException(
"Cannot register a backend with no name." );
157 $name = $config[
'name'];
158 if ( isset( $this->backends[$name] ) ) {
159 throw new LogicException(
"Backend with name '$name' already registered." );
160 } elseif ( !isset( $config[
'class'] ) ) {
161 throw new InvalidArgumentException(
"Backend with name '$name' has no class." );
163 $class = $config[
'class'];
165 $config[
'domainId'] ??= $config[
'wikiId'] ?? $this->options->get(
'fallbackWikiId' );
166 $config[
'readOnly'] ??= $readOnlyReason;
168 unset( $config[
'class'] );
169 $this->backends[$name] = [
183 public function get( $name ) {
185 if ( !isset( $this->backends[$name][
'instance'] ) ) {
186 $config = $this->
config( $name );
188 $class = $config[
'class'];
190 if ( $class === FileBackendMultiWrite::class || $class === \FileBackendMultiWrite::class ) {
192 foreach ( $config[
'backends'] as $index => $beConfig ) {
193 if ( isset( $beConfig[
'template'] ) ) {
196 $config[
'backends'][$index] += $this->
config( $beConfig[
'template'] );
201 $this->backends[$name][
'instance'] =
new $class( $config );
204 return $this->backends[$name][
'instance'];
214 if ( !isset( $this->backends[$name] ) ) {
215 throw new InvalidArgumentException(
"No backend defined with the name '$name'." );
218 $config = $this->backends[$name][
'config'];
224 'obResetFunc' =>
'wfResetOutputBuffers',
225 'asyncHandler' => DeferredUpdates::addCallableUpdate( ... ),
227 'tmpFileFactory' => $this->tmpFileFactory,
228 'statusWrapper' => Status::wrap( ... ),
229 'wanCache' => $this->wanCache,
230 'srvCache' => $this->srvCache,
231 'logger' => LoggerFactory::getInstance(
'FileOperation' ),
232 'profiler' =>
static function ( $section ) {
240 'class' => $this->backends[$name][
'class'],
242 $this->lmgFactory->getLockManagerGroup( $config[
'domainId'] )
243 ->get( $config[
'lockManager'] ),
256 if ( $backend !==
null && isset( $this->backends[$backend] ) ) {
257 return $this->
get( $backend );
273 $type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext );
275 if ( !$type && $fsPath ) {
276 $type = $this->mimeAnalyzer->guessMimeType( $fsPath,
false );
277 } elseif ( !$type && $content !==
null && $content !==
'' ) {
278 $tmpFile = $this->tmpFileFactory->newTempFSFile(
'mime_',
'' );
279 file_put_contents( $tmpFile->getPath(), $content );
280 $type = $this->mimeAnalyzer->guessMimeType( $tmpFile->getPath(),
false );
282 return $type ?:
'unknown/unknown';
286class_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.