2015-11-26 19:31:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
|
|
|
* (c) 2002-2015 various MediaWiki contributors
|
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
|
|
|
class MathRestbaseInterface {
|
|
|
|
private $hash = false;
|
|
|
|
private $tex;
|
|
|
|
private $type;
|
|
|
|
private $checkedTex;
|
|
|
|
private $success;
|
|
|
|
private $identifiers;
|
|
|
|
private $error;
|
2016-01-31 21:11:39 +00:00
|
|
|
private $mathoidStyle;
|
|
|
|
private $mml;
|
2016-06-02 14:30:48 +00:00
|
|
|
/** @var boolean is there a request to purge the existing mathematical content */
|
|
|
|
private $purge = false;
|
2015-11-26 19:31:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MathRestbaseInterface constructor.
|
|
|
|
* @param string $tex
|
2016-01-29 16:50:32 +00:00
|
|
|
* @param string $type
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
2016-01-29 16:50:32 +00:00
|
|
|
public function __construct( $tex = '', $type = 'tex' ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
$this->tex = $tex;
|
2016-01-29 16:50:32 +00:00
|
|
|
$this->type = $type;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:27:43 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$requests = [];
|
|
|
|
$skips = [];
|
2016-04-08 19:27:43 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ( $rbis as $rbi ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
2016-04-30 00:18:41 +00:00
|
|
|
if ( $rbi->getSuccess() ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
$requests[] = $rbi->getContentRequest( 'mml' );
|
|
|
|
} else {
|
|
|
|
$skips[] = $i;
|
|
|
|
}
|
|
|
|
$i ++;
|
|
|
|
}
|
|
|
|
$results = $serviceClient->runMulti( $requests );
|
2016-06-01 17:47:20 +00:00
|
|
|
$lenRbis = count( $rbis );
|
2016-04-08 19:27:43 +00:00
|
|
|
$j = 0;
|
2016-06-01 17:47:20 +00:00
|
|
|
for ( $i = 0; $i < $lenRbis; $i ++ ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
if ( !in_array( $i, $skips ) ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
|
|
|
$rbi = $rbis[$i];
|
|
|
|
try {
|
2016-06-01 17:47:20 +00:00
|
|
|
$mml = $rbi->evaluateContentResponse( 'mml', $results[$j], $requests[$j] );
|
2016-04-08 19:27:43 +00:00
|
|
|
$rbi->mml = $mml;
|
|
|
|
}
|
|
|
|
catch ( Exception $e ) {
|
|
|
|
}
|
|
|
|
$j ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 14:30:48 +00:00
|
|
|
/**
|
|
|
|
* Lets this instance know if this is a purge request. When set to true,
|
|
|
|
* it will cause the object to issue the first content request with a
|
|
|
|
* 'Cache-Control: no-cache' header to prompt the regeneration of the
|
|
|
|
* renders.
|
|
|
|
*
|
|
|
|
* @param bool $purge whether this is a purge request
|
|
|
|
*/
|
|
|
|
public function setPurge( $purge = true ) {
|
|
|
|
$this->purge = $purge;
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:31:09 +00:00
|
|
|
/**
|
|
|
|
* @return string MathML code
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getMathML() {
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( !$this->mml ){
|
|
|
|
$this->mml = $this->getContent( 'mml' );
|
|
|
|
}
|
|
|
|
return $this->mml;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getContent( $type ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
$request = $this->getContentRequest( $type );
|
2015-11-26 19:31:09 +00:00
|
|
|
$serviceClient = $this->getServiceClient();
|
|
|
|
$response = $serviceClient->run( $request );
|
2016-04-08 19:27:43 +00:00
|
|
|
return $this->evaluateContentResponse( $type, $response, $request );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateHash() {
|
|
|
|
if ( !$this->hash ) {
|
|
|
|
if ( !$this->checkTeX() ) {
|
|
|
|
throw new MWException( "TeX input is invalid." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkTeX() {
|
2016-01-31 21:11:39 +00:00
|
|
|
$request = $this->getCheckRequest();
|
|
|
|
$requestResult = $this->executeRestbaseCheckRequest( $request );
|
|
|
|
return $this->evaluateRestbaseCheckResponse( $requestResult );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a service request
|
|
|
|
* Generates error messages on failure
|
|
|
|
* @see Http::post()
|
|
|
|
*
|
2016-01-31 21:11:39 +00:00
|
|
|
* @param array $request the request object
|
2015-11-26 19:31:09 +00:00
|
|
|
* @return bool success
|
|
|
|
*/
|
2016-01-31 21:11:39 +00:00
|
|
|
private function executeRestbaseCheckRequest( $request ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
$res = null;
|
|
|
|
$serviceClient = $this->getServiceClient();
|
2016-01-31 21:11:39 +00:00
|
|
|
$response = $serviceClient->run( $request );
|
|
|
|
if ( $response['code'] !== 200 ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->log()->info( 'Tex check failed:', [
|
2016-01-31 21:11:39 +00:00
|
|
|
'post' => $request['body'],
|
|
|
|
'error' => $response['error'],
|
|
|
|
'url' => $request['url']
|
2016-04-12 20:53:25 +00:00
|
|
|
] );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $rbis array of MathRestbaseInterface instances
|
|
|
|
*/
|
|
|
|
public static function batchEvaluate( $rbis ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
if ( count( $rbis ) == 0 ) {
|
2016-01-31 21:11:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
$requests = [];
|
2016-01-31 21:11:39 +00:00
|
|
|
/** @var MathRestbaseInterface $first */
|
|
|
|
$first = $rbis[0];
|
|
|
|
$serviceClient = $first->getServiceClient();
|
|
|
|
foreach ( $rbis as $rbi ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
|
|
|
$requests[] = $rbi->getCheckRequest();
|
|
|
|
}
|
|
|
|
$results = $serviceClient->runMulti( $requests );
|
|
|
|
$i = 0;
|
|
|
|
foreach ( $results as $response ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
|
|
|
$rbi = $rbis[$i ++];
|
2016-04-08 19:27:43 +00:00
|
|
|
try {
|
|
|
|
$rbi->evaluateRestbaseCheckResponse( $response );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
self::batchGetMathML( $rbis, $serviceClient );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getServiceClient() {
|
2016-04-13 19:28:24 +00:00
|
|
|
global $wgVirtualRestConfig, $wgMathConcurrentReqs;
|
|
|
|
$http = new MultiHttpClient( [ 'maxConnsPerHost' => $wgMathConcurrentReqs ] );
|
|
|
|
$serviceClient = new VirtualRESTServiceClient( $http );
|
2015-11-26 19:31:09 +00:00
|
|
|
if ( isset( $wgVirtualRestConfig['modules']['restbase'] ) ) {
|
|
|
|
$cfg = $wgVirtualRestConfig['modules']['restbase'];
|
|
|
|
$cfg['parsoidCompat'] = false;
|
|
|
|
$vrsObject = new RestbaseVirtualRESTService( $cfg );
|
|
|
|
$serviceClient->mount( '/mathoid/', $vrsObject );
|
|
|
|
}
|
|
|
|
return $serviceClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL is generated accoding to the following logic:
|
|
|
|
*
|
|
|
|
* Case A: <code>$internal = false</code>, which means one needs an URL that is accessible from
|
|
|
|
* outside:
|
|
|
|
*
|
|
|
|
* --> If <code>$wgMathFullRestbaseURL</code> is configured use it, otherwise fall back try to
|
|
|
|
* <code>$wgVisualEditorFullRestbaseURL</code>. (Note, that this is not be worse than failing
|
|
|
|
* immediately.)
|
|
|
|
*
|
|
|
|
* Case B: <code> $internal= true</code>, which means one needs to access content from Restbase
|
|
|
|
* which does not need to be accessible from outside:
|
|
|
|
*
|
|
|
|
* --> Use the mount point whenever possible. If the mount point is not available, use
|
|
|
|
* <code>$wgMathFullRestbaseURL</code> with fallback to <code>wgVisualEditorFullRestbaseURL</code>
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param bool|true $internal
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2015-12-11 07:23:45 +00:00
|
|
|
public function getUrl( $path, $internal = true ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
global $wgVirtualRestConfig, $wgMathFullRestbaseURL, $wgVisualEditorFullRestbaseURL;
|
|
|
|
if ( $internal && isset( $wgVirtualRestConfig['modules']['restbase'] ) ) {
|
|
|
|
return "/mathoid/local/v1/$path";
|
|
|
|
}
|
|
|
|
if ( $wgMathFullRestbaseURL ) {
|
|
|
|
return "{$wgMathFullRestbaseURL}v1/$path";
|
|
|
|
}
|
|
|
|
if ( $wgVisualEditorFullRestbaseURL ) {
|
|
|
|
return "{$wgVisualEditorFullRestbaseURL}v1/$path";
|
|
|
|
}
|
2015-12-11 07:23:45 +00:00
|
|
|
$msg = 'Math extension can not find Restbase URL. Please specify $wgMathFullRestbaseURL.';
|
|
|
|
$this->setErrorMessage( $msg );
|
|
|
|
throw new MWException( $msg );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
private function log() {
|
|
|
|
return LoggerFactory::getInstance( 'Math' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSvg() {
|
|
|
|
return $this->getContent( 'svg' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool|false $skipConfigCheck
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkBackend( $skipConfigCheck = false ) {
|
|
|
|
try {
|
2016-04-12 20:53:25 +00:00
|
|
|
$request = [
|
2015-11-26 19:31:09 +00:00
|
|
|
'method' => 'GET',
|
2015-12-11 07:23:45 +00:00
|
|
|
'url' => $this->getUrl( '?spec' )
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2015-11-26 19:31:09 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$serviceClient = $this->getServiceClient();
|
|
|
|
$response = $serviceClient->run( $request );
|
|
|
|
if ( $response['code'] === 200 ) {
|
|
|
|
return $skipConfigCheck || $this->checkConfig();
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->log()->error( "Restbase backend is not correctly set up.", [
|
2015-11-26 19:31:09 +00:00
|
|
|
'request' => $request,
|
|
|
|
'response' => $response
|
2016-04-12 20:53:25 +00:00
|
|
|
] );
|
2015-11-26 19:31:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a unique TeX string, renders it and gets it via a public URL.
|
|
|
|
* The method fails, if the public URL does not point to the same server, who did render
|
|
|
|
* the unique TeX input in the first place.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function checkConfig() {
|
|
|
|
// Generates a TeX string that probably has not been generated before
|
|
|
|
$uniqueTeX = uniqid( 't=', true );
|
|
|
|
$testInterface = new MathRestbaseInterface( $uniqueTeX );
|
|
|
|
if ( ! $testInterface->checkTeX() ){
|
|
|
|
$this->log()->warning( 'Config check failed, since test expression was considered as invalid.',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'uniqueTeX' => $uniqueTeX ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$url = $testInterface->getFullSvgUrl();
|
|
|
|
$req = MWHttpRequest::factory( $url );
|
|
|
|
$status = $req->execute();
|
|
|
|
if ( $status->isOK() ){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->log()->warning( 'Config check failed, due to an invalid response code.',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'responseCode' => $status ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
} catch ( Exception $e ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->log()->warning( 'Config check failed, due to an exception.', [ $e ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a publicly accessible link to the generated SVG image.
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getFullSvgUrl() {
|
|
|
|
$this->calculateHash();
|
2015-12-11 07:23:45 +00:00
|
|
|
return $this->getUrl( "media/math/render/svg/{$this->hash}", false );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCheckedTex() {
|
|
|
|
return $this->checkedTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getSuccess() {
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( $this->success === null ) {
|
|
|
|
$this->checkTeX();
|
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIdentifiers() {
|
|
|
|
return $this->identifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function getError() {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTex() {
|
|
|
|
return $this->tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:23:45 +00:00
|
|
|
private function setErrorMessage( $msg ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->error = (object)[ 'error' => (object)[ 'message' => $msg ] ];
|
2015-12-11 07:23:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 21:11:39 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getCheckRequest() {
|
2016-04-12 20:53:25 +00:00
|
|
|
$request = [
|
2016-01-31 21:11:39 +00:00
|
|
|
'method' => 'POST',
|
2016-04-12 20:53:25 +00:00
|
|
|
'body' => [
|
2016-01-31 21:11:39 +00:00
|
|
|
'type' => $this->type,
|
|
|
|
'q' => $this->tex
|
2016-04-12 20:53:25 +00:00
|
|
|
],
|
2016-01-31 21:11:39 +00:00
|
|
|
'url' => $this->getUrl( "media/math/check/{$this->type}" )
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2016-01-31 21:11:39 +00:00
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $response
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function evaluateRestbaseCheckResponse( $response ) {
|
|
|
|
$json = json_decode( $response['body'] );
|
|
|
|
if ( $response['code'] === 200 ) {
|
|
|
|
$headers = $response['headers'];
|
|
|
|
$this->hash = $headers['x-resource-location'];
|
|
|
|
$this->success = $json->success;
|
|
|
|
$this->checkedTex = $json->checked;
|
|
|
|
$this->identifiers = $json->identifiers;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ( isset( $json->detail ) && isset( $json->detail->success ) ) {
|
|
|
|
$this->success = $json->detail->success;
|
|
|
|
$this->error = $json->detail;
|
|
|
|
} else {
|
|
|
|
$this->success = false;
|
|
|
|
$this->setErrorMessage( 'Math extension cannot connect to Restbase.' );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getMathoidStyle() {
|
|
|
|
return $this->mathoidStyle;
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @return array
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
private function getContentRequest( $type ) {
|
|
|
|
$this->calculateHash();
|
2016-04-12 20:53:25 +00:00
|
|
|
$request = [
|
2016-04-08 19:27:43 +00:00
|
|
|
'method' => 'GET',
|
|
|
|
'url' => $this->getUrl( "media/math/render/$type/{$this->hash}" )
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2016-06-02 14:30:48 +00:00
|
|
|
if ( $this->purge ) {
|
|
|
|
$request['headers'] = [
|
|
|
|
'Cache-Control' => 'no-cache'
|
|
|
|
];
|
|
|
|
$this->purge = false;
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
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'];
|
|
|
|
}
|
2016-06-23 22:08:03 +00:00
|
|
|
// Remove "convenience" duplicate keys put in place by MultiHttpClient
|
|
|
|
unset( $response[0], $response[1], $response[2], $response[3], $response[4] );
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->log()->error( 'Restbase math server problem:', [
|
2016-04-08 19:27:43 +00:00
|
|
|
'request' => $request,
|
|
|
|
'response' => $response,
|
2016-06-23 22:08:03 +00:00
|
|
|
'math_type' => $type,
|
2016-04-08 19:27:43 +00:00
|
|
|
'tex' => $this->tex
|
2016-04-12 20:53:25 +00:00
|
|
|
] );
|
2016-08-17 17:41:04 +00:00
|
|
|
self::throwContentError( $type, $response['body'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $body
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public static function throwContentError( $type, $body ) {
|
|
|
|
$detail = 'Server problem.';
|
|
|
|
$json = json_decode( $body );
|
|
|
|
if ( isset( $json->detail ) ) {
|
|
|
|
if ( is_array( $json->detail ) ){
|
|
|
|
$detail = $json->detail[0];
|
|
|
|
} elseif ( is_string( $json->detail ) ) {
|
|
|
|
$detail = $json->detail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new MWException( "Cannot get $type. $detail" );
|
2016-04-08 19:27:43 +00:00
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|