Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / virtualrest / ParsoidVirtualRESTService.php
bloba148a397075bbca8613632376e0eeebd49bdb4a2
1 <?php
2 /**
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
21 /**
22 * Virtual REST service for Parsoid
23 * @since 1.25
25 class ParsoidVirtualRESTService extends VirtualRESTService {
26 /**
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 * There are also deprecated "v1" requests; see onParsoid1Request
39 * for details.
40 * @param array $params Key/value map
41 * - url : Parsoid server URL
42 * - domain : Wiki domain to use
43 * - timeout : Parsoid timeout (optional)
44 * - forwardCookies : Cookies to forward to Parsoid, or false. (optional)
45 * - HTTPProxy : Parsoid HTTP proxy (optional)
46 * - restbaseCompat : whether to parse URL as if they were meant for RESTBase
47 * boolean (optional)
49 public function __construct( array $params ) {
50 // for backwards compatibility:
51 if ( isset( $params['URL'] ) ) {
52 $params['url'] = $params['URL'];
53 unset( $params['URL'] );
55 // set up defaults and merge them with the given params
56 $mparams = array_merge( [
57 'name' => 'parsoid',
58 'url' => 'http://localhost:8000/',
59 'prefix' => 'localhost',
60 'domain' => 'localhost',
61 'forwardCookies' => false,
62 'HTTPProxy' => null,
63 ], $params );
64 // Ensure that the url parameter has a trailing slash.
65 $mparams['url'] = preg_replace(
66 '#/?$#',
67 '/',
68 $mparams['url']
70 // Ensure the correct domain format: strip protocol, port,
71 // and trailing slash if present. This lets us use
72 // $wgCanonicalServer as a default value, which is very convenient.
73 $mparams['domain'] = preg_replace(
74 '/^(https?:\/\/)?([^\/:]+?)(:\d+)?\/?$/',
75 '$2',
76 $mparams['domain']
78 parent::__construct( $mparams );
81 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
82 $result = [];
83 foreach ( $reqs as $key => $req ) {
84 $parts = explode( '/', $req['url'] );
86 list(
87 $targetWiki, // 'local'
88 $version, // 'v3' ('v1' for restbase compatibility)
89 $reqType, // 'page' or 'transform'
90 $format, // 'html' or 'wikitext'
91 // $title (optional)
92 // $revision (optional)
93 ) = $parts;
95 if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
96 if ( $version !== 'v1' ) {
97 throw new Exception( "Only RESTBase v1 API is supported." );
99 # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
100 $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
101 } elseif ( $version !== 'v3' ) {
102 $result[$key] = $this->onParsoid1Request( $req, $idGeneratorFunc );
103 continue;
105 if ( $targetWiki !== 'local' ) {
106 throw new Exception( "Only 'local' target wiki is currently supported" );
108 if ( $reqType !== 'page' && $reqType !== 'transform' ) {
109 throw new Exception( "Request action must be either 'page' or 'transform'" );
111 if ( $format !== 'html' && $format !== 'wikitext' ) {
112 throw new Exception( "Request format must be either 'html' or 'wt'" );
114 // replace /local/ with the current domain
115 $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
116 // and prefix it with the service URL
117 $req['url'] = $this->params['url'] . $req['url'];
118 // set the appropriate proxy, timeout and headers
119 if ( $this->params['HTTPProxy'] ) {
120 $req['proxy'] = $this->params['HTTPProxy'];
122 if ( $this->params['timeout'] != null ) {
123 $req['reqTimeout'] = $this->params['timeout'];
125 if ( $this->params['forwardCookies'] ) {
126 $req['headers']['Cookie'] = $this->params['forwardCookies'];
128 $result[$key] = $req;
130 return $result;
134 * Remap a Parsoid v1 request to a Parsoid v3 request.
136 * Example Parsoid v1 requests:
137 * GET /local/v1/page/$title/html/$oldid
138 * * $oldid is optional
139 * POST /local/v1/transform/html/to/wikitext/$title/$oldid
140 * * body: array( 'html' => ... )
141 * * $title and $oldid are optional
142 * POST /local/v1/transform/wikitext/to/html/$title
143 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body' => true/false )
144 * * $title is optional
146 * NOTE: the POST APIs aren't "real" Parsoid v1 APIs, they are just what
147 * Visual Editor "pretends" the V1 API is like. A previous version of
148 * ParsoidVirtualRESTService translated these to the "real" Parsoid v1
149 * API. We now translate these to the "real" Parsoid v3 API.
151 public function onParsoid1Request( array $req, Closure $idGeneratorFunc ) {
152 $parts = explode( '/', $req['url'] );
153 list(
154 $targetWiki, // 'local'
155 $version, // 'v1'
156 $reqType // 'page' or 'transform'
157 ) = $parts;
158 if ( $targetWiki !== 'local' ) {
159 throw new Exception( "Only 'local' target wiki is currently supported" );
160 } elseif ( $version !== 'v1' ) {
161 throw new Exception( "Only v1 and v3 are supported." );
162 } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
163 throw new Exception( "Request type must be either 'page' or 'transform'" );
165 $req['url'] = $this->params['url'] . $this->params['domain'] . '/v3/';
166 if ( $reqType === 'page' ) {
167 $title = $parts[3];
168 if ( $parts[4] !== 'html' ) {
169 throw new Exception( "Only 'html' output format is currently supported" );
171 $req['url'] .= 'page/html/' . $title;
172 if ( isset( $parts[5] ) ) {
173 $req['url'] .= '/' . $parts[5];
174 } elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) {
175 $req['url'] .= '/' . $req['query']['oldid'];
176 unset( $req['query']['oldid'] );
178 } elseif ( $reqType === 'transform' ) {
179 $req['url'] .= 'transform/'. $parts[3] . '/to/' . $parts[5];
180 // the title
181 if ( isset( $parts[6] ) ) {
182 $req['url'] .= '/' . $parts[6];
184 // revision id
185 if ( isset( $parts[7] ) ) {
186 $req['url'] .= '/' . $parts[7];
187 } elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) {
188 $req['url'] .= '/' . $req['body']['oldid'];
189 unset( $req['body']['oldid'] );
191 if ( $parts[4] !== 'to' ) {
192 throw new Exception( "Part index 4 is not 'to'" );
194 if ( $parts[3] === 'html' && $parts[5] === 'wikitext' ) {
195 if ( !isset( $req['body']['html'] ) ) {
196 throw new Exception( "You must set an 'html' body key for this request" );
198 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
199 if ( !isset( $req['body']['wikitext'] ) ) {
200 throw new Exception( "You must set a 'wikitext' body key for this request" );
202 if ( isset( $req['body']['body'] ) ) {
203 $req['body']['body_only'] = $req['body']['body'];
204 unset( $req['body']['body'] );
206 } else {
207 throw new Exception( "Transformation unsupported" );
210 // set the appropriate proxy, timeout and headers
211 if ( $this->params['HTTPProxy'] ) {
212 $req['proxy'] = $this->params['HTTPProxy'];
214 if ( $this->params['timeout'] != null ) {
215 $req['reqTimeout'] = $this->params['timeout'];
217 if ( $this->params['forwardCookies'] ) {
218 $req['headers']['Cookie'] = $this->params['forwardCookies'];
221 return $req;