MediaWiki REL1_40
ParsoidVirtualRESTService.php
Go to the documentation of this file.
1<?php
47 public function __construct( array $params ) {
48 // for backwards compatibility:
49 if ( isset( $params['URL'] ) ) {
51 'Using all-caps URL parameter to $wgVirtualRestConfig ' .
52 'was deprecated in MediaWiki 1.35', '1.35'
53 );
54 $params['url'] = $params['URL'];
55 unset( $params['URL'] );
56 }
57 // set up defaults and merge them with the given params
58 $defaultURL = wfExpandUrl( wfScript( 'rest' ), PROTO_CANONICAL );
59 $mparams = array_merge( [
60 'name' => 'parsoid',
61 'url' => $defaultURL,
62 'domain' => wfParseUrl( $defaultURL )['host'] ?? 'localhost',
63 'timeout' => null,
64 'forwardCookies' => false,
65 'HTTPProxy' => null,
66 ], $params );
67 // Ensure that the url parameter has a trailing slash.
68 if ( substr( $mparams['url'], -1 ) !== '/' ) {
69 $mparams['url'] .= '/';
70 }
71 // Ensure the correct domain format: strip protocol, port,
72 // and trailing slash if present. This lets us use
73 // $wgCanonicalServer as a default value, which is very convenient.
74 $mparams['domain'] = preg_replace(
75 '/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
76 '$3',
77 $mparams['domain']
78 );
79 parent::__construct( $mparams );
80 }
81
86 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
87 $result = [];
88 foreach ( $reqs as $key => $req ) {
89 $parts = explode( '/', $req['url'] );
90
91 list(
92 $targetWiki, // 'local'
93 $version, // 'v3' ('v1' for restbase compatibility)
94 $reqType, // 'page' or 'transform'
95 $format, // 'html' or 'wikitext'
96 // $title (optional)
97 // $revision (optional)
98 ) = $parts;
99
100 if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
101 if ( $version !== 'v1' ) {
102 throw new Exception( "Only RESTBase v1 API is supported." );
103 }
104 # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
105 $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
106 } elseif ( $version !== 'v3' ) {
107 throw new Exception( "Only Parsoid v3 API is supported." );
108 }
109 if ( $targetWiki !== 'local' ) {
110 throw new Exception( "Only 'local' target wiki is currently supported" );
111 }
112 if ( $reqType !== 'page' && $reqType !== 'transform' ) {
113 throw new Exception( "Request action must be either 'page' or 'transform'" );
114 }
115 if ( $format !== 'html' && $format !== 'wikitext' && $format !== 'lint' ) {
116 throw new Exception( "Request format must be 'html', 'wt' or 'lint'" );
117 }
118 // replace /local/ with the current domain
119 $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
120 // and prefix it with the service URL
121 $req['url'] = $this->params['url'] . $req['url'];
122 // set the appropriate proxy, timeout and headers
123 if ( $this->params['HTTPProxy'] ) {
124 $req['proxy'] = $this->params['HTTPProxy'];
125 }
126 if ( $this->params['timeout'] != null ) {
127 $req['reqTimeout'] = $this->params['timeout'];
128 }
129 if ( $this->params['forwardCookies'] ) {
130 $req['headers']['Cookie'] = $this->params['forwardCookies'];
131 }
132 // Parsoid/PHP is a MW instance, so it needs the Host header set,
133 // otherwise the server replies with a 404, so apply it unconditionally
134 // to all requests
135 $req['headers']['Host'] = $this->params['domain'];
136 $result[$key] = $req;
137 }
138 return $result;
139 }
140
141}
const PROTO_CANONICAL
Definition Defines.php:199
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Virtual HTTP service client for Parsoid.
onRequests(array $reqs, Closure $idGeneratorFunc)
Prepare virtual HTTP(S) requests (for this service) for execution.This method should mangle any of th...
__construct(array $params)
Example Parsoid v3 requests: GET /local/v3/page/html/$title/{$revision}.
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.