MediaWiki  master
ParsoidVirtualRESTService.php
Go to the documentation of this file.
1 <?php
48  public function __construct( array $params ) {
49  // for backwards compatibility:
50  if ( isset( $params['URL'] ) ) {
52  'Using all-caps URL parameter to $wgVirtualRestConfig ' .
53  'was deprecated in MediaWiki 1.35', '1.35'
54  );
55  $params['url'] = $params['URL'];
56  unset( $params['URL'] );
57  }
58  // set up defaults and merge them with the given params
59  $defaultURL = wfExpandUrl( wfScript( 'rest' ), PROTO_CANONICAL );
60  $mparams = array_merge( [
61  'name' => 'parsoid',
62  'url' => $defaultURL,
63  'domain' => wfParseUrl( $defaultURL )['host'] ?? 'localhost',
64  'timeout' => null,
65  'forwardCookies' => false,
66  'HTTPProxy' => null,
67  ], $params );
68  // Ensure that the url parameter has a trailing slash.
69  if ( substr( $mparams['url'], -1 ) !== '/' ) {
70  $mparams['url'] .= '/';
71  }
72  // Ensure the correct domain format: strip protocol, port,
73  // and trailing slash if present. This lets us use
74  // $wgCanonicalServer as a default value, which is very convenient.
75  $mparams['domain'] = preg_replace(
76  '/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
77  '$3',
78  $mparams['domain']
79  );
80  parent::__construct( $mparams );
81  }
82 
87  public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
88  $result = [];
89  foreach ( $reqs as $key => $req ) {
90  $parts = explode( '/', $req['url'] );
91 
92  list(
93  $targetWiki, // 'local'
94  $version, // 'v3' ('v1' for restbase compatibility)
95  $reqType, // 'page' or 'transform'
96  $format, // 'html' or 'wikitext'
97  // $title (optional)
98  // $revision (optional)
99  ) = $parts;
100 
101  if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
102  if ( $version !== 'v1' ) {
103  throw new Exception( "Only RESTBase v1 API is supported." );
104  }
105  # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
106  $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
107  } elseif ( $version !== 'v3' ) {
108  throw new Exception( "Only Parsoid v3 API is supported." );
109  }
110  if ( $targetWiki !== 'local' ) {
111  throw new Exception( "Only 'local' target wiki is currently supported" );
112  }
113  if ( $reqType !== 'page' && $reqType !== 'transform' ) {
114  throw new Exception( "Request action must be either 'page' or 'transform'" );
115  }
116  if ( $format !== 'html' && $format !== 'wikitext' && $format !== 'lint' ) {
117  throw new Exception( "Request format must be 'html', 'wt' or 'lint'" );
118  }
119  // replace /local/ with the current domain
120  $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
121  // and prefix it with the service URL
122  $req['url'] = $this->params['url'] . $req['url'];
123  // set the appropriate proxy, timeout and headers
124  if ( $this->params['HTTPProxy'] ) {
125  $req['proxy'] = $this->params['HTTPProxy'];
126  }
127  if ( $this->params['timeout'] != null ) {
128  $req['reqTimeout'] = $this->params['timeout'];
129  }
130  if ( $this->params['forwardCookies'] ) {
131  $req['headers']['Cookie'] = $this->params['forwardCookies'];
132  }
133  // Parsoid/PHP is a MW instance, so it needs the Host header set,
134  // otherwise the server replies with a 404, so apply it unconditionally
135  // to all requests
136  $req['headers']['Host'] = $this->params['domain'];
137  $result[$key] = $req;
138  }
139  return $result;
140  }
141 
142 }
const PROTO_CANONICAL
Definition: Defines.php:197
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 using $wgServer (or one of its alternatives).
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
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.
array $params
Key/value map.