Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / libs / virtualrest / ParsoidVirtualRESTService.php
blob03bdf0d6a0878d5f3a2a45eb7f93f1508f3269b1
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 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)
43 public function __construct( array $params ) {
44 parent::__construct( $params );
47 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
48 $result = array();
49 foreach ( $reqs as $key => $req ) {
50 $parts = explode( '/', $req['url'] );
52 list(
53 $targetWiki, // 'local'
54 $version, // 'v1'
55 $reqType // 'page' or 'transform'
56 ) = $parts;
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'" );
66 $req['url'] = $this->params['URL'] . '/' . urlencode( $this->params['prefix'] ) . '/';
68 if ( $reqType === 'page' ) {
69 $title = $parts[3];
70 if ( $parts[4] !== 'html' ) {
71 throw new Exception( "Only 'html' output format is currently supported" );
73 if ( isset( $parts[5] ) ) {
74 $req['url'] .= $title . '?oldid=' . $parts[5];
75 } else {
76 $req['url'] .= $title;
78 } elseif ( $reqType === 'transform' ) {
79 if ( $parts[4] !== 'to' ) {
80 throw new Exception( "Part index 4 is not 'to'" );
83 if ( isset( $parts[6] ) ) {
84 $req['url'] .= $parts[6];
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" );
91 if ( isset( $parts[7] ) ) {
92 $req['body']['oldid'] = $parts[7];
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" );
98 $req['body']['wt'] = $req['body']['wikitext'];
99 unset( $req['body']['wikitext'] );
100 } else {
101 throw new Exception( "Transformation unsupported" );
105 if ( isset( $this->params['HTTPProxy'] ) && $this->params['HTTPProxy'] ) {
106 $req['proxy'] = $this->params['HTTPProxy'];
108 if ( isset( $this->params['timeout'] ) ) {
109 $req['reqTimeout'] = $this->params['timeout'];
112 // Forward cookies
113 if ( isset( $this->params['forwardCookies'] ) ) {
114 $req['headers']['Cookie'] = $this->params['forwardCookies'];
117 $result[$key] = $req;
119 return $result;