From 4ca287b43ad73b84b0616a7a63d3e4e683aab6a4 Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Wed, 28 May 2014 18:12:39 +0000 Subject: [PATCH] Improve MathML error handling This change improves the error handling for MathML. * Make MathHooks::getMathNames public * Allow to use the same error messages for LaTeXML and Mathoid * unify the string delimiter in error messages to " Change-Id: I6568e6a3c98cb008f45f887d42e446daafe0ed3c --- Math.hooks.php | 2 +- MathLaTeXML.php | 31 +++++++++++++++++++++---------- i18n/en.json | 12 ++++++------ i18n/qqq.json | 12 ++++++------ 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Math.hooks.php b/Math.hooks.php index 51ea2f7..f265abe 100644 --- a/Math.hooks.php +++ b/Math.hooks.php @@ -170,7 +170,7 @@ class MathHooks { * * @return array of strings */ - private static function getMathNames() { + public static function getMathNames() { global $wgMathValidModes; $MathConstantNames = array( MW_MATH_SOURCE => 'mw_math_source', diff --git a/MathLaTeXML.php b/MathLaTeXML.php index f18a9d3..9fcc111 100644 --- a/MathLaTeXML.php +++ b/MathLaTeXML.php @@ -230,8 +230,15 @@ class MathLaTeXML extends MathRenderer { * Does the actual web request to convert TeX to MathML. * @return boolean */ - private function doRender( ) { + protected function doRender() { + global $wgMathDebug; wfProfileIn( __METHOD__ ); + 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 = ''; $host = self::pickHost(); $post = $this->getPostData(); // There is an API-inconsistency between different versions of the LaTeXML daemon @@ -240,25 +247,29 @@ class MathLaTeXML extends MathRenderer { $post = preg_replace( '/&tex=/' , '&tex=literal:', $post , 1); } $this->lastError = ''; - if ( $this->makeRequest( $host, $post, $res, $this->lastError ) ) { - $result = json_decode( $res ); - if ( json_last_error() === JSON_ERROR_NONE ) { - if ( $this->isValidMathML( $result->result ) ) { - $this->setMathml( $result->result ); - wfProfileOut( __METHOD__ ); + $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 ); + } 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 ); + $this->lastError = $this->getError( 'math_invalidxml', $this->getModeStr(), $host ); wfDebugLog( "Math", "\nLaTeXML InvalidMathML:" . var_export( array( 'post' => $post, 'host' => $host - , 'result' => $result ), true ) . "\n\n" ); + , 'result' => $res ), true ) . "\n\n" ); wfProfileOut( __METHOD__ ); return false; } } else { - $this->lastError = $this->getError( 'math_latexml_invalidjson', $host ); + $this->lastError = $this->getError( 'math_invalidjson', $this->getModeStr(), $host ); wfDebugLog( "Math", "\nLaTeXML InvalidJSON:" . var_export( array( 'post' => $post, 'host' => $host , 'res' => $res ), true ) . "\n\n" ); diff --git a/i18n/en.json b/i18n/en.json index 20116ec..8cfe907 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -12,7 +12,7 @@ "mw_math_latexml": "LaTeXML (experimental; uses MathML)", "math_failure": "Failed to parse", "math_unknown_error": "unknown error", - "math_unknown_function": "unknown function '$1'", + "math_unknown_function": "unknown function \"$1\"", "math_lexing_error": "lexing error", "math_syntax_error": "syntax error", "math_image_error": "PNG conversion failed; check for correct installation of latex and dvipng (or dvips + gs + convert)", @@ -21,11 +21,11 @@ "math_notexvc": "Missing texvc executable. Please see math/README to configure.", "math_notexvccheck": "Missing texvccheck executable. Please see math/README to configure.", "math_output_error": "Cannot store math image on filesystem.", - "math_latexml_timeout": "LaTeXML timeout from '$1'", - "math_latexml_invalidresponse": "LaTeXML Invalid response ('$2') from server '$1':", - "math_latexml_invalidxml": "LaTeXML MathML is invalid XML.", - "math_latexml_invalidjson": "LaTeXML Server response is invalid JSON.", - "math_latexml_xmlversion": "Warning: XML type check skipped! Check if your MediaWiki installation is version wmf/1.22wmf7 or newer.", + "math_timeout": "$1 timeout from \"$2\"", + "math_invalidresponse": "$1: Invalid response (\"$3\") from server \"$2\":", + "math_invalidxml": "MathML is invalid XML.", + "math_invalidjson": "$1 server response is invalid JSON.", + "math_xmlversion": "Warning: XML type check skipped! Check if your MediaWiki installation is version wmf/1.22wmf7 or newer.", "math-visualeditor-mwmathinspector-title": "Formula", "math-preference-mwmathinspector-description": "Add experimental support to VisualEditor for creating and editing of mathematical formulae for testing, ahead of general release. Please remember to always review your changes before saving when using experimental features.", "math-preference-mwmathinspector-discussion-link": "https://www.mediawiki.org/wiki/Special:MyLanguage/Talk:VisualEditor/Beta_Features/Formulae", diff --git a/i18n/qqq.json b/i18n/qqq.json index 61e8bea..5dc2e24 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -18,7 +18,7 @@ "mw_math_source": "In user preferences (math). All mw_math_* messages MUST be different, things will break otherwise!\n\nUsed as label for source radio button.\n\nSee also:\n* {{msg-mw|Mw math png}}\n* {{msg-mw|Mw math mathjax}}\n* {{msg-mw|Mw math latexml}}", "mw_math_mathjax": "Used as label for mathjax radio button.\n\nSee also:\n* {{msg-mw|Mw math png}}\n* {{msg-mw|Mw math source}}\n* {{msg-mw|Mw math latexml}}", "mw_math_latexml": "Used as label for latexml radio button.\n\nSee also:\n* {{msg-mw|Mw math png}}\n* {{msg-mw|Mw math source}}\n* {{msg-mw|Mw math mathjax}}", - "math_failure": "Used as error message.\n\nThis message is followed by \"(\", Error message(*1), Additional message, \"): \" and Source code.\n\n(*1) The error message is any one of the following messages:\n* {{msg-mw|Math unknown error}}\n* {{msg-mw|Math unknown function}}\n* {{msg-mw|Math lexing error}}\n* {{msg-mw|Math syntax error}}\n* {{msg-mw|Math image error}}\n* {{msg-mw|Math bad tmpdir}}\n* {{msg-mw|Math bad output}}\n* {{msg-mw|Math notexvc}}\n* {{msg-mw|Math notexvccheck}}\n* {{msg-mw|Math output error}}\n* {{msg-mw|Math latexml timeout}}\n* {{msg-mw|Math latexml invalidresponse}}\n* {{msg-mw|Math latexml invalidxml}}\n* {{msg-mw|Math latexml invalidjson}}", + "math_failure": "Used as error message.\n\nThis message is followed by \"(\", Error message(*1), Additional message, \"): \" and Source code.\n\n(*1) The error message is any one of the following messages:\n* {{msg-mw|Math unknown error}}\n* {{msg-mw|Math unknown function}}\n* {{msg-mw|Math lexing error}}\n* {{msg-mw|Math syntax error}}\n* {{msg-mw|Math image error}}\n* {{msg-mw|Math bad tmpdir}}\n* {{msg-mw|Math bad output}}\n* {{msg-mw|Math notexvc}}\n* {{msg-mw|Math notexvccheck}}\n* {{msg-mw|Math output error}}\n* {{msg-mw|Math timeout}}\n* {{msg-mw|Math invalidresponse}}\n* {{msg-mw|Math invalidxml}}\n* {{msg-mw|Math invalidjson}}", "math_unknown_error": "Used as error message for unknown texvc error.\n\nThis message follows the message {{msg-mw|Math failure}}.\n{{Identical|Unknown error}}", "math_unknown_function": "Used as error message when texvc encounters an unknown function.\n\nPreceded by the message {{msg-mw|Math failure}}.\n\nParameters:\n* $1 - name of unknown function", "math_lexing_error": "Used as error message for a texvc lexing error.\n\nThis message follows the message {{msg-mw|Math failure}}.", @@ -29,11 +29,11 @@ "math_notexvc": "Used as error message.\n\nThis message follows the message {{msg-mw|Math failure}}.\n\nSee also:\n* {{msg-mw|Math notexvccheck}}", "math_notexvccheck": "Used as error message.\n\nThis message follows the message {{msg-mw|Math failure}}.\n\nSee also:\n* {{msg-mw|Math notexvc}}", "math_output_error": "Used as error message if the texvc output file could not be stored.\n\nThis message follows the message {{msg-mw|Math failure}}.", - "math_latexml_timeout": "Used as error message.\n\nParameters:\n* $1 - hostname or URL", - "math_latexml_invalidresponse": "Used as error message.\n\nFollows the message {{msg-mw|Math failure}}.\n\nParameters:\n* $1 - hostname\n* $2 - error message", - "math_latexml_invalidxml": "Used as error message.\n\nThis message follows the message {{msg-mw|Math failure}}.", - "math_latexml_invalidjson": "Used as error message.\n\nThis message follows the message {{msg-mw|Math failure}}.", - "math_latexml_xmlversion": "Warning that XML checking of MathML requires wmf/1.22wmf7 or newer.", + "math_timeout": "Used as error message.\n\nParameters:\n* $1 - rendering type (either LaTeXML or Mathoid)\n* $2 - hostname or URL", + "math_invalidresponse": "Used as error message.\n\nFollows the message {{msg-mw|Math failure}}.\n\nParameters:\n* $1 - rendering type (either LaTeXML or Mathoid)\n* $2 - hostname\n* $3 - error message", + "math_invalidxml": "Used as error message.\n\nThis message follows the message {{msg-mw|Math failure}}.", + "math_invalidjson": "Used as error message.\n\nParameters:\n* $1 - rendering type (either LaTeXML or Mathoid)\\n\nThis message follows the message {{msg-mw|Math failure}}.", + "math_xmlversion": "Warning that XML checking of MathML requires wmf/1.22wmf7 or newer.", "math-visualeditor-mwmathinspector-title": "Title for the inspector to edit formula blocks.\n{{Identical|Formula}}", "math-preference-mwmathinspector-description": "Used in [[Special:Preferences]].\n\nUsed as description for the checkbox to enable editing of mathematical formulae in VisualEditor.\n\nThe label for this checkbox is {{msg-mw|Math-preference-mwmathinspector-label}}.", "math-preference-mwmathinspector-discussion-link": "{{optional|Used on [[Special:Preferences]] as a link to a page where users can discuss this Beta Feature. Defaults to a page on MediaWiki.org.}}",