MediaWiki  master
FileBackendGroup.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\ObjectFactory\ObjectFactory;
30 
42  protected $backends = [];
43 
45  private $options;
46 
48  private $srvCache;
49 
51  private $wanCache;
52 
54  private $mimeAnalyzer;
55 
57  private $lmgFactory;
58 
60  private $tmpFileFactory;
61 
63  private $objectFactory;
64 
68  public const CONSTRUCTOR_OPTIONS = [
69  MainConfigNames::DirectoryMode,
70  MainConfigNames::FileBackends,
71  MainConfigNames::ForeignFileRepos,
72  MainConfigNames::LocalFileRepo,
73  'fallbackWikiId',
74  ];
75 
86  public function __construct(
87  ServiceOptions $options,
88  ConfiguredReadOnlyMode $configuredReadOnlyMode,
89  BagOStuff $srvCache,
90  WANObjectCache $wanCache,
91  MimeAnalyzer $mimeAnalyzer,
92  LockManagerGroupFactory $lmgFactory,
93  TempFSFileFactory $tmpFileFactory,
94  ObjectFactory $objectFactory
95  ) {
96  $this->options = $options;
97  $this->srvCache = $srvCache;
98  $this->wanCache = $wanCache;
99  $this->mimeAnalyzer = $mimeAnalyzer;
100  $this->lmgFactory = $lmgFactory;
101  $this->tmpFileFactory = $tmpFileFactory;
102  $this->objectFactory = $objectFactory;
103 
104  // Register explicitly defined backends
105  $this->register( $options->get( MainConfigNames::FileBackends ), $configuredReadOnlyMode->getReason() );
106 
107  $autoBackends = [];
108  // Automatically create b/c backends for file repos...
109  $repos = array_merge(
110  $options->get( MainConfigNames::ForeignFileRepos ), [ $options->get( MainConfigNames::LocalFileRepo ) ] );
111  foreach ( $repos as $info ) {
112  $backendName = $info['backend'];
113  if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
114  continue; // already defined (or set to the object for some reason)
115  }
116  $repoName = $info['name'];
117  // Local vars that used to be FSRepo members...
118  $directory = $info['directory'];
119  $deletedDir = $info['deletedDir'] ?? false; // deletion disabled
120  $thumbDir = $info['thumbDir'] ?? "{$directory}/thumb";
121  $transcodedDir = $info['transcodedDir'] ?? "{$directory}/transcoded";
122  $lockManager = $info['lockManager'] ?? 'fsLockManager';
123  // Get the FS backend configuration
124  $autoBackends[] = [
125  'name' => $backendName,
126  'class' => FSFileBackend::class,
127  'lockManager' => $lockManager,
128  'containerPaths' => [
129  "{$repoName}-public" => "{$directory}",
130  "{$repoName}-thumb" => $thumbDir,
131  "{$repoName}-transcoded" => $transcodedDir,
132  "{$repoName}-deleted" => $deletedDir,
133  "{$repoName}-temp" => "{$directory}/temp"
134  ],
135  'fileMode' => $info['fileMode'] ?? 0644,
136  'directoryMode' => $options->get( MainConfigNames::DirectoryMode ),
137  ];
138  }
139 
140  // Register implicitly defined backends
141  $this->register( $autoBackends, $configuredReadOnlyMode->getReason() );
142  }
143 
151  protected function register( array $configs, $readOnlyReason = null ) {
152  foreach ( $configs as $config ) {
153  if ( !isset( $config['name'] ) ) {
154  throw new InvalidArgumentException( "Cannot register a backend with no name." );
155  }
156  $name = $config['name'];
157  if ( isset( $this->backends[$name] ) ) {
158  throw new LogicException( "Backend with name '$name' already registered." );
159  } elseif ( !isset( $config['class'] ) ) {
160  throw new InvalidArgumentException( "Backend with name '$name' has no class." );
161  }
162  $class = $config['class'];
163 
164  $config['domainId'] ??= $config['wikiId'] ?? $this->options->get( 'fallbackWikiId' );
165  $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  [ $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 && $content !== null && $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 }
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:85
A read-only mode service which does not depend on LoadBalancer.
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Class to handle file backend registration.
config( $name)
Get the config array for a backend object with a given name.
guessMimeInternal( $storagePath, $content, $fsPath)
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $configuredReadOnlyMode, BagOStuff $srvCache, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer, LockManagerGroupFactory $lmgFactory, TempFSFileFactory $tmpFileFactory, ObjectFactory $objectFactory)
backendFromPath( $storagePath)
Get an appropriate backend object from a storage path.
array[] $backends
(name => ('class' => string, 'config' => array, 'instance' => object))
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
A class for passing options to services.
PSR-3 logger instance factory.
A class containing constants representing the names of configuration variables.
Implements functions related to MIME types such as detection and mapping to file extension.
static instance()
Singleton.
Definition: Profiler.php:94
Multi-datacenter aware caching interface.
$content
Definition: router.php:76
if(!is_readable( $file)) $ext
Definition: router.php:48