Physikerwelt has submitted this change and it was merged.

Change subject: Use i18n params for errors than appending raw string, with test
......................................................................


Use i18n params for errors than appending raw string, with test

* Remove parameters/append that are never outputted by texvc.ml
* Add missing math_output_error to i18n file
* Improve a few qqq descriptions

Change-Id: Iea5139682fbe8389e578549f5f62e5505f4c0b48
---
M Math.i18n.php
M MathRenderer.php
M MathTexvc.php
M tests/MathTexvcTest.php
4 files changed, 79 insertions(+), 23 deletions(-)

Approvals:
  Physikerwelt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Math.i18n.php b/Math.i18n.php
index c5ce97e..880699d 100644
--- a/Math.i18n.php
+++ b/Math.i18n.php
@@ -26,13 +26,14 @@
        // Math errors
        'math_failure' => 'Failed to parse',
        'math_unknown_error' => 'unknown error',
-       'math_unknown_function' => 'unknown function',
+       '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)',
        'math_bad_tmpdir' => 'Cannot write to or create math temp directory',
        'math_bad_output' => 'Cannot write to or create math output directory',
        'math_notexvc' => 'Missing texvc executable; please see math/README to 
configure.',
+       'math_output_error' => 'Cannot store math image on filesystem.',
 );
 
 /** Message documentation (Message documentation)
@@ -79,17 +80,19 @@
 * {{msg-mw|Math bad output}}
 * {{msg-mw|Math notexvc}}
 * {{msg-mw|Math output error}}',
-       'math_unknown_error' => 'Used as error message.
+       'math_unknown_error' => 'Used as error message for unknown texvc error.
 
 This message follows the message {{msg-mw|Math failure}}.
 {{Identical|Unknown error}}',
-       'math_unknown_function' => 'Used as error message.
+       'math_unknown_function' => 'Used as error message when texvc encounters 
an unknown function.
+
+$1 - Name of unknown function
 
 This message follows the message {{msg-mw|Math failure}}.',
-       'math_lexing_error' => 'Used as error message.
+       'math_lexing_error' => 'Used as error message for a texvc lexing error.
 
 This message follows the message {{msg-mw|Math failure}}.',
-       'math_syntax_error' => 'Used as error message.
+       'math_syntax_error' => 'Used as error message for a texvc syntax error.
 
 This message follows the message {{msg-mw|Math failure}}.
 {{Identical|Syntax error}}',
@@ -107,6 +110,9 @@
        'math_notexvc' => 'Used as error message.
 
 This message follows the message {{msg-mw|Math failure}}.',
+       'math_output_error' => 'Used as error message if the texvc output file 
could not be stored.
+
+This message follows the message {{msg-mw|Math failure}}.',
 );
 
 /** Achinese (Acèh)
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..493e83a 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' 
);
+                               break;
+                       case 'S':
+                               $errMsg = $this->getError( 'math_syntax_error' 
);
+                               break;
+                       case 'F':
+                               $errMsg = $this->getError( 
'math_unknown_function', $errDetails );
+                               break;
+                       default:
+                               $errMsg = $this->getError( 'math_unknown_error' 
);
+               }
+
+               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: merged
Gerrit-Change-Id: Iea5139682fbe8389e578549f5f62e5505f4c0b48
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Physikerwelt <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: Tim Landscheidt <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to