Remove some unused methods.

Bug: T310350
Change-Id: I0629b9a963bf57b1368ffc61f7c3b631b19f5260
This commit is contained in:
Stegmujo 2022-06-21 11:23:05 +02:00 committed by Physikerwelt
parent 2e630542ab
commit c60565c790
8 changed files with 72 additions and 104 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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" .

View file

@ -1,7 +1,6 @@
<?php
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\Extension\Math\MathRenderer;
use MediaWiki\Extension\Math\MathSource;
/**
@ -18,7 +17,11 @@ class MathSourceTest extends MediaWikiIntegrationTestCase {
* i.e. if the span element is generated right.
*/
public function testBasics() {
$real = MathRenderer::renderMath( "a+b", [], MathConfig::MODE_SOURCE );
$renderer = $this->getServiceContainer()
->get( 'Math.RendererFactory' )
->getRenderer( 'a+b', [], MathConfig::MODE_SOURCE );
$renderer->render();
$real = $renderer->getHtmlOutput();
$this->assertEquals(
'<span class="mwe-math-fallback-source-inline tex" dir="ltr">$ a+b $</span>',
$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(
'<span class="mwe-math-fallback-source-inline tex" dir="ltr">$ a b $</span>',
$real,

View file

@ -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 "<render>$this->mode:$this->tex</render>";
}
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 "<render>$this->mode:$this->tex</render>";
}
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( "<render>source:TEST_FORMULA</render>", $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(

View file

@ -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++;