2013-05-14 21:49:06 +00:00
|
|
|
<?php
|
2015-04-14 19:42:48 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
|
|
|
* (c)2012 Moritz Schubotz
|
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*
|
|
|
|
* Contains the driver function for the LaTeXML daemon
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
class MathLaTeXML extends MathMathML {
|
|
|
|
protected $defaultAllowedRootElements = array( 'math', 'div', 'table', 'query' );
|
|
|
|
/** @var String settings for LaTeXML daemon */
|
2013-05-14 21:49:06 +00:00
|
|
|
private $LaTeXMLSettings = '';
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
public function __construct( $tex = '', $params = array() ) {
|
|
|
|
global $wgMathLaTeXMLUrl;
|
|
|
|
parent::__construct( $tex, $params );
|
|
|
|
$this->hosts = $wgMathLaTeXMLUrl;
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMode( 'latexml' );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
2013-07-25 07:14:30 +00:00
|
|
|
/**
|
|
|
|
* Converts an array with LaTeXML settings to a URL encoded String.
|
|
|
|
* If the argument is a string the input will be returned.
|
|
|
|
* Thus the function has projector properties and can be applied a second time safely.
|
|
|
|
* @param (string|array) $array
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-09 19:02:15 +00:00
|
|
|
public function serializeSettings( $array ) {
|
|
|
|
if ( !is_array( $array ) ) {
|
2013-07-25 07:14:30 +00:00
|
|
|
return $array;
|
|
|
|
} else {
|
2014-02-09 19:02:15 +00:00
|
|
|
// removes the [1] [2]... for the unnamed subarrays since LaTeXML
|
|
|
|
// assigns multiple values to one key e.g.
|
|
|
|
// preload=amsmath.sty&preload=amsthm.sty&preload=amstext.sty
|
2014-04-08 10:39:12 +00:00
|
|
|
$cgi_string = wfArrayToCgi( $array );
|
|
|
|
$cgi_string = preg_replace( '|\%5B\d+\%5D|', '', $cgi_string );
|
|
|
|
$cgi_string = preg_replace( '|&\d+=|', '&', $cgi_string );
|
|
|
|
return $cgi_string;
|
2013-07-25 07:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* Gets the settings for the LaTeXML daemon.
|
2014-04-08 10:39:12 +00:00
|
|
|
* @global (array|string) $wgMathDefaultLaTeXMLSetting
|
2013-05-14 21:49:06 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLaTeXMLSettings() {
|
2014-01-26 00:43:41 +00:00
|
|
|
global $wgMathDefaultLaTeXMLSetting;
|
2013-05-14 21:49:06 +00:00
|
|
|
if ( $this->LaTeXMLSettings ) {
|
|
|
|
return $this->LaTeXMLSettings;
|
|
|
|
} else {
|
2014-01-26 00:43:41 +00:00
|
|
|
return $wgMathDefaultLaTeXMLSetting;
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the settings for the LaTeXML daemon.
|
|
|
|
* The settings affect only the current instance of the class.
|
|
|
|
* For a list of possible settings see:
|
|
|
|
* http://dlmf.nist.gov/LaTeXML/manual/commands/latexmlpost.xhtml
|
|
|
|
* An empty value indicates to use the default settings.
|
2013-07-25 07:14:30 +00:00
|
|
|
* @param string|array $settings
|
2013-05-14 21:49:06 +00:00
|
|
|
*/
|
|
|
|
public function setLaTeXMLSettings( $settings ) {
|
|
|
|
$this->LaTeXMLSettings = $settings;
|
|
|
|
}
|
|
|
|
|
2013-05-30 07:53:30 +00:00
|
|
|
/**
|
|
|
|
* Calculates the HTTP POST Data for the request. Depends on the settings
|
|
|
|
* and the input string only.
|
|
|
|
* @return string HTTP POST data
|
|
|
|
*/
|
2014-06-03 08:15:53 +00:00
|
|
|
public function getLaTeXMLPostData() {
|
2014-03-17 06:14:02 +00:00
|
|
|
$tex = $this->getTex();
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() == 'inlineDisplaystyle' ) {
|
|
|
|
// In 'inlineDisplaystyle' the old
|
2014-03-17 06:14:02 +00:00
|
|
|
// texvc behavior is reproduced:
|
|
|
|
// The equation is rendered in displaystyle
|
|
|
|
// (texvc used $$ $tex $$ to render)
|
|
|
|
// but the equation is not centered.
|
|
|
|
$tex = '{\displaystyle ' . $tex . '}';
|
|
|
|
}
|
|
|
|
$texcmd = rawurlencode( $tex );
|
2015-03-16 19:51:16 +00:00
|
|
|
$settings = $this->serializeSettings( $this->getLaTeXMLSettings() );
|
2014-06-03 08:15:53 +00:00
|
|
|
$postData = $settings . '&tex=' . $texcmd;
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Get post data: ' . $postData );
|
2014-06-03 08:15:53 +00:00
|
|
|
return $postData;
|
2013-05-30 07:53:30 +00:00
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* Does the actual web request to convert TeX to MathML.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-05-28 18:12:39 +00:00
|
|
|
protected function doRender() {
|
|
|
|
if ( trim( $this->getTex() ) === '' ) {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning(
|
2015-03-18 17:28:34 +00:00
|
|
|
'Rendering was requested, but no TeX string is specified.' );
|
2014-05-28 18:12:39 +00:00
|
|
|
$this->lastError = $this->getError( 'math_empty_tex' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$res = '';
|
2014-06-03 08:15:53 +00:00
|
|
|
$host = $this->pickHost();
|
|
|
|
$post = $this->getLaTeXMLPostData();
|
2014-05-05 16:13:43 +00:00
|
|
|
// 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' ) ){
|
2015-09-21 16:14:01 +00:00
|
|
|
$post = preg_replace( '/&tex=/', '&tex=literal:', $post, 1 );
|
2014-05-05 16:13:43 +00:00
|
|
|
}
|
2013-05-14 21:49:06 +00:00
|
|
|
$this->lastError = '';
|
2014-05-28 18:12:39 +00:00
|
|
|
$requestResult = $this->makeRequest( $host, $post, $res, $this->lastError );
|
|
|
|
if ( $requestResult ) {
|
|
|
|
$jsonResult = json_decode( $res );
|
|
|
|
if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) {
|
|
|
|
if ( $this->isValidMathML( $jsonResult->result ) ) {
|
|
|
|
$this->setMathml( $jsonResult->result );
|
2015-07-12 19:40:34 +00:00
|
|
|
Hooks::run( 'MathRenderingResultRetrieved',
|
2015-07-22 22:10:46 +00:00
|
|
|
array( &$this,
|
2015-07-12 19:40:34 +00:00
|
|
|
&$jsonResult ) );// Enables debugging of server results
|
2013-05-14 21:49:06 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Do not print bad mathml. It's probably too verbose and might
|
|
|
|
// mess up the browser output.
|
2014-05-28 18:12:39 +00:00
|
|
|
$this->lastError = $this->getError( 'math_invalidxml', $this->getModeStr(), $host );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning(
|
2015-03-18 17:28:34 +00:00
|
|
|
'LaTeXML InvalidMathML: ' . 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 ) );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-28 18:12:39 +00:00
|
|
|
$this->lastError = $this->getError( 'math_invalidjson', $this->getModeStr(), $host );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->warning(
|
2015-03-18 17:28:34 +00:00
|
|
|
'LaTeXML 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 ) );
|
2015-03-16 19:51:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-14 21:49:06 +00:00
|
|
|
} else {
|
|
|
|
// Error message has already been set.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal version of @link self::embedMathML
|
|
|
|
* @return string
|
|
|
|
* @return html element with rendered math
|
|
|
|
*/
|
2014-06-03 08:15:53 +00:00
|
|
|
protected function getMathMLTag() {
|
2013-05-14 21:49:06 +00:00
|
|
|
return self::embedMathML( $this->getMathml(), urldecode( $this->getTex() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Embeds the MathML-XML element in a HTML span element with class tex
|
2014-02-09 19:02:15 +00:00
|
|
|
* @param string $mml : the MathML string
|
|
|
|
* @param string $tagId : optional tagID for references like (pagename#equation2)
|
|
|
|
* @param bool $attribs
|
2013-05-14 21:49:06 +00:00
|
|
|
* @return html element with rendered math
|
|
|
|
*/
|
|
|
|
public static function embedMathML( $mml, $tagId = '', $attribs = false ) {
|
|
|
|
$mml = str_replace( "\n", " ", $mml );
|
|
|
|
if ( ! $attribs ) {
|
|
|
|
$attribs = array( 'class' => 'tex', 'dir' => 'ltr' );
|
|
|
|
if ( $tagId ) {
|
|
|
|
$attribs['id'] = $tagId;
|
|
|
|
}
|
|
|
|
$attribs = Sanitizer::validateTagAttributes( $attribs, 'span' );
|
|
|
|
}
|
|
|
|
return Xml::tags( 'span', $attribs, $mml );
|
|
|
|
}
|
|
|
|
|
2014-07-28 18:12:32 +00:00
|
|
|
/**
|
|
|
|
* Calculates the SVG image based on the MathML input
|
|
|
|
* No cache is used.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-11-18 09:09:29 +00:00
|
|
|
public function calculateSvg() {
|
2014-07-28 18:12:32 +00:00
|
|
|
$renderer = new MathMathML( $this->getTex() );
|
|
|
|
$renderer->setMathml( $this->getMathml() );
|
2015-07-22 22:10:46 +00:00
|
|
|
$renderer->setMode( 'latexml' );
|
2014-09-06 01:55:54 +00:00
|
|
|
$res = $renderer->render( true );
|
2014-07-28 18:12:32 +00:00
|
|
|
if ( $res == true ) {
|
2015-07-13 15:43:26 +00:00
|
|
|
$this->setSvg( $renderer->getSvg() );
|
2014-07-28 18:12:32 +00:00
|
|
|
} else {
|
|
|
|
$lastError = $renderer->getLastError();
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-03-18 17:28:34 +00:00
|
|
|
'Failed to convert LaTeXML-MathML to SVG:' . $lastError );
|
2014-07-28 18:12:32 +00:00
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:09:29 +00:00
|
|
|
|
2014-07-28 18:12:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the SVG image
|
2014-11-18 09:09:29 +00:00
|
|
|
*
|
|
|
|
* @param string $render if set to 'render' (default) and no SVG image exists, the function
|
|
|
|
* tries to generate it on the fly.
|
|
|
|
* Otherwise, if set to 'cached', and there is no SVG in the database
|
|
|
|
* cache, an empty string is returned.
|
|
|
|
*
|
2014-07-28 18:12:32 +00:00
|
|
|
* @return string XML-Document of the rendered SVG
|
|
|
|
*/
|
2014-11-18 09:09:29 +00:00
|
|
|
public function getSvg( $render = 'render' ) {
|
|
|
|
if ( $render == 'render' && ( $this->isPurge() || $this->svg == '' ) ) {
|
|
|
|
$this->calculateSvg();
|
2014-07-28 18:12:32 +00:00
|
|
|
}
|
2014-11-18 09:09:29 +00:00
|
|
|
return parent::getSvg( $render );
|
2014-07-28 18:12:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 04:46:53 +00:00
|
|
|
protected function getMathTableName() {
|
|
|
|
return 'mathlatexml';
|
|
|
|
}
|
2014-05-05 16:13:43 +00:00
|
|
|
}
|
2014-05-27 04:46:53 +00:00
|
|
|
|