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
{
28 * GET /local/v1/page/$title/html/$oldid
29 * * $oldid is optional
30 * POST /local/v1/transform/html/to/wikitext/$title/$oldid
31 * * body: array( 'html' => ... )
32 * * $title and $oldid are optional
33 * POST /local/v1/transform/wikitext/to/html/$title
34 * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body' => true/false )
35 * * $title is optional
36 * @param array $params Key/value map
37 * - url : Parsoid server URL
38 * - prefix : Parsoid prefix for this wiki
39 * - timeout : Parsoid timeout (optional)
40 * - forwardCookies : Cookies to forward to Parsoid, or false. (optional)
41 * - HTTPProxy : Parsoid HTTP proxy (optional)
43 public function __construct( array $params ) {
44 // for backwards compatibility:
45 if ( isset( $params['URL'] ) ) {
46 $params['url'] = $params['URL'];
47 unset( $params['URL'] );
49 parent
::__construct( $params );
52 public function onRequests( array $reqs, Closure
$idGeneratorFunc ) {
54 foreach ( $reqs as $key => $req ) {
55 $parts = explode( '/', $req['url'] );
58 $targetWiki, // 'local'
60 $reqType // 'page' or 'transform'
63 if ( $targetWiki !== 'local' ) {
64 throw new Exception( "Only 'local' target wiki is currently supported" );
65 } elseif ( $version !== 'v1' ) {
66 throw new Exception( "Only version 1 exists" );
67 } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
68 throw new Exception( "Request type must be either 'page' or 'transform'" );
71 $req['url'] = $this->params
['url'] . '/' . urlencode( $this->params
['prefix'] ) . '/';
73 if ( $reqType === 'page' ) {
75 if ( $parts[4] !== 'html' ) {
76 throw new Exception( "Only 'html' output format is currently supported" );
78 if ( isset( $parts[5] ) ) {
79 $req['url'] .= $title . '?oldid=' . $parts[5];
81 $req['url'] .= $title;
83 } elseif ( $reqType === 'transform' ) {
84 if ( $parts[4] !== 'to' ) {
85 throw new Exception( "Part index 4 is not 'to'" );
88 if ( isset( $parts[6] ) ) {
89 $req['url'] .= $parts[6];
92 if ( $parts[3] === 'html' & $parts[5] === 'wikitext' ) {
93 if ( !isset( $req['body']['html'] ) ) {
94 throw new Exception( "You must set an 'html' body key for this request" );
96 if ( isset( $parts[7] ) ) {
97 $req['body']['oldid'] = $parts[7];
99 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
100 if ( !isset( $req['body']['wikitext'] ) ) {
101 throw new Exception( "You must set a 'wikitext' body key for this request" );
103 $req['body']['wt'] = $req['body']['wikitext'];
104 unset( $req['body']['wikitext'] );
106 throw new Exception( "Transformation unsupported" );
110 if ( isset( $this->params
['HTTPProxy'] ) && $this->params
['HTTPProxy'] ) {
111 $req['proxy'] = $this->params
['HTTPProxy'];
113 if ( isset( $this->params
['timeout'] ) ) {
114 $req['reqTimeout'] = $this->params
['timeout'];
118 if ( isset( $this->params
['forwardCookies'] ) ) {
119 $req['headers']['Cookie'] = $this->params
['forwardCookies'];
122 $result[$key] = $req;