MediaWiki  1.27.2
VirtualRESTServiceClient.php
Go to the documentation of this file.
1 <?php
48  protected $http;
50  protected $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 
67  public function mount( $prefix, VirtualRESTService $instance ) {
68  if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
69  throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
70  } elseif ( isset( $this->instances[$prefix] ) ) {
71  throw new UnexpectedValueException( "A service is already mounted on '$prefix'." );
72  }
73  $this->instances[$prefix] = $instance;
74  }
75 
81  public function unmount( $prefix ) {
82  if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
83  throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
84  } elseif ( !isset( $this->instances[$prefix] ) ) {
85  throw new UnexpectedValueException( "No service is mounted on '$prefix'." );
86  }
87  unset( $this->instances[$prefix] );
88  }
89 
96  public function getMountAndService( $path ) {
97  $cmpFunc = function( $a, $b ) {
98  $al = substr_count( $a, '/' );
99  $bl = substr_count( $b, '/' );
100  if ( $al === $bl ) {
101  return 0; // should not actually happen
102  }
103  return ( $al < $bl ) ? 1 : -1; // largest prefix first
104  };
105 
106  $matches = []; // matching prefixes (mount points)
107  foreach ( $this->instances as $prefix => $service ) {
108  if ( strpos( $path, $prefix ) === 0 ) {
109  $matches[] = $prefix;
110  }
111  }
112  usort( $matches, $cmpFunc );
113 
114  // Return the most specific prefix and corresponding service
115  return isset( $matches[0] )
116  ? [ $matches[0], $this->instances[$matches[0]] ]
117  : [ null, null ];
118  }
119 
136  public function run( array $req ) {
137  return $this->runMulti( [ $req ] )[0];
138  }
139 
158  public function runMulti( array $reqs ) {
159  foreach ( $reqs as $index => &$req ) {
160  if ( isset( $req[0] ) ) {
161  $req['method'] = $req[0]; // short-form
162  unset( $req[0] );
163  }
164  if ( isset( $req[1] ) ) {
165  $req['url'] = $req[1]; // short-form
166  unset( $req[1] );
167  }
168  $req['chain'] = []; // chain or list of replaced requests
169  }
170  unset( $req ); // don't assign over this by accident
171 
172  $curUniqueId = 0;
173  $armoredIndexMap = []; // (original index => new index)
174 
175  $doneReqs = []; // (index => request)
176  $executeReqs = []; // (index => request)
177  $replaceReqsByService = []; // (prefix => index => request)
178  $origPending = []; // (index => 1) for original requests
179 
180  foreach ( $reqs as $origIndex => $req ) {
181  // Re-index keys to consecutive integers (they will be swapped back later)
182  $index = $curUniqueId++;
183  $armoredIndexMap[$origIndex] = $index;
184  $origPending[$index] = 1;
185  if ( preg_match( '#^(http|ftp)s?://#', $req['url'] ) ) {
186  // Absolute FTP/HTTP(S) URL, run it as normal
187  $executeReqs[$index] = $req;
188  } else {
189  // Must be a virtual HTTP URL; resolve it
190  list( $prefix, $service ) = $this->getMountAndService( $req['url'] );
191  if ( !$service ) {
192  throw new UnexpectedValueException( "Path '{$req['url']}' has no service." );
193  }
194  // Set the URL to the mount-relative portion
195  $req['url'] = substr( $req['url'], strlen( $prefix ) );
196  $replaceReqsByService[$prefix][$index] = $req;
197  }
198  }
199 
200  // Function to get IDs that won't collide with keys in $armoredIndexMap
201  $idFunc = function() use ( &$curUniqueId ) {
202  return $curUniqueId++;
203  };
204 
205  $rounds = 0;
206  do {
207  if ( ++$rounds > 5 ) { // sanity
208  throw new Exception( "Too many replacement rounds detected. Aborting." );
209  }
210  // Track requests executed this round that have a prefix/service.
211  // Note that this also includes requests where 'response' was forced.
212  $checkReqIndexesByPrefix = [];
213  // Resolve the virtual URLs valid and qualified HTTP(S) URLs
214  // and add any required authentication headers for the backend.
215  // Services can also replace requests with new ones, either to
216  // defer the original or to set a proxy response to the original.
217  $newReplaceReqsByService = [];
218  foreach ( $replaceReqsByService as $prefix => $servReqs ) {
219  $service = $this->instances[$prefix];
220  foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) {
221  // Services use unique IDs for replacement requests
222  if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
223  // A current or original request which was not modified
224  } else {
225  // Replacement request that will convert to original requests
226  $newReplaceReqsByService[$prefix][$index] = $req;
227  }
228  if ( isset( $req['response'] ) ) {
229  // Replacement requests with pre-set responses should not execute
230  unset( $executeReqs[$index] );
231  unset( $origPending[$index] );
232  $doneReqs[$index] = $req;
233  } else {
234  // Original or mangled request included
235  $executeReqs[$index] = $req;
236  }
237  $checkReqIndexesByPrefix[$prefix][$index] = 1;
238  }
239  }
240  // Update index of requests to inspect for replacement
241  $replaceReqsByService = $newReplaceReqsByService;
242  // Run the actual work HTTP requests
243  foreach ( $this->http->runMulti( $executeReqs ) as $index => $ranReq ) {
244  $doneReqs[$index] = $ranReq;
245  unset( $origPending[$index] );
246  }
247  $executeReqs = [];
248  // Services can also replace requests with new ones, either to
249  // defer the original or to set a proxy response to the original.
250  // Any replacement requests executed above will need to be replaced
251  // with new requests (eventually the original). The responses can be
252  // forced by setting 'response' rather than actually be sent over the wire.
253  $newReplaceReqsByService = [];
254  foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
255  $service = $this->instances[$prefix];
256  // $doneReqs actually has the requests (with 'response' set)
257  $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
258  foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
259  // Services use unique IDs for replacement requests
260  if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
261  // A current or original request which was not modified
262  } else {
263  // Replacement requests with pre-set responses should not execute
264  $newReplaceReqsByService[$prefix][$index] = $req;
265  }
266  if ( isset( $req['response'] ) ) {
267  // Replacement requests with pre-set responses should not execute
268  unset( $origPending[$index] );
269  $doneReqs[$index] = $req;
270  } else {
271  // Update the request in case it was mangled
272  $executeReqs[$index] = $req;
273  }
274  }
275  }
276  // Update index of requests to inspect for replacement
277  $replaceReqsByService = $newReplaceReqsByService;
278  } while ( count( $origPending ) );
279 
280  $responses = [];
281  // Update $reqs to include 'response' and normalized request 'headers'.
282  // This maintains the original order of $reqs.
283  foreach ( $reqs as $origIndex => $req ) {
284  $index = $armoredIndexMap[$origIndex];
285  if ( !isset( $doneReqs[$index] ) ) {
286  throw new UnexpectedValueException( "Response for request '$index' is NULL." );
287  }
288  $responses[$origIndex] = $doneReqs[$index]['response'];
289  }
290 
291  return $responses;
292  }
293 }
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
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Virtual HTTP service client loosely styled after a Virtual File System.
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.
Apache License January http
mount($prefix, VirtualRESTService $instance)
Map a prefix to service handler.
getMountAndService($path)
Get the prefix and service that a virtual path is serviced by.
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
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
this hook is for auditing only $req
Definition: hooks.txt:965
runMulti(array $reqs)
Execute a set of virtual HTTP(S) requests concurrently.
unmount($prefix)
Unmap a prefix to service handler.
VirtualRESTService[] $instances
Map of (prefix => VirtualRESTService)
__construct(MultiHttpClient $http)
run(array $req)
Execute a virtual HTTP(S) request.
Class to handle concurrent HTTP requests.
$matches