Fix RestbaseVirtualRESTService URL standardization
[lhc/web/wiklou.git] / includes / libs / virtualrest / RestbaseVirtualRESTService.php
index 16c9331..90865ff 100644 (file)
@@ -44,6 +44,9 @@ 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
@@ -54,14 +57,13 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                        'timeout' => 100,
                        'forwardCookies' => false,
                        'HTTPProxy' => null,
-                       'parsoidCompat' => false
+                       'parsoidCompat' => false,
+                       'fixedUrl' => false,
                ], $params );
                // Ensure that the url parameter has a trailing slash.
-               $mparams['url'] = preg_replace(
-                       '#/?$#',
-                       '/',
-                       $mparams['url']
-               );
+               if ( substr( $mparams['url'], -1 ) !== '/' ) {
+                       $mparams['url'] .= '/';
+               }
                // 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.
@@ -74,17 +76,24 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
        }
 
        public function onRequests( array $reqs, Closure $idGenFunc ) {
-
                if ( $this->params['parsoidCompat'] ) {
                        return $this->onParsoidRequests( $reqs, $idGenFunc );
                }
 
                $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,14 +108,12 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                }
 
                return $result;
-
        }
 
        /**
         * Remaps Parsoid v1/v3 requests to RESTBase v1 requests.
         */
        public function onParsoidRequests( array $reqs, Closure $idGeneratorFunc ) {
-
                $result = [];
                foreach ( $reqs as $key => $req ) {
                        $version = explode( '/', $req['url'] )[1];
@@ -120,7 +127,6 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                }
 
                return $result;
-
        }
 
        /**
@@ -212,7 +218,6 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                }
 
                return $req;
-
        }
 
        /**
@@ -230,7 +235,6 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
         *   * $revision is optional
         */
        public function onParsoid3Request( array $req, Closure $idGeneratorFunc ) {
-
                $parts = explode( '/', $req['url'] );
                list(
                        $targetWiki, // 'local'
@@ -261,7 +265,6 @@ class RestbaseVirtualRESTService extends VirtualRESTService {
                }
 
                return $req;
-
        }
 
 }