2014-06-03 08:15:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
2015-04-14 19:42:48 +00:00
|
|
|
* (c) 2002-2015 various MediaWiki contributors
|
2014-06-03 08:15:53 +00:00
|
|
|
* GPLv2 license; info in main package.
|
2015-04-14 19:42:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts LaTeX to MathML using the mathoid-server
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
class MathMathML extends MathRenderer {
|
|
|
|
|
2016-04-12 20:53:25 +00:00
|
|
|
protected $defaultAllowedRootElements = [ 'math' ];
|
|
|
|
protected $restbaseInputTypes = [ 'tex', 'inline-tex', 'chem' ];
|
2014-06-03 08:15:53 +00:00
|
|
|
protected $allowedRootElements = '';
|
|
|
|
protected $hosts;
|
2015-01-22 18:56:13 +00:00
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/** @var boolean if false MathML output is not validated */
|
|
|
|
private $XMLValidation = true;
|
2015-12-01 22:51:22 +00:00
|
|
|
private $svgPath = false;
|
2016-01-31 21:11:39 +00:00
|
|
|
private $mathoidStyle;
|
2014-06-10 16:49:20 +00:00
|
|
|
|
2016-04-12 20:53:25 +00:00
|
|
|
public function __construct( $tex = '', $params = [] ) {
|
2014-06-10 16:49:20 +00:00
|
|
|
global $wgMathMathMLUrl;
|
|
|
|
parent::__construct( $tex, $params );
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMode( 'mathml' );
|
2014-06-10 16:49:20 +00:00
|
|
|
$this->hosts = $wgMathMathMLUrl;
|
|
|
|
if ( isset( $params['type'] ) ) {
|
|
|
|
if ( $params['type'] == 'pmml' ) {
|
|
|
|
$this->inputType = 'pmml';
|
|
|
|
$this->setMathml( '<math>' . $tex . '</math>' );
|
|
|
|
} elseif ( $params['type'] == 'ascii' ) {
|
|
|
|
$this->inputType = 'ascii';
|
2016-01-29 13:05:54 +00:00
|
|
|
} elseif ( $params['type'] == 'chem' ){
|
|
|
|
$this->inputType = 'chem';
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-29 16:50:32 +00:00
|
|
|
if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) {
|
|
|
|
// default preserve the (broken) layout as it was
|
|
|
|
$this->tex = '{\\displaystyle ' . $tex . '}';
|
|
|
|
}
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-06-10 09:57:24 +00:00
|
|
|
public function setAllowedRootElements( $settings ) {
|
2014-06-03 08:15:53 +00:00
|
|
|
$this->allowedRootElements = $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-PHPdoc)
|
|
|
|
* @see MathRenderer::render()
|
|
|
|
*/
|
|
|
|
public function render( $forceReRendering = false ) {
|
2016-05-10 13:45:26 +00:00
|
|
|
global $wgMathFullRestbaseURL;
|
|
|
|
try {
|
2016-06-02 14:30:48 +00:00
|
|
|
if ( $forceReRendering ) {
|
|
|
|
$this->setPurge( true );
|
|
|
|
}
|
2016-05-10 13:45:26 +00:00
|
|
|
if ( in_array( $this->inputType, $this->restbaseInputTypes ) &&
|
|
|
|
$this->mode == 'mathml'
|
|
|
|
) {
|
|
|
|
if ( !$this->rbi ) {
|
|
|
|
$this->rbi =
|
|
|
|
new MathRestbaseInterface( $this->getTex(), $this->getInputType() );
|
2016-06-02 14:30:48 +00:00
|
|
|
$this->rbi->setPurge( $this->isPurge() );
|
2016-05-10 13:45:26 +00:00
|
|
|
}
|
|
|
|
$rbi = $this->rbi;
|
|
|
|
if ( $rbi->getSuccess() ) {
|
|
|
|
$this->mathml = $rbi->getMathML();
|
|
|
|
$this->mathoidStyle = $rbi->getMathoidStyle();
|
|
|
|
$this->svgPath = $rbi->getFullSvgUrl();
|
|
|
|
} elseif ( $this->lastError === '' ) {
|
|
|
|
$this->doCheck();
|
|
|
|
}
|
|
|
|
$this->changed = false;
|
|
|
|
return $rbi->getSuccess();
|
2015-12-01 22:51:22 +00:00
|
|
|
}
|
2016-05-10 13:45:26 +00:00
|
|
|
if ( $this->renderingRequired() ) {
|
|
|
|
return $this->doRender();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$this->lastError = $this->getError( 'math_mathoid_error',
|
|
|
|
$wgMathFullRestbaseURL, $e->getMessage() );
|
|
|
|
LoggerFactory::getInstance( 'Math' )->error( $e->getMessage(), [ $e, $this ] );
|
|
|
|
return false;
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to checks if the math tag must be rendered.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function renderingRequired() {
|
2015-04-14 19:42:48 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'Math' );
|
2014-06-03 08:15:53 +00:00
|
|
|
if ( $this->isPurge() ) {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'Rerendering was requested.' );
|
2014-06-03 08:15:53 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2014-09-06 01:55:54 +00:00
|
|
|
$dbres = $this->isInDatabase();
|
2014-06-03 08:15:53 +00:00
|
|
|
if ( $dbres ) {
|
|
|
|
if ( $this->isValidMathML( $this->getMathml() ) ) {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'Valid MathML entry found in database.' );
|
2014-11-18 09:09:29 +00:00
|
|
|
if ( $this->getSvg( 'cached' ) ) {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'SVG-fallback found in database.' );
|
2014-06-10 16:49:20 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'SVG-fallback missing.' );
|
2014-06-10 16:49:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
} else {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'Malformatted entry found in database' );
|
2014-06-03 08:15:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'No entry found in database.' );
|
2014-06-03 08:15:53 +00:00
|
|
|
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
|
|
|
|
*/
|
2015-09-21 16:14:01 +00:00
|
|
|
public function makeRequest(
|
|
|
|
$host, $post, &$res, &$error = '', $httpRequestClass = 'MWHttpRequest'
|
|
|
|
) {
|
2014-06-03 08:15:53 +00:00
|
|
|
// TODO: Change the timeout mechanism.
|
|
|
|
global $wgMathLaTeXMLTimeout;
|
|
|
|
|
|
|
|
$error = '';
|
|
|
|
$res = null;
|
2014-06-10 16:49:20 +00:00
|
|
|
if ( !$host ) {
|
|
|
|
$host = self::pickHost();
|
|
|
|
}
|
|
|
|
if ( !$post ) {
|
|
|
|
$this->getPostData();
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
$options = [ 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout ];
|
2014-06-03 08:15:53 +00:00
|
|
|
/** @var $req (CurlHttpRequest|PhpHttpRequest) the request object */
|
|
|
|
$req = $httpRequestClass::factory( $host, $options );
|
2014-12-09 14:35:33 +00:00
|
|
|
/** @var Status $req Status the request status */
|
2014-06-03 08:15:53 +00:00
|
|
|
$status = $req->execute();
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
$res = $req->getContent();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ( $status->hasMessage( 'http-timed-out' ) ) {
|
|
|
|
$error = $this->getError( 'math_timeout', $this->getModeStr(), $host );
|
|
|
|
$res = false;
|
2016-04-12 20:53:25 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning( 'Timeout:' . var_export( [
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'timeout' => $wgMathLaTeXMLTimeout
|
2016-04-12 20:53:25 +00:00
|
|
|
], true ) );
|
2014-06-03 08:15:53 +00:00
|
|
|
} else {
|
|
|
|
// for any other unkonwn http error
|
|
|
|
$errormsg = $status->getHtml();
|
2015-03-16 19:51:16 +00:00
|
|
|
$error =
|
|
|
|
$this->getError( 'math_invalidresponse', $this->getModeStr(), $host, $errormsg,
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->getModeStr( 'mathml' ) );
|
2016-04-12 20:53:25 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning( 'NoResponse:' . var_export( [
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'errormsg' => $errormsg
|
2016-04-12 20:53:25 +00:00
|
|
|
], true ) );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:49:20 +00:00
|
|
|
/**
|
2015-01-22 18:56:13 +00:00
|
|
|
* Return a MathML daemon host.
|
|
|
|
*
|
|
|
|
* If more than one demon is available, one is chosen at random.
|
|
|
|
*
|
2014-06-10 16:49:20 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function pickHost() {
|
|
|
|
if ( is_array( $this->hosts ) ) {
|
|
|
|
$host = array_rand( $this->hosts );
|
2015-07-12 19:40:34 +00:00
|
|
|
$this->hosts = $host; // Use the same host for this class instance
|
2014-06-10 16:49:20 +00:00
|
|
|
} else {
|
|
|
|
$host = $this->hosts;
|
|
|
|
}
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Picking host ' . $host );
|
2014-06-10 16:49:20 +00:00
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the HTTP POST Data for the request. Depends on the settings
|
|
|
|
* and the input string only.
|
|
|
|
* @return string HTTP POST data
|
2016-01-29 16:50:32 +00:00
|
|
|
* @throws MWException
|
2014-06-10 16:49:20 +00:00
|
|
|
*/
|
|
|
|
public function getPostData() {
|
|
|
|
$input = $this->getTex();
|
2015-03-16 19:51:16 +00:00
|
|
|
if ( $this->inputType == 'pmml' ||
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->getMode() == 'latexml' && $this->getMathml() ) {
|
2014-06-10 16:49:20 +00:00
|
|
|
$out = 'type=mml&q=' . rawurlencode( $this->getMathml() );
|
|
|
|
} elseif ( $this->inputType == 'ascii' ) {
|
|
|
|
$out = 'type=asciimath&q=' . rawurlencode( $input );
|
|
|
|
} else {
|
2015-12-01 22:51:22 +00:00
|
|
|
throw new MWException( 'Internal error: Restbase should be used for tex rendering' );
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Get post data: ' . $out );
|
2014-06-10 16:49:20 +00:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the actual web request to convert TeX to MathML.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function doRender() {
|
|
|
|
if ( $this->getTex() === '' ) {
|
2015-09-21 16:14:01 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug(
|
|
|
|
'Rendering was requested, but no TeX string is specified.'
|
|
|
|
);
|
2014-06-10 16:49:20 +00:00
|
|
|
$this->lastError = $this->getError( 'math_empty_tex' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$res = '';
|
|
|
|
$host = self::pickHost();
|
|
|
|
$post = $this->getPostData();
|
|
|
|
$this->lastError = '';
|
|
|
|
$requestResult = $this->makeRequest( $host, $post, $res, $this->lastError );
|
|
|
|
if ( $requestResult ) {
|
|
|
|
$jsonResult = json_decode( $res );
|
|
|
|
if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) {
|
|
|
|
if ( $jsonResult->success ) {
|
2015-01-24 14:53:24 +00:00
|
|
|
return $this->processJsonResult( $jsonResult, $host );
|
2014-06-10 16:49:20 +00:00
|
|
|
} else {
|
2014-08-28 15:57:53 +00:00
|
|
|
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 );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning(
|
2016-04-12 20:53:25 +00:00
|
|
|
'Mathoid conversion error:' . var_export( [
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'result' => $res
|
2016-04-12 20:53:25 +00:00
|
|
|
], true ) );
|
2014-06-10 16:49:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->lastError = $this->getError( 'math_invalidjson', $host );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2016-04-12 20:53:25 +00:00
|
|
|
'MathML InvalidJSON:' . var_export( [
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'res' => $res
|
2016-04-12 20:53:25 +00:00
|
|
|
], true ) );
|
2014-06-10 16:49:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error message has already been set.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2015-02-20 00:22:46 +00:00
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
$xmlObject = new XmlTypeCheck( $XML, null, false );
|
|
|
|
if ( !$xmlObject->wellFormed ) {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-03-18 17:28:34 +00:00
|
|
|
'XML validation error: ' . var_export( $XML, true ) );
|
2014-06-03 08:15:53 +00:00
|
|
|
} else {
|
|
|
|
$name = $xmlObject->getRootElement();
|
|
|
|
$elementSplit = explode( ':', $name );
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( is_array( $elementSplit ) ) {
|
2014-06-03 08:15:53 +00:00
|
|
|
$localName = end( $elementSplit );
|
|
|
|
} else {
|
|
|
|
$localName = $name;
|
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( in_array( $localName, $this->getAllowedRootElements() ) ) {
|
2014-06-03 08:15:53 +00:00
|
|
|
$out = true;
|
|
|
|
} else {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error( "Got wrong root element : $name" );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:49:20 +00:00
|
|
|
/**
|
|
|
|
* @param boolean $noRender
|
|
|
|
* @return type
|
|
|
|
*/
|
2014-10-11 14:13:04 +00:00
|
|
|
private function getFallbackImageUrl( $noRender = false ) {
|
2015-12-01 22:51:22 +00:00
|
|
|
if ( $this->svgPath ) {
|
|
|
|
return $this->svgPath;
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
return SpecialPage::getTitleFor( 'MathShowImage' )->getLocalURL( [
|
2014-06-10 16:49:20 +00:00
|
|
|
'hash' => $this->getMd5(),
|
2014-10-11 14:13:04 +00:00
|
|
|
'mode' => $this->getMode(),
|
2016-04-12 20:53:25 +00:00
|
|
|
'noRender' => $noRender
|
|
|
|
]
|
2014-06-10 16:49:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to correct the style information for a
|
|
|
|
* linked SVG image.
|
|
|
|
* @param string $style current style information to be updated
|
|
|
|
*/
|
2016-04-13 12:30:48 +00:00
|
|
|
public function correctSvgStyle( &$style ) {
|
|
|
|
if ( preg_match( '/style="([^"]*)"/', $this->getSvg(), $styles ) ) {
|
2015-01-22 18:56:13 +00:00
|
|
|
$style .= ' ' . $styles[1]; // merge styles
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() === 'display' ) {
|
2014-06-10 16:49:20 +00:00
|
|
|
// TODO: Improve style cleaning
|
2015-09-21 16:14:01 +00:00
|
|
|
$style = preg_replace(
|
|
|
|
'/margin\-(left|right)\:\s*\d+(\%|in|cm|mm|em|ex|pt|pc|px)\;/', '', $style
|
|
|
|
);
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
2016-04-13 12:30:48 +00:00
|
|
|
$style = trim( preg_replace( '/position:\s*absolute;\s*left:\s*0px;/', '', $style ),
|
|
|
|
"; \t\n\r\0\x0B" ) .'; ';
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
2014-07-09 11:32:28 +00:00
|
|
|
// TODO: Figure out if there is a way to construct
|
|
|
|
// a SVGReader from a string that represents the SVG
|
|
|
|
// content
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( preg_match( "/height=\"(.*?)\"/", $this->getSvg(), $matches ) ) {
|
2015-01-22 18:56:13 +00:00
|
|
|
$style .= 'height: ' . $matches[1] . '; ';
|
2014-07-09 11:32:28 +00:00
|
|
|
}
|
|
|
|
if ( preg_match( "/width=\"(.*?)\"/", $this->getSvg(), $matches ) ) {
|
2015-01-22 18:56:13 +00:00
|
|
|
$style .= 'width: ' . $matches[1] . ';';
|
2014-07-09 11:32:28 +00:00
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-10 16:49:20 +00:00
|
|
|
* Gets img tag for math image
|
2015-09-21 16:14:01 +00:00
|
|
|
* @param boolean $noRender if true no rendering will be performed
|
|
|
|
* if the image is not stored in the database
|
|
|
|
* @param boolean|string $classOverride if classOverride
|
|
|
|
* is false the class name will be calculated by getClassName
|
2014-06-10 16:49:20 +00:00
|
|
|
* @return string XML the image html tag
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
2014-10-11 14:13:04 +00:00
|
|
|
private function getFallbackImage( $noRender = false, $classOverride = false ) {
|
2016-06-02 10:12:32 +00:00
|
|
|
$attribs = [
|
|
|
|
'src' => $this->getFallbackImageUrl( $noRender )
|
|
|
|
];
|
2014-06-10 16:49:20 +00:00
|
|
|
if ( $classOverride === false ) { // $class = '' suppresses class attribute
|
2014-10-11 14:13:04 +00:00
|
|
|
$class = $this->getClassName( true );
|
2014-06-10 16:49:20 +00:00
|
|
|
} else {
|
2015-09-21 16:14:01 +00:00
|
|
|
$class = $classOverride;
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( ! $this->mathoidStyle ) {
|
2016-04-13 12:30:48 +00:00
|
|
|
$this->correctSvgStyle( $this->mathoidStyle );
|
2016-01-31 21:11:39 +00:00
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( $class ) {
|
|
|
|
$attribs['class'] = $class;
|
|
|
|
}
|
|
|
|
|
2016-06-02 10:12:32 +00:00
|
|
|
return Xml::element( 'img', $this->getAttributes( 'span', $attribs, [
|
|
|
|
'aria-hidden' => 'true',
|
2016-06-06 16:03:24 +00:00
|
|
|
'style' => $this->mathoidStyle,
|
|
|
|
'alt' => $this->tex
|
2016-04-12 20:53:25 +00:00
|
|
|
] ) );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMathTableName() {
|
|
|
|
return 'mathoid';
|
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
/**
|
|
|
|
* Calculates the default class name for a math element
|
|
|
|
* @param boolean $fallback
|
|
|
|
* @return string the class name
|
|
|
|
*/
|
2014-10-11 14:13:04 +00:00
|
|
|
private function getClassName( $fallback = false ) {
|
2015-01-22 18:56:13 +00:00
|
|
|
$class = 'mwe-math-';
|
2014-06-05 21:06:43 +00:00
|
|
|
if ( $fallback ) {
|
2014-10-12 09:26:57 +00:00
|
|
|
$class .= 'fallback-image-';
|
2014-06-05 21:06:43 +00:00
|
|
|
} else {
|
|
|
|
$class .= 'mathml-';
|
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() == 'display' ) {
|
2014-06-05 21:06:43 +00:00
|
|
|
$class .= 'display';
|
|
|
|
} else {
|
|
|
|
$class .= 'inline';
|
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( !$fallback ) {
|
|
|
|
// @codingStandardsIgnoreStart
|
2014-10-10 10:38:23 +00:00
|
|
|
$class .= ' mwe-math-mathml-a11y';
|
2015-09-21 16:14:01 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2014-10-10 10:38:23 +00:00
|
|
|
}
|
2016-12-24 13:04:07 +00:00
|
|
|
return $class;
|
2014-06-05 21:06:43 +00:00
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
/**
|
|
|
|
* @return string Html output that is embedded in the page
|
|
|
|
*/
|
|
|
|
public function getHtmlOutput() {
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() == 'display' ) {
|
2014-06-05 21:06:43 +00:00
|
|
|
$element = 'div';
|
|
|
|
} else {
|
|
|
|
$element = 'span';
|
|
|
|
}
|
2017-01-11 18:43:10 +00:00
|
|
|
$attribs = [ 'class' => 'mwe-math-element' ];
|
2014-06-10 16:49:20 +00:00
|
|
|
if ( $this->getID() !== '' ) {
|
|
|
|
$attribs['id'] = $this->getID();
|
|
|
|
}
|
2016-02-08 17:35:49 +00:00
|
|
|
$output = Html::openElement( $element, $attribs );
|
2014-06-05 21:06:43 +00:00
|
|
|
// MathML has to be wrapped into a div or span in order to be able to hide it.
|
2014-07-10 15:14:26 +00:00
|
|
|
// Remove displayStyle attributes set by the MathML converter
|
|
|
|
// (Beginning from Mathoid 0.2.5 block is the default layout.)
|
2015-09-21 16:14:01 +00:00
|
|
|
$mml = preg_replace(
|
|
|
|
'/(<math[^>]*)(display|mode)=["\'](inline|block)["\']/', '$1', $this->getMathml()
|
|
|
|
);
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() == 'display' ) {
|
2014-06-05 21:06:43 +00:00
|
|
|
$mml = preg_replace( '/<math/', '<math display="block"', $mml );
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
$output .= Xml::tags( $element, [
|
2015-09-21 16:14:01 +00:00
|
|
|
'class' => $this->getClassName(), 'style' => 'display: none;'
|
2016-04-12 20:53:25 +00:00
|
|
|
], $mml );
|
2015-09-21 16:14:01 +00:00
|
|
|
$output .= $this->getFallbackImage();
|
2016-02-08 17:35:49 +00:00
|
|
|
$output .= Html::closeElement( $element );
|
2014-06-05 21:06:43 +00:00
|
|
|
return $output;
|
|
|
|
}
|
2014-06-10 16:49:20 +00:00
|
|
|
|
|
|
|
protected function dbOutArray() {
|
|
|
|
$out = parent::dbOutArray();
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( $this->getMathTableName() == 'mathoid' ) {
|
2014-06-10 16:49:20 +00:00
|
|
|
$out['math_input'] = $out['math_inputtex'];
|
2015-09-21 16:14:01 +00:00
|
|
|
unset( $out['math_inputtex'] );
|
2014-06-10 16:49:20 +00:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dbInArray() {
|
|
|
|
$out = parent::dbInArray();
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( $this->getMathTableName() == 'mathoid' ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$out = array_diff( $out, [ 'math_inputtex' ] );
|
2014-06-10 16:49:20 +00:00
|
|
|
$out[] = 'math_input';
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2014-07-25 20:09:34 +00:00
|
|
|
|
|
|
|
protected function initializeFromDatabaseRow( $rpage ) {
|
|
|
|
// mathoid allows different input formats
|
|
|
|
// therefore the column name math_inputtex was changed to math_input
|
|
|
|
if ( $this->getMathTableName() == 'mathoid' && ! empty( $rpage->math_input ) ) {
|
|
|
|
$this->userInputTex = $rpage->math_input;
|
|
|
|
}
|
|
|
|
parent::initializeFromDatabaseRow( $rpage );
|
|
|
|
|
|
|
|
}
|
2015-01-24 14:53:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $jsonResult
|
|
|
|
* @param $host
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function processJsonResult( $jsonResult, $host ) {
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMode() == 'latexml' || $this->inputType == 'pmml' ||
|
2015-01-24 14:53:24 +00:00
|
|
|
$this->isValidMathML( $jsonResult->mml )
|
|
|
|
) {
|
|
|
|
if ( isset( $jsonResult->svg ) ) {
|
|
|
|
$xmlObject = new XmlTypeCheck( $jsonResult->svg, null, false );
|
|
|
|
if ( !$xmlObject->wellFormed ) {
|
|
|
|
$this->lastError = $this->getError( 'math_invalidxml', $host );
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$this->setSvg( $jsonResult->svg );
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-03-16 21:43:20 +00:00
|
|
|
'Missing SVG property in JSON result.' );
|
2015-01-24 14:53:24 +00:00
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMode() != 'latexml' && $this->inputType != 'pmml' ) {
|
2015-01-24 14:53:24 +00:00
|
|
|
$this->setMathml( $jsonResult->mml );
|
|
|
|
}
|
2017-02-01 03:54:07 +00:00
|
|
|
// Avoid PHP 7.1 warning from passing $this by reference
|
|
|
|
$renderer = $this;
|
2015-07-12 19:40:34 +00:00
|
|
|
Hooks::run( 'MathRenderingResultRetrieved',
|
2017-02-01 03:54:07 +00:00
|
|
|
[ &$renderer, &$jsonResult ] ); // Enables debugging of server results
|
2015-01-24 14:53:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$this->lastError = $this->getError( 'math_unknown_error', $host );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 11:32:28 +00:00
|
|
|
}
|