MediaWiki  1.34.0
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'] ) ) {
50  $params['url'] = $params['URL'];
51  unset( $params['URL'] );
52  }
53  // set up defaults and merge them with the given params
54  $mparams = array_merge( [
55  'name' => 'parsoid',
56  'url' => 'http://localhost:8000/',
57  'prefix' => 'localhost',
58  'domain' => 'localhost',
59  'timeout' => null,
60  'forwardCookies' => false,
61  'HTTPProxy' => null,
62  ], $params );
63  // Ensure that the url parameter has a trailing slash.
64  if ( substr( $mparams['url'], -1 ) !== '/' ) {
65  $mparams['url'] .= '/';
66  }
67  // Ensure the correct domain format: strip protocol, port,
68  // and trailing slash if present. This lets us use
69  // $wgCanonicalServer as a default value, which is very convenient.
70  $mparams['domain'] = preg_replace(
71  '/^(https?:\/\/)?([^\/:]+?)(:\d+)?\/?$/',
72  '$2',
73  $mparams['domain']
74  );
75  parent::__construct( $mparams );
76  }
77 
78  public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
79  $result = [];
80  foreach ( $reqs as $key => $req ) {
81  $parts = explode( '/', $req['url'] );
82 
83  list(
84  $targetWiki, // 'local'
85  $version, // 'v3' ('v1' for restbase compatibility)
86  $reqType, // 'page' or 'transform'
87  $format, // 'html' or 'wikitext'
88  // $title (optional)
89  // $revision (optional)
90  ) = $parts;
91 
92  if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
93  if ( $version !== 'v1' ) {
94  throw new Exception( "Only RESTBase v1 API is supported." );
95  }
96  # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
97  $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
98  } elseif ( $version !== 'v3' ) {
99  throw new Exception( "Only Parsoid v3 API is supported." );
100  }
101  if ( $targetWiki !== 'local' ) {
102  throw new Exception( "Only 'local' target wiki is currently supported" );
103  }
104  if ( $reqType !== 'page' && $reqType !== 'transform' ) {
105  throw new Exception( "Request action must be either 'page' or 'transform'" );
106  }
107  if ( $format !== 'html' && $format !== 'wikitext' ) {
108  throw new Exception( "Request format must be either 'html' or 'wt'" );
109  }
110  // replace /local/ with the current domain
111  $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
112  // and prefix it with the service URL
113  $req['url'] = $this->params['url'] . $req['url'];
114  // set the appropriate proxy, timeout and headers
115  if ( $this->params['HTTPProxy'] ) {
116  $req['proxy'] = $this->params['HTTPProxy'];
117  }
118  if ( $this->params['timeout'] != null ) {
119  $req['reqTimeout'] = $this->params['timeout'];
120  }
121  if ( $this->params['forwardCookies'] ) {
122  $req['headers']['Cookie'] = $this->params['forwardCookies'];
123  }
124  $result[$key] = $req;
125  }
126  return $result;
127  }
128 
129 }
ParsoidVirtualRESTService\onRequests
onRequests(array $reqs, Closure $idGeneratorFunc)
Prepare virtual HTTP(S) requests (for this service) for execution.
Definition: ParsoidVirtualRESTService.php:78
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