diff --git a/src/MathNativeMML.php b/src/MathNativeMML.php index 45be2e70f..817eb0a97 100644 --- a/src/MathNativeMML.php +++ b/src/MathNativeMML.php @@ -10,7 +10,10 @@ namespace MediaWiki\Extension\Math; use MediaWiki\Extension\Math\InputCheck\LocalChecker; use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmath; +use MediaWiki\MediaWikiServices; +use SpecialPage; use StatusValue; +use Title; /** * Converts LaTeX to MathML using PHP @@ -26,7 +29,22 @@ class MathNativeMML extends MathMathML { protected function doRender(): StatusValue { $presentation = $this->getChecker()->getParseTree()->renderMML(); - $root = new MMLmath(); + $config = MediaWikiServices::getInstance()->getMainConfig(); + $attributes = [ 'class' => 'mwe-math-element' ]; + if ( $this->getID() !== '' ) { + $attributes['id'] = $this->getID(); + } + if ( $config->get( 'MathEnableFormulaLinks' ) && + isset( $this->params['qid'] ) && + preg_match( '/Q\d+/', $this->params['qid'] ) ) { + $titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) ); + $attributes['href'] = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] ); + } + if ( $this->getMathStyle() == 'display' ) { + $attributes['display'] = 'block'; + } + $root = new MMLmath( "", $attributes ); + $this->setMathml( $root->encapsulateRaw( $presentation ) ); return StatusValue::newGood(); } @@ -37,4 +55,11 @@ class MathNativeMML extends MathMathML { return $this->checker; } + /** + * @inheritDoc + */ + public function getHtmlOutput(): string { + return $this->getMathml(); + } + } diff --git a/src/TexVC/MMLnodes/MMLmath.php b/src/TexVC/MMLnodes/MMLmath.php index 044aa739e..c496c7467 100644 --- a/src/TexVC/MMLnodes/MMLmath.php +++ b/src/TexVC/MMLnodes/MMLmath.php @@ -4,8 +4,8 @@ namespace MediaWiki\Extension\Math\TexVC\MMLnodes; class MMLmath extends MMLbase { - public function __construct( string $texclass = "" ) { - $attributes = [ "xmlns" => "http://www.w3.org/1998/Math/MathML" ]; + public function __construct( string $texclass = "", $attributes = [] ) { + $attributes["xmlns"] = "http://www.w3.org/1998/Math/MathML"; parent::__construct( "math", $texclass, $attributes ); } } diff --git a/tests/phpunit/MathNativeMMLTest.php b/tests/phpunit/MathNativeMMLTest.php index 65b806d1e..ba6e6d660 100644 --- a/tests/phpunit/MathNativeMMLTest.php +++ b/tests/phpunit/MathNativeMMLTest.php @@ -26,4 +26,29 @@ class MathNativeMMLTest extends MediaWikiIntegrationTestCase { $this->assertStringContainsString( 'sin', $mml->getMathml() ); } + public function testNoLink() { + $this->setMwGlobals( 'wgMathEnableFormulaLinks', false ); + $mml = new MathNativeMML( '\sin', [ 'qid' => 'Q1' ] ); + $this->assertTrue( $mml->render() ); + $this->assertStringNotContainsString( 'href', $mml->getMathml() ); + } + + public function testLink() { + $this->setMwGlobals( 'wgMathEnableFormulaLinks', true ); + $mml = new MathNativeMML( '\sin', [ 'qid' => 'Q1' ] ); + $this->assertTrue( $mml->render() ); + $this->assertStringContainsString( 'href', $mml->getMathml() ); + } + + public function testId() { + $mml = new MathNativeMML( '\sin', [ 'id' => 'unique-id' ] ); + $this->assertTrue( $mml->render() ); + $this->assertStringContainsString( 'unique-id', $mml->getMathml() ); + } + + public function testBlock() { + $mml = new MathNativeMML( '\sin', [ 'display' => 'block' ] ); + $this->assertTrue( $mml->render() ); + $this->assertStringContainsString( 'block', $mml->getMathml() ); + } }