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