2013-05-14 21:49:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
$this->setMode( MW_MATH_LATEXML );
|
|
|
|
}
|
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();
|
|
|
|
if ( $this->getMathStyle() == MW_MATHSTYLE_INLINE_DISPLAYSTYLE ) {
|
|
|
|
// In MW_MATHSTYLE_INLINE_DISPLAYSTYLE the old
|
|
|
|
// 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 );
|
|
|
|
$settings = $this->serializeSettings( $this->getLaTeXMLSettings( ) );
|
2014-06-03 08:15:53 +00:00
|
|
|
$postData = $settings . '&tex=' . $texcmd;
|
|
|
|
wfDebugLog( "Math", 'Get post data: ' . $postData );
|
|
|
|
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() {
|
|
|
|
global $wgMathDebug;
|
2015-02-12 20:14:24 +00:00
|
|
|
|
2014-05-28 18:12:39 +00:00
|
|
|
if ( trim( $this->getTex() ) === '' ) {
|
|
|
|
wfDebugLog( "Math", "Rendering was requested, but no TeX string is specified." );
|
|
|
|
$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' ) ){
|
|
|
|
$post = preg_replace( '/&tex=/' , '&tex=literal:', $post , 1);
|
|
|
|
}
|
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 );
|
|
|
|
if ( $wgMathDebug ) {
|
|
|
|
$this->setLog( $jsonResult->log );
|
|
|
|
$this->setStatusCode( $jsonResult->status_code );
|
|
|
|
}
|
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 );
|
2013-05-14 21:49:06 +00:00
|
|
|
wfDebugLog( "Math", "\nLaTeXML InvalidMathML:"
|
2014-05-27 04:46:53 +00:00
|
|
|
. var_export( array( 'post' => $post, 'host' => $host
|
2014-05-28 18:12:39 +00:00
|
|
|
, 'result' => $res ), true ) . "\n\n" );
|
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 );
|
2013-05-14 21:49:06 +00:00
|
|
|
wfDebugLog( "Math", "\nLaTeXML InvalidJSON:"
|
|
|
|
. var_export( array( 'post' => $post, 'host' => $host
|
2014-05-27 04:46:53 +00:00
|
|
|
, 'res' => $res ), true ) . "\n\n" );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} 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() );
|
|
|
|
$renderer->setMode( MW_MATH_LATEXML );
|
2014-09-06 01:55:54 +00:00
|
|
|
$res = $renderer->render( true );
|
2014-07-28 18:12:32 +00:00
|
|
|
if ( $res == true ) {
|
|
|
|
$this->svg = $renderer->getSvg();
|
|
|
|
} else {
|
|
|
|
$lastError = $renderer->getLastError();
|
|
|
|
wfDebugLog( 'Math', 'failed to convert LaTeXML-MathML to SVG:' . $lastError );
|
|
|
|
}
|
|
|
|
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
|
|
|
|