MathMathML

The LaTeXML class contains some code that is
not specific to LaTeXML but to MathML.
This code can be shared between LaTeXML and Mathoid.
This change introduces a new class called MathMathML
and moves the shared code from the LaTeXML class to this
new class.

Bug: 65973
Change-Id: I50517ba83f9a0d2aa8e237f062f18e4319ddbac8
This commit is contained in:
physikerwelt (Moritz Schubotz) 2014-06-03 10:15:53 +02:00 committed by Physikerwelt
parent ed018d04b6
commit 52656990b4
5 changed files with 388 additions and 346 deletions

View File

@ -191,6 +191,7 @@ $wgAutoloadClasses['MathHooks'] = $dir . 'Math.hooks.php';
$wgAutoloadClasses['MathRenderer'] = $dir . 'MathRenderer.php';
$wgAutoloadClasses['MathTexvc'] = $dir . 'MathTexvc.php';
$wgAutoloadClasses['MathSource'] = $dir . 'MathSource.php';
$wgAutoloadClasses['MathMathML'] = $dir . 'MathMathML.php';
$wgAutoloadClasses['MathLaTeXML'] = $dir . 'MathLaTeXML.php';
$wgAutoloadClasses['MathInputCheck'] = $dir . 'MathInputCheck.php';
$wgAutoloadClasses['MathInputCheckTexvc'] = $dir . 'MathInputCheckTexvc.php';

View File

@ -9,17 +9,17 @@
* @file
*/
class MathLaTeXML extends MathRenderer {
/**
* @var String settings for LaTeXML daemon
*/
class MathLaTeXML extends MathMathML {
protected $defaultAllowedRootElements = array( 'math', 'div', 'table', 'query' );
/** @var String settings for LaTeXML daemon */
private $LaTeXMLSettings = '';
/** @var boolean if false LaTeXML output is not validated */
private $XMLValidation = true;
protected static $DEFAULT_ALLOWED_ROOT_ELEMENTS = array( 'math', 'div', 'table', 'query' );
protected $allowedRootElements = '';
public function __construct( $tex = '', $params = array() ) {
global $wgMathLaTeXMLUrl;
parent::__construct( $tex, $params );
$this->hosts = $wgMathLaTeXMLUrl;
$this->setMode( MW_MATH_LATEXML );
}
/**
* Converts an array with LaTeXML settings to a URL encoded String.
* If the argument is a string the input will be returned.
@ -66,153 +66,12 @@ class MathLaTeXML extends MathRenderer {
$this->LaTeXMLSettings = $settings;
}
/**
* Gets the allowed root elements the rendered math tag might have.
*
* @return array
*/
public function getAllowedRootElements() {
if ( $this->allowedRootElements ) {
return $this->allowedRootElements;
} else {
return self::$DEFAULT_ALLOWED_ROOT_ELEMENTS;
}
}
/**
* Sets the allowed root elements the rendered math tag might have.
* An empty value indicates to use the default settings.
* @param array $settings
*/
public function setAllowedRootElments( $settings ) {
$this->allowedRootElements = $settings;
}
/* (non-PHPdoc)
* @see MathRenderer::render()
*/
public function render( $forceReRendering = false ) {
wfProfileIn( __METHOD__ );
if ( $forceReRendering ) {
$this->setPurge( true );
}
if ( $this->renderingRequired() ) {
$res = $this->doRender( );
if ( ! $res ) {
wfProfileOut( __METHOD__ );
return $this->getLastError();
}
}
$result = $this->getMathMLTag();
wfProfileOut( __METHOD__ );
return $result;
}
/**
* Helper function to checks if the math tag must be rendered.
* @return boolean
*/
private function renderingRequired() {
if ( $this->isPurge() ) {
wfDebugLog( "Math", "Rerendering was requested." );
return true;
} else {
$dbres = $this->readFromDatabase();
if ( $dbres ) {
if ( $this->isValidMathML( $this->getMathml() ) ) {
wfDebugLog( "Math", "Valid entry found in database." );
return false;
} else {
wfDebugLog( "Math", "Malformatted entry found in database" );
return true;
}
} else {
wfDebugLog( "Math", "No entry found in database." );
return true;
}
}
}
/**
* Performs a HTTP Post request to the given host.
* Uses $wgMathLaTeXMLTimeout as timeout.
* Generates error messages on failure
* @see Http::post()
*
* @global int $wgMathLaTeXMLTimeout
* @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;
wfProfileIn( __METHOD__ );
$error = '';
$res = null;
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout );
/** @var $req (CurlHttpRequest|PhpHttpRequest) the request object */
$req = $httpRequestClass::factory( $host, $options );
$status = $req->execute();
if ( $status->isGood() ) {
$res = $req->getContent();
wfProfileOut( __METHOD__ );
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
, 'timeout' => $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" );
}
wfProfileOut( __METHOD__ );
return false;
}
}
/* (non-PHPdoc)
* @see MathRenderer::writeCache()
*/
public function writeCache() {
if ( $this->isChanged() ) {
$this->writeToDatabase();
}
}
/**
* Picks a LaTeXML daemon.
* If more than one daemon are available one is chosen from the
* $wgMathLaTeXMLUrl array.
* @return string
*/
private static function pickHost() {
global $wgMathLaTeXMLUrl;
if ( is_array( $wgMathLaTeXMLUrl ) ) {
$host = array_rand( $wgMathLaTeXMLUrl );
} else {
$host = $wgMathLaTeXMLUrl;
}
wfDebugLog( "Math", "picking host " . $host );
return $host;
}
/**
* Calculates the HTTP POST Data for the request. Depends on the settings
* and the input string only.
* @return string HTTP POST data
*/
public function getPostData() {
public function getLaTeXMLPostData() {
$tex = $this->getTex();
if ( $this->getMathStyle() == MW_MATHSTYLE_INLINE_DISPLAYSTYLE ) {
// In MW_MATHSTYLE_INLINE_DISPLAYSTYLE the old
@ -224,8 +83,11 @@ class MathLaTeXML extends MathRenderer {
}
$texcmd = rawurlencode( $tex );
$settings = $this->serializeSettings( $this->getLaTeXMLSettings( ) );
return $settings. '&tex=' . $texcmd;
$postData = $settings . '&tex=' . $texcmd;
wfDebugLog( "Math", 'Get post data: ' . $postData );
return $postData;
}
/**
* Does the actual web request to convert TeX to MathML.
* @return boolean
@ -239,8 +101,8 @@ class MathLaTeXML extends MathRenderer {
return false;
}
$res = '';
$host = self::pickHost();
$post = $this->getPostData();
$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' ) ){
@ -283,59 +145,13 @@ class MathLaTeXML extends MathRenderer {
}
}
/**
* Sets the XML validation.
* If set to false the output of LaTeXML is not validated.
* @param boolean $validation
*/
public function setXMLValidation( $validation = true ) {
$this->XMLValidation = $validation;
}
/**
* 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 ) {
$out = false;
if ( !$this->XMLValidation ) {
return true;
}
// 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 ( is_array($elementSplit) ){
$localName = end( $elementSplit );
} else {
$localName = $name;
}
if ( in_array( $localName , $this->getAllowedRootElements() ) ) {
$out = true;
} else {
wfDebugLog( "Math", "got wrong root element : $name" );
}
}
return $out;
}
/**
* Internal version of @link self::embedMathML
* @return string
* @return html element with rendered math
*/
private function getMathMLTag() {
protected function getMathMLTag() {
return self::embedMathML( $this->getMathml(), urldecode( $this->getTex() ) );
}

211
MathMathML.php Normal file
View File

@ -0,0 +1,211 @@
<?php
/**
* MediaWiki math extension
*
* (c)2012 Moritz Schubotz
* GPLv2 license; info in main package.
*
* Contains the driver function for the MathML daemon
* @file
*/
class MathMathML extends MathRenderer {
protected $defaultAllowedRootElements = array( 'math' );
protected $allowedRootElements = '';
protected $hosts;
/** @var boolean if false MathML output is not validated */
private $XMLValidation = true;
/**
* Gets the allowed root elements the rendered math tag might have.
*
* @return array
*/
public function getAllowedRootElements() {
if ( $this->allowedRootElements ) {
return $this->allowedRootElements;
} else {
return $this->defaultAllowedRootElements;
}
}
/**
* Sets the XML validation.
* If set to false the output of MathML is not validated.
* @param boolean $validation
*/
public function setXMLValidation( $validation = true ) {
$this->XMLValidation = $validation;
}
/**
* Sets the allowed root elements the rendered math tag might have.
* An empty value indicates to use the default settings.
* @param array $settings
*/
public function setAllowedRootElments( $settings ) {
$this->allowedRootElements = $settings;
}
/* (non-PHPdoc)
* @see MathRenderer::render()
*/
public function render( $forceReRendering = false ) {
wfProfileIn( __METHOD__ );
if ( $forceReRendering ) {
$this->setPurge( true );
}
if ( $this->renderingRequired() ) {
$res = $this->doRender( );
if ( ! $res ) {
wfProfileOut( __METHOD__ );
return $this->getLastError();
}
}
$result = $this->getMathMLTag();
wfProfileOut( __METHOD__ );
return $result;
}
/**
* Helper function to checks if the math tag must be rendered.
* @return boolean
*/
private function renderingRequired() {
if ( $this->isPurge() ) {
wfDebugLog( "Math", "Rerendering was requested." );
return true;
} else {
$dbres = $this->readFromDatabase();
if ( $dbres ) {
if ( $this->isValidMathML( $this->getMathml() ) ) {
wfDebugLog( "Math", "Valid entry found in database." );
return false;
} else {
wfDebugLog( "Math", "Malformatted entry found in database" );
return true;
}
} else {
wfDebugLog( "Math", "No entry found in database." );
return true;
}
}
}
/**
* Performs a HTTP Post request to the given host.
* Uses $wgMathLaTeXMLTimeout as timeout.
* Generates error messages on failure
* @see Http::post()
*
* @global int $wgMathLaTeXMLTimeout
* @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' ) {
// TODO: Change the timeout mechanism.
global $wgMathLaTeXMLTimeout;
wfProfileIn( __METHOD__ );
$error = '';
$res = null;
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout );
/** @var $req (CurlHttpRequest|PhpHttpRequest) the request object */
$req = $httpRequestClass::factory( $host, $options );
$status = $req->execute();
if ( $status->isGood() ) {
$res = $req->getContent();
wfProfileOut( __METHOD__ );
return true;
} else {
if ( $status->hasMessage( 'http-timed-out' ) ) {
$error = $this->getError( 'math_timeout', $this->getModeStr(), $host );
$res = false;
wfDebugLog( "Math", "\nTimeout:"
. var_export( array( 'post' => $post, 'host' => $host
, 'timeout' => $wgMathLaTeXMLTimeout ), true ) . "\n\n" );
} else {
// for any other unkonwn http error
$errormsg = $status->getHtml();
$error = $this->getError( 'math_invalidresponse', $this->getModeStr(), $host, $errormsg, $this->getModeStr( MW_MATH_MATHML ) );
wfDebugLog( "Math", "\nNoResponse:"
. var_export( array( 'post' => $post, 'host' => $host
, 'errormsg' => $errormsg ), true ) . "\n\n" );
}
wfProfileOut( __METHOD__ );
return false;
}
}
/**
* 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 ) {
$out = false;
if ( !$this->XMLValidation ) {
return true;
}
// depends on https://gerrit.wikimedia.org/r/#/c/66365/
if ( !is_callable( 'XmlTypeCheck::newFromString' ) ) {
$msg = wfMessage( 'math_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 ( is_array($elementSplit) ){
$localName = end( $elementSplit );
} else {
$localName = $name;
}
if ( in_array( $localName , $this->getAllowedRootElements() ) ) {
$out = true;
} else {
wfDebugLog( "Math", "got wrong root element : $name" );
}
}
return $out;
}
/* (non-PHPdoc)
* @see MathRenderer::writeCache()
*/
public function writeCache() {
if ( $this->isChanged() ) {
$this->writeToDatabase();
}
}
/**
* Picks a daemon.
* If more than one demon are available one is chosen from the
* hosts array.
* @return string
*/
protected function pickHost() {
if ( is_array( $this->hosts ) ) {
$host = array_rand( $this->hosts );
} else {
$host = $this->hosts;
}
wfDebugLog( "Math", "picking host " . $host );
return $host;
}
protected function getMathTableName() {
return 'mathoid';
}
}

View File

@ -6,111 +6,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;
}
/**
* 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 MathLaTeXML::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" );
}
/**
* Checks if a String is a valid MathML element
* @covers MathLaTeXML::isValidXML
*/
public function testisValidXML() {
$validSample = '<math>content</math>';
$invalidSample = '<notmath />';
$renderer = $this->getMockBuilder( 'MathLaTeXML' )
->setMethods( NULL ) // avoid mocking any methods
->disableOriginalConstructor()
->getMock();
$this->assertTrue(
$renderer->isValidMathML( $validSample ),
'test if math expression is valid mathml sample'
);
$this->assertFalse(
$renderer->isValidMathML( $invalidSample ),
'test if math expression is invalid mathml sample'
);
}
/**
* Tests the serialiazation of the LaTeXML settings
* @covers MathLaTeXML::serializeSettings
@ -138,6 +33,7 @@ class MathLaTeXMLTest extends MediaWikiTestCase {
'test serialization of a string setting'
);
}
/**
* Checks the basic functionality
* i.e. if the span element is generated right.
@ -153,43 +49,3 @@ class MathLaTeXMLTest extends MediaWikiTestCase {
$renderer->getLastError() );
}
}
/**
* 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 MathLaTeXMLTest::$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;
}
}

158
tests/MathMathMLTest.php Normal file
View File

@ -0,0 +1,158 @@
<?php
/**
* Test the MathML output format.
*
* @group Math
*/
class MathMathMLTest 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;
}
/**
* 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( 'MathMathML' )
->setMethods( NULL )
->disableOriginalConstructor()
->getMock();
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
, 'MathMLHttpRequestTester' );
$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_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( 'MathMathML' )
->setMethods( NULL )
->disableOriginalConstructor()
->getMock();
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
, 'MathMLHttpRequestTester' );
$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( 'MathMathML' )
->setMethods( NULL )
->disableOriginalConstructor()
->getMock();
$requestReturn = $renderer->makeRequest( $url, '$\longcommand$', $res
, $error, 'MathMLHttpRequestTester' );
$this->assertEquals( false, $requestReturn, "timeout call return" );
$this->assertEquals( false, $res, "timeout call return" );
$errmsg = wfMessage( 'math_timeout', '', $url )->inContentLanguage()->escaped();
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
}
/**
* Checks if a String is a valid MathML element
* @covers MathMathML::isValidXML
*/
public function testisValidXML() {
$renderer = $this->getMockBuilder( 'MathMathML' )
->setMethods( NULL )
->disableOriginalConstructor()
->getMock();
$validSample = '<math>content</math>';
$invalidSample = '<notmath />';
$this->assertTrue( $renderer->isValidMathML( $validSample ), 'test if math expression is valid mathml sample' );
$this->assertFalse( $renderer->isValidMathML( $invalidSample ), 'test if math expression is invalid mathml sample' );
}
}
/**
* Helper class for testing
* @author physikerwelt
* @see MWHttpRequestTester
*
*/
class MathMLHttpRequestTester {
public static function factory() {
return new MathMLHttpRequestTester();
}
public static function execute() {
return new MathMLTestStatus();
}
public static function getContent() {
return MathMathMLTest::$content;
}
}
/**
* Helper class for testing
* @author physikerwelt
* @see Status
*/
class MathMLTestStatus {
static function isGood() {
return MathMathMLTest::$good;
}
static function hasMessage( $s ) {
if ( $s == 'http-timed-out' ) {
return MathMathMLTest::$timeout;
} else {
return false;
}
}
static function getHtml() {
return MathMathMLTest::$html;
}
static function getWikiText() {
return MathMathMLTest::$html;
}
}