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
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MathLaTeXML extends MathRenderer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String settings for LaTeXML daemon
|
|
|
|
*/
|
|
|
|
private $LaTeXMLSettings = '';
|
2014-04-08 10:39:12 +00:00
|
|
|
/** @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 = '';
|
2013-05-14 21:49:06 +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;
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:39:12 +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 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;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
/* (non-PHPdoc)
|
|
|
|
* @see MathRenderer::render()
|
|
|
|
*/
|
|
|
|
public function render( $forceReRendering = false ) {
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
if ( $forceReRendering ) {
|
|
|
|
$this->setPurge( true );
|
|
|
|
}
|
|
|
|
if ( $this->renderingRequired() ) {
|
|
|
|
$res = $this->doRender( );
|
|
|
|
if ( ! $res ) {
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
return $this->getLastError();
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 10:45:43 +00:00
|
|
|
$result = $this->getMathMLTag();
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $result;
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2014-04-08 10:39:12 +00:00
|
|
|
if ( $this->isValidMathML( $this->getMathml() ) ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
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.
|
2014-01-26 00:43:41 +00:00
|
|
|
* Uses $wgMathLaTeXMLTimeout as timeout.
|
2013-05-14 21:49:06 +00:00
|
|
|
* Generates error messages on failure
|
|
|
|
* @see Http::post()
|
|
|
|
*
|
2014-04-08 10:39:12 +00:00
|
|
|
* @global int $wgMathLaTeXMLTimeout
|
2013-05-14 21:49:06 +00:00
|
|
|
* @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' ) {
|
2014-01-26 00:43:41 +00:00
|
|
|
global $wgMathLaTeXMLTimeout;
|
2013-11-18 10:45:43 +00:00
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
$error = '';
|
|
|
|
$res = null;
|
2014-01-26 00:43:41 +00:00
|
|
|
$options = array( 'method' => 'POST', 'postData' => $post, 'timeout' => $wgMathLaTeXMLTimeout );
|
2014-04-08 10:39:12 +00:00
|
|
|
/** @var $req (CurlHttpRequest|PhpHttpRequest) the request object */
|
2013-05-14 21:49:06 +00:00
|
|
|
$req = $httpRequestClass::factory( $host, $options );
|
|
|
|
$status = $req->execute();
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
$res = $req->getContent();
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
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
|
2014-01-26 00:43:41 +00:00
|
|
|
, 'timeout' => $wgMathLaTeXMLTimeout ), true ) . "\n\n" );
|
2013-05-14 21:49:06 +00:00
|
|
|
} 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" );
|
|
|
|
}
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-PHPdoc)
|
|
|
|
* @see MathRenderer::writeCache()
|
|
|
|
*/
|
|
|
|
public function writeCache() {
|
|
|
|
if ( $this->isChanged() ) {
|
|
|
|
$this->writeToDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Picks a LaTeXML daemon.
|
|
|
|
* If more than one demon are availible one is chosen from the
|
2014-01-26 00:43:41 +00:00
|
|
|
* $wgMathLaTeXMLUrl array.
|
2013-05-14 21:49:06 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function pickHost() {
|
2014-01-26 00:43:41 +00:00
|
|
|
global $wgMathLaTeXMLUrl;
|
|
|
|
if ( is_array( $wgMathLaTeXMLUrl ) ) {
|
|
|
|
$host = array_rand( $wgMathLaTeXMLUrl );
|
2013-05-14 21:49:06 +00:00
|
|
|
} else {
|
2014-01-26 00:43:41 +00:00
|
|
|
$host = $wgMathLaTeXMLUrl;
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
|
|
|
wfDebugLog( "Math", "picking host " . $host );
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2013-07-07 12:42:31 +00:00
|
|
|
public function getPostData() {
|
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( ) );
|
|
|
|
return $settings. '&tex=' . $texcmd;
|
2013-05-30 07:53:30 +00:00
|
|
|
}
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* Does the actual web request to convert TeX to MathML.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function doRender( ) {
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
$host = self::pickHost();
|
2013-05-30 07:53:30 +00:00
|
|
|
$post = $this->getPostData();
|
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 = '';
|
|
|
|
if ( $this->makeRequest( $host, $post, $res, $this->lastError ) ) {
|
|
|
|
$result = json_decode( $res );
|
|
|
|
if ( json_last_error() === JSON_ERROR_NONE ) {
|
2014-04-08 10:39:12 +00:00
|
|
|
if ( $this->isValidMathML( $result->result ) ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
$this->setMathml( $result->result );
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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.
|
|
|
|
$this->lastError = $this->getError( 'math_latexml_invalidxml', $host );
|
|
|
|
wfDebugLog( "Math", "\nLaTeXML InvalidMathML:"
|
|
|
|
. var_export( array( 'post' => $post, 'host' => $host
|
|
|
|
, 'result' => $result ), true ) . "\n\n" );
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->lastError = $this->getError( 'math_latexml_invalidjson', $host );
|
|
|
|
wfDebugLog( "Math", "\nLaTeXML InvalidJSON:"
|
|
|
|
. var_export( array( 'post' => $post, 'host' => $host
|
|
|
|
, 'res' => $res ), true ) . "\n\n" );
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error message has already been set.
|
2013-11-18 10:45:43 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-05-14 21:49:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:39:12 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* Checks if the input is valid MathML,
|
|
|
|
* and if the root element has the name math
|
|
|
|
* @param string $XML
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-04-08 10:39:12 +00:00
|
|
|
public function isValidMathML( $XML ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
$out = false;
|
2014-04-08 10:39:12 +00:00
|
|
|
if ( !$this->XMLValidation ) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-07 12:42:31 +00:00
|
|
|
// depends on https://gerrit.wikimedia.org/r/#/c/66365/
|
2014-04-08 10:39:12 +00:00
|
|
|
if ( !is_callable( 'XmlTypeCheck::newFromString' ) ) {
|
2013-07-07 12:42:31 +00:00
|
|
|
$msg = wfMessage( 'math_latexml_xmlversion' )->inContentLanguage()->escaped();
|
|
|
|
trigger_error( $msg, E_USER_NOTICE );
|
|
|
|
wfDebugLog( 'Math', $msg );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$xmlObject = new XmlTypeCheck( $XML, null, false );
|
2014-04-08 10:39:12 +00:00
|
|
|
if ( !$xmlObject->wellFormed ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
wfDebugLog( "Math", "XML validation error:\n " . var_export( $XML, true ) . "\n" );
|
|
|
|
} else {
|
2013-06-07 23:43:45 +00:00
|
|
|
$name = $xmlObject->getRootElement();
|
2014-04-08 10:39:12 +00:00
|
|
|
$elementSplit = explode( ':', $name );
|
|
|
|
if ( is_array($elementSplit) ){
|
|
|
|
$localName = end( $elementSplit );
|
|
|
|
} else {
|
|
|
|
$localName = $name;
|
|
|
|
}
|
|
|
|
if ( in_array( $localName , $this->getAllowedRootElements() ) ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
$out = true;
|
|
|
|
} else {
|
2014-04-08 10:39:12 +00:00
|
|
|
wfDebugLog( "Math", "got wrong root element : $name" );
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal version of @link self::embedMathML
|
|
|
|
* @return string
|
|
|
|
* @return html element with rendered math
|
|
|
|
*/
|
|
|
|
private function getMathMLTag() {
|
|
|
|
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-05-05 16:13:43 +00:00
|
|
|
}
|