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