mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-24 07:34:22 +00:00
Clean up Wikibase related code
This fixes some warnings in my PHPStorm, gets rid of duplicate code, inlines constants that do not make the code easier to read, and more. Change-Id: I02f446491509043f5d3e51e26e932f76c9ecb6cf
This commit is contained in:
parent
9742440a7a
commit
8b8d91a771
|
@ -2,7 +2,6 @@
|
|||
|
||||
use DataValues\StringValue;
|
||||
use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
|
||||
use ValueFormatters\FormattingException;
|
||||
use ValueFormatters\ValueFormatter;
|
||||
use Wikibase\Lib\SnakFormatter;
|
||||
|
||||
|
@ -17,22 +16,25 @@ use Wikibase\Lib\SnakFormatter;
|
|||
|
||||
class MathFormatter implements ValueFormatter {
|
||||
|
||||
/**
|
||||
* @var string One of the SnakFormatter::FORMAT_... constants.
|
||||
*/
|
||||
private $format;
|
||||
|
||||
/*
|
||||
/**
|
||||
* Loads format to distinguish the type of formatting
|
||||
*
|
||||
* @param string $format
|
||||
* @param string $format One of the SnakFormatter::FORMAT_... constants.
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct( $format ) {
|
||||
switch ( $format ) {
|
||||
case ( SnakFormatter::FORMAT_HTML ):
|
||||
case ( SnakFormatter::FORMAT_HTML_DIFF ):
|
||||
case ( SnakFormatter::FORMAT_HTML_WIDGET ):
|
||||
case ( SnakFormatter::FORMAT_WIKI ):
|
||||
case ( SnakFormatter::FORMAT_PLAIN ):
|
||||
case SnakFormatter::FORMAT_PLAIN:
|
||||
case SnakFormatter::FORMAT_WIKI:
|
||||
case SnakFormatter::FORMAT_HTML:
|
||||
case SnakFormatter::FORMAT_HTML_DIFF:
|
||||
case SnakFormatter::FORMAT_HTML_WIDGET:
|
||||
$this->format = $format;
|
||||
break;
|
||||
default:
|
||||
|
@ -40,44 +42,40 @@ class MathFormatter implements ValueFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
/**
|
||||
* @param StringValue $value
|
||||
*
|
||||
* @throws MismatchingDataValueTypeException
|
||||
* @return string
|
||||
* @throws \ValueFormatters\Exceptions\MismatchingDataValueTypeException
|
||||
*/
|
||||
public function format( $value ) {
|
||||
if ( !( $value instanceof StringValue ) ) {
|
||||
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
|
||||
}
|
||||
|
||||
$tex = $value->getValue();
|
||||
|
||||
switch ( $this->format ) {
|
||||
case ( SnakFormatter::FORMAT_PLAIN ):
|
||||
return "$tex";
|
||||
case ( SnakFormatter::FORMAT_WIKI ):
|
||||
case SnakFormatter::FORMAT_PLAIN:
|
||||
return $tex;
|
||||
case SnakFormatter::FORMAT_WIKI:
|
||||
return "<math>$tex</math>";
|
||||
case ( SnakFormatter::FORMAT_HTML ):
|
||||
case ( SnakFormatter::FORMAT_HTML_WIDGET ):
|
||||
case ( SnakFormatter::FORMAT_HTML_DIFF ):
|
||||
default:
|
||||
$renderer = new MathMathML( $tex );
|
||||
if ( $renderer->checkTex() ) {
|
||||
if ( $renderer->render() ) {
|
||||
return $renderer->getHtmlOutput();
|
||||
}
|
||||
if ( $renderer->checkTex() && $renderer->render() ) {
|
||||
return $renderer->getHtmlOutput();
|
||||
}
|
||||
|
||||
// TeX string is not valid or rendering failed
|
||||
return $renderer->getLastError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return format
|
||||
* @return string One of the SnakFormatter::FORMAT_... constants.
|
||||
*/
|
||||
|
||||
public function getFormat() {
|
||||
return $this->format;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
* @author Moritz Schubotz
|
||||
*/
|
||||
|
||||
use MediaWiki\Logger\LoggerFactory;
|
||||
|
||||
class MathInputCheckRestbase extends MathInputCheck {
|
||||
private $restbaseInterface;
|
||||
|
||||
|
|
|
@ -9,12 +9,8 @@ use DataValues\StringValue;
|
|||
use DataValues\NumberValue;
|
||||
|
||||
class MathFormatterTest extends MediaWikiTestCase {
|
||||
const SOME_TEX = "a^2+b^2=c^2";
|
||||
const FORMAT_PLAIN = 'text/plain';
|
||||
const FORMAT_HTML = 'text/html';
|
||||
const FORMAT_XWIKI = 'text/x-wiki';
|
||||
const FORMAT_UNKNOWN = 'unknown/unknown';
|
||||
const FORMAT_VALUE = "";
|
||||
|
||||
const SOME_TEX = 'a^2+b^2=c^2';
|
||||
|
||||
protected static $hasRestbase;
|
||||
|
||||
|
@ -23,19 +19,12 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
self::$hasRestbase = $rbi->checkBackend( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
if ( !self::$hasRestbase ) {
|
||||
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
parent::tearDown();
|
||||
if ( !self::$hasRestbase ) {
|
||||
$this->markTestSkipped( 'Can not connect to Restbase Math interface.' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,16 +32,16 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
* @covers MathFormatter::__construct()
|
||||
*/
|
||||
public function testBasics() {
|
||||
$formatter = new MathFormatter( self::FORMAT_PLAIN );
|
||||
$formatter = new MathFormatter( 'text/plain' );
|
||||
// check if the format input was corretly passed to the class
|
||||
$this->assertEquals( self::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
|
||||
$this->assertSame( 'text/plain', $formatter->getFormat(), 'test getFormat' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
|
||||
*/
|
||||
public function testNotStringValue() {
|
||||
$formatter = new MathFormatter( self::FORMAT_PLAIN );
|
||||
$formatter = new MathFormatter( 'text/plain' );
|
||||
$formatter->format( new NumberValue( 0 ) );
|
||||
}
|
||||
|
||||
|
@ -60,7 +49,7 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
|
||||
*/
|
||||
public function testNullValue() {
|
||||
$formatter = new MathFormatter( self::FORMAT_PLAIN );
|
||||
$formatter = new MathFormatter( 'text/plain' );
|
||||
$formatter->format( null );
|
||||
}
|
||||
|
||||
|
@ -68,33 +57,29 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testUnknownFormat() {
|
||||
$formatter = new MathFormatter( self::FORMAT_UNKNOWN );
|
||||
new MathFormatter( 'unknown/unknown' );
|
||||
}
|
||||
|
||||
public function testFormatPlain() {
|
||||
$formatter = new MathFormatter( self::FORMAT_PLAIN );
|
||||
$formatter = new MathFormatter( 'text/plain' );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
$this->assertEquals( self::SOME_TEX, $resultFormat,
|
||||
'Results should be equal' );
|
||||
|
||||
$this->assertSame( self::SOME_TEX, $resultFormat );
|
||||
}
|
||||
|
||||
public function testFormatHtml() {
|
||||
$formatter = new MathFormatter( self::FORMAT_HTML );
|
||||
$formatter = new MathFormatter( 'text/html' );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
$this->assertContains( '</math>', $resultFormat,
|
||||
'Result must contain math-tag' );
|
||||
$this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
|
||||
}
|
||||
|
||||
public function testFormatXWiki() {
|
||||
$tex = self::SOME_TEX;
|
||||
$formatter = new MathFormatter( self::FORMAT_XWIKI );
|
||||
$formatter = new MathFormatter( 'text/x-wiki' );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
$this->assertEquals( "<math>$tex</math>", $resultFormat,
|
||||
'Tex wasn\'t properly wrapped' );
|
||||
|
||||
$this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue