From c60565c79044998a32221b2123131f020cf5d70e Mon Sep 17 00:00:00 2001 From: Stegmujo Date: Tue, 21 Jun 2022 11:23:05 +0200 Subject: [PATCH] Remove some unused methods. Bug: T310350 Change-Id: I0629b9a963bf57b1368ffc61f7c3b631b19f5260 --- src/MathLaTeXML.php | 34 +------- src/MathMathML.php | 12 +-- src/MathMathMLCli.php | 4 +- src/MathRenderer.php | 21 +---- src/SpecialMathStatus.php | 5 +- tests/phpunit/MathSourceTest.php | 13 +++- .../integration/ParserIntegrationTests.php | 77 +++++++++++-------- tests/wfTest.php | 10 ++- 8 files changed, 72 insertions(+), 104 deletions(-) diff --git a/src/MathLaTeXML.php b/src/MathLaTeXML.php index 50fac9862..34ba1e612 100644 --- a/src/MathLaTeXML.php +++ b/src/MathLaTeXML.php @@ -4,9 +4,7 @@ namespace MediaWiki\Extension\Math; use Hooks; use MediaWiki\Logger\LoggerFactory; -use Sanitizer; use StatusValue; -use Xml; /** * Contains the driver function for the LaTeXML daemon @@ -152,33 +150,6 @@ class MathLaTeXML extends MathMathML { } } - /** - * Internal version of @link self::embedMathML - * @return string html element with rendered math - */ - protected function getMathMLTag() { - return self::embedMathML( $this->getMathml(), urldecode( $this->getTex() ) ); - } - - /** - * Embeds the MathML-XML element in a HTML span element with class tex - * @param string $mml the MathML string - * @param string $tagId optional tagID for references like (pagename#equation2) - * @param array|false $attribs - * @return string html element with rendered math - */ - public static function embedMathML( $mml, $tagId = '', $attribs = false ) { - $mml = str_replace( "\n", " ", $mml ); - if ( !$attribs ) { - $attribs = [ 'class' => 'tex', 'dir' => 'ltr' ]; - if ( $tagId ) { - $attribs['id'] = $tagId; - } - $attribs = Sanitizer::validateTagAttributes( $attribs, 'span' ); - } - return Xml::tags( 'span', $attribs, $mml ); - } - /** * Calculates the SVG image based on the MathML input * No cache is used. @@ -188,7 +159,8 @@ class MathLaTeXML extends MathMathML { $renderer = new MathMathML( $this->getTex() ); $renderer->setMathml( $this->getMathml() ); $renderer->setMode( MathConfig::MODE_LATEXML ); - $res = $renderer->render( true ); + $renderer->setPurge(); + $res = $renderer->render(); if ( $res == true ) { $this->setSvg( $renderer->getSvg() ); } else { @@ -223,3 +195,5 @@ class MathLaTeXML extends MathMathML { return 'mathlatexml'; } } + +class_alias( MathLaTeXML::class, 'MathLaTeXML' ); diff --git a/src/MathMathML.php b/src/MathMathML.php index 98a48a9b8..9336a60c6 100644 --- a/src/MathMathML.php +++ b/src/MathMathML.php @@ -38,7 +38,7 @@ class MathMathML extends MathRenderer { protected $host; /** @var LoggerInterface */ - private $logger; + protected $logger; /** @var bool if false MathML output is not validated */ private $XMLValidation = true; @@ -139,17 +139,9 @@ class MathMathML extends MathRenderer { $this->allowedRootElements = $settings; } - /** - * @see MathRenderer::render() - * @param bool $forceReRendering - * @return bool - */ - public function render( $forceReRendering = false ) { + public function render() { global $wgMathFullRestbaseURL; try { - if ( $forceReRendering ) { - $this->setPurge( true ); - } if ( in_array( $this->inputType, $this->restbaseInputTypes ) && in_array( $this->mode, $this->restbaseRenderingModes ) ) { diff --git a/src/MathMathMLCli.php b/src/MathMathMLCli.php index 51ea3bf41..5672e4e15 100644 --- a/src/MathMathMLCli.php +++ b/src/MathMathMLCli.php @@ -121,7 +121,7 @@ class MathMathMLCli extends MathMathML { * @return mixed * @throws MWException */ - public static function evaluateWithCli( $req, &$exitCode = null ) { + private static function evaluateWithCli( $req, &$exitCode = null ) { global $wgMathoidCli; $json_req = json_encode( $req ); $cmd = MediaWikiServices::getInstance()->getShellCommandFactory()->create(); @@ -146,7 +146,7 @@ class MathMathMLCli extends MathMathML { return $res; } - public function render( $forceReRendering = false ) { + public function render() { if ( $this->getLastError() ) { return false; } diff --git a/src/MathRenderer.php b/src/MathRenderer.php index 5ba8fd7fd..492453b4d 100644 --- a/src/MathRenderer.php +++ b/src/MathRenderer.php @@ -122,25 +122,6 @@ abstract class MathRenderer { $this->logger = LoggerFactory::getInstance( 'Math' ); } - /** - * Static method for rendering math tag - * - * @param string $tex LaTeX markup - * @param array $params HTML attributes - * @param string $mode constant indicating rendering mode - * @return string HTML for math tag - */ - public static function renderMath( $tex, $params = [], $mode = MathConfig::MODE_PNG ) { - $renderer = MediaWikiServices::getInstance() - ->get( 'Math.RendererFactory' ) - ->getRenderer( $tex, $params, $mode ); - if ( $renderer->render() ) { - return $renderer->getHtmlOutput(); - } else { - return $renderer->getLastError(); - } - } - /** * @param string $md5 * @return self the MathRenderer generated from md5 @@ -668,7 +649,7 @@ abstract class MathRenderer { * * @return string XML-Document of the rendered SVG */ - public function getSvg( /** @noinspection PhpUnusedParameterInspection */ $render = 'render' ) { + public function getSvg( $render = 'render' ) { // Spaces will prevent the image from being displayed correctly in the browser if ( !$this->svg && $this->rbi ) { $this->svg = $this->rbi->getSvg(); diff --git a/src/SpecialMathStatus.php b/src/SpecialMathStatus.php index ade22994e..fb49555d3 100644 --- a/src/SpecialMathStatus.php +++ b/src/SpecialMathStatus.php @@ -119,7 +119,7 @@ class SpecialMathStatus extends SpecialPage { $renderer = new MathMathML( $inputSample, $attribs ); $this->assertEquals( 'pmml', $renderer->getInputType(), 'Checking if MathML input is supported' ); $this->assertTrue( $renderer->render(), 'Rendering Presentation MathML sample' ); - $real = MathRenderer::renderMath( $inputSample, $attribs, MathConfig::MODE_MATHML ); + $real = $renderer->getHtmlOutput(); $expected = 'hash=5628b8248b79267ecac656102334d5e3&mode=mathml'; $this->assertContains( $expected, $real, 'Checking if the link to SVG image is correct' ); } @@ -153,7 +153,8 @@ class SpecialMathStatus extends SpecialPage { } $tex .= $testMax; $renderer = new MathLaTeXML( $tex, [ 'display' => 'linebreak' ] ); - $this->assertTrue( $renderer->render( true ), "Rendering of linebreak test in LaTeXML mode" ); + $renderer->setPurge(); + $this->assertTrue( $renderer->render(), "Rendering of linebreak test in LaTeXML mode" ); $expected = 'mtr'; $real = preg_replace( "/\n\\s*/", '', $renderer->getHtmlOutput() ); $this->assertContains( $expected, $real, "Checking for linebreak" . diff --git a/tests/phpunit/MathSourceTest.php b/tests/phpunit/MathSourceTest.php index 3b8ec4d1d..19e196db9 100644 --- a/tests/phpunit/MathSourceTest.php +++ b/tests/phpunit/MathSourceTest.php @@ -1,7 +1,6 @@ getServiceContainer() + ->get( 'Math.RendererFactory' ) + ->getRenderer( 'a+b', [], MathConfig::MODE_SOURCE ); + $renderer->render(); + $real = $renderer->getHtmlOutput(); $this->assertEquals( '$ a+b $', $real, @@ -30,7 +33,11 @@ class MathSourceTest extends MediaWikiIntegrationTestCase { * Checks if newlines are converted to spaces correctly. */ public function testNewLines() { - $real = MathRenderer::renderMath( "a\n b", [], MathConfig::MODE_SOURCE ); + $renderer = $this->getServiceContainer() + ->get( 'Math.RendererFactory' ) + ->getRenderer( "a\n b", [], MathConfig::MODE_SOURCE ); + $renderer->render(); + $real = $renderer->getHtmlOutput(); $this->assertSame( '$ a b $', $real, diff --git a/tests/phpunit/integration/ParserIntegrationTests.php b/tests/phpunit/integration/ParserIntegrationTests.php index 77dcd405b..19346bf7d 100644 --- a/tests/phpunit/integration/ParserIntegrationTests.php +++ b/tests/phpunit/integration/ParserIntegrationTests.php @@ -2,12 +2,14 @@ namespace MediaWiki\Extension\Math\Tests; +use MediaWiki\Config\ServiceOptions; use MediaWiki\Extension\Math\MathConfig; use MediaWiki\Extension\Math\MathRenderer; use MediaWiki\Extension\Math\Render\RendererFactory; use MediaWiki\Page\PageReferenceValue; use MediaWikiIntegrationTestCase; use ParserOptions; +use Psr\Log\NullLogger; /** * @group Database @@ -16,40 +18,47 @@ use ParserOptions; */ class ParserIntegrationTests extends MediaWikiIntegrationTestCase { - private function getDummyRenderer( $mode, $tex, $params ): MathRenderer { - return new class( $mode, $tex, $params ) extends MathRenderer { - public function __construct( $mode, $tex = '', $params = [] ) { - parent::__construct( $tex, $params ); - $this->mode = $mode; - } - - public function render( $forceReRendering = false ) { - return true; - } - - public function checkTeX() { - return true; - } - - public function getHtmlOutput() { - return "$this->mode:$this->tex"; - } - - protected function getMathTableName() { - return 'whatever'; - } - }; - } - private function setupDummyRendering() { $this->setMwGlobals( 'wgMathValidModes', [ MathConfig::MODE_SOURCE, MathConfig::MODE_PNG ] ); $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'math' => MathConfig::MODE_SOURCE ] ); - $dummyRendererFactory = $this->createMock( RendererFactory::class ); - $dummyRendererFactory->method( 'getRenderer' ) - ->willReturnCallback( function ( $tex, $params, $mode ) { - return $this->getDummyRenderer( $mode, $tex, $params ); - } ); - $this->setService( 'Math.RendererFactory', $dummyRendererFactory ); + $this->setService( 'Math.RendererFactory', new class( + new ServiceOptions( RendererFactory::CONSTRUCTOR_OPTIONS, [ + 'MathoidCli' => false, + 'MathEnableExperimentalInputFormats' => false, + 'MathValidModes' => [ MathConfig::MODE_SOURCE, MathConfig::MODE_PNG ], + ] ), + $this->getServiceContainer()->getUserOptionsLookup(), + new NullLogger() + ) extends RendererFactory { + public function getRenderer( + string $tex, + array $params = [], + string $mode = MathConfig::MODE_PNG + ): MathRenderer { + return new class( $mode, $tex, $params ) extends MathRenderer { + public function __construct( $mode, $tex = '', $params = [] ) { + parent::__construct( $tex, $params ); + $this->mode = $mode; + } + + public function render() { + return true; + } + + public function checkTeX() { + return true; + } + + public function getHtmlOutput() { + return "$this->mode:$this->tex"; + } + + protected function getMathTableName() { + return 'whatever'; + } + }; + } + } ); } /** @@ -77,14 +86,14 @@ class ParserIntegrationTests extends MediaWikiIntegrationTestCase { // source was set as a default, so the rendering will be shared with // canonical rendering produced by page edit - $parserOptions1 = ParserOptions::newFromAnon(); + $parserOptions1 = ParserOptions::newCanonical( 'canonical' ); $parserOptions1->setOption( 'math', MathConfig::MODE_SOURCE ); $render = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions1 ); $this->assertNotNull( $render ); $this->assertStringContainsString( "source:TEST_FORMULA", $render->getText() ); // Now render with 'png' and make sure we didn't get the cached output - $parserOptions2 = ParserOptions::newFromAnon(); + $parserOptions2 = ParserOptions::newCanonical( 'canonical' ); $parserOptions2->setOption( 'math', MathConfig::MODE_PNG ); $this->assertNull( $parserOutputAccess->getCachedParserOutput( $page, $parserOptions2 ) ); $renderStatus = $parserOutputAccess->getParserOutput( $page, $parserOptions2 ); @@ -114,7 +123,7 @@ class ParserIntegrationTests extends MediaWikiIntegrationTestCase { public function testMathInLink() { $this->setupDummyRendering(); $po = ParserOptions::newFromAnon(); - $po->setOption( 'math', 'png' ); + $po->setOption( 'math', MathConfig::MODE_PNG ); $res = $this->getServiceContainer() ->getParser() ->parse( diff --git a/tests/wfTest.php b/tests/wfTest.php index f835e7102..389df44b1 100644 --- a/tests/wfTest.php +++ b/tests/wfTest.php @@ -6,7 +6,6 @@ require_once __DIR__ . '/../../../maintenance/Maintenance.php'; use MediaWiki\Extension\Math\MathConfig; use MediaWiki\Extension\Math\MathMathMLCli; -use MediaWiki\Extension\Math\MathRenderer; use MediaWiki\MediaWikiServices; class WfTest extends Maintenance { @@ -70,8 +69,13 @@ class WfTest extends Maintenance { $rend = []; $rendererFactory = MediaWikiServices::getInstance()->get( 'Math.RendererFactory' ); foreach ( array_slice( $allEquations, $offset, $length, true ) as $input ) { - $output = MathRenderer::renderMath( $input[1], $input[2], MathConfig::MODE_MATHML ); - $rend[] = [ $rendererFactory->getRenderer( $input[1], $input[2], MathConfig::MODE_MATHML ), $input ]; + $renderer = $rendererFactory->getRenderer( $input[1], $input[2], MathConfig::MODE_MATHML ); + if ( $renderer->render() ) { + $output = $renderer->getHtmlOutput(); + } else { + $output = $renderer->getLastError(); + } + $rend[] = [ $renderer, $input ]; $output = preg_replace( '#src="(.*?)/(([a-f]|\d)*).png"#', 'src="\2.png"', $output ); $parserTests[] = [ (string)$input[1], $output ]; $i++;