bc520aa1062d7c46e06118410a7ee92700f41325
[lhc/web/wiklou.git] / includes / libs / virtualrest / RestbaseVirtualRESTService.php
1 <?php
2 /**
3 * Virtual HTTP service client for RESTBase
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 */
20
21 /**
22 * Virtual REST service for RESTBase
23 * @since 1.25
24 */
25 class RestbaseVirtualRESTService extends VirtualRESTService {
26 /**
27 * Example RESTBase v1 requests:
28 * GET /local/v1/page/html/{title}{/revision}
29 * POST /local/v1/transform/html/to/wikitext{/title}{/revision}
30 * * body: array( 'html' => ... )
31 * POST /local/v1/transform/wikitext/to/html{/title}{/revision}
32 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'bodyOnly' => true/false )
33 *
34 * @param array $params Key/value map
35 * - url : RESTBase server URL
36 * - domain : Wiki domain to use
37 * - timeout : request timeout in seconds (optional)
38 * - forwardCookies : cookies to forward to RESTBase/Parsoid (as a Cookie
39 * header string) or false (optional)
40 * Note: forwardCookies will in the future be a boolean
41 * only, signifing request cookies should be forwarded
42 * to the service; the current state is due to the way
43 * VE handles this particular parameter
44 * - HTTPProxy : HTTP proxy to use (optional)
45 * - parsoidCompat : whether to parse URL as if they were meant for Parsoid
46 * boolean (optional)
47 */
48 public function __construct( array $params ) {
49 // set up defaults and merge them with the given params
50 $mparams = array_merge( array(
51 'url' => 'http://localhost:7231/',
52 'domain' => 'localhost',
53 'timeout' => 100,
54 'forwardCookies' => false,
55 'HTTPProxy' => null,
56 'parsoidCompat' => false
57 ), $params );
58 // Ensure that the url parameter has a trailing slash.
59 $mparams['url'] = preg_replace(
60 '#/?$#',
61 '/',
62 $mparams['url']
63 );
64 // Ensure the correct domain format: strip protocol, port,
65 // and trailing slash if present. This lets us use
66 // $wgCanonicalServer as a default value, which is very convenient.
67 $mparams['domain'] = preg_replace(
68 '/^(https?:\/\/)?([^\/:]+?)(:\d+)?\/?$/',
69 '$2',
70 $mparams['domain']
71 );
72 parent::__construct( $mparams );
73 }
74
75 public function onRequests( array $reqs, Closure $idGenFunc ) {
76
77 if ( $this->params['parsoidCompat'] ) {
78 return $this->onParsoidRequests( $reqs, $idGenFunc );
79 }
80
81 $result = array();
82 foreach ( $reqs as $key => $req ) {
83 // replace /local/ with the current domain
84 $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
85 // and prefix it with the service URL
86 $req['url'] = $this->params['url'] . $req['url'];
87 // set the appropriate proxy, timeout and headers
88 if ( $this->params['HTTPProxy'] ) {
89 $req['proxy'] = $this->params['HTTPProxy'];
90 }
91 if ( $this->params['timeout'] != null ) {
92 $req['reqTimeout'] = $this->params['timeout'];
93 }
94 if ( $this->params['forwardCookies'] ) {
95 $req['headers']['Cookie'] = $this->params['forwardCookies'];
96 }
97 $result[$key] = $req;
98 }
99
100 return $result;
101
102 }
103
104 /**
105 * Remaps Parsoid v1/v3 requests to RESTBase v1 requests.
106 */
107 public function onParsoidRequests( array $reqs, Closure $idGeneratorFunc ) {
108
109 $result = array();
110 foreach ( $reqs as $key => $req ) {
111 $parts = explode( '/', $req['url'] );
112 if ( $parts[1] === 'v3' ) {
113 $result[$key] = $this->onParsoid3Request( $req, $idGeneratorFunc );
114 } elseif ( $parts[1] === 'v1' ) {
115 $result[$key] = $this->onParsoid1Request( $req, $idGeneratorFunc );
116 } else {
117 throw new Exception( "Only v1 and v3 are supported." );
118 }
119 }
120
121 return $result;
122
123 }
124
125 /**
126 * Remap a Parsoid v1 request to a RESTBase v1 request.
127 *
128 * Example Parsoid v1 requests:
129 * GET /local/v1/page/$title/html/$oldid
130 * * $oldid is optional
131 * POST /local/v1/transform/html/to/wikitext/$title/$oldid
132 * * body: array( 'html' => ... )
133 * * $title and $oldid are optional
134 * POST /local/v1/transform/wikitext/to/html/$title
135 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body' => true/false )
136 * * $title is optional
137 *
138 * NOTE: the POST APIs aren't "real" Parsoid v1 APIs, they are just what
139 * Visual Editor "pretends" the V1 API is like. (See
140 * ParsoidVirtualRESTService.)
141 */
142 public function onParsoid1Request( array $req, Closure $idGeneratorFunc ) {
143 $parts = explode( '/', $req['url'] );
144 list(
145 $targetWiki, // 'local'
146 $version, // 'v1'
147 $reqType // 'page' or 'transform'
148 ) = $parts;
149 if ( $targetWiki !== 'local' ) {
150 throw new Exception( "Only 'local' target wiki is currently supported" );
151 } elseif ( $version !== 'v1' ) {
152 throw new Exception( "Version mismatch: should not happen." );
153 } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
154 throw new Exception( "Request type must be either 'page' or 'transform'" );
155 }
156 $req['url'] = $this->params['url'] . $this->params['domain'] . '/v1/' . $reqType . '/';
157 if ( $reqType === 'page' ) {
158 $title = $parts[3];
159 if ( $parts[4] !== 'html' ) {
160 throw new Exception( "Only 'html' output format is currently supported" );
161 }
162 $req['url'] .= 'html/' . $title;
163 if ( isset( $parts[5] ) ) {
164 $req['url'] .= '/' . $parts[5];
165 } elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) {
166 $req['url'] .= '/' . $req['query']['oldid'];
167 unset( $req['query']['oldid'] );
168 }
169 } elseif ( $reqType === 'transform' ) {
170 // from / to transform
171 $req['url'] .= $parts[3] . '/to/' . $parts[5];
172 // the title
173 if ( isset( $parts[6] ) ) {
174 $req['url'] .= '/' . $parts[6];
175 }
176 // revision id
177 if ( isset( $parts[7] ) ) {
178 $req['url'] .= '/' . $parts[7];
179 } elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) {
180 $req['url'] .= '/' . $req['body']['oldid'];
181 unset( $req['body']['oldid'] );
182 }
183 if ( $parts[4] !== 'to' ) {
184 throw new Exception( "Part index 4 is not 'to'" );
185 }
186 if ( $parts[3] === 'html' && $parts[5] === 'wikitext' ) {
187 if ( !isset( $req['body']['html'] ) ) {
188 throw new Exception( "You must set an 'html' body key for this request" );
189 }
190 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
191 if ( !isset( $req['body']['wikitext'] ) ) {
192 throw new Exception( "You must set a 'wikitext' body key for this request" );
193 }
194 if ( isset( $req['body']['body'] ) ) {
195 $req['body']['bodyOnly'] = $req['body']['body'];
196 unset( $req['body']['body'] );
197 }
198 } else {
199 throw new Exception( "Transformation unsupported" );
200 }
201 }
202 // set the appropriate proxy, timeout and headers
203 if ( $this->params['HTTPProxy'] ) {
204 $req['proxy'] = $this->params['HTTPProxy'];
205 }
206 if ( $this->params['timeout'] != null ) {
207 $req['reqTimeout'] = $this->params['timeout'];
208 }
209 if ( $this->params['forwardCookies'] ) {
210 $req['headers']['Cookie'] = $this->params['forwardCookies'];
211 }
212
213 return $req;
214
215 }
216
217 /**
218 * Remap a Parsoid v3 request to a RESTBase v1 request.
219 *
220 * Example Parsoid v3 requests:
221 * GET /local/v3/page/html/$title/{$revision}
222 * * $revision is optional
223 * POST /local/v3/transform/html/to/wikitext/{$title}{/$revision}
224 * * body: array( 'html' => ... )
225 * * $title and $revision are optional
226 * POST /local/v3/transform/wikitext/to/html/{$title}{/$revision}
227 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'bodyOnly' => true/false )
228 * * $title is optional
229 * * $revision is optional
230 */
231 public function onParsoid3Request( array $req, Closure $idGeneratorFunc ) {
232
233 $parts = explode( '/', $req['url'] );
234 list(
235 $targetWiki, // 'local'
236 $version, // 'v3'
237 $action, // 'transform' or 'page'
238 $format, // 'html' or 'wikitext'
239 // $title, // optional
240 // $revision, // optional
241 ) = $parts;
242 if ( $targetWiki !== 'local' ) {
243 throw new Exception( "Only 'local' target wiki is currently supported" );
244 } elseif ( $version !== 'v3' ) {
245 throw new Exception( "Version mismatch: should not happen." );
246 }
247 // replace /local/ with the current domain, change v3 to v1,
248 $req['url'] = preg_replace( '#^local/v3/#', $this->params['domain'] . '/v1/', $req['url'] );
249 // and prefix it with the service URL
250 $req['url'] = $this->params['url'] . $req['url'];
251 // set the appropriate proxy, timeout and headers
252 if ( $this->params['HTTPProxy'] ) {
253 $req['proxy'] = $this->params['HTTPProxy'];
254 }
255 if ( $this->params['timeout'] != null ) {
256 $req['reqTimeout'] = $this->params['timeout'];
257 }
258 if ( $this->params['forwardCookies'] ) {
259 $req['headers']['Cookie'] = $this->params['forwardCookies'];
260 }
261
262 return $req;
263
264 }
265
266 }