3 * Virtual HTTP service client for Parsoid
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.
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.
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
22 * Virtual REST service for Parsoid
25 class ParsoidVirtualRESTService
extends VirtualRESTService
{
27 * Example Parsoid v3 requests:
28 * GET /local/v3/page/html/$title/{$revision}
29 * * $revision is optional
30 * POST /local/v3/transform/html/to/wikitext/{$title}{/$revision}
31 * * body: array( 'html' => ... )
32 * * $title and $revision are optional
33 * POST /local/v3/transform/wikitext/to/html/{$title}{/$revision}
34 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body_only' => true/false )
35 * * $title is optional
36 * * $revision is optional
38 * @param array $params Key/value map
39 * - url : Parsoid server URL
40 * - domain : Wiki domain to use
41 * - timeout : Parsoid timeout (optional)
42 * - forwardCookies : Cookies to forward to Parsoid, or false. (optional)
43 * - HTTPProxy : Parsoid HTTP proxy (optional)
44 * - restbaseCompat : whether to parse URL as if they were meant for RESTBase
47 public function __construct( array $params ) {
48 // for backwards compatibility:
49 if ( isset( $params['URL'] ) ) {
50 $params['url'] = $params['URL'];
51 unset( $params['URL'] );
53 // set up defaults and merge them with the given params
54 $mparams = array_merge( [
56 'url' => 'http://localhost:8000/',
57 'prefix' => 'localhost',
58 'domain' => 'localhost',
60 'forwardCookies' => false,
63 // Ensure that the url parameter has a trailing slash.
64 if ( substr( $mparams['url'], -1 ) !== '/' ) {
65 $mparams['url'] .= '/';
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+)?\/?$/',
75 parent
::__construct( $mparams );
78 public function onRequests( array $reqs, Closure
$idGeneratorFunc ) {
80 foreach ( $reqs as $key => $req ) {
81 $parts = explode( '/', $req['url'] );
84 $targetWiki, // 'local'
85 $version, // 'v3' ('v1' for restbase compatibility)
86 $reqType, // 'page' or 'transform'
87 $format, // 'html' or 'wikitext'
89 // $revision (optional)
92 if ( isset( $this->params
['restbaseCompat'] ) && $this->params
['restbaseCompat'] ) {
93 if ( $version !== 'v1' ) {
94 throw new Exception( "Only RESTBase v1 API is supported." );
96 # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
97 $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
98 } elseif ( $version !== 'v3' ) {
99 throw new Exception( "Only Parsoid v3 API is supported." );
101 if ( $targetWiki !== 'local' ) {
102 throw new Exception( "Only 'local' target wiki is currently supported" );
104 if ( $reqType !== 'page' && $reqType !== 'transform' ) {
105 throw new Exception( "Request action must be either 'page' or 'transform'" );
107 if ( $format !== 'html' && $format !== 'wikitext' ) {
108 throw new Exception( "Request format must be either 'html' or 'wt'" );
110 // replace /local/ with the current domain
111 $req['url'] = preg_replace( '#^local/#', $this->params
['domain'] . '/', $req['url'] );
112 // and prefix it with the service URL
113 $req['url'] = $this->params
['url'] . $req['url'];
114 // set the appropriate proxy, timeout and headers
115 if ( $this->params
['HTTPProxy'] ) {
116 $req['proxy'] = $this->params
['HTTPProxy'];
118 if ( $this->params
['timeout'] != null ) {
119 $req['reqTimeout'] = $this->params
['timeout'];
121 if ( $this->params
['forwardCookies'] ) {
122 $req['headers']['Cookie'] = $this->params
['forwardCookies'];
124 $result[$key] = $req;