diff --git a/i18n/en.json b/i18n/en.json index a04be6783..88d0b5eb7 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -91,6 +91,8 @@ "math_tip": "Mathematical formula (LaTeX)", "math-tracking-category-error": "Pages with math errors", "math-tracking-category-error-desc": "Pages in this category have errors in the usage of math tags.", + "math-tracking-category-mhchem-deprecation": "Pages that use a deprecated format of the chem tags", + "math-tracking-category-mhchem-deprecation-desc": "Pages in this category use a deprecated format of the chem tags", "math-tracking-category-render-error": "Pages with math render errors", "math-tracking-category-render-error-desc": "Pages in this category have rendering errors in the math tags.", "math_unknown_error": "unknown error", diff --git a/i18n/qqq.json b/i18n/qqq.json index 79fe233a9..8b2bc04ad 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -91,6 +91,8 @@ "math_tip": "This is the text that appears when you hover the mouse over the fourth button from the right on the edit toolbar.", "math-tracking-category-error": "Tracking category name.", "math-tracking-category-error-desc": "Tracking category description.", + "math-tracking-category-mhchem-deprecation": "Tracking category name.", + "math-tracking-category-mhchem-deprecation-desc": "Tracking category description.", "math-tracking-category-render-error": "Tracking category name.", "math-tracking-category-render-error-desc": "Tracking category description.", "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}}", diff --git a/src/MathHooks.php b/src/MathHooks.php index e5c5fdafe..ca0f4a092 100644 --- a/src/MathHooks.php +++ b/src/MathHooks.php @@ -224,22 +224,21 @@ class MathHooks { $checkResult = $renderer->checkTeX(); if ( $checkResult !== true ) { - // Returns the error message and add tracking category - $parser->addTrackingCategory( 'math-tracking-category-error' ); + $renderer->addTrackingCategories( $parser ); return $renderer->getLastError(); } if ( $renderer->render() ) { LoggerFactory::getInstance( 'Math' )->debug( "Rendering successful. Writing output" ); $renderedMath = $renderer->getHtmlOutput(); + $renderer->addTrackingCategories( $parser ); } else { LoggerFactory::getInstance( 'Math' )->warning( "Rendering failed. Printing error message." ); // Set a short parser cache time (10 minutes) after encountering // render issues, but not syntax issues. $parser->getOutput()->updateCacheExpiry( 600 ); - // Add a tracking category specialized on render errors. - $parser->addTrackingCategory( 'math-tracking-category-render-error' ); + $renderer->addTrackingCategories( $parser ); return $renderer->getLastError(); } Hooks::run( 'MathFormulaPostRender', diff --git a/src/MathMathML.php b/src/MathMathML.php index 904c1640d..9a7bd3187 100644 --- a/src/MathMathML.php +++ b/src/MathMathML.php @@ -52,6 +52,20 @@ class MathMathML extends MathRenderer { } } + /** + * @inheritDoc + */ + public function addTrackingCategories( $parser ) { + parent::addTrackingCategories( $parser ); + if ( $this->hasWarnings() ) { + foreach ( $this->warnings as $warning ) { + if ( isset( $warning->type ) && $warning->type === 'mhchem-deprecation' ) { + $parser->addTrackingCategory( 'math-tracking-category-mhchem-deprecation' ); + } + } + } + } + public static function batchEvaluate( array $tags ) { $rbis = []; foreach ( $tags as $key => $tag ) { @@ -120,6 +134,7 @@ class MathMathML extends MathRenderer { $this->mathoidStyle = $rbi->getMathoidStyle(); $this->svgPath = $rbi->getFullSvgUrl(); $this->pngPath = $rbi->getFullPngUrl(); + $this->warnings = $rbi->getWarnings(); } elseif ( $this->lastError === '' ) { $this->doCheck(); } diff --git a/src/MathRenderer.php b/src/MathRenderer.php index e40bfe7ea..60686cb5a 100644 --- a/src/MathRenderer.php +++ b/src/MathRenderer.php @@ -62,6 +62,8 @@ abstract class MathRenderer { protected $inputType = 'tex'; /** @var MathRestbaseInterface used for checking */ protected $rbi; + /** @var array with rendering warnings*/ + protected $warnings; /** * Constructs a base MathRenderer @@ -394,6 +396,29 @@ abstract class MathRenderer { $this->rbi->setPurge( $this->isPurge() ); } + public function hasWarnings() { + if ( is_array( $this->warnings ) && count( $this->warnings ) ) { + return true; + } + + return false; + } + + /** + * Adds tracking categories to the parser + * + * @param Parser $parser + */ + public function addTrackingCategories( $parser ) { + if ( !$this->checkTeX() ) { + $parser->addTrackingCategory( 'math-tracking-category-error' ); + } + if ( $this->lastError ) { + // Add a tracking category specialized on render errors. + $parser->addTrackingCategory( 'math-tracking-category-render-error' ); + } + } + /** * Returns sanitized attributes * @@ -593,6 +618,7 @@ abstract class MathRenderer { } else { if ( self::getDisableTexFilter() == 'new' && $this->mode != 'source' ) { if ( $this->readFromDatabase() ) { + $this->texSecure = true; return true; } } diff --git a/src/MathRestbaseInterface.php b/src/MathRestbaseInterface.php index 0ce23be84..e37cd71a0 100644 --- a/src/MathRestbaseInterface.php +++ b/src/MathRestbaseInterface.php @@ -18,6 +18,7 @@ class MathRestbaseInterface { private $error; private $mathoidStyle; private $mml; + private $warnings = []; /** @var bool is there a request to purge the existing mathematical content */ private $purge = false; @@ -352,6 +353,13 @@ class MathRestbaseInterface { $this->error = (object)[ 'error' => (object)[ 'message' => $msg ] ]; } + /** + * @return array + */ + public function getWarnings() { + return $this->warnings; + } + /** * @return array * @throws MWException @@ -380,6 +388,9 @@ class MathRestbaseInterface { $this->success = $json->success; $this->checkedTex = $json->checked; $this->identifiers = $json->identifiers; + if ( isset( $json->warnings ) ) { + $this->warnings = $json->warnings; + } return true; } else { if ( isset( $json->detail ) && isset( $json->detail->success ) ) { diff --git a/tests/phpunit/MathMathMLTest.php b/tests/phpunit/MathMathMLTest.php index 0ec6c2862..b39f79e76 100644 --- a/tests/phpunit/MathMathMLTest.php +++ b/tests/phpunit/MathMathMLTest.php @@ -238,6 +238,28 @@ class MathMathMLTest extends MediaWikiTestCase { $host3 = $method->invoke( $m2, [] ); $this->assertEquals( $h2, $host3 ); } + + public function testWarning() { + $this->setMwGlobals( "wgMathDisableTexFilter", 'always' ); + $renderer = new MathMathML(); + $rbi = $this->getMockBuilder( MathRestbaseInterface::class ) + ->setMethods( [ 'getWarnings', 'getSuccess' ] ) + ->setConstructorArgs( [ 'a+b' ] ) + ->getMock(); + $rbi->method( 'getWarnings' )->willReturn( [ (object)[ 'type' => 'mhchem-deprecation' ] ] ); + $rbi->method( 'getSuccess' )->willReturn( true ); + $renderer->setRestbaseInterface( $rbi ); + $renderer->render(); + $parser = $this->getMockBuilder( Parser::class ) + ->setMethods( [ 'addTrackingCategory' ] ) + ->disableOriginalConstructor() + ->getMock(); + $parser->method( 'addTrackingCategory' )->willReturn( true ); + $parser->expects( $this->once() ) + ->method( 'addTrackingCategory' ) + ->with( 'math-tracking-category-mhchem-deprecation' ); + $renderer->addTrackingCategories( $parser ); + } } /** diff --git a/tests/phpunit/MathRendererTest.php b/tests/phpunit/MathRendererTest.php index 2416e656c..91d1ce54e 100644 --- a/tests/phpunit/MathRendererTest.php +++ b/tests/phpunit/MathRendererTest.php @@ -150,7 +150,7 @@ class MathRendererTest extends MediaWikiTestCase { 'readFromDatabase', 'setTex' ] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock(); - $renderer->expects( $this->exactly( 2 ) )->method( 'readFromDatabase' ) + $renderer->expects( $this->exactly( 1 ) )->method( 'readFromDatabase' ) ->will( $this->returnValue( true ) ); $renderer->expects( $this->never() )->method( 'setTex' );