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