diff --git a/MathMathML.php b/MathMathML.php index f66950338..6d8f6a832 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -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(); diff --git a/MathRenderer.php b/MathRenderer.php index 8781b86d2..d51be40ad 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -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; + } } diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php index 2273c2bff..86ac50272 100644 --- a/MathRestbaseInterface.php +++ b/MathRestbaseInterface.php @@ -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." ); + } }