Merge "RestbaseVirtualRESTService: Support production URL layout"
[lhc/web/wiklou.git] / includes / libs / virtualrest / RestbaseVirtualRESTService.php
index 81442ad..3afbaa3 100644 (file)
@@ -44,18 +44,22 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
         *   - HTTPProxy      : HTTP proxy to use (optional)
         *   - parsoidCompat  : whether to parse URL as if they were meant for Parsoid
         *                       boolean (optional)
+        *   - fixedUrl       : Do not append domain to the url. For example to use
+        *                       English Wikipedia restbase, you would this to true
+        *                       and url to https://en.wikipedia.org/api/rest_#version#
         */
        public function __construct( array $params ) {
                // set up defaults and merge them with the given params
-               $mparams = array_merge( array(
+               $mparams = array_merge( [
                        'name' => 'restbase',
                        'url' => 'http://localhost:7231/',
                        'domain' => 'localhost',
                        'timeout' => 100,
                        'forwardCookies' => false,
                        'HTTPProxy' => null,
-                       'parsoidCompat' => false
-               ), $params );
+                       'parsoidCompat' => false,
+                       'fixedUrl' => false,
+               ], $params );
                // Ensure that the url parameter has a trailing slash.
                $mparams['url'] = preg_replace(
                        '#/?$#',
@@ -79,12 +83,20 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                        return $this->onParsoidRequests( $reqs, $idGenFunc );
                }
 
-               $result = array();
+               $result = [];
                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'];
+                       if ( $this->params['fixedUrl'] ) {
+                               $version = explode( '/', $req['url'] )[1];
+                               $req['url'] =
+                                       str_replace( '#version#', $version, $this->params['url'] ) .
+                                       preg_replace( '#^local/v./#', '', $req['url'] );
+                       } else {
+                               // 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'];
@@ -99,7 +111,6 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                }
 
                return $result;
-
        }
 
        /**
@@ -107,12 +118,12 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
         */
        public function onParsoidRequests( array $reqs, Closure $idGeneratorFunc ) {
 
-               $result = array();
+               $result = [];
                foreach ( $reqs as $key => $req ) {
-                       $parts = explode( '/', $req['url'] );
-                       if ( $parts[1] === 'v3' ) {
+                       $version = explode( '/', $req['url'] )[1];
+                       if ( $version === 'v3' ) {
                                $result[$key] = $this->onParsoid3Request( $req, $idGeneratorFunc );
-                       } elseif ( $parts[1] === 'v1' ) {
+                       } elseif ( $version === 'v1' ) {
                                $result[$key] = $this->onParsoid1Request( $req, $idGeneratorFunc );
                        } else {
                                throw new Exception( "Only v1 and v3 are supported." );