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
This commit is contained in:
physikerwelt 2016-04-08 15:27:43 -04:00 committed by Mobrovac
parent dba01a653e
commit 2f8c92cb79
3 changed files with 104 additions and 32 deletions

View file

@ -88,6 +88,8 @@ class MathMathML extends MathRenderer {
$this->mathml = $rbi->getMathML();
$this->mathoidStyle = $rbi->getMathoidStyle();
$this->svgPath = $rbi->getFullSvgUrl();
} elseif ( $this->lastError === '' ) {
$this->doCheck();
}
$this->changed = false;
return $rbi->getSuccess();

View file

@ -594,17 +594,7 @@ abstract class MathRenderer {
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 @@ abstract class MathRenderer {
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;
}
}

View file

@ -29,6 +29,44 @@ class MathRestbaseInterface {
$this->type = $type;
}
/**
* 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 @@ class MathRestbaseInterface {
}
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 @@ class MathRestbaseInterface {
* @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 @@ class MathRestbaseInterface {
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 @@ class MathRestbaseInterface {
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." );
}
}