MediaWiki  master
ExternalStore.php
Go to the documentation of this file.
1 <?php
7 
56  public static function getStoreObject( $proto, array $params = [] ) {
57  return MediaWikiServices::getInstance()
58  ->getExternalStoreFactory()
59  ->getStoreObject( $proto, $params );
60  }
61 
70  public static function fetchFromURL( $url, array $params = [] ) {
71  $parts = explode( '://', $url, 2 );
72  if ( count( $parts ) != 2 ) {
73  return false; // invalid URL
74  }
75 
76  list( $proto, $path ) = $parts;
77  if ( $path == '' ) { // bad URL
78  return false;
79  }
80 
81  $store = self::getStoreObject( $proto, $params );
82  if ( $store === false ) {
83  return false;
84  }
85 
86  return $store->fetchFromURL( $url );
87  }
88 
97  public static function batchFetchFromURLs( array $urls ) {
98  $batches = [];
99  foreach ( $urls as $url ) {
100  $scheme = parse_url( $url, PHP_URL_SCHEME );
101  if ( $scheme ) {
102  $batches[$scheme][] = $url;
103  }
104  }
105  $retval = [];
106  foreach ( $batches as $proto => $batchedUrls ) {
107  $store = self::getStoreObject( $proto );
108  if ( $store === false ) {
109  continue;
110  }
111  $retval += $store->batchFetchFromURLs( $batchedUrls );
112  }
113  // invalid, not found, db dead, etc.
114  $missing = array_diff( $urls, array_keys( $retval ) );
115  if ( $missing ) {
116  foreach ( $missing as $url ) {
117  $retval[$url] = false;
118  }
119  }
120 
121  return $retval;
122  }
123 
135  public static function insert( $url, $data, array $params = [] ) {
136  $parts = explode( '://', $url, 2 );
137  if ( count( $parts ) != 2 ) {
138  return false; // invalid URL
139  }
140 
141  list( $proto, $path ) = $parts;
142  if ( $path == '' ) { // bad URL
143  return false;
144  }
145 
146  $store = self::getStoreObject( $proto, $params );
147  if ( $store === false ) {
148  return false;
149  } else {
150  return $store->store( $path, $data );
151  }
152  }
153 
165  public static function insertToDefault( $data, array $params = [] ) {
167 
168  return self::insertWithFallback( (array)$wgDefaultExternalStore, $data, $params );
169  }
170 
183  public static function insertWithFallback( array $tryStores, $data, array $params = [] ) {
184  $error = false;
185  while ( count( $tryStores ) > 0 ) {
186  $index = mt_rand( 0, count( $tryStores ) - 1 );
187  $storeUrl = $tryStores[$index];
188  wfDebug( __METHOD__ . ": trying $storeUrl\n" );
189  list( $proto, $path ) = explode( '://', $storeUrl, 2 );
190  $store = self::getStoreObject( $proto, $params );
191  if ( $store === false ) {
192  throw new MWException( "Invalid external storage protocol - $storeUrl" );
193  }
194 
195  try {
196  if ( $store->isReadOnly( $path ) ) {
197  $msg = 'read only';
198  } else {
199  $url = $store->store( $path, $data );
200  if ( $url !== false ) {
201  return $url; // a store accepted the write; done!
202  }
203  $msg = 'operation failed';
204  }
205  } catch ( Exception $error ) {
206  $msg = 'caught exception';
207  }
208 
209  unset( $tryStores[$index] ); // Don't try this one again!
210  $tryStores = array_values( $tryStores ); // Must have consecutive keys
211  wfDebugLog( 'ExternalStorage',
212  "Unable to store text to external storage $storeUrl ($msg)" );
213  }
214  // All stores failed
215  if ( $error ) {
216  throw $error; // rethrow the last error
217  } else {
218  throw new MWException( "Unable to store text to external storage" );
219  }
220  }
221 
226  public static function defaultStoresAreReadOnly() {
228 
229  $tryStores = (array)$wgDefaultExternalStore;
230  if ( !$tryStores ) {
231  return false; // no stores exists which can be "read only"
232  }
233 
234  foreach ( $tryStores as $storeUrl ) {
235  list( $proto, $path ) = explode( '://', $storeUrl, 2 );
236  $store = self::getStoreObject( $proto, [] );
237  if ( !$store->isReadOnly( $path ) ) {
238  return false; // at least one store is not read-only
239  }
240  }
241 
242  return true; // all stores are read-only
243  }
244 
251  public static function insertToForeignDefault( $data, $wiki ) {
252  return self::insertToDefault( $data, [ 'wiki' => $wiki ] );
253  }
254 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
static insertWithFallback(array $tryStores, $data, array $params=[])
Like insert() above, but does more of the work for us.
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
array $wgDefaultExternalStore
The place to put new revisions, false to put them in the local text table.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static batchFetchFromURLs(array $urls)
Fetch data from multiple URLs with a minimum of round trips.
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
static insertToDefault( $data, array $params=[])
Like insert() above, but does more of the work for us.
static insertToForeignDefault( $data, $wiki)
static defaultStoresAreReadOnly()
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$params
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
static getStoreObject( $proto, array $params=[])
Get an external store object of the given type, with the given parameters.
static insert( $url, $data, array $params=[])
Store a data item to an external store, identified by a partial URL The protocol part is used to iden...
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
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
static fetchFromURL( $url, array $params=[])
Fetch data from given URL.