4use Psr\Log\LoggerAwareInterface;
5use Psr\Log\LoggerInterface;
18 private $writeBaseUrls;
20 private $localDomainId;
33 array $externalStores,
35 string $localDomainId,
36 ?LoggerInterface $logger =
null
38 $this->protocols = array_map(
'strtolower', $externalStores );
39 $this->writeBaseUrls = $defaultStores;
40 $this->localDomainId = $localDomainId;
41 $this->logger = $logger ?:
new NullLogger();
44 public function setLogger( LoggerInterface $logger ) {
45 $this->logger = $logger;
53 return $this->protocols;
61 return $this->writeBaseUrls;
77 $cacheKey = $proto .
':' . json_encode(
$params );
78 if ( isset( $this->stores[$cacheKey] ) ) {
79 return $this->stores[$cacheKey];
81 $protoLowercase = strtolower( $proto );
82 if ( !$this->protocols || !in_array( $protoLowercase, $this->protocols ) ) {
86 if ( $protoLowercase ===
'db' ) {
87 $class =
'ExternalStoreDB';
89 $class =
'ExternalStore' . ucfirst( $proto );
91 if ( isset(
$params[
'wiki'] ) ) {
94 if ( !isset(
$params[
'domain'] ) ||
$params[
'domain'] ===
false ) {
95 $params[
'domain'] = $this->localDomainId;
96 $params[
'isDomainImplicit'] =
true;
100 if ( $protoLowercase ===
'db' ) {
101 $params[
'lbFactory'] = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
102 } elseif ( $protoLowercase ===
'mwstore' ) {
103 $params[
'fbGroup'] = MediaWikiServices::getInstance()->getFileBackendGroup();
105 $params[
'logger'] = $this->logger;
107 if ( !class_exists( $class ) ) {
112 $this->stores[$cacheKey] =
new $class(
$params );
113 return $this->stores[$cacheKey];
130 [ $proto,
$path ] = self::splitStorageUrl(
$url );
147 [ , $location ] = self::splitStorageUrl(
$url );
148 if ( $location ==
'' ) {
162 $urlsByProtocol = [];
163 foreach ( $urls as
$url ) {
164 [ $proto, ] = self::splitStorageUrl(
$url );
165 $urlsByProtocol[$proto][] =
$url;
168 return $urlsByProtocol;
176 private static function splitStorageUrl( $storeUrl ) {
177 $parts = explode(
'://', $storeUrl );
178 if ( count( $parts ) != 2 || $parts[0] ===
'' || $parts[1] ===
'' ) {
array $params
The job parameters.
getUrlsByProtocol(array $urls)
setLogger(LoggerInterface $logger)
getStore( $proto, array $params=[])
Get an external store object of the given type, with the given parameters.
__construct(array $externalStores, array $defaultStores, string $localDomainId, ?LoggerInterface $logger=null)
getStoreForUrl( $url, array $params=[])
Get the ExternalStoreMedium for a given URL.
getStoreLocationFromUrl( $url)
Get the location within the appropriate store for a given a URL.
Base class for external storage.