Make a VirtualRESTService class for Parsoid
[lhc/web/wiklou.git] / includes / libs / virtualrest / ParsoidVirtualRESTService.php
1 <?php
2 /**
3 * Virtual HTTP service client for Parsoid
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 Parsoid
23 * @since 1.25
24 */
25 class ParsoidVirtualRESTService extends VirtualRESTService {
26 /**
27 * Example requests:
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)
42 */
43 public function __construct( array $params ) {
44 parent::__construct( $params );
45 }
46
47 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
48 global $wgVisualEditorParsoidPrefix; // TODO: Move this to core
49
50 $result = array();
51 foreach ( $reqs as $key => $req ) {
52 $parts = explode( '/', $req['url'] );
53
54 list(
55 $targetWiki, // 'local'
56 $version, // 'v1'
57 $reqType // 'page' or 'transform'
58 ) = $parts;
59
60 if ( $targetWiki !== 'local' ) {
61 throw new MWException( "Only 'local' target wiki is currently supported" );
62 } elseif ( $version !== 'v1' ) {
63 throw new MWException( "Only version 1 exists" );
64 } else if ( $reqType !== 'page' && $reqType !== 'transform' ) {
65 throw new MWException( "Request type must be either 'page' or 'transform'" );
66 }
67
68 $req['url'] = $this->params['URL'] . '/' . urlencode( $this->params['prefix'] ) . '/';
69
70 if ( $reqType === 'page' ) {
71 $title = $parts[3];
72 if ( $parts[4] !== 'html' ) {
73 throw new MWException( "Only 'html' output format is currently supported" );
74 }
75 if ( isset( $parts[5] ) ) {
76 $req['url'] .= $title . '?oldid=' . $parts[5];
77 } else {
78 $req['url'] .= $title;
79 }
80 } elseif ( $reqType === 'transform' ) {
81 if ( $parts[4] !== 'to' ) {
82 throw new MWException( "Part index 4 is not 'to'" );
83 }
84
85 if ( isset( $parts[6] ) ) {
86 $req['url'] .= $parts[6];
87 }
88
89 if ( $parts[3] === 'html' & $parts[5] === 'wikitext' ) {
90 if ( !isset( $req['body']['html'] ) ) {
91 throw new MWException( "You must set an 'html' body key for this request" );
92 }
93 if ( isset( $parts[6] ) ) {
94 $req['body']['oldid'] = $parts[6];
95 }
96 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
97 if ( !isset( $req['body']['wikitext'] ) ) {
98 throw new MWException( "You must set a 'wikitext' body key for this request" );
99 }
100 $req['body']['wt'] = $req['body']['wikitext'];
101 unset( $req['body']['wikitext'] );
102 } else {
103 throw new MWException( "Transformation unsupported" );
104 }
105 }
106
107 if ( isset( $this->params['HTTPProxy'] ) && $this->params['HTTPProxy'] ) {
108 $req['proxy'] = $this->params['HTTPProxy'];
109 }
110 if ( isset( $this->params['timeout'] ) ) {
111 $req['reqTimeout'] = $this->params['timeout'];
112 }
113
114 // Forward cookies
115 if ( isset( $this->params['forwardCookies'] ) ) {
116 $req['headers']['Cookie'] = $this->params['forwardCookies'];
117 }
118
119 $result[$key] = $req;
120 }
121 return $result;
122 }
123 }