MediaWiki  master
ExternalStoreFactory.php
Go to the documentation of this file.
1 <?php
11 
15 class ExternalStoreFactory implements LoggerAwareInterface {
17  private $protocols;
19  private $writeBaseUrls;
21  private $localDomainId;
23  private $logger;
24 
31  public function __construct(
32  array $externalStores,
33  array $defaultStores,
35  LoggerInterface $logger = null
36  ) {
37  Assert::parameterType( 'string', $localDomainId, '$localDomainId' );
38 
39  $this->protocols = array_map( 'strtolower', $externalStores );
40  $this->writeBaseUrls = $defaultStores;
41  $this->localDomainId = $localDomainId;
42  $this->logger = $logger ?: new NullLogger();
43  }
44 
45  public function setLogger( LoggerInterface $logger ) {
46  $this->logger = $logger;
47  }
48 
53  public function getProtocols() {
54  return $this->protocols;
55  }
56 
61  public function getWriteBaseUrls() {
62  return $this->writeBaseUrls;
63  }
64 
77  public function getStore( $proto, array $params = [] ) {
78  $protoLowercase = strtolower( $proto ); // normalize
79  if ( !$this->protocols || !in_array( $protoLowercase, $this->protocols ) ) {
80  throw new ExternalStoreException( "Protocol '$proto' is not enabled." );
81  }
82 
83  $class = 'ExternalStore' . ucfirst( $proto );
84  if ( isset( $params['wiki'] ) ) {
85  $params += [ 'domain' => $params['wiki'] ]; // b/c
86  }
87  if ( !isset( $params['domain'] ) || $params['domain'] === false ) {
88  $params['domain'] = $this->localDomainId; // default
89  $params['isDomainImplicit'] = true; // b/c for ExternalStoreDB
90  }
91  // @TODO: ideally, this class should not hardcode what classes need what backend factory
92  // objects. For now, inject the factory instances into __construct() for those that do.
93  if ( $protoLowercase === 'db' ) {
94  $params['lbFactory'] = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
95  } elseif ( $protoLowercase === 'mwstore' ) {
96  $params['fbGroup'] = FileBackendGroup::singleton();
97  }
98  $params['logger'] = $this->logger;
99 
100  if ( !class_exists( $class ) ) {
101  throw new ExternalStoreException( "Class '$class' is not defined." );
102  }
103 
104  // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading
105  return new $class( $params );
106  }
107 
121  public function getStoreForUrl( $url, array $params = [] ) {
122  list( $proto, $path ) = self::splitStorageUrl( $url );
123  if ( $path == '' ) { // bad URL
124  throw new ExternalStoreException( "Invalid URL '$url'" );
125  }
126 
127  return $this->getStore( $proto, $params );
128  }
129 
138  public function getStoreLocationFromUrl( $url ) {
139  list( , $location ) = self::splitStorageUrl( $url );
140  if ( $location == '' ) { // bad URL
141  throw new ExternalStoreException( "Invalid URL '$url'" );
142  }
143 
144  return $location;
145  }
146 
153  public function getUrlsByProtocol( array $urls ) {
154  $urlsByProtocol = [];
155  foreach ( $urls as $url ) {
156  list( $proto, ) = self::splitStorageUrl( $url );
157  $urlsByProtocol[$proto][] = $url;
158  }
159 
160  return $urlsByProtocol;
161  }
162 
168  private static function splitStorageUrl( $storeUrl ) {
169  $parts = explode( '://', $storeUrl );
170  if ( count( $parts ) != 2 || $parts[0] === '' || $parts[1] === '' ) {
171  throw new ExternalStoreException( "Invalid storage URL '$storeUrl'" );
172  }
173 
174  return $parts;
175  }
176 }
setLogger(LoggerInterface $logger)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
getStoreForUrl( $url, array $params=[])
Get the ExternalStoreMedium for a given URL.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
string [] $protocols
List of storage access protocols.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
string $localDomainId
Default database domain to store content under.
__construct(array $externalStores, array $defaultStores, $localDomainId, LoggerInterface $logger=null)
getStoreLocationFromUrl( $url)
Get the location within the appropriate store for a given a URL.
getStore( $proto, array $params=[])
Get an external store object of the given type, with the given parameters.
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
string [] $writeBaseUrls
List of base storage URLs that define locations for writes.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static splitStorageUrl( $storeUrl)