MediaWiki  1.29.2
VirtualRESTServiceClient.php
Go to the documentation of this file.
1 <?php
48  private $http;
50  private $instances = [];
51 
52  const VALID_MOUNT_REGEX = '#^/[0-9a-z]+/([0-9a-z]+/)*$#';
53 
57  public function __construct( MultiHttpClient $http ) {
58  $this->http = $http;
59  }
60 
71  public function mount( $prefix, $instance ) {
72  if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
73  throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
74  } elseif ( isset( $this->instances[$prefix] ) ) {
75  throw new UnexpectedValueException( "A service is already mounted on '$prefix'." );
76  }
77  if ( !( $instance instanceof VirtualRESTService ) ) {
78  if ( !isset( $instance['class'] ) || !isset( $instance['config'] ) ) {
79  throw new UnexpectedValueException( "Missing 'class' or 'config' ('$prefix')." );
80  }
81  }
82  $this->instances[$prefix] = $instance;
83  }
84 
90  public function unmount( $prefix ) {
91  if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
92  throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
93  } elseif ( !isset( $this->instances[$prefix] ) ) {
94  throw new UnexpectedValueException( "No service is mounted on '$prefix'." );
95  }
96  unset( $this->instances[$prefix] );
97  }
98 
105  public function getMountAndService( $path ) {
106  $cmpFunc = function( $a, $b ) {
107  $al = substr_count( $a, '/' );
108  $bl = substr_count( $b, '/' );
109  if ( $al === $bl ) {
110  return 0; // should not actually happen
111  }
112  return ( $al < $bl ) ? 1 : -1; // largest prefix first
113  };
114 
115  $matches = []; // matching prefixes (mount points)
116  foreach ( $this->instances as $prefix => $unused ) {
117  if ( strpos( $path, $prefix ) === 0 ) {
118  $matches[] = $prefix;
119  }
120  }
121  usort( $matches, $cmpFunc );
122 
123  // Return the most specific prefix and corresponding service
124  return $matches
125  ? [ $matches[0], $this->getInstance( $matches[0] ) ]
126  : [ null, null ];
127  }
128 
145  public function run( array $req ) {
146  return $this->runMulti( [ $req ] )[0];
147  }
148 
167  public function runMulti( array $reqs ) {
168  foreach ( $reqs as $index => &$req ) {
169  if ( isset( $req[0] ) ) {
170  $req['method'] = $req[0]; // short-form
171  unset( $req[0] );
172  }
173  if ( isset( $req[1] ) ) {
174  $req['url'] = $req[1]; // short-form
175  unset( $req[1] );
176  }
177  $req['chain'] = []; // chain or list of replaced requests
178  }
179  unset( $req ); // don't assign over this by accident
180 
181  $curUniqueId = 0;
182  $armoredIndexMap = []; // (original index => new index)
183 
184  $doneReqs = []; // (index => request)
185  $executeReqs = []; // (index => request)
186  $replaceReqsByService = []; // (prefix => index => request)
187  $origPending = []; // (index => 1) for original requests
188 
189  foreach ( $reqs as $origIndex => $req ) {
190  // Re-index keys to consecutive integers (they will be swapped back later)
191  $index = $curUniqueId++;
192  $armoredIndexMap[$origIndex] = $index;
193  $origPending[$index] = 1;
194  if ( preg_match( '#^(http|ftp)s?://#', $req['url'] ) ) {
195  // Absolute FTP/HTTP(S) URL, run it as normal
196  $executeReqs[$index] = $req;
197  } else {
198  // Must be a virtual HTTP URL; resolve it
199  list( $prefix, $service ) = $this->getMountAndService( $req['url'] );
200  if ( !$service ) {
201  throw new UnexpectedValueException( "Path '{$req['url']}' has no service." );
202  }
203  // Set the URL to the mount-relative portion
204  $req['url'] = substr( $req['url'], strlen( $prefix ) );
205  $replaceReqsByService[$prefix][$index] = $req;
206  }
207  }
208 
209  // Function to get IDs that won't collide with keys in $armoredIndexMap
210  $idFunc = function() use ( &$curUniqueId ) {
211  return $curUniqueId++;
212  };
213 
214  $rounds = 0;
215  do {
216  if ( ++$rounds > 5 ) { // sanity
217  throw new Exception( "Too many replacement rounds detected. Aborting." );
218  }
219  // Track requests executed this round that have a prefix/service.
220  // Note that this also includes requests where 'response' was forced.
221  $checkReqIndexesByPrefix = [];
222  // Resolve the virtual URLs valid and qualified HTTP(S) URLs
223  // and add any required authentication headers for the backend.
224  // Services can also replace requests with new ones, either to
225  // defer the original or to set a proxy response to the original.
226  $newReplaceReqsByService = [];
227  foreach ( $replaceReqsByService as $prefix => $servReqs ) {
228  $service = $this->getInstance( $prefix );
229  foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) {
230  // Services use unique IDs for replacement requests
231  if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
232  // A current or original request which was not modified
233  } else {
234  // Replacement request that will convert to original requests
235  $newReplaceReqsByService[$prefix][$index] = $req;
236  }
237  if ( isset( $req['response'] ) ) {
238  // Replacement requests with pre-set responses should not execute
239  unset( $executeReqs[$index] );
240  unset( $origPending[$index] );
241  $doneReqs[$index] = $req;
242  } else {
243  // Original or mangled request included
244  $executeReqs[$index] = $req;
245  }
246  $checkReqIndexesByPrefix[$prefix][$index] = 1;
247  }
248  }
249  // Run the actual work HTTP requests
250  foreach ( $this->http->runMulti( $executeReqs ) as $index => $ranReq ) {
251  $doneReqs[$index] = $ranReq;
252  unset( $origPending[$index] );
253  }
254  $executeReqs = [];
255  // Services can also replace requests with new ones, either to
256  // defer the original or to set a proxy response to the original.
257  // Any replacement requests executed above will need to be replaced
258  // with new requests (eventually the original). The responses can be
259  // forced by setting 'response' rather than actually be sent over the wire.
260  $newReplaceReqsByService = [];
261  foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
262  $service = $this->getInstance( $prefix );
263  // $doneReqs actually has the requests (with 'response' set)
264  $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
265  foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
266  // Services use unique IDs for replacement requests
267  if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
268  // A current or original request which was not modified
269  } else {
270  // Replacement requests with pre-set responses should not execute
271  $newReplaceReqsByService[$prefix][$index] = $req;
272  }
273  if ( isset( $req['response'] ) ) {
274  // Replacement requests with pre-set responses should not execute
275  unset( $origPending[$index] );
276  $doneReqs[$index] = $req;
277  } else {
278  // Update the request in case it was mangled
279  $executeReqs[$index] = $req;
280  }
281  }
282  }
283  // Update index of requests to inspect for replacement
284  $replaceReqsByService = $newReplaceReqsByService;
285  } while ( count( $origPending ) );
286 
287  $responses = [];
288  // Update $reqs to include 'response' and normalized request 'headers'.
289  // This maintains the original order of $reqs.
290  foreach ( $reqs as $origIndex => $req ) {
291  $index = $armoredIndexMap[$origIndex];
292  if ( !isset( $doneReqs[$index] ) ) {
293  throw new UnexpectedValueException( "Response for request '$index' is NULL." );
294  }
295  $responses[$origIndex] = $doneReqs[$index]['response'];
296  }
297 
298  return $responses;
299  }
300 
305  private function getInstance( $prefix ) {
306  if ( !isset( $this->instances[$prefix] ) ) {
307  throw new RuntimeException( "No service registered at prefix '{$prefix}'." );
308  }
309 
310  if ( !( $this->instances[$prefix] instanceof VirtualRESTService ) ) {
311  $config = $this->instances[$prefix]['config'];
312  $class = $this->instances[$prefix]['class'];
313  $service = new $class( $config );
314  if ( !( $service instanceof VirtualRESTService ) ) {
315  throw new UnexpectedValueException( "Registered service has the wrong class." );
316  }
317  $this->instances[$prefix] = $service;
318  }
319 
320  return $this->instances[$prefix];
321  }
322 }
VirtualRESTServiceClient\run
run(array $req)
Execute a virtual HTTP(S) request.
Definition: VirtualRESTServiceClient.php:145
MultiHttpClient
Class to handle concurrent HTTP requests.
Definition: MultiHttpClient.php:45
VirtualRESTServiceClient\$instances
array $instances
Map of (prefix => VirtualRESTService|array)
Definition: VirtualRESTServiceClient.php:50
captcha-old.count
count
Definition: captcha-old.py:225
VirtualRESTServiceClient\$http
MultiHttpClient $http
Definition: VirtualRESTServiceClient.php:48
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$req
this hook is for auditing only $req
Definition: hooks.txt:990
http
Apache License January http
Definition: APACHE-LICENSE-2.0.txt:3
php
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
VirtualRESTServiceClient\runMulti
runMulti(array $reqs)
Execute a set of virtual HTTP(S) requests concurrently.
Definition: VirtualRESTServiceClient.php:167
VirtualRESTServiceClient\VALID_MOUNT_REGEX
const VALID_MOUNT_REGEX
Definition: VirtualRESTServiceClient.php:52
$matches
$matches
Definition: NoLocalSettings.php:24
VirtualRESTServiceClient
Virtual HTTP service client loosely styled after a Virtual File System.
Definition: VirtualRESTServiceClient.php:46
VirtualRESTServiceClient\mount
mount( $prefix, $instance)
Map a prefix to service handler.
Definition: VirtualRESTServiceClient.php:71
list
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
VirtualRESTServiceClient\getMountAndService
getMountAndService( $path)
Get the prefix and service that a virtual path is serviced by.
Definition: VirtualRESTServiceClient.php:105
VirtualRESTServiceClient\getInstance
getInstance( $prefix)
Definition: VirtualRESTServiceClient.php:305
VirtualRESTServiceClient\__construct
__construct(MultiHttpClient $http)
Definition: VirtualRESTServiceClient.php:57
VirtualRESTServiceClient\unmount
unmount( $prefix)
Unmap a prefix to service handler.
Definition: VirtualRESTServiceClient.php:90
$path
$path
Definition: NoLocalSettings.php:26
as
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
VirtualRESTService
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.
Definition: VirtualRESTService.php:36
array
the array() calling protocol came about after MediaWiki 1.4rc1.