diff options
Diffstat (limited to 'includes/libs/virtualrest')
3 files changed, 321 insertions, 13 deletions
diff --git a/includes/libs/virtualrest/ParsoidVirtualRESTService.php b/includes/libs/virtualrest/ParsoidVirtualRESTService.php new file mode 100644 index 00000000..32a27f79 --- /dev/null +++ b/includes/libs/virtualrest/ParsoidVirtualRESTService.php @@ -0,0 +1,126 @@ +<?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 { + /** + * Example requests: + * GET /local/v1/page/$title/html/$oldid + * * $oldid is optional + * POST /local/v1/transform/html/to/wikitext/$title/$oldid + * * body: array( 'html' => ... ) + * * $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 ) { + // for backwards compatibility: + if ( isset( $params['URL'] ) ) { + $params['url'] = $params['URL']; + unset( $params['URL'] ); + } + parent::__construct( $params ); + } + + public function onRequests( array $reqs, Closure $idGeneratorFunc ) { + $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 Exception( "Only 'local' target wiki is currently supported" ); + } elseif ( $version !== 'v1' ) { + throw new Exception( "Only version 1 exists" ); + } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) { + throw new Exception( "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 Exception( "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 Exception( "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 Exception( "You must set an 'html' body key for this request" ); + } + if ( isset( $parts[7] ) ) { + $req['body']['oldid'] = $parts[7]; + } + } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) { + if ( !isset( $req['body']['wikitext'] ) ) { + throw new Exception( "You must set a 'wikitext' body key for this request" ); + } + $req['body']['wt'] = $req['body']['wikitext']; + unset( $req['body']['wikitext'] ); + } else { + throw new Exception( "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; + } +} diff --git a/includes/libs/virtualrest/RestbaseVirtualRESTService.php b/includes/libs/virtualrest/RestbaseVirtualRESTService.php new file mode 100644 index 00000000..8fe5b921 --- /dev/null +++ b/includes/libs/virtualrest/RestbaseVirtualRESTService.php @@ -0,0 +1,177 @@ +<?php +/** + * Virtual HTTP service client for Restbase + * + * 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 Restbase + * @since 1.25 + */ +class RestbaseVirtualRESTService extends VirtualRESTService { + /** + * Example requests: + * GET /local/v1/page/{title}/html{/revision} + * POST /local/v1/transform/html/to/wikitext{/title}{/revision} + * * body: array( 'html' => ... ) + * POST /local/v1/transform/wikitext/to/html{/title}{/revision} + * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'bodyOnly' => true/false ) + * + * @param array $params Key/value map + * - url : Restbase server URL + * - domain : Wiki domain to use + * - timeout : request timeout in seconds (optional) + * - forwardCookies : cookies to forward to Restbase/Parsoid (as a Cookie + * header string) or false (optional) + * Note: forwardCookies will in the future be a boolean + * only, signifing request cookies should be forwarded + * to the service; the current state is due to the way + * VE handles this particular parameter + * - HTTPProxy : HTTP proxy to use (optional) + * - parsoidCompat : whether to parse URL as if they were meant for Parsoid + * boolean (optional) + */ + public function __construct( array $params ) { + // set up defaults and merge them with the given params + $mparams = array_merge( array( + 'url' => 'http://localhost:7231', + 'domain' => 'localhost', + 'timeout' => 100, + 'forwardCookies' => false, + 'HTTPProxy' => null, + 'parsoidCompat' => false + ), $params ); + // ensure the correct domain format + $mparams['domain'] = preg_replace( + '/^(https?:\/\/)?([^\/:]+?)(\/|:\d+\/?)?$/', + '$2', + $mparams['domain'] + ); + parent::__construct( $mparams ); + } + + public function onRequests( array $reqs, Closure $idGenFunc ) { + + if ( $this->params['parsoidCompat'] ) { + return $this->onParsoidRequests( $reqs, $idGenFunc ); + } + + $result = array(); + foreach ( $reqs as $key => $req ) { + // 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'] ) { + $req['proxy'] = $this->params['HTTPProxy']; + } + if ( $this->params['timeout'] != null ) { + $req['reqTimeout'] = $this->params['timeout']; + } + if ( $this->params['forwardCookies'] ) { + $req['headers']['Cookie'] = $this->params['forwardCookies']; + } + $result[$key] = $req; + } + + return $result; + + } + + /** + * Remaps Parsoid requests to Restbase paths + */ + public function onParsoidRequests( array $reqs, Closure $idGeneratorFunc ) { + + $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 Exception( "Only 'local' target wiki is currently supported" ); + } elseif ( $reqType !== 'page' && $reqType !== 'transform' ) { + throw new Exception( "Request type must be either 'page' or 'transform'" ); + } + $req['url'] = $this->params['url'] . '/' . $this->params['domain'] . '/v1/' . $reqType . '/'; + if ( $reqType === 'page' ) { + $title = $parts[3]; + if ( $parts[4] !== 'html' ) { + throw new Exception( "Only 'html' output format is currently supported" ); + } + $req['url'] .= 'html/' . $title; + if ( isset( $parts[5] ) ) { + $req['url'] .= '/' . $parts[5]; + } elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) { + $req['url'] .= '/' . $req['query']['oldid']; + unset( $req['query']['oldid'] ); + } + } elseif ( $reqType === 'transform' ) { + // from / to transform + $req['url'] .= $parts[3] . '/to/' . $parts[5]; + // the title + if ( isset( $parts[6] ) ) { + $req['url'] .= '/' . $parts[6]; + } + // revision id + if ( isset( $parts[7] ) ) { + $req['url'] .= '/' . $parts[7]; + } elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) { + $req['url'] .= '/' . $req['body']['oldid']; + unset( $req['body']['oldid'] ); + } + if ( $parts[4] !== 'to' ) { + throw new Exception( "Part index 4 is not 'to'" ); + } + if ( $parts[3] === 'html' & $parts[5] === 'wikitext' ) { + if ( !isset( $req['body']['html'] ) ) { + throw new Exception( "You must set an 'html' body key for this request" ); + } + } elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) { + if ( !isset( $req['body']['wikitext'] ) ) { + throw new Exception( "You must set a 'wikitext' body key for this request" ); + } + if ( isset( $req['body']['body'] ) ) { + $req['body']['bodyOnly'] = $req['body']['body']; + unset( $req['body']['body'] ); + } + } else { + throw new Exception( "Transformation unsupported" ); + } + } + // set the appropriate proxy, timeout and headers + if ( $this->params['HTTPProxy'] ) { + $req['proxy'] = $this->params['HTTPProxy']; + } + if ( $this->params['timeout'] != null ) { + $req['reqTimeout'] = $this->params['timeout']; + } + if ( $this->params['forwardCookies'] ) { + $req['headers']['Cookie'] = $this->params['forwardCookies']; + } + $result[$key] = $req; + } + + return $result; + + } + +} diff --git a/includes/libs/virtualrest/VirtualRESTServiceClient.php b/includes/libs/virtualrest/VirtualRESTServiceClient.php index 2d21d3cf..e8bb38d8 100644 --- a/includes/libs/virtualrest/VirtualRESTServiceClient.php +++ b/includes/libs/virtualrest/VirtualRESTServiceClient.php @@ -125,17 +125,17 @@ class VirtualRESTServiceClient { * - reason : HTTP response reason (empty if there was a serious cURL error) * - headers : <header name/value associative array> * - body : HTTP response body or resource (if "stream" was set) - * - err : Any cURL error string + * - error : Any cURL error string * The map also stores integer-indexed copies of these values. This lets callers do: * <code> * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $client->run( $req ); * </code> - * @param array $req Virtual HTTP request array + * @param array $req Virtual HTTP request maps * @return array Response array for request */ public function run( array $req ) { - $req = $this->runMulti( array( $req ) ); - return $req[0]['response']; + $responses = $this->runMulti( array( $req ) ); + return $responses[0]; } /** @@ -146,14 +146,15 @@ class VirtualRESTServiceClient { * - reason : HTTP response reason (empty if there was a serious cURL error) * - headers : <header name/value associative array> * - body : HTTP response body or resource (if "stream" was set) - * - err : Any cURL error string + * - error : Any cURL error string * The map also stores integer-indexed copies of these values. This lets callers do: - * <code> - * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $responses[0]; + * <code> + * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $responses[0]; * </code> * - * @param array $req Map of Virtual HTTP request arrays + * @param array $reqs Map of Virtual HTTP request maps * @return array $reqs Map of corresponding response values with the same keys/order + * @throws Exception */ public function runMulti( array $reqs ) { foreach ( $reqs as $index => &$req ) { @@ -207,6 +208,9 @@ class VirtualRESTServiceClient { if ( ++$rounds > 5 ) { // sanity throw new Exception( "Too many replacement rounds detected. Aborting." ); } + // Track requests executed this round that have a prefix/service. + // Note that this also includes requests where 'response' was forced. + $checkReqIndexesByPrefix = array(); // Resolve the virtual URLs valid and qualified HTTP(S) URLs // and add any required authentication headers for the backend. // Services can also replace requests with new ones, either to @@ -219,7 +223,7 @@ class VirtualRESTServiceClient { if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) { // A current or original request which was not modified } else { - // Replacement requests with pre-set responses should not execute + // Replacement request that will convert to original requests $newReplaceReqsByService[$prefix][$index] = $req; } if ( isset( $req['response'] ) ) { @@ -231,6 +235,7 @@ class VirtualRESTServiceClient { // Original or mangled request included $executeReqs[$index] = $req; } + $checkReqIndexesByPrefix[$prefix][$index] = 1; } } // Update index of requests to inspect for replacement @@ -245,12 +250,12 @@ class VirtualRESTServiceClient { // defer the original or to set a proxy response to the original. // Any replacement requests executed above will need to be replaced // with new requests (eventually the original). The responses can be - // forced instead of having the request sent over the wire. + // forced by setting 'response' rather than actually be sent over the wire. $newReplaceReqsByService = array(); - foreach ( $replaceReqsByService as $prefix => $servReqs ) { + foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) { $service = $this->instances[$prefix]; - // Only the request copies stored in $doneReqs actually have the response - $servReqs = array_intersect_key( $doneReqs, $servReqs ); + // $doneReqs actually has the requests (with 'response' set) + $servReqs = array_intersect_key( $doneReqs, $servReqIndexes ); foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) { // Services use unique IDs for replacement requests if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) { |