MediaWiki  master
RestbaseVirtualRESTService.php
Go to the documentation of this file.
1 <?php
51  public function __construct( array $params ) {
52  // set up defaults and merge them with the given params
53  $mparams = array_merge( [
54  'name' => 'restbase',
55  'url' => 'http://localhost:7231/',
56  'domain' => 'localhost',
57  'timeout' => 100,
58  'forwardCookies' => false,
59  'HTTPProxy' => null,
60  'parsoidCompat' => false,
61  'fixedUrl' => false,
62  ], $params );
63  // Ensure that the url parameter has a trailing slash.
64  // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal url has default value
65  if ( substr( $mparams['url'], -1 ) !== '/' ) {
66  $mparams['url'] .= '/';
67  }
68  // Ensure the correct domain format: strip protocol, port,
69  // and trailing slash if present. This lets us use
70  // $wgCanonicalServer as a default value, which is very convenient.
71  $mparams['domain'] = preg_replace(
72  '/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
73  '$3',
74  // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal domain has default value
75  $mparams['domain']
76  );
77  parent::__construct( $mparams );
78  }
79 
84  public function onRequests( array $reqs, Closure $idGenFunc ) {
85  if ( $this->params['parsoidCompat'] ) {
86  return $this->onParsoidRequests( $reqs, $idGenFunc );
87  }
88 
89  $result = [];
90  foreach ( $reqs as $key => $req ) {
91  if ( $this->params['fixedUrl'] ) {
92  $version = explode( '/', $req['url'] )[1];
93  $req['url'] =
94  str_replace( '#version#', $version, $this->params['url'] ) .
95  preg_replace( '#^local/v./#', '', $req['url'] );
96  } else {
97  // replace /local/ with the current domain
98  $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
99  // and prefix it with the service URL
100  $req['url'] = $this->params['url'] . $req['url'];
101  }
102 
103  // set the appropriate proxy, timeout and headers
104  if ( $this->params['HTTPProxy'] ) {
105  $req['proxy'] = $this->params['HTTPProxy'];
106  }
107  if ( $this->params['timeout'] != null ) {
108  $req['reqTimeout'] = $this->params['timeout'];
109  }
110  if ( $this->params['forwardCookies'] ) {
111  $req['headers']['Cookie'] = $this->params['forwardCookies'];
112  }
113  $result[$key] = $req;
114  }
115 
116  return $result;
117  }
118 
126  public function onParsoidRequests( array $reqs, Closure $idGeneratorFunc ) {
127  $result = [];
128  foreach ( $reqs as $key => $req ) {
129  $version = explode( '/', $req['url'] )[1];
130  if ( $version === 'v3' ) {
131  $result[$key] = $this->onParsoid3Request( $req, $idGeneratorFunc );
132  } else {
133  throw new Exception( "Only Parsoid v3 is supported." );
134  }
135  }
136 
137  return $result;
138  }
139 
158  public function onParsoid3Request( array $req, Closure $idGeneratorFunc ) {
159  $parts = explode( '/', $req['url'] );
160  list(
161  $targetWiki, // 'local'
162  $version, // 'v3'
163  $action, // 'transform' or 'page'
164  $format, // 'html' or 'wikitext'
165  // $title, // optional
166  // $revision, // optional
167  ) = $parts;
168  if ( $targetWiki !== 'local' ) {
169  throw new Exception( "Only 'local' target wiki is currently supported" );
170  } elseif ( $version !== 'v3' ) {
171  throw new Exception( "Version mismatch: should not happen." );
172  }
173  // replace /local/ with the current domain, change v3 to v1,
174  $req['url'] = preg_replace( '#^local/v3/#', $this->params['domain'] . '/v1/', $req['url'] );
175  // and prefix it with the service URL
176  $req['url'] = $this->params['url'] . $req['url'];
177  // set the appropriate proxy, timeout and headers
178  if ( $this->params['HTTPProxy'] ) {
179  $req['proxy'] = $this->params['HTTPProxy'];
180  }
181  if ( $this->params['timeout'] != null ) {
182  $req['reqTimeout'] = $this->params['timeout'];
183  }
184  if ( $this->params['forwardCookies'] ) {
185  $req['headers']['Cookie'] = $this->params['forwardCookies'];
186  }
187 
188  return $req;
189  }
190 
191 }
Virtual HTTP service client for RESTBase.
onRequests(array $reqs, Closure $idGenFunc)
Prepare virtual HTTP(S) requests (for this service) for execution.This method should mangle any of th...
onParsoidRequests(array $reqs, Closure $idGeneratorFunc)
Remaps Parsoid v3 requests to RESTBase v1 requests.
onParsoid3Request(array $req, Closure $idGeneratorFunc)
Remap a Parsoid v3 request to a RESTBase v1 request.
__construct(array $params)
Example RESTBase v1 requests: GET /local/v1/page/html/{title}{/revision} POST /local/v1/transform/htm...
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.
array $params
Key/value map.