Merge "Remove deprecated feature to declare multiple mathoid hosts"

This commit is contained in:
jenkins-bot 2021-08-05 07:00:13 +00:00 committed by Gerrit Code Review
commit 32185cd9d0
3 changed files with 19 additions and 80 deletions

View file

@ -24,7 +24,7 @@ class MathLaTeXML extends MathMathML {
public function __construct( $tex = '', $params = [] ) {
global $wgMathLaTeXMLUrl;
parent::__construct( $tex, $params );
$this->hosts = $wgMathLaTeXMLUrl;
$this->host = $wgMathLaTeXMLUrl;
$this->setMode( 'latexml' );
}
@ -109,15 +109,14 @@ class MathLaTeXML extends MathMathML {
return false;
}
$res = '';
$host = $this->pickHost();
$post = $this->getLaTeXMLPostData();
// There is an API-inconsistency between different versions of the LaTeXML daemon
// some versions require the literal prefix other don't allow it.
if ( !strpos( $host, '/convert' ) ) {
if ( !strpos( $this->host, '/convert' ) ) {
$post = preg_replace( '/&tex=/', '&tex=literal:', $post, 1 );
}
$this->lastError = '';
$requestResult = $this->makeRequest( $host, $post, $res, $this->lastError );
$requestResult = $this->makeRequest( $this->host, $post, $res, $this->lastError );
if ( $requestResult ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
$jsonResult = json_decode( $res );
@ -134,22 +133,22 @@ class MathLaTeXML extends MathMathML {
// Do not print bad mathml. It's probably too verbose and might
// mess up the browser output.
$this->lastError = $this->getError( 'math_invalidxml', $this->getModeStr(), $host );
$this->lastError = $this->getError( 'math_invalidxml', $this->getModeStr(), $this->host );
LoggerFactory::getInstance( 'Math' )->warning(
'LaTeXML InvalidMathML: ' . var_export( [
'post' => $post,
'host' => $host,
'host' => $this->host,
'result' => $res
], true ) );
return false;
}
$this->lastError = $this->getError( 'math_invalidjson', $this->getModeStr(), $host );
$this->lastError = $this->getError( 'math_invalidjson', $this->getModeStr(), $this->host );
LoggerFactory::getInstance( 'Math' )->warning(
'LaTeXML InvalidJSON:' . var_export( [
'post' => $post,
'host' => $host,
'host' => $this->host,
'res' => $res
], true ) );

View file

@ -33,8 +33,8 @@ class MathMathML extends MathRenderer {
protected $restbaseRenderingModes = [ 'mathml', 'png' ];
/** @var string[] */
protected $allowedRootElements = [];
/** @var string|string[] */
protected $hosts;
/** @var string */
protected $host;
/** @var LoggerInterface */
private $logger;
@ -57,7 +57,7 @@ class MathMathML extends MathRenderer {
global $wgMathMathMLUrl;
parent::__construct( $tex, $params );
$this->setMode( 'mathml' );
$this->hosts = $wgMathMathMLUrl;
$this->host = $wgMathMathMLUrl;
if ( isset( $params['type'] ) ) {
$allowedTypes = [ 'pmml', 'ascii', 'chem' ];
if ( in_array( $params['type'], $allowedTypes ) ) {
@ -233,12 +233,8 @@ class MathMathML extends MathRenderer {
$error = '';
$res = null;
if ( !$host ) {
$host = $this->pickHost();
}
if ( !$post ) {
$this->getPostData();
}
$host = $host ?: $this->host;
$post = $post ?: $this->getPostData();
$options = [ 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout ];
$req = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( $host, $options );
$status = $req->execute();
@ -270,25 +266,6 @@ class MathMathML extends MathRenderer {
}
}
/**
* Return a MathML daemon host.
*
* If more than one demon is available, one is chosen at random.
*
* @return string
* @deprecated
*/
protected function pickHost() {
if ( is_array( $this->hosts ) ) {
$host = $this->hosts[array_rand( $this->hosts )];
$this->hosts = $host; // Use the same host for this class instance
} else {
$host = $this->hosts;
}
$this->logger->debug( 'Picking host ' . $host );
return $host;
}
/**
* Calculates the HTTP POST Data for the request. Depends on the settings
* and the input string only.
@ -324,37 +301,36 @@ class MathMathML extends MathRenderer {
return false;
}
$res = '';
$host = $this->pickHost();
$post = $this->getPostData();
$this->lastError = '';
$requestResult = $this->makeRequest( $host, $post, $res, $this->lastError );
$requestResult = $this->makeRequest( $this->host, $post, $res, $this->lastError );
if ( $requestResult ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
$jsonResult = json_decode( $res );
if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) {
if ( $jsonResult->success ) {
return $this->processJsonResult( $jsonResult, $host );
return $this->processJsonResult( $jsonResult, $this->host );
} else {
if ( property_exists( $jsonResult, 'log' ) ) {
$log = $jsonResult->log;
} else {
$log = wfMessage( 'math_unknown_error' )->inContentLanguage()->escaped();
}
$this->lastError = $this->getError( 'math_mathoid_error', $host, $log );
$this->lastError = $this->getError( 'math_mathoid_error', $this->host, $log );
$this->logger->warning(
'Mathoid conversion error:' . var_export( [
'post' => $post,
'host' => $host,
'host' => $this->host,
'result' => $res
], true ) );
return false;
}
} else {
$this->lastError = $this->getError( 'math_invalidjson', $host );
$this->lastError = $this->getError( 'math_invalidjson', $this->host );
$this->logger->error(
'MathML InvalidJSON:' . var_export( [
'post' => $post,
'host' => $host,
'host' => $this->host,
'res' => $res
], true ) );
return false;

View file

@ -2,7 +2,6 @@
use MediaWiki\Extension\Math\MathMathML;
use MediaWiki\MediaWikiServices;
use Wikimedia\TestingAccessWrapper;
/**
* Test the MathML output format.
@ -128,22 +127,6 @@ class MathMathMLTest extends MediaWikiTestCase {
$renderer->makeRequest( $url, false, $res, $error );
}
/**
* Tests behavior of makeRequest() that communicates with the host.
* Test case: Get host.
* @covers \MediaWiki\Extension\Math\MathMathML::pickHost
*/
public function testMakeRequestGetHost() {
$this->installMockHttp( $this->makeFakeHttpRequest() );
$renderer = $this->getMockBuilder( MathMathML::class )
->onlyMethods( [ 'getPostData', 'pickHost' ] )
->getMock();
$renderer->expects( $this->once() )->method( 'pickHost' );
/** @var MathMathML $renderer */
$renderer->makeRequest( false, false, $res, $error );
}
/**
* Checks if a String is a valid MathML element
* @covers \MediaWiki\Extension\Math\MathMathML::isValidMathML
@ -171,7 +154,7 @@ class MathMathMLTest extends MediaWikiTestCase {
'test if math expression is invalid mathml sample' );
}
public function testintegrationTestWithLinks() {
public function testIntegrationTestWithLinks() {
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
$p = MediaWikiServices::getInstance()->getParserFactory()->create();
$po = ParserOptions::newFromAnon();
@ -195,25 +178,6 @@ class MathMathMLTest extends MediaWikiTestCase {
$this->assertSame( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
}
public function testPickHost() {
$hosts = [ 'a', 'b', 'c' ];
$this->setMwGlobals( 'wgMathMathMLUrl', $hosts );
srand( 0 ); // Make array_rand always return the same elements
$h1 = $hosts[array_rand( $hosts )];
$h2 = $hosts[array_rand( $hosts )];
srand( 0 );
/** @var MathMathML $m */
$m = TestingAccessWrapper::newFromObject( new MathMathML() );
$host1 = $m->pickHost();
$this->assertSame( $h1, $host1, 'first call' );
$host2 = $m->pickHost();
$this->assertSame( $host1, $host2, 'second call' );
/** @var MathMathML $m2 */
$m2 = TestingAccessWrapper::newFromObject( new MathMathML() );
$host3 = $m2->pickHost();
$this->assertSame( $h2, $host3, 'third call' );
}
public function testWarning() {
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );