2014-12-24 00:18:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Virtual HTTP service client for Parsoid
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Virtual REST service for Parsoid
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
class ParsoidVirtualRESTService extends VirtualRESTService {
|
|
|
|
|
/**
|
2015-05-27 21:46:45 +00:00
|
|
|
* Example Parsoid v3 requests:
|
|
|
|
|
* GET /local/v3/page/html/$title/{$revision}
|
|
|
|
|
* * $revision is optional
|
|
|
|
|
* POST /local/v3/transform/html/to/wikitext/{$title}{/$revision}
|
2014-12-24 00:18:40 +00:00
|
|
|
* * body: array( 'html' => ... )
|
2015-05-27 21:46:45 +00:00
|
|
|
* * $title and $revision are optional
|
|
|
|
|
* POST /local/v3/transform/wikitext/to/html/{$title}{/$revision}
|
2015-09-30 14:27:04 +00:00
|
|
|
* * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body_only' => true/false )
|
2014-12-24 00:18:40 +00:00
|
|
|
* * $title is optional
|
2015-05-27 21:46:45 +00:00
|
|
|
* * $revision is optional
|
2015-09-23 20:31:04 +00:00
|
|
|
*
|
2014-12-24 00:18:40 +00:00
|
|
|
* @param array $params Key/value map
|
2015-03-02 14:35:21 +00:00
|
|
|
* - url : Parsoid server URL
|
2015-05-27 21:46:45 +00:00
|
|
|
* - domain : Wiki domain to use
|
2014-12-24 00:18:40 +00:00
|
|
|
* - timeout : Parsoid timeout (optional)
|
|
|
|
|
* - forwardCookies : Cookies to forward to Parsoid, or false. (optional)
|
|
|
|
|
* - HTTPProxy : Parsoid HTTP proxy (optional)
|
2015-05-27 21:46:45 +00:00
|
|
|
* - restbaseCompat : whether to parse URL as if they were meant for RESTBase
|
|
|
|
|
* boolean (optional)
|
2014-12-24 00:18:40 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $params ) {
|
2015-03-02 14:35:21 +00:00
|
|
|
// for backwards compatibility:
|
|
|
|
|
if ( isset( $params['URL'] ) ) {
|
2020-03-25 18:59:39 +00:00
|
|
|
wfDeprecated(
|
|
|
|
|
'Using all-caps URL parameter to $wgVirtualRestConfig', '1.35'
|
|
|
|
|
);
|
2015-03-02 14:35:21 +00:00
|
|
|
$params['url'] = $params['URL'];
|
|
|
|
|
unset( $params['URL'] );
|
|
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
// set up defaults and merge them with the given params
|
2020-03-25 19:51:23 +00:00
|
|
|
$defaultURL = wfExpandUrl( wfScript( 'rest' ), PROTO_CANONICAL );
|
2016-02-17 09:09:32 +00:00
|
|
|
$mparams = array_merge( [
|
2015-09-14 18:36:18 +00:00
|
|
|
'name' => 'parsoid',
|
2020-03-25 19:51:23 +00:00
|
|
|
'url' => $defaultURL,
|
|
|
|
|
'domain' => wfParseUrl( $defaultURL )['host'] ?? 'localhost',
|
2017-09-07 03:13:17 +00:00
|
|
|
'timeout' => null,
|
2015-05-27 21:46:45 +00:00
|
|
|
'forwardCookies' => false,
|
|
|
|
|
'HTTPProxy' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $params );
|
2015-05-27 21:46:45 +00:00
|
|
|
// Ensure that the url parameter has a trailing slash.
|
2017-09-07 03:13:17 +00:00
|
|
|
if ( substr( $mparams['url'], -1 ) !== '/' ) {
|
|
|
|
|
$mparams['url'] .= '/';
|
|
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
// Ensure the correct domain format: strip protocol, port,
|
|
|
|
|
// and trailing slash if present. This lets us use
|
|
|
|
|
// $wgCanonicalServer as a default value, which is very convenient.
|
|
|
|
|
$mparams['domain'] = preg_replace(
|
2020-03-25 19:32:11 +00:00
|
|
|
'/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
|
|
|
|
|
'$3',
|
2015-05-27 21:46:45 +00:00
|
|
|
$mparams['domain']
|
|
|
|
|
);
|
|
|
|
|
parent::__construct( $mparams );
|
2014-12-24 00:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-12 15:13:38 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @phan-param array[] $reqs
|
|
|
|
|
*/
|
2014-12-24 00:18:40 +00:00
|
|
|
public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = [];
|
2014-12-24 00:18:40 +00:00
|
|
|
foreach ( $reqs as $key => $req ) {
|
|
|
|
|
$parts = explode( '/', $req['url'] );
|
|
|
|
|
|
|
|
|
|
list(
|
|
|
|
|
$targetWiki, // 'local'
|
2015-05-27 21:46:45 +00:00
|
|
|
$version, // 'v3' ('v1' for restbase compatibility)
|
|
|
|
|
$reqType, // 'page' or 'transform'
|
|
|
|
|
$format, // 'html' or 'wikitext'
|
|
|
|
|
// $title (optional)
|
|
|
|
|
// $revision (optional)
|
2014-12-24 00:18:40 +00:00
|
|
|
) = $parts;
|
|
|
|
|
|
2015-09-02 03:50:05 +00:00
|
|
|
if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
|
2015-05-27 21:46:45 +00:00
|
|
|
if ( $version !== 'v1' ) {
|
|
|
|
|
throw new Exception( "Only RESTBase v1 API is supported." );
|
|
|
|
|
}
|
|
|
|
|
# Map RESTBase v1 API to Parsoid v3 API (pretty easy)
|
|
|
|
|
$req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
|
|
|
|
|
} elseif ( $version !== 'v3' ) {
|
2018-09-25 19:44:33 +00:00
|
|
|
throw new Exception( "Only Parsoid v3 API is supported." );
|
2015-05-27 21:46:45 +00:00
|
|
|
}
|
2014-12-24 00:18:40 +00:00
|
|
|
if ( $targetWiki !== 'local' ) {
|
2015-01-07 02:11:25 +00:00
|
|
|
throw new Exception( "Only 'local' target wiki is currently supported" );
|
2014-12-24 00:18:40 +00:00
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
if ( $reqType !== 'page' && $reqType !== 'transform' ) {
|
|
|
|
|
throw new Exception( "Request action must be either 'page' or 'transform'" );
|
2014-12-24 00:18:40 +00:00
|
|
|
}
|
2020-04-15 18:11:05 +00:00
|
|
|
if ( $format !== 'html' && $format !== 'wikitext' && $format !== 'lint' ) {
|
|
|
|
|
throw new Exception( "Request format must be 'html', 'wt' or 'lint'" );
|
2015-05-27 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
// replace /local/ with the current domain
|
|
|
|
|
$req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
|
|
|
|
|
// and prefix it with the service URL
|
|
|
|
|
$req['url'] = $this->params['url'] . $req['url'];
|
|
|
|
|
// set the appropriate proxy, timeout and headers
|
|
|
|
|
if ( $this->params['HTTPProxy'] ) {
|
2014-12-24 00:18:40 +00:00
|
|
|
$req['proxy'] = $this->params['HTTPProxy'];
|
|
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
if ( $this->params['timeout'] != null ) {
|
2014-12-24 00:18:40 +00:00
|
|
|
$req['reqTimeout'] = $this->params['timeout'];
|
|
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
if ( $this->params['forwardCookies'] ) {
|
2014-12-24 00:18:40 +00:00
|
|
|
$req['headers']['Cookie'] = $this->params['forwardCookies'];
|
|
|
|
|
}
|
2019-11-20 16:50:01 +00:00
|
|
|
// Parsoid/PHP is a MW instance, so it needs the Host header set,
|
|
|
|
|
// otherwise the server replies with a 404, so apply it unconditionally
|
|
|
|
|
// to all requests
|
|
|
|
|
$req['headers']['Host'] = $this->params['domain'];
|
2014-12-24 00:18:40 +00:00
|
|
|
$result[$key] = $req;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2015-05-27 21:46:45 +00:00
|
|
|
|
2014-12-24 00:18:40 +00:00
|
|
|
}
|