MediaWiki  master
FileBackendGroup.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\ObjectFactory;
29 
41  protected $backends = [];
42 
44  private $options;
45 
47  private $srvCache;
48 
50  private $wanCache;
51 
53  private $mimeAnalyzer;
54 
56  private $lmgFactory;
57 
59  private $tmpFileFactory;
60 
62  private $objectFactory;
63 
67  public const CONSTRUCTOR_OPTIONS = [
68  'DirectoryMode',
69  'FileBackends',
70  'ForeignFileRepos',
71  'LocalFileRepo',
72  'fallbackWikiId',
73  ];
74 
85  public function __construct(
87  ConfiguredReadOnlyMode $configuredReadOnlyMode,
93  ObjectFactory $objectFactory
94  ) {
95  $this->options = $options;
96  $this->srvCache = $srvCache;
97  $this->wanCache = $wanCache;
98  $this->mimeAnalyzer = $mimeAnalyzer;
99  $this->lmgFactory = $lmgFactory;
100  $this->tmpFileFactory = $tmpFileFactory;
101  $this->objectFactory = $objectFactory;
102 
103  // Register explicitly defined backends
104  $this->register( $options->get( 'FileBackends' ), $configuredReadOnlyMode->getReason() );
105 
106  $autoBackends = [];
107  // Automatically create b/c backends for file repos...
108  $repos = array_merge(
109  $options->get( 'ForeignFileRepos' ), [ $options->get( 'LocalFileRepo' ) ] );
110  foreach ( $repos as $info ) {
111  $backendName = $info['backend'];
112  if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
113  continue; // already defined (or set to the object for some reason)
114  }
115  $repoName = $info['name'];
116  // Local vars that used to be FSRepo members...
117  $directory = $info['directory'];
118  $deletedDir = $info['deletedDir'] ?? false; // deletion disabled
119  $thumbDir = $info['thumbDir'] ?? "{$directory}/thumb";
120  $transcodedDir = $info['transcodedDir'] ?? "{$directory}/transcoded";
121  $lockManager = $info['lockManager'] ?? 'fsLockManager';
122  // Get the FS backend configuration
123  $autoBackends[] = [
124  'name' => $backendName,
125  'class' => FSFileBackend::class,
126  'lockManager' => $lockManager,
127  'containerPaths' => [
128  "{$repoName}-public" => "{$directory}",
129  "{$repoName}-thumb" => $thumbDir,
130  "{$repoName}-transcoded" => $transcodedDir,
131  "{$repoName}-deleted" => $deletedDir,
132  "{$repoName}-temp" => "{$directory}/temp"
133  ],
134  'fileMode' => $info['fileMode'] ?? 0644,
135  'directoryMode' => $options->get( 'DirectoryMode' ),
136  ];
137  }
138 
139  // Register implicitly defined backends
140  $this->register( $autoBackends, $configuredReadOnlyMode->getReason() );
141  }
142 
150  protected function register( array $configs, $readOnlyReason = null ) {
151  foreach ( $configs as $config ) {
152  if ( !isset( $config['name'] ) ) {
153  throw new InvalidArgumentException( "Cannot register a backend with no name." );
154  }
155  $name = $config['name'];
156  if ( isset( $this->backends[$name] ) ) {
157  throw new LogicException( "Backend with name '$name' already registered." );
158  } elseif ( !isset( $config['class'] ) ) {
159  throw new InvalidArgumentException( "Backend with name '$name' has no class." );
160  }
161  $class = $config['class'];
162 
163  $config['domainId'] =
164  $config['domainId'] ?? $config['wikiId'] ?? $this->options->get( 'fallbackWikiId' );
165  $config['readOnly'] = $config['readOnly'] ?? $readOnlyReason;
166 
167  unset( $config['class'] ); // backend won't need this
168  $this->backends[$name] = [
169  'class' => $class,
170  'config' => $config,
171  'instance' => null
172  ];
173  }
174  }
175 
183  public function get( $name ) {
184  // Lazy-load the actual backend instance
185  if ( !isset( $this->backends[$name]['instance'] ) ) {
186  $config = $this->config( $name );
187 
188  $class = $config['class'];
189  if ( $class === FileBackendMultiWrite::class ) {
190  // @todo How can we test this? What's the intended use-case?
191  foreach ( $config['backends'] as $index => $beConfig ) {
192  if ( isset( $beConfig['template'] ) ) {
193  // Config is just a modified version of a registered backend's.
194  // This should only be used when that config is used only by this backend.
195  $config['backends'][$index] += $this->config( $beConfig['template'] );
196  }
197  }
198  }
199 
200  $this->backends[$name]['instance'] = new $class( $config );
201  }
202 
203  return $this->backends[$name]['instance'];
204  }
205 
213  public function config( $name ) {
214  if ( !isset( $this->backends[$name] ) ) {
215  throw new InvalidArgumentException( "No backend defined with the name '$name'." );
216  }
217 
218  $config = $this->backends[$name]['config'];
219 
220  return array_merge(
221  // Default backend parameters
222  [
223  'mimeCallback' => [ $this, 'guessMimeInternal' ],
224  'obResetFunc' => 'wfResetOutputBuffers',
225  'streamMimeFunc' => [ StreamFile::class, 'contentTypeFromPath' ],
226  'tmpFileFactory' => $this->tmpFileFactory,
227  'statusWrapper' => [ Status::class, 'wrap' ],
228  'wanCache' => $this->wanCache,
229  'srvCache' => $this->srvCache,
230  'logger' => LoggerFactory::getInstance( 'FileOperation' ),
231  'profiler' => static function ( $section ) {
232  return Profiler::instance()->scopedProfileIn( $section );
233  }
234  ],
235  // Configured backend parameters
236  $config,
237  // Resolved backend parameters
238  [
239  'class' => $this->backends[$name]['class'],
240  'lockManager' =>
241  $this->lmgFactory->getLockManagerGroup( $config['domainId'] )
242  ->get( $config['lockManager'] ),
243  ]
244  );
245  }
246 
253  public function backendFromPath( $storagePath ) {
254  list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
255  if ( $backend !== null && isset( $this->backends[$backend] ) ) {
256  return $this->get( $backend );
257  }
258 
259  return null;
260  }
261 
269  public function guessMimeInternal( $storagePath, $content, $fsPath ) {
270  // Trust the extension of the storage path (caller must validate)
271  $ext = FileBackend::extensionFromPath( $storagePath );
272  $type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext );
273  // For files without a valid extension (or one at all), inspect the contents
274  if ( !$type && $fsPath ) {
275  $type = $this->mimeAnalyzer->guessMimeType( $fsPath, false );
276  } elseif ( !$type && strlen( $content ) ) {
277  $tmpFile = $this->tmpFileFactory->newTempFSFile( 'mime_', '' );
278  file_put_contents( $tmpFile->getPath(), $content );
279  $type = $this->mimeAnalyzer->guessMimeType( $tmpFile->getPath(), false );
280  }
281  return $type ?: 'unknown/unknown';
282  }
283 }
FileBackendGroup\__construct
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $configuredReadOnlyMode, BagOStuff $srvCache, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer, LockManagerGroupFactory $lmgFactory, TempFSFileFactory $tmpFileFactory, ObjectFactory $objectFactory)
Definition: FileBackendGroup.php:85
FileBackend\splitStoragePath
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
Definition: FileBackend.php:1521
FileBackendGroup
Class to handle file backend registration.
Definition: FileBackendGroup.php:36
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:33
MediaWiki\FileBackend\LockManager\LockManagerGroupFactory
Service to construct LockManagerGroups.
Definition: LockManagerGroupFactory.php:11
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1583
ConfiguredReadOnlyMode
A read-only mode service which does not depend on LoadBalancer.
Definition: ConfiguredReadOnlyMode.php:9
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
FileBackendGroup\config
config( $name)
Get the config array for a backend object with a given name.
Definition: FileBackendGroup.php:213
ConfiguredReadOnlyMode\getReason
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Definition: ConfiguredReadOnlyMode.php:42
FileBackendGroup\$objectFactory
ObjectFactory $objectFactory
Definition: FileBackendGroup.php:62
FileBackendGroup\guessMimeInternal
guessMimeInternal( $storagePath, $content, $fsPath)
Definition: FileBackendGroup.php:269
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
FileBackendGroup\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: FileBackendGroup.php:67
FileBackendGroup\$lmgFactory
LockManagerGroupFactory $lmgFactory
Definition: FileBackendGroup.php:56
MediaWiki\FileBackend\FSFile\TempFSFileFactory
Definition: TempFSFileFactory.php:10
$content
$content
Definition: router.php:76
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:131
FileBackendGroup\$options
ServiceOptions $options
Definition: FileBackendGroup.php:44
FileBackendGroup\$backends
array[] $backends
(name => ('class' => string, 'config' => array, 'instance' => object))
Definition: FileBackendGroup.php:41
FileBackendGroup\$tmpFileFactory
TempFSFileFactory $tmpFileFactory
Definition: FileBackendGroup.php:59
FileBackendGroup\backendFromPath
backendFromPath( $storagePath)
Get an appropriate backend object from a storage path.
Definition: FileBackendGroup.php:253
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:93
FileBackendGroup\$srvCache
BagOStuff $srvCache
Definition: FileBackendGroup.php:47
FileBackendGroup\$wanCache
WANObjectCache $wanCache
Definition: FileBackendGroup.php:50
$type
$type
Definition: testCompression.php:52
FileBackendGroup\$mimeAnalyzer
MimeAnalyzer $mimeAnalyzer
Definition: FileBackendGroup.php:53