Merge "Revert "Revert "Add getMultiHttpClient function to make HTTP requests to Mathoid."""

This commit is contained in:
jenkins-bot 2023-05-17 13:56:41 +00:00 committed by Gerrit Code Review
commit edc480ae62
2 changed files with 100 additions and 29 deletions

View file

@ -13,9 +13,7 @@ use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MWException;
use Psr\Log\LoggerInterface;
use RestbaseVirtualRESTService;
use stdClass;
use VirtualRESTServiceClient;
class MathRestbaseInterface {
/** @var string|false */
@ -57,9 +55,9 @@ class MathRestbaseInterface {
* Bundles several requests for fetching MathML.
* Does not send requests, if the input TeX is invalid.
* @param MathRestbaseInterface[] $rbis
* @param VirtualRESTServiceClient $serviceClient
* @param \MultiHttpClient $multiHttpClient
*/
private static function batchGetMathML( array $rbis, VirtualRESTServiceClient $serviceClient ) {
private static function batchGetMathML( array $rbis, \MultiHttpClient $multiHttpClient ) {
$requests = [];
$skips = [];
$i = 0;
@ -72,7 +70,7 @@ class MathRestbaseInterface {
}
$i++;
}
$results = $serviceClient->runMulti( $requests );
$results = $multiHttpClient->runMulti( $requests );
$lenRbis = count( $rbis );
$j = 0;
for ( $i = 0; $i < $lenRbis; $i++ ) {
@ -80,7 +78,8 @@ class MathRestbaseInterface {
/** @var MathRestbaseInterface $rbi */
$rbi = $rbis[$i];
try {
$mml = $rbi->evaluateContentResponse( 'mml', $results[$j], $requests[$j] );
$response = $results[ $j ][ 'response' ];
$mml = $rbi->evaluateContentResponse( 'mml', $response, $requests[$j] );
$rbi->mml = $mml;
} catch ( Exception $e ) {
// FIXME: Why is this silenced? Doesn't this leave invalid data behind?
@ -115,8 +114,8 @@ class MathRestbaseInterface {
private function getContent( $type ) {
$request = $this->getContentRequest( $type );
$serviceClient = $this->getServiceClient();
$response = $serviceClient->run( $request );
$multiHttpClient = $this->getMultiHttpClient();
$response = $multiHttpClient->run( $request );
return $this->evaluateContentResponse( $type, $response, $request );
}
@ -144,8 +143,8 @@ class MathRestbaseInterface {
*/
private function executeRestbaseCheckRequest( $request ) {
$res = null;
$serviceClient = $this->getServiceClient();
$response = $serviceClient->run( $request );
$multiHttpClient = $this->getMultiHttpClient();
$response = $multiHttpClient->run( $request );
if ( $response['code'] !== 200 ) {
$this->log()->info( 'Tex check failed', [
'post' => $request['body'],
@ -166,39 +165,31 @@ class MathRestbaseInterface {
$requests = [];
/** @var MathRestbaseInterface $first */
$first = $rbis[0];
$serviceClient = $first->getServiceClient();
$multiHttpClient = $first->getMultiHttpClient();
foreach ( $rbis as $rbi ) {
/** @var MathRestbaseInterface $rbi */
$requests[] = $rbi->getCheckRequest();
}
$results = $serviceClient->runMulti( $requests );
$results = $multiHttpClient->runMulti( $requests );
$i = 0;
foreach ( $results as $response ) {
foreach ( $results as $requestResponse ) {
/** @var MathRestbaseInterface $rbi */
$rbi = $rbis[$i++];
try {
$response = $requestResponse[ 'response' ];
$rbi->evaluateRestbaseCheckResponse( $response );
} catch ( Exception $e ) {
}
}
self::batchGetMathML( $rbis, $serviceClient );
self::batchGetMathML( $rbis, $multiHttpClient );
}
/**
* @return VirtualRESTServiceClient
*/
private function getServiceClient() {
global $wgVirtualRestConfig, $wgMathConcurrentReqs;
$http = MediaWikiServices::getInstance()->getHttpRequestFactory()->createMultiClient(
private function getMultiHttpClient() {
global $wgMathConcurrentReqs;
$multiHttpClient = MediaWikiServices::getInstance()->getHttpRequestFactory()->createMultiClient(
[ 'maxConnsPerHost' => $wgMathConcurrentReqs ] );
$serviceClient = new VirtualRESTServiceClient( $http );
if ( isset( $wgVirtualRestConfig['modules']['restbase'] ) ) {
$cfg = $wgVirtualRestConfig['modules']['restbase'];
$cfg['parsoidCompat'] = false;
$vrsObject = new RestbaseVirtualRESTService( $cfg );
$serviceClient->mount( '/mathoid/', $vrsObject );
}
return $serviceClient;
return $multiHttpClient;
}
/**
@ -227,7 +218,10 @@ class MathRestbaseInterface {
global $wgMathUseInternalRestbasePath, $wgVirtualRestConfig, $wgMathFullRestbaseURL,
$wgVisualEditorFullRestbaseURL;
if ( $internal && $wgMathUseInternalRestbasePath && isset( $wgVirtualRestConfig['modules']['restbase'] ) ) {
return "/mathoid/local/v1/$path";
$restBaseUrl = $wgVirtualRestConfig['modules']['restbase']['url'];
$restBaseDomain = $wgVirtualRestConfig['modules']['restbase']['domain'] ?? 'localhost';
$restBaseUrl = rtrim( $restBaseUrl, '/' );
return "$restBaseUrl/$restBaseDomain/v1/$path";
}
if ( $wgMathFullRestbaseURL ) {
return "{$wgMathFullRestbaseURL}v1/$path";

View file

@ -335,4 +335,81 @@ class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
$rbi->getMathML();
}
public function dataProviderForTestGetUrl() {
$path = 'media/math/render/svg/2uejd9dj3jd';
yield 'Math FullRestbaseURL default case' => [
$path, false, [], 'https://wikimedia.org/api/rest_v1/media/math/render/svg/2uejd9dj3jd'
];
yield 'Math FullRestbaseURL case' => [ $path, false, [
'MathFullRestbaseURL' => "https://myWiki.test/",
'VisualEditorFullRestbaseURL' => 'VisualEditor/api/rest_' // This should be ignored
], 'https://myWiki.test/v1/media/math/render/svg/2uejd9dj3jd' ];
yield 'VirtualRestConfig case' => [
$path,
true,
[
'MathUseInternalRestbasePath' => true,
'VirtualRestConfig' => [
'modules' => [ 'restbase' => [ 'url' => 'http://restbase.test.internal/api/' ] ]
],
'MathFullRestbaseURL' => "https://myWiki.test/",
'VisualEditorFullRestbaseURL' => 'VisualEditor/api/rest_' // This should be ignored
],
'http://restbase.test.internal/api/localhost/v1/media/math/render/svg/2uejd9dj3jd'
];
yield 'VirtualRestConfig case with Domain' => [
$path,
true,
[
'MathUseInternalRestbasePath' => true,
'VirtualRestConfig' => [
'modules' => [
'restbase' => [
'url' => 'http://restbase.test.internal/api', // Should work with trailing slash '/'
'domain' => 'testDomain'
]
]
],
'MathFullRestbaseURL' => "https://myWiki.test/",
'VisualEditorFullRestbaseURL' => 'VisualEditor/api/rest_' // This should be ignored
],
'http://restbase.test.internal/api/testDomain/v1/media/math/render/svg/2uejd9dj3jd'
];
yield 'VisualEditor case' => [
$path,
true,
[
'MathFullRestbaseURL' => null,
'VisualEditorFullRestbaseURL' => 'VisualEditor/api/rest_'
],
'VisualEditor/api/rest_v1/media/math/render/svg/2uejd9dj3jd'
];
yield 'Exception case' => [ $path, false, [ 'MathFullRestbaseURL' => "", ], '', true ];
}
/**
* @dataProvider dataProviderForTestGetUrl
* @param string $path
* @param bool $internal
* @param array $config
* @param string $expected
* @param bool $expectingException
*/
public function testGetUrl( $path, $internal, $config, $expected, $expectingException = false ) {
if ( $expectingException ) {
$this->expectException( MWException::class );
}
$this->overrideConfigValues( $config );
$input = '\\sin\\newcommand';
$rbi = new MathRestbaseInterface( $input );
$actual = $rbi->getUrl( $path, $internal );
$this->assertSame( $expected, $actual );
}
}