Physikerwelt has uploaded a new change for review.

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

Change subject: Reduce the number or requests sent to Restbase
......................................................................

Reduce the number or requests sent to Restbase

Currently two post request and one get request is sent
for each formula.
This change gets rid of the second post request.

Change-Id: I805a2c0fa619c9143c38a0cc60e76c9480578ed3
---
M MathInputCheckRestbase.php
M MathMathML.php
M MathRenderer.php
M MathRestbaseInterface.php
4 files changed, 51 insertions(+), 40 deletions(-)


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

diff --git a/MathInputCheckRestbase.php b/MathInputCheckRestbase.php
index eb70d66..8d21b63 100644
--- a/MathInputCheckRestbase.php
+++ b/MathInputCheckRestbase.php
@@ -16,11 +16,11 @@
         * Default constructor
         * (performs no checking)
         * @param string $tex the TeX input string to be checked
-        * @param bool $displayStyle
+        * @param string $type
         */
-       public function __construct( $tex = '', $displayStyle = true ) {
+       public function __construct( $tex = '', $type = 'tex' ) {
                parent::__construct( $tex );
-               $this->restbaseInterface = new MathRestbaseInterface( $tex, 
$displayStyle );
+               $this->restbaseInterface = new MathRestbaseInterface( $tex, 
$type );
        }
 
        /**
@@ -82,4 +82,8 @@
                return $this->errorObjectToHtml( $err );
        }
 
+       public function getRbi() {
+               return $this->restbaseInterface;
+       }
+
 }
diff --git a/MathMathML.php b/MathMathML.php
index 7f3c185..515925e 100644
--- a/MathMathML.php
+++ b/MathMathML.php
@@ -14,27 +14,13 @@
 class MathMathML extends MathRenderer {
 
        protected $defaultAllowedRootElements = array( 'math' );
+       protected $restbaseInputTypes = array( 'tex', 'inline-tex' );
        protected $allowedRootElements = '';
        protected $hosts;
 
        /** @var boolean if false MathML output is not validated */
        private $XMLValidation = true;
-       protected $inputType = 'tex';
        private $svgPath = false;
-
-       /**
-        * @param string $inputType
-        */
-       public function setInputType( $inputType ) {
-               $this->inputType = $inputType;
-       }
-
-       /**
-        * @return string
-        */
-       public function getInputType() {
-               return $this->inputType;
-       }
 
        public function __construct( $tex = '', $params = array() ) {
                global $wgMathMathMLUrl;
@@ -48,6 +34,10 @@
                        } elseif ( $params['type'] == 'ascii' ) {
                                $this->inputType = 'ascii';
                        }
+               }
+               if ( !isset( $params['display'] ) && $this->getMathStyle() == 
'inlineDisplaystyle' ) {
+                       // default preserve the (broken) layout as it was
+                       $this->tex = '{\\displaystyle ' . $tex . '}';
                }
        }
 
@@ -86,17 +76,13 @@
         * @see MathRenderer::render()
        */
        public function render( $forceReRendering = false ) {
-               if ( $this->inputType == 'tex' && $this->mode == 'mathml' ) {
-                       $tex = $this->getTex();
-                       $displaystyle = false;
-                       if ( $this->getMathStyle() == 'inlineDisplaystyle' ) {
-                               // default preserve the (broken) layout as it 
was
-                               $tex = '{\\displaystyle ' . $tex . '}';
-                       } elseif ( $this->getMathStyle() !== 'inline' ) {
-                               $displaystyle = true;
+               if ( in_array( $this->inputType, $this->restbaseInputTypes ) && 
$this->mode == 'mathml' ) {
+                       $rbi = $this->rbi;
+                       if ( !$rbi ){
+                               $rbi = new MathRestbaseInterface( 
$this->getTex(), $this->getInputType() );
+                               $rbi->checkTeX();
                        }
-                       $rbi = new MathRestbaseInterface( $tex, $displaystyle );
-                       if ( $rbi->checkTeX() ) {
+                       if ( $rbi->getSuccess() ) {
                                $this->mathml = $rbi->getMathML();
                                $this->svg = $rbi->getSvg();
                                $this->svgPath = $rbi->getFullSvgUrl();
@@ -228,6 +214,7 @@
         * Calculates the HTTP POST Data for the request. Depends on the 
settings
         * and the input string only.
         * @return string HTTP POST data
+        * @throws MWException
         */
        public function getPostData() {
                $input = $this->getTex();
diff --git a/MathRenderer.php b/MathRenderer.php
index 0b49f12..e61a5b3 100644
--- a/MathRenderer.php
+++ b/MathRenderer.php
@@ -56,6 +56,10 @@
        protected $inputHash = '';
        /** @var string rendering mode */
        protected $mode = 'png';
+       /** @var string input type */
+       protected $inputType = 'tex';
+       /** @var MathRestbaseInterface used for checking */
+       protected $rbi;
 
        /**
         * Constructs a base MathRenderer
@@ -73,8 +77,10 @@
                        if ( $layoutMode == 'block' ) {
                                $this->mathStyle = 'display';
                                $tex = '{\displaystyle ' . $tex . '}';
+                               $this->inputType = 'tex';
                        } elseif ( $layoutMode == 'inline' ) {
                                $this->mathStyle = 'inline';
+                               $this->inputType = 'inline-tex';
                                $tex = '{\textstyle ' . $tex . '}';
                        } elseif ( $layoutMode == 'linebreak' ) {
                                $this->mathStyle = 'linebreak';
@@ -540,6 +546,11 @@
                        $this->changed = true;
                }
                $this->mathStyle = $mathStyle;
+               if ( $mathStyle == 'inline' ){
+                       $this->inputType = 'inline-tex';
+               } else {
+                       $this->inputType = 'tex';
+               }
        }
 
        /**
@@ -572,11 +583,12 @@
                                        return true;
                                }
                        }
-                       $checker = new MathInputCheckRestbase( 
$this->userInputTex );
+                       $checker = new MathInputCheckRestbase( $this->tex, 
$this->getInputType() );
                        try {
                                if ( $checker->isValid() ) {
                                        $this->setTex( $checker->getValidTex() 
);
                                        $this->texSecure = true;
+                                       $this->rbi = $checker->getRbi();
                                        return true;
                                }
                        } catch ( MWException $e ) {
@@ -659,4 +671,19 @@
                global $wgMathDisableTexFilter;
                return MathHooks::mathCheckToString( $wgMathDisableTexFilter );
        }
+
+
+       /**
+        * @param string $inputType
+        */
+       public function setInputType( $inputType ) {
+               $this->inputType = $inputType;
+       }
+
+       /**
+        * @return string
+        */
+       public function getInputType() {
+               return $this->inputType;
+       }
 }
diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php
index 3ee88db..2e24e22 100644
--- a/MathRestbaseInterface.php
+++ b/MathRestbaseInterface.php
@@ -20,15 +20,11 @@
        /**
         * MathRestbaseInterface constructor.
         * @param string $tex
-        * @param bool $displayStyle
+        * @param string $type
         */
-       public function __construct( $tex = '', $displayStyle = true ) {
+       public function __construct( $tex = '', $type = 'tex' ) {
                $this->tex = $tex;
-               if ( $displayStyle ) {
-                       $this->type = 'tex';
-               } else {
-                       $this->type = 'inline-tex';
-               }
+               $this->type = $type;
        }
 
        /**
@@ -294,10 +290,7 @@
        }
 
        private function setErrorMessage( $msg ) {
-               $this->error = (object)array(
-                               'error' =>
-                                               (object)array( 'message' => 
$msg )
-               );
+               $this->error = (object)array( 'error' => (object)array( 
'message' => $msg ) );
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I805a2c0fa619c9143c38a0cc60e76c9480578ed3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>

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

Reply via email to