From 24409664f1d80cce0d82bc0bf60999feb6d2d6b0 Mon Sep 17 00:00:00 2001 From: Alex Monk Date: Wed, 24 Dec 2014 00:18:40 +0000 Subject: [PATCH] Make a VirtualRESTService class for Parsoid So that code to deal with Parsoid can be shared across different extensions. Relies on I1d57ff24 Bug: T1218 Change-Id: Id658d925b722b885fd425c27f7c4fd2cd02ad0b4 --- autoload.php | 1 + includes/libs/MultiHttpClient.php | 7 +- .../virtualrest/ParsoidVirtualRESTService.php | 123 ++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 includes/libs/virtualrest/ParsoidVirtualRESTService.php diff --git a/autoload.php b/autoload.php index 6cefa13269..aac49e1aee 100644 --- a/autoload.php +++ b/autoload.php @@ -835,6 +835,7 @@ $wgAutoloadLocalClasses = array( 'ParserDiffTest' => __DIR__ . '/includes/parser/ParserDiffTest.php', 'ParserOptions' => __DIR__ . '/includes/parser/ParserOptions.php', 'ParserOutput' => __DIR__ . '/includes/parser/ParserOutput.php', + 'ParsoidVirtualRESTService' => __DIR__ . '/includes/libs/virtualrest/ParsoidVirtualRESTService.php', 'Password' => __DIR__ . '/includes/password/Password.php', 'PasswordError' => __DIR__ . '/includes/password/PasswordError.php', 'PasswordFactory' => __DIR__ . '/includes/password/PasswordFactory.php', diff --git a/includes/libs/MultiHttpClient.php b/includes/libs/MultiHttpClient.php index 8ed9ee42d2..8d9e8107c2 100644 --- a/includes/libs/MultiHttpClient.php +++ b/includes/libs/MultiHttpClient.php @@ -34,6 +34,7 @@ * array bodies are encoded as multipart/form-data and strings * use application/x-www-form-urlencoded (headers sent automatically) * - stream : resource to stream the HTTP response body to + * - proxy : HTTP proxy to use * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'. * * @author Aaron Schulz @@ -52,11 +53,14 @@ class MultiHttpClient { protected $usePipelining = false; /** @var integer */ protected $maxConnsPerHost = 50; + /** @var string|null proxy */ + protected $proxy; /** * @param array $options * - connTimeout : default connection timeout * - reqTimeout : default request timeout + * - proxy : HTTP proxy to use * - usePipelining : whether to use HTTP pipelining if possible (for all hosts) * - maxConnsPerHost : maximum number of concurrent connections (per host) */ @@ -67,7 +71,7 @@ class MultiHttpClient { throw new Exception( "Cannot find CA bundle: " . $this->caBundlePath ); } } - static $opts = array( 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost' ); + static $opts = array( 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost', 'proxy' ); foreach ( $opts as $key ) { if ( isset( $options[$key] ) ) { $this->$key = $options[$key]; @@ -250,6 +254,7 @@ class MultiHttpClient { curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, isset( $opts['connTimeout'] ) ? $opts['connTimeout'] : $this->connTimeout ); + curl_setopt( $ch, CURLOPT_PROXY, isset( $req['proxy'] ) ? $req['proxy'] : $this->proxy ); curl_setopt( $ch, CURLOPT_TIMEOUT, isset( $opts['reqTimeout'] ) ? $opts['reqTimeout'] : $this->reqTimeout ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); diff --git a/includes/libs/virtualrest/ParsoidVirtualRESTService.php b/includes/libs/virtualrest/ParsoidVirtualRESTService.php new file mode 100644 index 0000000000..410d8068ce --- /dev/null +++ b/includes/libs/virtualrest/ParsoidVirtualRESTService.php @@ -0,0 +1,123 @@ + ... ) + * * $title and $oldid are optional + * POST /local/v1/transform/wikitext/to/html/$title + * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body' => true/false ) + * * $title is optional + * @param array $params Key/value map + * - URL : Parsoid server URL + * - prefix : Parsoid prefix for this wiki + * - timeout : Parsoid timeout (optional) + * - forwardCookies : Cookies to forward to Parsoid, or false. (optional) + * - HTTPProxy : Parsoid HTTP proxy (optional) + */ + public function __construct( array $params ) { + parent::__construct( $params ); + } + + public function onRequests( array $reqs, Closure $idGeneratorFunc ) { + global $wgVisualEditorParsoidPrefix; // TODO: Move this to core + + $result = array(); + foreach ( $reqs as $key => $req ) { + $parts = explode( '/', $req['url'] ); + + list( + $targetWiki, // 'local' + $version, // 'v1' + $reqType // 'page' or 'transform' + ) = $parts; + + if ( $targetWiki !== 'local' ) { + throw new MWException( "Only 'local' target wiki is currently supported" ); + } elseif ( $version !== 'v1' ) { + throw new MWException( "Only version 1 exists" ); + } else if ( $reqType !== 'page' && $reqType !== 'transform' ) { + throw new MWException( "Request type must be either 'page' or 'transform'" ); + } + + $req['url'] = $this->params['URL'] . '/' . urlencode( $this->params['prefix'] ) . '/'; + + if ( $reqType === 'page' ) { + $title = $parts[3]; + if ( $parts[4] !== 'html' ) { + throw new MWException( "Only 'html' output format is currently supported" ); + } + if ( isset( $parts[5] ) ) { + $req['url'] .= $title . '?oldid=' . $parts[5]; + } else { + $req['url'] .= $title; + } + } elseif ( $reqType === 'transform' ) { + if ( $parts[4] !== 'to' ) { + throw new MWException( "Part index 4 is not 'to'" ); + } + + if ( isset( $parts[6] ) ) { + $req['url'] .= $parts[6]; + } + + if ( $parts[3] === 'html' & $parts[5] === 'wikitext' ) { + if ( !isset( $req['body']['html'] ) ) { + throw new MWException( "You must set an 'html' body key for this request" ); + } + if ( isset( $parts[6] ) ) { + $req['body']['oldid'] = $parts[6]; + } + } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) { + if ( !isset( $req['body']['wikitext'] ) ) { + throw new MWException( "You must set a 'wikitext' body key for this request" ); + } + $req['body']['wt'] = $req['body']['wikitext']; + unset( $req['body']['wikitext'] ); + } else { + throw new MWException( "Transformation unsupported" ); + } + } + + if ( isset( $this->params['HTTPProxy'] ) && $this->params['HTTPProxy'] ) { + $req['proxy'] = $this->params['HTTPProxy']; + } + if ( isset( $this->params['timeout'] ) ) { + $req['reqTimeout'] = $this->params['timeout']; + } + + // Forward cookies + if ( isset( $this->params['forwardCookies'] ) ) { + $req['headers']['Cookie'] = $this->params['forwardCookies']; + } + + $result[$key] = $req; + } + return $result; + } +} -- 2.20.1