mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-24 07:34:22 +00:00
Add tracking category for deprecated mhchem syntax
Gets warning information from restbase and adds tracking categories for deprecated chemical syntax. Refactor mechanism to add tracking categories, and avoiding rechecking already checked formulae. Depends on: Ieca66f45ae7685d61eece1624bd7ff65ccad2eaa Change-Id: I10e78ab79015dc1331f645c60b25bbbd237e23fe
This commit is contained in:
parent
42da15f63a
commit
a865a8565a
|
@ -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",
|
||||
|
|
|
@ -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}}",
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 ) ) {
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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' );
|
||||
|
||||
|
|
Loading…
Reference in a new issue