Physikerwelt has uploaded a new change for review.
https://gerrit.wikimedia.org/r/62219
Change subject: manual merge
......................................................................
manual merge
Change-Id: I26d43ff4ce72d404e1c88f8ecc57f07c0d383f3f
---
M Math.hooks.php
M Math.i18n.php
M Math.php
A MathLaTeXML.php
M MathRenderer.php
5 files changed, 268 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math
refs/changes/19/62219/1
diff --git a/Math.hooks.php b/Math.hooks.php
index a1d9fea..3489da5 100644
--- a/Math.hooks.php
+++ b/Math.hooks.php
@@ -81,7 +81,7 @@
* @return array of strings
*/
private static function getMathNames() {
- global $wgUseMathJax;
+ global $wgUseMathJax, $wgUseLaTeXML;
$names = array(
MW_MATH_PNG => wfMessage( 'mw_math_png' )->escaped(),
MW_MATH_SOURCE => wfMessage( 'mw_math_source'
)->escaped(),
@@ -89,7 +89,9 @@
if ( $wgUseMathJax ) {
$names[MW_MATH_MATHJAX] = wfMessage( 'mw_math_mathjax'
)->escaped();
}
-
+ if ( $wgUseLaTeXML ) {
+ $names[MW_MATH_LATEXML] = wfMessage( 'mw_math_latexml'
)->escaped();
+ }
return $names;
}
diff --git a/Math.i18n.php b/Math.i18n.php
index 1a09e63..b32c73c 100644
--- a/Math.i18n.php
+++ b/Math.i18n.php
@@ -22,6 +22,7 @@
'mw_math_png' => 'Always render PNG',
'mw_math_source' => 'Leave it as TeX (for text browsers)',
'mw_math_mathjax' => 'MathJax (experimental; best for most browsers)',
+ 'mw_math_latexml' => 'LaTeXML (experimental; uses MathML)',
// Math errors
'math_failure' => 'Failed to parse',
diff --git a/Math.php b/Math.php
index 83bd58a..52a81ca 100644
--- a/Math.php
+++ b/Math.php
@@ -22,7 +22,7 @@
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'Math',
- 'version' => '1.0',
+ 'version' => '1.1',
'author' => array( 'Tomasz Wegrzanowski', 'Brion Vibber', '...' ),
'descriptionmsg' => 'math-desc',
'url' => 'https://www.mediawiki.org/wiki/Extension:Math',
@@ -38,6 +38,7 @@
define( 'MW_MATH_MODERN', 4 ); /// @deprecated
define( 'MW_MATH_MATHML', 5 ); /// @deprecated
define( 'MW_MATH_MATHJAX', 6 ); /// new in 1.19/1.20
+define( 'MW_MATH_LATEXML', 7 ); /// new in 1.21
/**@}*/
/** For back-compat */
@@ -101,6 +102,25 @@
*/
$wgUseMathJax = false;
+/**
+ * Use of LaTeXML for details see
+ * <http://latexml.mathweb.org/help>
+ *
+ * If you don't like them, install your own server see
+ * <https://svn.mathweb.org/repos/LaTeXML/branches/arXMLiv/INSTALL>
+ */
+$wgLaTeXMLUrl = 'http://latexml.mathweb.org/convert';
+/**
+ * Allows to use LaTeXML as renderer for mathematical equation, if used in
+ * combination with $wgUseMathJax the MathML elements are rendered in browser
+ * by MathJaX.
+ */
+$wgUseLaTeXML = true;
+/**
+ * The timeout for the LaTeXML server to render an equaiton.
+ */
+$wgLaTeXMLTimout=240;
+
////////// end of config settings.
$wgDefaultUserOptions['math'] = MW_MATH_PNG;
@@ -119,6 +139,7 @@
$wgAutoloadClasses['MathTexvc'] = $dir . 'MathTexvc.php';
$wgAutoloadClasses['MathSource'] = $dir . 'MathSource.php';
$wgAutoloadClasses['MathMathJax'] = $dir . 'MathMathJax.php';
+$wgAutoloadClasses['MathLaTeXML'] = $dir . 'MathLaTeXML.php';
$wgExtensionMessagesFiles['Math'] = $dir . 'Math.i18n.php';
$wgParserTestFiles[] = $dir . 'mathParserTests.txt';
diff --git a/MathLaTeXML.php b/MathLaTeXML.php
new file mode 100644
index 0000000..64e959a
--- /dev/null
+++ b/MathLaTeXML.php
@@ -0,0 +1,236 @@
+<?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 {
+
+ private $LaTeXMLSettings='';
+
+ /**
+ * Gets the setting for the LaTeXML deamon.
+ *
+ * @return string
+ */
+ public function getLaTeXMLSettings(){
+ if($this->LaTeXMLSettings){
+ return $this->LaTeXMLSettings;
+ } else {
+ return 'format=xhtml&'.
+ 'whatsin=math&'.
+ 'whatsout=math&'.
+ 'pmml&'.
+ 'cmml&'.
+ 'preload=LaTeX.pool&'.
+ 'preload=article.cls&'.
+ 'preload=amsmath&'.
+ 'preload=amsthm&'.
+ 'preload=amstext&'.
+ 'preload=amssymb&'.
+ 'preload=eucal&'.
+ 'preload=[dvipsnames]xcolor&'.
+ 'preload=url&'.
+ 'preload=hyperref&'.
+ 'preload=mws&'.
+ 'preload=ids&'.
+ 'preload=texvc';
+ }
+ }
+
+ /**
+ * Sets the setting for the LaTeXML deamon.
+ * 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
+ * @param string $settings
+ */
+ public function setLaTeXMLSettings($settings){
+ $this->LaTeXMLSettings=$settings;
+ }
+ /* (non-PHPdoc)
+ * @see MathRenderer::render()
+ */
+ function render($purge=false) {
+ $recall =false;
+ if ( !$purge&& !$this->isPurge()){
+ $dbres=$this->readFromDatabase();
+ if ($dbres) {
+ if (self::isValidMathML($this->mathml)){
+ $recall=true;
+ }
+ }
+ }
+ if (!$recall) {
+ wfDebugLog( "Math", "no recall" );
+ $this->recall=false;
+ $success=$this->dorender();
+ }
+ return $this->getEncodedMathML();
+ }
+
+ /* (non-PHPdoc)
+ * @see MathRenderer::writeCache()
+ */
+ function writeCache() {
+ $this->writeToDatabase();
+ }
+
+ /**
+ * Picks a LaTeXML daemon.
+ * If more than one demon are availible one is chosen from the
+ * $wgLaTeXMLUrl array.
+ * @return string
+ */
+ private static function pickHost() {
+ global $wgLaTeXMLUrl;
+ if ( is_array( $wgLaTeXMLUrl ) ) {
+ $pick = mt_rand( 0, count( $wgLaTeXMLUrl ) - 1 );
+ $host = $wgLaTeXMLUrl[$pick];
+ } else {
+ $host = $wgLaTeXMLUrl;
+ }
+ wfDebugLog( "Math", "picking host " . $host );
+ return $host;
+ }
+ /**
+ * Helper function to repair bugs on the fly.
+ * @param string $bad
+ * @param string $correct
+ * @param string $input
+ * @return string
+ */
+ private static function generalize($bad,$correct,$input){
+ return
str_replace($bad,$correct,str_replace($correct,$bad,$input));
+ }
+
+ /**
+ * @return boolean
+ */
+ private function dorender() {
+ global $wgLaTeXMLTimeout;
+ $host = self::pickHost();
+ $texcmd = urlencode( $this->tex );
+ $post= $this->getLaTeXMLSettings();
+ $post .='&tex='.$texcmd;
+ $time_start = microtime( true );
+ $res = Http::post( $host, array( "postData" => $post, "timeout"
=> $wgLaTeXMLTimeout) );
+ $time_end = microtime( true );
+ $time = $time_end - $time_start;
+ if($time>$wgLaTeXMLTimeout){
+ $this->mathml = "[ERROR (timeout)]";
+ wfDebugLog( "Math", "\nLaTeXML Timeout:" . var_export(
array( $wgLaTeXMLTimeout, $post, $host ), true ) . "\n\n" );
+ return false;
+ }
+ $result = json_decode( $res );
+ if ( $result ) {
+ if ( ( strpos( $result->result, '<?xml version="1.0"
encoding="utf-8"?>' ) === 0 ) )
+ {
+ wfDebugLog( "Math", "ERROR: Result is invalid "
. $result->result );
+ return false;
+ }
+ $this->mathml = $result->result;
+ if(!self::isValidMathML($this->mathml)){
+ $this->setMathml( "[ERROR
(invalid)]".$this->mathml);
+ wfDebugLog( "Math", "\nLaTeXML Error:" .
var_export( array( $result, $post, $host ), true ) . "\n\n" );
+ }
+ }
+ else {
+ if($res==false){
+ wfDebugLog( "Math", "\nLaTeXML Error: no
response from $host \n" );
+ } else {
+ wfDebugLog( "Math", "\nLaTeXML Error:" .
var_export( array( $result, $post, $host ), true ) . "\n" );
+ }
+ return false;
+ }
+ wfDebugLog( "Math", "Latexml request: $post\n processed in
$time seconds." );
+ return true;
+ }
+ /**
+ * Checks if there is an explicit user request to rerender the math-tag.
+ * @return boolean
+ */
+ function isPurge() {
+ // TODO: Figure out if ?action=purge
+ // until this issue is resolved we use ?mathpurge=true instead
+ global $wgRequest;
+ return ( $wgRequest->getVal( 'mathpurge' ) === "true" );
+ }
+ /**
+ * Checks if the input is valid MathML,
+ * and if the root element has the name math
+ * @param string $XML
+ * @return boolean
+ */
+ static public function isValidMathML($XML){
+ //TODO: Check: Is simpleXML core php?
+ // Is libxml_use_internal_error permanent (side effects
with other methods)?
+ libxml_use_internal_errors( true );
+ $xml = simplexml_load_string( $XML );
+ if ( !$xml ) {
+ wfDebugLog("Math", "XML validation error:\n " .
var_export( $XML, true ) . "\n");
+ foreach ( libxml_get_errors() as $error ){
+ wfDebugLog("Math", "\t". $error->message);
+ }
+ libxml_clear_errors();
+ return false;
+ } else {
+ $name= $xml->getName();
+ if ( $name=="math" or $name=="table" or $name=="div" ){
+ return true;
+ } else {
+ wfDebugLog("Math", "got wrong root element"
.$name);
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Internal version of @link self::embedMathML
+ * @return string
+ * @return html element with rendered math
+ */
+ private function getEncodedMathML() {
+ return self::embedMathML($this->mathml,urldecode($this->tex));
+ }
+
+ /**
+ * Embedds the MathML-XML element in a HTML span element with class tex
+ * @param string $mml: the MathML string
+ * @param string $tagId: optional tagID for references like
(pagename#equation2)
+ * @return html element with rendered math
+ */
+ public static function embedMathML($mml,$tagId=''){
+ $mml = str_replace( "\n", " ", $mml );
+ return Xml::tags( 'span',
+ self::attribs( 'span',
+ array(
+ 'class' =>
'tex',
+ 'dir' => 'ltr',
+ 'id' => $tagId
+ )
+ ),
+ $mml
+ );
+ }
+
+ /**
+ * Generic function for handling element attributes
+ *
+ * @param string $tag
+ * @param array $defaults
+ * @param array $overrides
+ * @return array
+ */
+ protected static function attribs( $tag, $defaults = array(),
$overrides = array() ) {
+ $attribs = Sanitizer::validateTagAttributes( array(), $tag );
+ $attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
+ $attribs = Sanitizer::mergeAttributes( $attribs, $overrides );
+ return $attribs;
+ }
+}
\ No newline at end of file
diff --git a/MathRenderer.php b/MathRenderer.php
index 95a2089..be8a8ff 100644
--- a/MathRenderer.php
+++ b/MathRenderer.php
@@ -73,7 +73,7 @@
*/
public static function getRenderer( $tex, $params = array(), $mode =
MW_MATH_PNG ) {
global $wgDefaultUserOptions;
- $validModes = array( MW_MATH_PNG, MW_MATH_SOURCE,
MW_MATH_MATHJAX );
+ $validModes = array( MW_MATH_PNG, MW_MATH_SOURCE,
MW_MATH_MATHJAX, MW_MATH_LATEXML );
if ( !in_array( $mode, $validModes ) )
$mode = $wgDefaultUserOptions['math'];
switch ( $mode ) {
@@ -83,11 +83,14 @@
case MW_MATH_MATHJAX:
$renderer = new MathMathJax( $tex, $params );
break;
+ case MW_MATH_LATEXML:
+ $renderer = new MathLaTeXML( $tex, $params );
+ break;
case MW_MATH_PNG:
default:
$renderer = new MathTexvc( $tex, $params );
}
- wfDebugLog ( "Math", 'start rendering $' . $renderer->tex . '$'
);
+ wfDebugLog ( "Math", 'start rendering $' . $renderer->tex . '$
in mode '.$mode );
return $renderer;
}
--
To view, visit https://gerrit.wikimedia.org/r/62219
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I26d43ff4ce72d404e1c88f8ecc57f07c0d383f3f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits