Merge "Add tracking category for deprecated mhchem syntax"

This commit is contained in:
jenkins-bot 2018-11-09 12:19:12 +00:00 committed by Gerrit Code Review
commit 817ada2cf1
8 changed files with 82 additions and 5 deletions

View File

@ -89,6 +89,8 @@
"math-test-equals-diff" : "Returned string $1 is different from expected string $2.",
"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",

View File

@ -89,6 +89,8 @@
"math-test-equals-diff": "Used as special-page wikitext.\n\nParameters:\n* $1 syntax highlight element of the returned string \n* $2 syntax highlight element of the expected string",
"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}}",

View File

@ -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',

View File

@ -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();
}

View File

@ -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
@ -395,6 +397,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
*
@ -594,6 +619,7 @@ abstract class MathRenderer {
} else {
if ( self::getDisableTexFilter() == 'new' && $this->mode != 'source' ) {
if ( $this->readFromDatabase() ) {
$this->texSecure = true;
return true;
}
}

View File

@ -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 ) ) {

View File

@ -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 );
}
}
/**

View File

@ -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' );