MediaWiki REL1_37
ExternalStoreFactory.php
Go to the documentation of this file.
1<?php
7use Psr\Log\LoggerAwareInterface;
8use Psr\Log\LoggerInterface;
9use Psr\Log\NullLogger;
10
14class ExternalStoreFactory implements LoggerAwareInterface {
16 private $protocols;
22 private $logger;
23
30 public function __construct(
31 array $externalStores,
32 array $defaultStores,
33 string $localDomainId,
34 LoggerInterface $logger = null
35 ) {
36 $this->protocols = array_map( 'strtolower', $externalStores );
37 $this->writeBaseUrls = $defaultStores;
38 $this->localDomainId = $localDomainId;
39 $this->logger = $logger ?: new NullLogger();
40 }
41
42 public function setLogger( LoggerInterface $logger ) {
43 $this->logger = $logger;
44 }
45
50 public function getProtocols() {
51 return $this->protocols;
52 }
53
58 public function getWriteBaseUrls() {
59 return $this->writeBaseUrls;
60 }
61
74 public function getStore( $proto, array $params = [] ) {
75 $protoLowercase = strtolower( $proto ); // normalize
76 if ( !$this->protocols || !in_array( $protoLowercase, $this->protocols ) ) {
77 throw new ExternalStoreException( "Protocol '$proto' is not enabled." );
78 }
79
80 $class = 'ExternalStore' . ucfirst( $proto );
81 if ( isset( $params['wiki'] ) ) {
82 $params += [ 'domain' => $params['wiki'] ]; // b/c
83 }
84 if ( !isset( $params['domain'] ) || $params['domain'] === false ) {
85 $params['domain'] = $this->localDomainId; // default
86 $params['isDomainImplicit'] = true; // b/c for ExternalStoreDB
87 }
88 // @TODO: ideally, this class should not hardcode what classes need what backend factory
89 // objects. For now, inject the factory instances into __construct() for those that do.
90 if ( $protoLowercase === 'db' ) {
91 $params['lbFactory'] = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
92 } elseif ( $protoLowercase === 'mwstore' ) {
93 $params['fbGroup'] = MediaWikiServices::getInstance()->getFileBackendGroup();
94 }
95 $params['logger'] = $this->logger;
96
97 if ( !class_exists( $class ) ) {
98 throw new ExternalStoreException( "Class '$class' is not defined." );
99 }
100
101 // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading
102 return new $class( $params );
103 }
104
118 public function getStoreForUrl( $url, array $params = [] ) {
119 list( $proto, $path ) = self::splitStorageUrl( $url );
120 if ( $path == '' ) { // bad URL
121 throw new ExternalStoreException( "Invalid URL '$url'" );
122 }
123
124 return $this->getStore( $proto, $params );
125 }
126
135 public function getStoreLocationFromUrl( $url ) {
136 list( , $location ) = self::splitStorageUrl( $url );
137 if ( $location == '' ) { // bad URL
138 throw new ExternalStoreException( "Invalid URL '$url'" );
139 }
140
141 return $location;
142 }
143
150 public function getUrlsByProtocol( array $urls ) {
151 $urlsByProtocol = [];
152 foreach ( $urls as $url ) {
153 list( $proto, ) = self::splitStorageUrl( $url );
154 $urlsByProtocol[$proto][] = $url;
155 }
156
157 return $urlsByProtocol;
158 }
159
165 private static function splitStorageUrl( $storeUrl ) {
166 $parts = explode( '://', $storeUrl );
167 if ( count( $parts ) != 2 || $parts[0] === '' || $parts[1] === '' ) {
168 throw new ExternalStoreException( "Invalid storage URL '$storeUrl'" );
169 }
170
171 return $parts;
172 }
173}
setLogger(LoggerInterface $logger)
string[] $protocols
List of storage access protocols.
string $localDomainId
Default database domain to store content under.
static splitStorageUrl( $storeUrl)
__construct(array $externalStores, array $defaultStores, string $localDomainId, LoggerInterface $logger=null)
getStore( $proto, array $params=[])
Get an external store object of the given type, with the given parameters.
getStoreForUrl( $url, array $params=[])
Get the ExternalStoreMedium for a given URL.
string[] $writeBaseUrls
List of base storage URLs that define locations for writes.
getStoreLocationFromUrl( $url)
Get the location within the appropriate store for a given a URL.
MediaWikiServices is the service locator for the application scope of MediaWiki.