Merge "More debug diagnostics for upload by URL"
[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 $result = array();
49 foreach ( $reqs as $key => $req ) {
50 $parts = explode( '/', $req['url'] );
51
52 list(
53 $targetWiki, // 'local'
54 $version, // 'v1'
55 $reqType // 'page' or 'transform'
56 ) = $parts;
57
58 if ( $targetWiki !== 'local' ) {
59 throw new Exception( "Only 'local' target wiki is currently supported" );
60 } elseif ( $version !== 'v1' ) {
61 throw new Exception( "Only version 1 exists" );
62 } else if ( $reqType !== 'page' && $reqType !== 'transform' ) {
63 throw new Exception( "Request type must be either 'page' or 'transform'" );
64 }
65
66 $req['url'] = $this->params['URL'] . '/' . urlencode( $this->params['prefix'] ) . '/';
67
68 if ( $reqType === 'page' ) {
69 $title = $parts[3];
70 if ( $parts[4] !== 'html' ) {
71 throw new Exception( "Only 'html' output format is currently supported" );
72 }
73 if ( isset( $parts[5] ) ) {
74 $req['url'] .= $title . '?oldid=' . $parts[5];
75 } else {
76 $req['url'] .= $title;
77 }
78 } elseif ( $reqType === 'transform' ) {
79 if ( $parts[4] !== 'to' ) {
80 throw new Exception( "Part index 4 is not 'to'" );
81 }
82
83 if ( isset( $parts[6] ) ) {
84 $req['url'] .= $parts[6];
85 }
86
87 if ( $parts[3] === 'html' & $parts[5] === 'wikitext' ) {
88 if ( !isset( $req['body']['html'] ) ) {
89 throw new Exception( "You must set an 'html' body key for this request" );
90 }
91 if ( isset( $parts[7] ) ) {
92 $req['body']['oldid'] = $parts[7];
93 }
94 } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
95 if ( !isset( $req['body']['wikitext'] ) ) {
96 throw new Exception( "You must set a 'wikitext' body key for this request" );
97 }
98 $req['body']['wt'] = $req['body']['wikitext'];
99 unset( $req['body']['wikitext'] );
100 } else {
101 throw new Exception( "Transformation unsupported" );
102 }
103 }
104
105 if ( isset( $this->params['HTTPProxy'] ) && $this->params['HTTPProxy'] ) {
106 $req['proxy'] = $this->params['HTTPProxy'];
107 }
108 if ( isset( $this->params['timeout'] ) ) {
109 $req['reqTimeout'] = $this->params['timeout'];
110 }
111
112 // Forward cookies
113 if ( isset( $this->params['forwardCookies'] ) ) {
114 $req['headers']['Cookie'] = $this->params['forwardCookies'];
115 }
116
117 $result[$key] = $req;
118 }
119 return $result;
120 }
121 }