| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\FileBackend; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use LogicException; |
| 28 |
use MediaWiki\Config\ServiceOptions; |
| 29 |
use MediaWiki\FileBackend\FSFile\TempFSFileFactory; |
| 30 |
use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory; |
| 31 |
use MediaWiki\Logger\LoggerFactory; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\Output\StreamFile; |
| 34 |
use MediaWiki\Status\Status; |
| 35 |
use Profiler; |
| 36 |
use Wikimedia\FileBackend\FileBackend; |
| 37 |
use Wikimedia\FileBackend\FileBackendMultiWrite; |
| 38 |
use Wikimedia\FileBackend\FSFileBackend; |
| 39 |
use Wikimedia\Mime\MimeAnalyzer; |
| 40 |
use Wikimedia\ObjectCache\BagOStuff; |
| 41 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 42 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 43 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class FileBackendGroup { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected $backends = []; |
| 57 |
|
| 58 |
|
| 59 |
private $options; |
| 60 |
|
| 61 |
|
| 62 |
private $srvCache; |
| 63 |
|
| 64 |
|
| 65 |
private $wanCache; |
| 66 |
|
| 67 |
|
| 68 |
private $mimeAnalyzer; |
| 69 |
|
| 70 |
|
| 71 |
private $lmgFactory; |
| 72 |
|
| 73 |
|
| 74 |
private $tmpFileFactory; |
| 75 |
|
| 76 |
|
| 77 |
private $objectFactory; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public const CONSTRUCTOR_OPTIONS = [ |
| 83 |
MainConfigNames::DirectoryMode, |
| 84 |
MainConfigNames::FileBackends, |
| 85 |
MainConfigNames::ForeignFileRepos, |
| 86 |
MainConfigNames::LocalFileRepo, |
| 87 |
'fallbackWikiId', |
| 88 |
]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function __construct( |
| 101 |
ServiceOptions $options, |
| 102 |
ReadOnlyMode $readOnlyMode, |
| 103 |
BagOStuff $srvCache, |
| 104 |
WANObjectCache $wanCache, |
| 105 |
MimeAnalyzer $mimeAnalyzer, |
| 106 |
LockManagerGroupFactory $lmgFactory, |
| 107 |
TempFSFileFactory $tmpFileFactory, |
| 108 |
ObjectFactory $objectFactory |
| 109 |
) { |
| 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; |
| 117 |
|
| 118 |
|
| 119 |
$this->register( $options->get( MainConfigNames::FileBackends ), $readOnlyMode->getConfiguredReason() ); |
| 120 |
|
| 121 |
$autoBackends = []; |
| 122 |
|
| 123 |
$repos = array_merge( |
| 124 |
$options->get( MainConfigNames::ForeignFileRepos ), [ $options->get( MainConfigNames::LocalFileRepo ) ] ); |
| 125 |
foreach ( $repos as $info ) { |
| 126 |
$backendName = $info['backend']; |
| 127 |
if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
$repoName = $info['name']; |
| 131 |
|
| 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'; |
| 137 |
|
| 138 |
$autoBackends[] = [ |
| 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" |
| 148 |
], |
| 149 |
'fileMode' => $info['fileMode'] ?? 0644, |
| 150 |
'directoryMode' => $options->get( MainConfigNames::DirectoryMode ), |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
$this->register( $autoBackends, $readOnlyMode->getConfiguredReason() ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
protected function register( array $configs, $readOnlyReason = null ) { |
| 165 |
foreach ( $configs as $config ) { |
| 166 |
if ( !isset( $config['name'] ) ) { |
| 167 |
throw new InvalidArgumentException( "Cannot register a backend with no name." ); |
| 168 |
} |
| 169 |
$name = $config['name']; |
| 170 |
if ( isset( $this->backends[$name] ) ) { |
| 171 |
throw new LogicException( "Backend with name '$name' already registered." ); |
| 172 |
} elseif ( !isset( $config['class'] ) ) { |
| 173 |
throw new InvalidArgumentException( "Backend with name '$name' has no class." ); |
| 174 |
} |
| 175 |
$class = $config['class']; |
| 176 |
|
| 177 |
$config['domainId'] ??= $config['wikiId'] ?? $this->options->get( 'fallbackWikiId' ); |
| 178 |
$config['readOnly'] ??= $readOnlyReason; |
| 179 |
|
| 180 |
unset( $config['class'] ); |
| 181 |
$this->backends[$name] = [ |
| 182 |
'class' => $class, |
| 183 |
'config' => $config, |
| 184 |
'instance' => null |
| 185 |
]; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function get( $name ) { |
| 196 |
|
| 197 |
if ( !isset( $this->backends[$name]['instance'] ) ) { |
| 198 |
$config = $this->config( $name ); |
| 199 |
|
| 200 |
$class = $config['class']; |
| 201 |
|
| 202 |
if ( $class === FileBackendMultiWrite::class || $class === \FileBackendMultiWrite::class ) { |
| 203 |
|
| 204 |
foreach ( $config['backends'] as $index => $beConfig ) { |
| 205 |
if ( isset( $beConfig['template'] ) ) { |
| 206 |
|
| 207 |
|
| 208 |
$config['backends'][$index] += $this->config( $beConfig['template'] ); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$this->backends[$name]['instance'] = new $class( $config ); |
| 214 |
} |
| 215 |
|
| 216 |
return $this->backends[$name]['instance']; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function config( $name ) { |
| 226 |
if ( !isset( $this->backends[$name] ) ) { |
| 227 |
throw new InvalidArgumentException( "No backend defined with the name '$name'." ); |
| 228 |
} |
| 229 |
|
| 230 |
$config = $this->backends[$name]['config']; |
| 231 |
|
| 232 |
return array_merge( |
| 233 |
|
| 234 |
[ |
| 235 |
'mimeCallback' => [ $this, 'guessMimeInternal' ], |
| 236 |
'obResetFunc' => 'wfResetOutputBuffers', |
| 237 |
'streamMimeFunc' => [ StreamFile::class, 'contentTypeFromPath' ], |
| 238 |
'tmpFileFactory' => $this->tmpFileFactory, |
| 239 |
'statusWrapper' => [ Status::class, 'wrap' ], |
| 240 |
'wanCache' => $this->wanCache, |
| 241 |
'srvCache' => $this->srvCache, |
| 242 |
'logger' => LoggerFactory::getInstance( 'FileOperation' ), |
| 243 |
'profiler' => static function ( $section ) { |
| 244 |
return Profiler::instance()->scopedProfileIn( $section ); |
| 245 |
} |
| 246 |
], |
| 247 |
|
| 248 |
$config, |
| 249 |
|
| 250 |
[ |
| 251 |
'class' => $this->backends[$name]['class'], |
| 252 |
'lockManager' => |
| 253 |
$this->lmgFactory->getLockManagerGroup( $config['domainId'] ) |
| 254 |
->get( $config['lockManager'] ), |
| 255 |
] |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function backendFromPath( $storagePath ) { |
| 266 |
[ $backend, , ] = FileBackend::splitStoragePath( $storagePath ); |
| 267 |
if ( $backend !== null && isset( $this->backends[$backend] ) ) { |
| 268 |
return $this->get( $backend ); |
| 269 |
} |
| 270 |
|
| 271 |
return null; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function guessMimeInternal( $storagePath, $content, $fsPath ) { |
| 282 |
|
| 283 |
$ext = FileBackend::extensionFromPath( $storagePath ); |
| 284 |
$type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext ); |
| 285 |
|
| 286 |
if ( !$type && $fsPath ) { |
| 287 |
$type = $this->mimeAnalyzer->guessMimeType( $fsPath, false ); |
| 288 |
} elseif ( !$type && $content !== null && $content !== '' ) { |
| 289 |
$tmpFile = $this->tmpFileFactory->newTempFSFile( 'mime_', '' ); |
| 290 |
file_put_contents( $tmpFile->getPath(), $content ); |
| 291 |
$type = $this->mimeAnalyzer->guessMimeType( $tmpFile->getPath(), false ); |
| 292 |
} |
| 293 |
return $type ?: 'unknown/unknown'; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
class_alias( FileBackendGroup::class, 'FileBackendGroup' ); |