Mobrovac has submitted this change and it was merged.

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 MathMathML.php
M MathRenderer.php
M MathRestbaseInterface.php
3 files changed, 104 insertions(+), 32 deletions(-)

Approvals:
  Mobrovac: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/MathMathML.php b/MathMathML.php
index f669503..6d8f6a8 100644
--- a/MathMathML.php
+++ b/MathMathML.php
@@ -88,6 +88,8 @@
                                $this->mathml = $rbi->getMathML();
                                $this->mathoidStyle = $rbi->getMathoidStyle();
                                $this->svgPath = $rbi->getFullSvgUrl();
+                       } elseif ( $this->lastError === '' ) {
+                               $this->doCheck();
                        }
                        $this->changed = false;
                        return $rbi->getSuccess();
diff --git a/MathRenderer.php b/MathRenderer.php
index 8781b86..d51be40 100644
--- a/MathRenderer.php
+++ b/MathRenderer.php
@@ -594,17 +594,7 @@
                                        return true;
                                }
                        }
-                       $checker = new MathInputCheckRestbase( $this->tex, 
$this->getInputType(), $this->rbi );
-                       try {
-                               if ( $checker->isValid() ) {
-                                       $this->setTex( $checker->getValidTex() 
);
-                                       $this->texSecure = true;
-                                       return true;
-                               }
-                       } catch ( MWException $e ) {
-                       }
-                       $this->lastError = $checker->getError();
-                       return false;
+                       return $this->doCheck();
                }
        }
 
@@ -699,4 +689,22 @@
        public function getInputType() {
                return $this->inputType;
        }
+
+       /**
+        * @return bool
+        */
+       protected function doCheck() {
+               $checker = new MathInputCheckRestbase( $this->tex, 
$this->getInputType(), $this->rbi );
+               try {
+                       if ( $checker->isValid() ) {
+                               $this->setTex( $checker->getValidTex() );
+                               $this->texSecure = true;
+                               return true;
+                       }
+               }
+               catch ( MWException $e ) {
+               }
+               $this->lastError = $checker->getError();
+               return false;
+       }
 }
diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php
index 2273c2b..86ac502 100644
--- a/MathRestbaseInterface.php
+++ b/MathRestbaseInterface.php
@@ -30,6 +30,44 @@
        }
 
        /**
+        * Bundles several requests for fetching MathML.
+        * Does not send requests, if the the input TeX is invalid.
+        * @param $rbis
+        * @param $serviceClient
+        */
+       private static function batchGetMathML( $rbis, $serviceClient ) {
+               $requests = array();
+               $skips = array();
+               $i = 0;
+               foreach ( $rbis as $rbi ) {
+                       /** @var MathRestbaseInterface $rbi */
+                       if ( $rbi->checkTeX() ) {
+                               $requests[] = $rbi->getContentRequest( 'mml' );
+                       } else {
+                               $skips[] = $i;
+                       }
+                       $i ++;
+               }
+               $results = $serviceClient->runMulti( $requests );
+               $i = 0;
+               $j = 0;
+               foreach ( $results as $response ) {
+                       if ( !in_array( $i, $skips ) ) {
+                               /** @var MathRestbaseInterface $rbi */
+                               $rbi = $rbis[$i];
+                               try {
+                                       $mml = $rbi->evaluateContentResponse( 
'mml', $response, $requests[$j] );
+                                       $rbi->mml = $mml;
+                               }
+                               catch ( Exception $e ) {
+                               }
+                               $j ++;
+                       }
+                       $i ++;
+               }
+       }
+
+       /**
         * @return string MathML code
         * @throws MWException
         */
@@ -41,26 +79,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 +126,7 @@
         * @param array $rbis array of MathRestbaseInterface instances
         */
        public static function batchEvaluate( $rbis ) {
-               if ( count( $rbis ) == 0 ){
+               if ( count( $rbis ) == 0 ) {
                        return;
                }
                $requests = array();
@@ -120,9 +142,12 @@
                foreach ( $results as $response ) {
                        /** @var MathRestbaseInterface $rbi */
                        $rbi = $rbis[$i ++];
-                       $rbi->evaluateRestbaseCheckResponse( $response );
+                       try {
+                               $rbi->evaluateRestbaseCheckResponse( $response 
);
+                       } catch ( Exception $e ) {
+                       }
                }
-
+               self::batchGetMathML( $rbis, $serviceClient );
        }
 
        private function getServiceClient() {
@@ -347,4 +372,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: merged
Gerrit-Change-Id: Idfc29eeeca754738fe78ca0372e6b6725065528d
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Physikerwelt <[email protected]>
Gerrit-Reviewer: TheDJ <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to