From c77118d37d73ee49d200234df5d705f06af523a8 Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Fri, 29 Jan 2016 17:50:32 +0100 Subject: [PATCH] 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 --- MathInputCheckRestbase.php | 10 +++++++--- MathMathML.php | 37 ++++++++++++------------------------- MathRenderer.php | 29 ++++++++++++++++++++++++++++- MathRestbaseInterface.php | 15 ++++----------- 4 files changed, 51 insertions(+), 40 deletions(-) diff --git a/MathInputCheckRestbase.php b/MathInputCheckRestbase.php index eb70d66..8d21b63 100644 --- a/MathInputCheckRestbase.php +++ b/MathInputCheckRestbase.php @@ -16,11 +16,11 @@ class MathInputCheckRestbase extends MathInputCheck { * 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 @@ class MathInputCheckRestbase extends MathInputCheck { 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,28 +14,14 @@ use MediaWiki\Logger\LoggerFactory; 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; parent::__construct( $tex, $params ); @@ -49,6 +35,10 @@ class MathMathML extends MathRenderer { $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 @@ class MathMathML extends MathRenderer { * @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 @@ class MathMathML extends MathRenderer { * 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 @@ abstract class MathRenderer { 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 @@ abstract class MathRenderer { 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 @@ abstract class MathRenderer { $this->changed = true; } $this->mathStyle = $mathStyle; + if ( $mathStyle == 'inline' ){ + $this->inputType = 'inline-tex'; + } else { + $this->inputType = 'tex'; + } } /** @@ -572,11 +583,12 @@ abstract class MathRenderer { 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 @@ abstract class MathRenderer { 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 @@ class MathRestbaseInterface { /** * 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 @@ class MathRestbaseInterface { } private function setErrorMessage( $msg ) { - $this->error = (object)array( - 'error' => - (object)array( 'message' => $msg ) - ); + $this->error = (object)array( 'error' => (object)array( 'message' => $msg ) ); } }