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
This commit is contained in:
physikerwelt 2016-01-29 17:50:32 +01:00
parent c024c544c3
commit c77118d37d
4 changed files with 51 additions and 40 deletions

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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 ) );
}
}