Merge "PostgreSQL: Fix text search on moved pages"
[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 // for backwards compatibility:
45 if ( isset( $params['URL'] ) ) {
46 $params['url'] = $params['URL'];
47 unset( $params['URL'] );
48 }
49 parent::__construct( $params );
50 }
51
52 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
53 $result = array();
54 foreach ( $reqs as $key => $req ) {
55 $parts = explode( '/', $req['url'] );
56
57 list(
58 $targetWiki, // 'local'
59 $version, // 'v1'
60 $reqType // 'page' or 'transform'
61 ) = $parts;
62
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'" );
69 }
70
71 $req['url'] = $this->params['url'] . '/' . urlencode( $this->params['prefix'] ) . '/';
72
73 if ( $reqType === 'page' ) {
74 $title = $parts[3];
75 if ( $parts[4] !== 'html' ) {
76 throw new Exception( "Only 'html' output format is currently supported" );
77 }
78 if ( isset( $parts[5] ) ) {
79 $req['url'] .= $title . '?oldid=' . $parts[5];
80 } else {
81 $req['url'] .= $title;
82 }
83 } elseif ( $reqType === 'transform' ) {
84 if ( $parts[4] !== 'to' ) {
85 throw new Exception( "Part index 4 is not 'to'" );
86 }
87
88 if ( isset( $parts[6] ) ) {
89 $req['url'] .= $parts[6];
90 }
91
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" );
95 }
96 if ( isset( $parts[7] ) ) {
97 $req['body']['oldid'] = $parts[7];
98 }
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" );
102 }
103 $req['body']['wt'] = $req['body']['wikitext'];
104 unset( $req['body']['wikitext'] );
105 } else {
106 throw new Exception( "Transformation unsupported" );
107 }
108 }
109
110 if ( isset( $this->params['HTTPProxy'] ) && $this->params['HTTPProxy'] ) {
111 $req['proxy'] = $this->params['HTTPProxy'];
112 }
113 if ( isset( $this->params['timeout'] ) ) {
114 $req['reqTimeout'] = $this->params['timeout'];
115 }
116
117 // Forward cookies
118 if ( isset( $this->params['forwardCookies'] ) ) {
119 $req['headers']['Cookie'] = $this->params['forwardCookies'];
120 }
121
122 $result[$key] = $req;
123 }
124 return $result;
125 }
126 }