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 {
|
|
|
|
|
|
|
|
protected $defaultAllowedRootElements = array( 'math' );
|
2016-01-29 13:05:54 +00:00
|
|
|
protected $restbaseInputTypes = array( '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;
|
2014-06-10 16:49:20 +00:00
|
|
|
|
|
|
|
public function __construct( $tex = '', $params = array() ) {
|
|
|
|
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-01-29 16:50:32 +00:00
|
|
|
if ( in_array( $this->inputType, $this->restbaseInputTypes ) && $this->mode == 'mathml' ) {
|
|
|
|
$rbi = $this->rbi;
|
|
|
|
if ( !$rbi ){
|
|
|
|
$rbi = new MathRestbaseInterface( $this->getTex(), $this->getInputType() );
|
|
|
|
$rbi->checkTeX();
|
2015-12-01 22:51:22 +00:00
|
|
|
}
|
2016-01-29 16:50:32 +00:00
|
|
|
if ( $rbi->getSuccess() ) {
|
2015-12-14 22:28:40 +00:00
|
|
|
$this->mathml = $rbi->getMathML();
|
|
|
|
$this->svg = $rbi->getSvg();
|
|
|
|
$this->svgPath = $rbi->getFullSvgUrl();
|
|
|
|
}
|
2015-12-09 14:17:15 +00:00
|
|
|
$this->changed = false;
|
2015-12-01 22:51:22 +00:00
|
|
|
return $rbi->getSuccess();
|
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
if ( $forceReRendering ) {
|
|
|
|
$this->setPurge( true );
|
|
|
|
}
|
|
|
|
if ( $this->renderingRequired() ) {
|
2015-02-12 20:14:24 +00:00
|
|
|
return $this->doRender();
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
2014-06-05 21:06:43 +00:00
|
|
|
return true;
|
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();
|
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout );
|
|
|
|
/** @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;
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning( 'Timeout:' . var_export( array(
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'timeout' => $wgMathLaTeXMLTimeout
|
2015-03-18 17:28:34 +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' ) );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning( 'NoResponse:' . var_export( array(
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'errormsg' => $errormsg
|
2015-03-18 17:28:34 +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(
|
2015-03-16 21:43:20 +00:00
|
|
|
'Mathoid conversion error:' . var_export( array(
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'result' => $res
|
2015-03-18 17:28:34 +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(
|
2015-03-18 17:28:34 +00:00
|
|
|
'MathML InvalidJSON:' . var_export( array(
|
2015-03-16 19:51:16 +00:00
|
|
|
'post' => $post,
|
|
|
|
'host' => $host,
|
|
|
|
'res' => $res
|
2015-03-18 17:28:34 +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;
|
|
|
|
}
|
2014-06-10 16:49:20 +00:00
|
|
|
return SpecialPage::getTitleFor( 'MathShowImage' )->getLocalURL( array(
|
|
|
|
'hash' => $this->getMd5(),
|
2014-10-11 14:13:04 +00:00
|
|
|
'mode' => $this->getMode(),
|
2014-06-10 16:49:20 +00:00
|
|
|
'noRender' => $noRender )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to correct the style information for a
|
|
|
|
* linked SVG image.
|
|
|
|
* @param string $svg SVG-image data
|
|
|
|
* @param string $style current style information to be updated
|
|
|
|
*/
|
|
|
|
public function correctSvgStyle( $svg, &$style ) {
|
|
|
|
if ( preg_match( '/style="([^"]*)"/', $svg, $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
|
|
|
}
|
2014-10-09 18:28:49 +00:00
|
|
|
$style = preg_replace( '/position:\s*absolute;\s*left:\s*0px;/', '', $style );
|
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 ) {
|
|
|
|
$url = $this->getFallbackImageUrl( $noRender );
|
2014-07-09 11:32:28 +00:00
|
|
|
|
2014-06-10 16:49:20 +00:00
|
|
|
$attribs = array();
|
|
|
|
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
|
|
|
}
|
2014-07-09 11:32:28 +00:00
|
|
|
|
2014-10-15 17:03:35 +00:00
|
|
|
// TODO: move the common styles to the global stylesheet!
|
2015-03-16 21:43:20 +00:00
|
|
|
$style = 'background-image: url(\''. $url .
|
|
|
|
'\'); background-repeat: no-repeat; background-size: 100% 100%;';
|
2014-10-11 14:13:04 +00:00
|
|
|
$this->correctSvgStyle( $this->getSvg(), $style );
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( $class ) {
|
|
|
|
$attribs['class'] = $class;
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:08:41 +00:00
|
|
|
// Don't use an empty span, as that is going to be stripped by HTML tidy
|
|
|
|
// when enabled (which is true in production).
|
2015-09-21 16:14:01 +00:00
|
|
|
return Xml::element( 'meta', $this->getAttributes(
|
|
|
|
'span', $attribs, array( 'aria-hidden' => 'true', 'style' => $style
|
|
|
|
) ) );
|
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
|
|
|
}
|
2014-06-05 21:06:43 +00:00
|
|
|
return $class;
|
|
|
|
}
|
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';
|
|
|
|
}
|
|
|
|
$attribs = array();
|
2014-06-10 16:49:20 +00:00
|
|
|
if ( $this->getID() !== '' ) {
|
|
|
|
$attribs['id'] = $this->getID();
|
|
|
|
}
|
2014-06-05 21:06:43 +00:00
|
|
|
$output = HTML::openElement( $element, $attribs );
|
|
|
|
// 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 );
|
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
$output .= Xml::tags( $element, array(
|
|
|
|
'class' => $this->getClassName(), 'style' => 'display: none;'
|
|
|
|
), $mml );
|
|
|
|
$output .= $this->getFallbackImage();
|
2014-06-05 21:06:43 +00:00
|
|
|
$output .= HTML::closeElement( $element );
|
|
|
|
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' ) {
|
2014-06-10 16:49:20 +00:00
|
|
|
$out = array_diff( $out, array( 'math_inputtex' ) );
|
|
|
|
$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 );
|
|
|
|
}
|
2015-07-12 19:40:34 +00:00
|
|
|
Hooks::run( 'MathRenderingResultRetrieved',
|
2015-07-16 14:33:34 +00:00
|
|
|
array( &$this,
|
|
|
|
&$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
|
|
|
}
|