mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-27 17:01:07 +00:00
Remove duplicated code from the LaTeXML class
Change-Id: I61645196aa1d69db6a25781330e34f64d8616de7
This commit is contained in:
parent
49c6816fa3
commit
f4b6e74d64
|
@ -116,7 +116,7 @@ class MathHooks {
|
|||
*
|
||||
* @return array of strings
|
||||
*/
|
||||
private static function getMathNames() {
|
||||
public static function getMathNames() {
|
||||
global $wgMathValidModes;
|
||||
$MathConstantNames = array(
|
||||
MW_MATH_SOURCE => 'mw_math_source',
|
||||
|
|
|
@ -36,10 +36,10 @@ $messages['en'] = array(
|
|||
'math_bad_output' => 'Cannot write to or create math output directory',
|
||||
'math_notexvc' => 'Missing texvc executable; please see math/README to configure.',
|
||||
'math_output_error' => 'Cannot store math image on filesystem.',
|
||||
'math_latexml_timeout' => 'LaTeXML timeout from \'$1\'',
|
||||
'math_latexml_invalidresponse' => 'LaTeXML Invalid response (\'$2\') from server \'$1\':',
|
||||
'math_latexml_invalidxml' => 'LaTeXML MathML is invalid XML.',
|
||||
'math_latexml_invalidjson' => 'LaTeXML Server response is invalid JSON.',
|
||||
'math_timeout' => '$1 timeout from \'$2\'',
|
||||
'math_invalidresponse' => '$1: Invalid response (\'$3\') from server \'$2\':',
|
||||
'math_invalidxml' => 'MathML is invalid XML.',
|
||||
'math_invalidjson' => '$1 server response is invalid JSON.',
|
||||
'math_latexml_xmlversion' => 'Warning: XML type check skipped! Check if your MediaWiki installation is version wmf/1.22wmf7 or newer.'
|
||||
);
|
||||
|
||||
|
@ -99,10 +99,10 @@ This message is followed by "(", Error message(*1), Additional message, "): " a
|
|||
* {{msg-mw|Math bad output}}
|
||||
* {{msg-mw|Math notexvc}}
|
||||
* {{msg-mw|Math output error}}
|
||||
* {{msg-mw|Math latexml timeout}}
|
||||
* {{msg-mw|Math latexml invalidresponse}}
|
||||
* {{msg-mw|Math latexml invalidxml}}
|
||||
* {{msg-mw|Math latexml invalidjson}}',
|
||||
* {{msg-mw|Math timeout}}
|
||||
* {{msg-mw|Math invalidresponse}}
|
||||
* {{msg-mw|Math invalidxml}}
|
||||
* {{msg-mw|Math invalidjson}}',
|
||||
'math_unknown_error' => 'Used as error message for unknown texvc error.
|
||||
|
||||
This message follows the message {{msg-mw|Math failure}}.
|
||||
|
@ -137,21 +137,26 @@ This message follows the message {{msg-mw|Math failure}}.',
|
|||
'math_output_error' => 'Used as error message if the texvc output file could not be stored.
|
||||
|
||||
This message follows the message {{msg-mw|Math failure}}.',
|
||||
'math_latexml_timeout' => 'Used as error message.
|
||||
'math_timeout' => 'Used as error message.
|
||||
|
||||
Parameters:
|
||||
* $1 - hostname or URL',
|
||||
'math_latexml_invalidresponse' => 'Used as error message.
|
||||
* $1 - redering type (either LaTeXML or Mathoid)
|
||||
* $2 - hostname or URL',
|
||||
'math_invalidresponse' => 'Used as error message.
|
||||
|
||||
Follows the message {{msg-mw|Math failure}}.
|
||||
|
||||
Parameters:
|
||||
* $1 - hostname
|
||||
* $2 - error message',
|
||||
'math_latexml_invalidxml' => 'Used as error message.
|
||||
* $1 - redering type (either LaTeXML or Mathoid)
|
||||
* $2 - hostname
|
||||
* $3 - error message',
|
||||
'math_invalidxml' => 'Used as error message.
|
||||
|
||||
This message follows the message {{msg-mw|Math failure}}.',
|
||||
'math_latexml_invalidjson' => 'Used as error message.
|
||||
'math_invalidjson' => 'Used as error message.
|
||||
|
||||
Parameters:
|
||||
* $1 - redering type (either LaTeXML or Mathoid)
|
||||
|
||||
This message follows the message {{msg-mw|Math failure}}.',
|
||||
'math_latexml_xmlversion' => 'Warning that XML checking of MathML requires wmf/1.22wmf7 or newer.',
|
||||
|
|
|
@ -86,56 +86,6 @@ class MathLaTeXML extends MathMathML {
|
|||
$this->allowedRootElements = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a HTTP Post request to the given host.
|
||||
* Uses $wgLaTeXMLTimeout as timeout.
|
||||
* Generates error messages on failure
|
||||
* @see Http::post()
|
||||
*
|
||||
* @param string $host
|
||||
* @param string $post the encoded post request
|
||||
* @param mixed $res the result
|
||||
* @param mixed $error the formatted error message or null
|
||||
* @param String $httpRequestClass class name of MWHttpRequest (needed for testing only)
|
||||
* @return boolean success
|
||||
*/
|
||||
public function makeRequest( $host, $post, &$res, &$error = '', $httpRequestClass = 'MWHttpRequest' ) {
|
||||
global $wgMathLaTeXMLTimeout;
|
||||
$error = '';
|
||||
$res = null;
|
||||
if ( !$host ) {
|
||||
$host = self::pickHost();
|
||||
}
|
||||
if ( !$post ) {
|
||||
$this->getPostData();
|
||||
}
|
||||
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout );
|
||||
$req = $httpRequestClass::factory( $host, $options );
|
||||
$status = $req->execute();
|
||||
|
||||
if ( $status->isGood() ) {
|
||||
$res = $req->getContent();
|
||||
return true;
|
||||
} else {
|
||||
if ( $status->hasMessage( 'http-timed-out' ) ) {
|
||||
$error = $this->getError( 'math_latexml_timeout', $host );
|
||||
$res = false;
|
||||
wfDebugLog( "Math", "\nLaTeXML Timeout:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'wgLaTeXMLTimeout' => $wgMathLaTeXMLTimeout ), true ) . "\n\n" );
|
||||
} else {
|
||||
// for any other unkonwn http error
|
||||
$errormsg = $status->getHtml();
|
||||
$error = $this->getError( 'math_latexml_invalidresponse', $host, $errormsg );
|
||||
wfDebugLog( "Math", "\nLaTeXML NoResponse:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'errormsg' => $errormsg ), true ) . "\n\n" );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Picks a LaTeXML daemon.
|
||||
* If more than one demon are availible one is chosen from the
|
||||
|
@ -201,14 +151,14 @@ class MathLaTeXML extends MathMathML {
|
|||
} else {
|
||||
// Do not print bad mathml. It's probably too verbose and might
|
||||
// mess up the browser output.
|
||||
$this->lastError = $this->getError( 'math_latexml_invalidxml', $host );
|
||||
$this->lastError = $this->getError( 'math_invalidxml', $this->getModeStr(), $host );
|
||||
wfDebugLog( "Math", "\nLaTeXML InvalidMathML:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'result' => $res ), true ) . "\n\n" );
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->lastError = $this->getError( 'math_latexml_invalidjson', $host );
|
||||
$this->lastError = $this->getError( 'math_invalidjson', $this->getModeStr(), $host );
|
||||
wfDebugLog( "Math", "\nLaTeXML InvalidJSON:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'res' => $res ), true ) . "\n\n" );
|
||||
|
@ -228,37 +178,4 @@ class MathLaTeXML extends MathMathML {
|
|||
$this->XMLValidation = $newval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the input is valid MathML,
|
||||
* and if the root element has the name math
|
||||
* @param string $XML
|
||||
* @return boolean
|
||||
*/
|
||||
public function isValidMathML( $XML ) {
|
||||
if ( !$this->XMLValidation ) {
|
||||
return true;
|
||||
}
|
||||
$out = false;
|
||||
// depends on https://gerrit.wikimedia.org/r/#/c/66365/
|
||||
if ( !is_callable( 'XmlTypeCheck::newFromString' ) ) {
|
||||
$msg = wfMessage( 'math_latexml_xmlversion' )->inContentLanguage()->escaped();
|
||||
trigger_error( $msg, E_USER_NOTICE );
|
||||
wfDebugLog( 'Math', $msg );
|
||||
return true;
|
||||
}
|
||||
$xmlObject = new XmlTypeCheck( $XML, null, false );
|
||||
if ( !$xmlObject->wellFormed ) {
|
||||
wfDebugLog( "Math", "XML validation error:\n " . var_export( $XML, true ) . "\n" );
|
||||
} else {
|
||||
$name = $xmlObject->getRootElement();
|
||||
$elementSplit = explode( ':', $name );
|
||||
if ( in_array( end( $elementSplit ), $this->getAllowedRootElements() ) ) {
|
||||
$out = true;
|
||||
} else {
|
||||
wfDebugLog( "Math", 'got wrong root element :' . end( $elementSplit ) . ' with namespace ' . $name );
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
}
|
|
@ -99,6 +99,7 @@ class MathMathML extends MathRenderer {
|
|||
global $wgMathMathMLTimeout;
|
||||
$error = '';
|
||||
$res = null;
|
||||
$modeStr = $this->getModeStr();
|
||||
if ( !$host ) {
|
||||
$host = self::pickHost();
|
||||
}
|
||||
|
@ -106,23 +107,25 @@ class MathMathML extends MathRenderer {
|
|||
$this->getPostData();
|
||||
}
|
||||
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathMathMLTimeout );
|
||||
/**@var MWHttpRequest the request*/
|
||||
$req = $httpRequestClass::factory( $host, $options );
|
||||
/**@var Status the request status */
|
||||
$status = $req->execute();
|
||||
if ( $status->isGood() ) {
|
||||
$res = $req->getContent();
|
||||
return true;
|
||||
} else {
|
||||
if ( $status->hasMessage( 'http-timed-out' ) ) {
|
||||
$error = $this->getError( 'math_latexml_timeout', $host );
|
||||
$error = $this->getError( 'math_timeout', $modeStr, $host );
|
||||
$res = false;
|
||||
wfDebugLog( "Math", "\nMathML Timeout:"
|
||||
wfDebugLog( "Math", "$modeStr Timeout:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'wgMathMLTimeout' => $wgMathMathMLTimeout ), true ) . "\n\n" );
|
||||
} else {
|
||||
// for any other unkonwn http error
|
||||
$errormsg = $status->getHtml();
|
||||
$error = $this->getError( 'math_latexml_invalidresponse', $host, $errormsg );
|
||||
wfDebugLog( "Math", "\nMathML NoResponse:"
|
||||
$errormsg = $status->getWikiText();
|
||||
$error = $this->getError( 'math_invalidresponse', $modeStr, $host, $errormsg );
|
||||
wfDebugLog( "Math", "$modeStr NoResponse:"
|
||||
. var_export( array( 'post' => $post, 'host' => $host
|
||||
, 'errormsg' => $errormsg ), true ) . "\n\n" );
|
||||
}
|
||||
|
|
|
@ -671,4 +671,9 @@ abstract class MathRenderer {
|
|||
protected function getMathTableName() {
|
||||
return 'mathoid';
|
||||
}
|
||||
|
||||
public function getModeStr( ){
|
||||
$names = MathHooks::getMathNames();
|
||||
return $names[ $this->getMode() ];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,24 +7,6 @@
|
|||
*/
|
||||
class MathLaTeXMLTest extends MediaWikiTestCase {
|
||||
|
||||
// State-variables for HTTP Mockup classes
|
||||
public static $content = null;
|
||||
public static $good = false;
|
||||
public static $html = false;
|
||||
public static $timeout = false;
|
||||
|
||||
/**
|
||||
* Set the mock values for the HTTP Mockup classes
|
||||
*
|
||||
* @param boolean $good
|
||||
* @param mixed $html HTML of the error message or false if no error is present.
|
||||
* @param boolean $timeout true if
|
||||
*/
|
||||
public static function setMockValues( $good, $html, $timeout ) {
|
||||
self::$good = $good;
|
||||
self::$html = $html;
|
||||
self::$timeout = $timeout;
|
||||
}
|
||||
protected function setUp() {
|
||||
global $wgMathValidModes;
|
||||
$wgMathValidModes[] = MW_MATH_LATEXML;
|
||||
|
@ -65,77 +47,6 @@ class MathLaTeXMLTest extends MediaWikiTestCase {
|
|||
$this->assertContains( $expected, $renderer->getHtmlOutput(), 'Rendering the String "\text{CR}"' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests behavior of makeRequest() that communicates with the host.
|
||||
* Testcase: Invalid request.
|
||||
* @covers MathTexvc::makeRequest
|
||||
*/
|
||||
public function testMakeRequestInvalid() {
|
||||
self::setMockValues( false, false, false );
|
||||
$url = 'http://example.com/invalid';
|
||||
|
||||
$renderer = $this->getMockBuilder( 'MathLaTeXML' )
|
||||
->setMethods( NULL )
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
|
||||
, 'LaTeXMLHttpRequestTester' );
|
||||
$this->assertEquals( false, $requestReturn
|
||||
, "requestReturn is false if HTTP::post returns false." );
|
||||
$this->assertEquals( false, $res
|
||||
, "res is false if HTTP:post returns false." );
|
||||
$errmsg = wfMessage( 'math_latexml_invalidresponse', $url, '' )
|
||||
->inContentLanguage()->escaped();
|
||||
$this->assertContains( $errmsg, $error
|
||||
, "return an error if HTTP::post returns false" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests behavior of makeRequest() that communicates with the host.
|
||||
* Testcase: Valid request.
|
||||
* @covers MathTexvc::makeRequest
|
||||
*/
|
||||
public function testMakeRequestSuccess() {
|
||||
self::setMockValues( true, true, false );
|
||||
$url = 'http://example.com/valid';
|
||||
$renderer = $this->getMockBuilder( 'MathLaTeXML' )
|
||||
->setMethods( NULL )
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
|
||||
, 'LaTeXMLHttpRequestTester' );
|
||||
$this->assertEquals( true, $requestReturn, "successful call return" );
|
||||
$this->isTrue( $res, "successfull call" );
|
||||
$this->assertEquals( $error, '', "successfull call errormessage" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests behavior of makeRequest() that communicates with the host.
|
||||
* Testcase: Timeout.
|
||||
* @covers MathMathML::makeRequest
|
||||
*/
|
||||
public function testMakeRequestTimeout() {
|
||||
self::setMockValues( false, true, true );
|
||||
$url = 'http://example.com/timeout';
|
||||
$renderer = $this->getMockBuilder( 'MathLaTeXML' )
|
||||
->setMethods( NULL )
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$requestReturn = $renderer->makeRequest( $url, '$\longcommand$', $res
|
||||
, $error, 'LaTeXMLHttpRequestTester' );
|
||||
$this->assertEquals( false, $requestReturn, "timeout call return" );
|
||||
$this->assertEquals( false, $res, "timeout call return" );
|
||||
$errmsg = wfMessage( 'math_latexml_timeout', $url )
|
||||
->inContentLanguage()->escaped();
|
||||
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
|
||||
}
|
||||
|
||||
// public function testisValidXML() {
|
||||
// $validSample = '<math>content</math>';
|
||||
// $invalidSample = '<notmath />';
|
||||
// $this->assertTrue( MathMathML::isValidMathML( $validSample ), 'test if math expression is valid mathml sample' );
|
||||
// $this->assertFalse( MathMathML::isValidMathML( $invalidSample ), 'test if math expression is invalid mathml sample' );
|
||||
// }
|
||||
|
||||
/**
|
||||
* Checks if a String is a valid MathML element
|
||||
|
@ -188,52 +99,4 @@ class MathLaTeXMLTest extends MediaWikiTestCase {
|
|||
$this->assertContains( $expected, $real
|
||||
, "Rendering of a+b in plain Text mode" );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class for testing
|
||||
* @author physikerwelt
|
||||
* @see MWHttpRequestTester
|
||||
*
|
||||
*/
|
||||
class LaTeXMLHttpRequestTester {
|
||||
|
||||
public static function factory() {
|
||||
return new LaTeXMLHttpRequestTester();
|
||||
}
|
||||
|
||||
public static function execute() {
|
||||
return new LaTeXMLTestStatus();
|
||||
}
|
||||
|
||||
public static function getContent() {
|
||||
return MathMathMLTest::$content;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class for testing
|
||||
* @author physikerwelt
|
||||
* @see Status
|
||||
*/
|
||||
class LaTeXMLTestStatus {
|
||||
|
||||
static function isGood() {
|
||||
return MathLaTeXMLTest::$good;
|
||||
}
|
||||
|
||||
static function hasMessage( $s ) {
|
||||
if ( $s == 'http-timed-out' ) {
|
||||
return MathLaTeXMLTest::$timeout;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static function getHtml() {
|
||||
return MathLaTeXMLTest::$html;
|
||||
}
|
||||
|
||||
}
|
|
@ -32,12 +32,8 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
* https://trac.mathweb.org/LaTeXML/ticket/1752
|
||||
*/
|
||||
public function testSpecialCaseText() {
|
||||
$this->markTestSkipped( 'currently no live svgtex server availible' );
|
||||
if ( wfGetDB( DB_MASTER )->getType() === 'sqlite' ) {
|
||||
$this->markTestSkipped( "SQLite has global indices. We cannot " .
|
||||
"create the `unitest_math` table, its math_inputhash index " .
|
||||
"would conflict with the one from the real `math` table."
|
||||
);
|
||||
if ( gethostname() === 'gallium' ) {
|
||||
$this->markTestSkipped( 'currently no live svgtex server availible' );
|
||||
}
|
||||
|
||||
$renderer = MathRenderer::getRenderer( 'x^2+\text{a sample Text}', array( ), MW_MATH_MATHML );
|
||||
|
@ -65,7 +61,7 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
, "requestReturn is false if HTTP::post returns false." );
|
||||
$this->assertEquals( false, $res
|
||||
, "res is false if HTTP:post returns false." );
|
||||
$errmsg = wfMessage( 'math_latexml_invalidresponse', $url, '' )
|
||||
$errmsg = wfMessage( 'math_invalidresponse', wfMessage('mw_math_mathml'), $url, '' )
|
||||
->inContentLanguage()->escaped();
|
||||
$this->assertContains( $errmsg, $error
|
||||
, "return an error if HTTP::post returns false" );
|
||||
|
@ -106,7 +102,7 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
, $error, 'MathMLHttpRequestTester' );
|
||||
$this->assertEquals( false, $requestReturn, "timeout call return" );
|
||||
$this->assertEquals( false, $res, "timeout call return" );
|
||||
$errmsg = wfMessage( 'math_latexml_timeout', $url )
|
||||
$errmsg = wfMessage( 'math_timeout', wfMessage('mw_math_mathml'), $url )
|
||||
->inContentLanguage()->escaped();
|
||||
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
|
||||
}
|
||||
|
@ -140,14 +136,15 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
*/
|
||||
public function testIntegration() {
|
||||
global $wgMathMathMLTimeout;
|
||||
$this->markTestSkipped( 'currently no live svgtex server availible' );
|
||||
if ( gethostname() === 'gallium' ) {
|
||||
$this->markTestSkipped( 'currently no live svgtex server availible' );
|
||||
}
|
||||
$wgMathMathMLTimeout = 20;
|
||||
$renderer = MathRenderer::getRenderer( "a+b", array( ), MW_MATH_MATHML );
|
||||
$this->assertTrue( $renderer->render( true ) );
|
||||
$real = str_replace( "\n", '', $renderer->getHtmlOutput() );
|
||||
$expected = '<plus';
|
||||
$this->assertContains( $expected, $real
|
||||
, "Rendering of a+b in plain Text mode" );
|
||||
$expected = '<mo>+</mo>';
|
||||
$this->assertContains( $expected, $real, "Rendering of a+b in plain MathML mode" );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -197,4 +194,8 @@ class MathMLTestStatus {
|
|||
return MathMathMLTest::$html;
|
||||
}
|
||||
|
||||
static function getWikiText() {
|
||||
return MathMathMLTest::$html;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue