Mattflaschen has uploaded a new change for review.
https://gerrit.wikimedia.org/r/64263
Change subject: Use i18n params for errors than appending raw string, with test
......................................................................
Use i18n params for errors than appending raw string, with test
Change-Id: Iea5139682fbe8389e578549f5f62e5505f4c0b48
---
M Math.i18n.php
M MathRenderer.php
M MathTexvc.php
M tests/MathTexvcTest.php
4 files changed, 80 insertions(+), 22 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math
refs/changes/63/64263/1
diff --git a/Math.i18n.php b/Math.i18n.php
index c5ce97e..251d89a 100644
--- a/Math.i18n.php
+++ b/Math.i18n.php
@@ -25,10 +25,10 @@
// Math errors
'math_failure' => 'Failed to parse',
- 'math_unknown_error' => 'unknown error',
- 'math_unknown_function' => 'unknown function',
- 'math_lexing_error' => 'lexing error',
- 'math_syntax_error' => 'syntax error',
+ 'math_unknown_error' => 'unknown error $1',
+ 'math_unknown_function' => 'unknown function \'$1\'',
+ 'math_lexing_error' => 'lexing error $1',
+ 'math_syntax_error' => 'syntax error $1',
'math_image_error' => 'PNG conversion failed; check for correct
installation of latex and dvipng (or dvips + gs + convert)',
'math_bad_tmpdir' => 'Cannot write to or create math temp directory',
'math_bad_output' => 'Cannot write to or create math output directory',
@@ -81,16 +81,24 @@
* {{msg-mw|Math output error}}',
'math_unknown_error' => 'Used as error message.
+$1 - Further error details from texvc (if any)
+
This message follows the message {{msg-mw|Math failure}}.
{{Identical|Unknown error}}',
'math_unknown_function' => 'Used as error message.
+$1 - Name of unknown function
+
This message follows the message {{msg-mw|Math failure}}.',
'math_lexing_error' => 'Used as error message.
+$1 - Further error details from texvc (if any)
+
This message follows the message {{msg-mw|Math failure}}.',
'math_syntax_error' => 'Used as error message.
+$1 - Further error details from texvc (if any)
+
This message follows the message {{msg-mw|Math failure}}.
{{Identical|Syntax error}}',
'math_image_error' => '{{doc-important|Do not change
<code>latex</code>, <code>dvipng</code>, <code>dvips</code>, <code>gs</code>
and <code>convert</code>. These are UNIX commands.}}
diff --git a/MathRenderer.php b/MathRenderer.php
index 95a2089..868a4dd 100644
--- a/MathRenderer.php
+++ b/MathRenderer.php
@@ -105,14 +105,16 @@
* Returns an internationalized HTML error string
*
* @param string $msg message key for specific error
- * @param string $append string to append after error
+ * @param Varargs $parameters (optional) zero or more message
parameters for specific error
* @return string HTML error string
*/
- protected function getError( $msg, $append = '' ) {
+ protected function getError( $msg /*, ... */ ) {
$mf = wfMessage( 'math_failure'
)->inContentLanguage()->escaped();
- $errmsg = wfMessage( $msg )->inContentLanguage()->escaped();
+ $parameters = func_get_args();
+ array_shift( $parameters );
+ $errmsg = wfMessage( $msg, $parameters
)->inContentLanguage()->escaped();
$source = htmlspecialchars( str_replace( "\n", ' ', $this->tex
) );
- return "<strong class='error'>$mf ($errmsg$append):
$source</strong>\n";
+ return "<strong class='error'>$mf ($errmsg):
$source</strong>\n";
}
/**
diff --git a/MathTexvc.php b/MathTexvc.php
index 275ed7d..f193a44 100644
--- a/MathTexvc.php
+++ b/MathTexvc.php
@@ -95,6 +95,32 @@
}
/**
+ * Converts an error returned by texvc to a localized exception
+ *
+ * @param string $texvcResult error result returned by texvc
+ */
+ public function convertTexvcError( $texvcResult ) {
+ $texvcStatus = substr( $texvcResult, 0, 1 );
+
+ $errDetails = htmlspecialchars( substr( $texvcResult, 1 ) );
+ switch( $texvcStatus ) {
+ case 'E':
+ $errMsg = $this->getError( 'math_lexing_error',
$errDetails );
+ break;
+ case 'S':
+ $errMsg = $this->getError( 'math_syntax_error',
$errDetails );
+ break;
+ case 'F':
+ $errMsg = $this->getError(
'math_unknown_function', $errDetails );
+ break;
+ default:
+ $errMsg = $this->getError(
'math_unknown_error', $errDetails );
+ }
+
+ return $errMsg;
+ }
+
+ /**
* Does the actual call to texvc
*
* @return int|string MW_TEXVC_SUCCESS or error string
@@ -169,20 +195,7 @@
$this->setMathml( null );
$this->setConservativeness( self::LIBERAL );
} else {
- $errbit = htmlspecialchars( substr( $contents, 1 ) );
- switch( $retval ) {
- case 'E':
- $errmsg = $this->getError(
'math_lexing_error', $errbit );
- break;
- case 'S':
- $errmsg = $this->getError(
'math_syntax_error', $errbit );
- break;
- case 'F':
- $errmsg = $this->getError(
'math_unknown_function', $errbit );
- break;
- default:
- $errmsg = $this->getError(
'math_unknown_error', $errbit );
- }
+ $errmsg = $this->convertTexvcError( $contents );
}
if ( !$errmsg ) {
diff --git a/tests/MathTexvcTest.php b/tests/MathTexvcTest.php
index b995786..cb66951 100644
--- a/tests/MathTexvcTest.php
+++ b/tests/MathTexvcTest.php
@@ -114,4 +114,39 @@
// ... it will return the error result instead:
$this->assertEquals( $texvc->render(), 'error' );
}
+
+ /**
+ * Tests behavior of convertTexvcError
+ *
+ * @covers MathTexvc::convertTexvcError
+ */
+ public function testConvertTexvcError() {
+ $texvc = $this->getMockBuilder( 'MathTexvc' )
+ ->setMethods(NULL)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mathFailure = wfMessage( 'math_failure'
)->inContentLanguage()->escaped();
+
+ $actualLexing = $texvc->convertTexvcError( 'E' );
+ $expectedLexing = wfMessage( 'math_lexing_error', ''
)->inContentLanguage()->escaped();
+ $this->assertContains( $mathFailure, $actualLexing, 'Lexing
error contains general math failure message' );
+ $this->assertContains( $expectedLexing, $actualLexing, 'Lexing
error contains detailed error for lexing' );
+
+ $actualSyntax = $texvc->convertTexvcError( 'S' );
+ $expectedSyntax = wfMessage( 'math_syntax_error', ''
)->inContentLanguage()->escaped();
+ $this->assertContains( $mathFailure, $actualSyntax, 'Syntax
error contains general math failure message' );
+ $this->assertContains( $expectedSyntax, $actualSyntax, 'Syntax
error contains detailed error for syntax' );
+
+ $unknownFunction = 'figureEightIntegral';
+ $actualUnknownFunction = $texvc->convertTexvcError(
"F$unknownFunction" );
+ $expectedUnknownFunction = wfMessage( 'math_unknown_function',
$unknownFunction )->inContentLanguage()->escaped();
+ $this->assertContains( $mathFailure, $actualUnknownFunction,
'Unknown function error contains general math failure message' );
+ $this->assertContains( $expectedUnknownFunction,
$actualUnknownFunction, 'Unknown function error contains detailed error for
unknown function' );
+
+ $actualUnknownError = $texvc->convertTexvcError( 'Q' );
+ $expectedUnknownError = wfMessage( 'math_unknown_error', ''
)->inContentLanguage()->escaped();
+ $this->assertContains( $mathFailure, $actualUnknownError,
'Unknown error contains general math failure message' );
+ $this->assertContains( $expectedUnknownError,
$actualUnknownError, 'Unknown error contains detailed error for unknownError' );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/64263
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iea5139682fbe8389e578549f5f62e5505f4c0b48
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits