Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / libs / virtualrest / ParsoidVirtualRESTService.php
blobda47d356512f52700132dfff5598cccd494d9078
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' ) {
107 throw new Exception( "Only 'local' target wiki is currently supported" );
109 if ( $reqType !== 'page' && $reqType !== 'transform' ) {
110 throw new Exception( "Request action must be either 'page' or 'transform'" );
112 if ( $format !== 'html' && $format !== 'wikitext' ) {
113 throw new Exception( "Request format must be either 'html' or 'wt'" );
115 // replace /local/ with the current domain
116 $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
117 // and prefix it with the service URL
118 $req['url'] = $this->params['url'] . $req['url'];
119 // set the appropriate proxy, timeout and headers
120 if ( $this->params['HTTPProxy'] ) {
121 $req['proxy'] = $this->params['HTTPProxy'];
123 if ( $this->params['timeout'] != null ) {
124 $req['reqTimeout'] = $this->params['timeout'];
126 if ( $this->params['forwardCookies'] ) {
127 $req['headers']['Cookie'] = $this->params['forwardCookies'];
129 $result[$key] = $req;
131 return $result;
135 * Remap a Parsoid v1 request to a Parsoid v3 request.
137 * Example Parsoid v1 requests:
138 * GET /local/v1/page/$title/html/$oldid
139 * * $oldid is optional
140 * POST /local/v1/transform/html/to/wikitext/$title/$oldid
141 * * body: array( 'html' => ... )
142 * * $title and $oldid are optional
143 * POST /local/v1/transform/wikitext/to/html/$title
144 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body' => true/false )
145 * * $title is optional
147 * NOTE: the POST APIs aren't "real" Parsoid v1 APIs, they are just what
148 * Visual Editor "pretends" the V1 API is like. A previous version of
149 * ParsoidVirtualRESTService translated these to the "real" Parsoid v1
150 * API. We now translate these to the "real" Parsoid v3 API.
152 public function onParsoid1Request( array $req, Closure $idGeneratorFunc ) {
154 $parts = explode( '/', $req['url'] );
155 list(
156 $targetWiki, // 'local'
157 $version, // 'v1'
158 $reqType // 'page' or 'transform'
159 ) = $parts;
160 if ( $targetWiki !== 'local' ) {
161 throw new Exception( "Only 'local' target wiki is currently supported" );
162 } elseif ( $version !== 'v1' ) {
163 throw new Exception( "Only v1 and v3 are supported." );
164 } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
165 throw new Exception( "Request type must be either 'page' or 'transform'" );
167 $req['url'] = $this->params['url'] . $this->params['domain'] . '/v3/';
168 if ( $reqType === 'page' ) {
169 $title = $parts[3];
170 if ( $parts[4] !== 'html' ) {
171 throw new Exception( "Only 'html' output format is currently supported" );
173 $req['url'] .= 'page/html/' . $title;
174 if ( isset( $parts[5] ) ) {
175 $req['url'] .= '/' . $parts[5];
176 } elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) {
177 $req['url'] .= '/' . $req['query']['oldid'];
178 unset( $req['query']['oldid'] );
180 } elseif ( $reqType === 'transform' ) {
181 $req['url'] .= 'transform/'. $parts[3] . '/to/' . $parts[5];
182 // the title
183 if ( isset( $parts[6] ) ) {
184 $req['url'] .= '/' . $parts[6];
186 // revision id
187 if ( isset( $parts[7] ) ) {
188 $req['url'] .= '/' . $parts[7];
189 } elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) {
190 $req['url'] .= '/' . $req['body']['oldid'];
191 unset( $req['body']['oldid'] );
193 if ( $parts[4] !== 'to' ) {
194 throw new Exception( "Part index 4 is not 'to'" );
196 if ( $parts[3] === 'html' && $parts[5] === 'wikitext' ) {
197 if ( !isset( $req['body']['html'] ) ) {
198 throw new Exception( "You must set an 'html' body key for this request" );
200 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
201 if ( !isset( $req['body']['wikitext'] ) ) {
202 throw new Exception( "You must set a 'wikitext' body key for this request" );
204 if ( isset( $req['body']['body'] ) ) {
205 $req['body']['body_only'] = $req['body']['body'];
206 unset( $req['body']['body'] );
208 } else {
209 throw new Exception( "Transformation unsupported" );
212 // set the appropriate proxy, timeout and headers
213 if ( $this->params['HTTPProxy'] ) {
214 $req['proxy'] = $this->params['HTTPProxy'];
216 if ( $this->params['timeout'] != null ) {
217 $req['reqTimeout'] = $this->params['timeout'];
219 if ( $this->params['forwardCookies'] ) {
220 $req['headers']['Cookie'] = $this->params['forwardCookies'];
223 return $req;