Physikerwelt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/282413

Change subject: Reduce number of Restbase requests
......................................................................

Reduce number of Restbase requests

This change reduces the number of Restbase requests to two
by bundling all requests for MathML

Bug: T132096
Change-Id: Idfc29eeeca754738fe78ca0372e6b6725065528d
---
M MathRestbaseInterface.php
1 file changed, 57 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math 
refs/changes/13/282413/1

diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php
index 2273c2b..c407630 100644
--- a/MathRestbaseInterface.php
+++ b/MathRestbaseInterface.php
@@ -41,26 +41,10 @@
        }
 
        private function getContent( $type ) {
-               $this->calculateHash();
-               $request = array(
-                       'method' => 'GET',
-                       'url'    => $this->getUrl( 
"media/math/render/$type/{$this->hash}" )
-               );
+               $request = $this->getContentRequest( $type );
                $serviceClient = $this->getServiceClient();
                $response = $serviceClient->run( $request );
-               if ( $response['code'] === 200 ) {
-                       if ( array_key_exists( 'x-mathoid-style', 
$response['headers'] ) ) {
-                               $this->mathoidStyle =  
$response['headers']['x-mathoid-style'];
-                       }
-                       return $response['body'];
-               }
-               $this->log()->error( 'Restbase math server problem:', array(
-                       'request'  => $request,
-                       'response' => $response,
-                       'type'     => $type,
-                       'tex'      => $this->tex
-               ) );
-               throw new MWException( "Cannot get $type. Server problem." );
+               return $this->evaluateContentResponse( $type, $response, 
$request );
        }
 
        private function calculateHash() {
@@ -104,7 +88,7 @@
         * @param array $rbis array of MathRestbaseInterface instances
         */
        public static function batchEvaluate( $rbis ) {
-               if ( count( $rbis ) == 0 ){
+               if ( count( $rbis ) == 0 ) {
                        return;
                }
                $requests = array();
@@ -121,6 +105,23 @@
                        /** @var MathRestbaseInterface $rbi */
                        $rbi = $rbis[$i ++];
                        $rbi->evaluateRestbaseCheckResponse( $response );
+               }
+               $requests = array();
+               foreach ( $rbis as $rbi ) {
+                       /** @var MathRestbaseInterface $rbi */
+                       $requests[] = $rbi->getContentRequest( 'mml' );
+               }
+               $results = $serviceClient->runMulti( $requests );
+               $i = 0;
+               foreach ( $results as $response ) {
+                       /** @var MathRestbaseInterface $rbi */
+                       $rbi = $rbis[$i];
+                       try {
+                               $mml = $rbi->evaluateContentResponse( 'mml', 
$response, $requests[$i] );
+                               $rbi->mml = $mml;
+                       } catch ( Exception $e ) {
+                       }
+                       $i ++;
                }
 
        }
@@ -347,4 +348,41 @@
        public function getMathoidStyle() {
                return $this->mathoidStyle;
        }
+
+       /**
+        * @param $type
+        * @return array
+        * @throws MWException
+        */
+       private function getContentRequest( $type ) {
+               $this->calculateHash();
+               $request = array(
+                       'method' => 'GET',
+                       'url' => $this->getUrl( 
"media/math/render/$type/{$this->hash}" )
+               );
+               return $request;
+       }
+
+       /**
+        * @param $type
+        * @param $response
+        * @param $request
+        * @return mixed
+        * @throws MWException
+        */
+       private function evaluateContentResponse( $type, $response, $request ) {
+               if ( $response['code'] === 200 ) {
+                       if ( array_key_exists( 'x-mathoid-style', 
$response['headers'] ) ) {
+                               $this->mathoidStyle = 
$response['headers']['x-mathoid-style'];
+                       }
+                       return $response['body'];
+               }
+               $this->log()->error( 'Restbase math server problem:', array(
+                       'request' => $request,
+                       'response' => $response,
+                       'type' => $type,
+                       'tex' => $this->tex
+               ) );
+               throw new MWException( "Cannot get $type. Server problem." );
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/282413
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idfc29eeeca754738fe78ca0372e6b6725065528d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <w...@physikerwelt.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to