RDF Formatter for Math data type
Bug: T126349 Change-Id: I15fbeec282868b4267a3e3d15740f2c3ff37ea48
This commit is contained in:
parent
129f0e4bcc
commit
5f16307582
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Wikibase\DataModel\Snak\PropertyValueSnak;
|
||||
use Wikibase\Rdf\ValueSnakRdfBuilder;
|
||||
use Wikimedia\Purtle\RdfWriter;
|
||||
|
||||
class MathMLRdfBuilder implements ValueSnakRdfBuilder {
|
||||
|
||||
/**
|
||||
* Adds a value
|
||||
*
|
||||
* @param RdfWriter $writer
|
||||
* @param string $propertyValueNamespace Property value relation namespace
|
||||
* @param string $propertyValueLName Property value relation name
|
||||
* @param string $dataType Property data type
|
||||
* @param PropertyValueSnak $snak
|
||||
*/
|
||||
public function addValue(
|
||||
RdfWriter $writer,
|
||||
$propertyValueNamespace,
|
||||
$propertyValueLName,
|
||||
$dataType,
|
||||
PropertyValueSnak $snak
|
||||
) {
|
||||
$renderer = new MathMathML( $snak->getDataValue()->getValue() );
|
||||
if ( $renderer->checkTeX() && $renderer->render() ) {
|
||||
$mml = $renderer->getMathml();
|
||||
} else {
|
||||
$err = $renderer->getLastError();
|
||||
$mml = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><merror>$err</merror></math>";
|
||||
}
|
||||
$writer->say( $propertyValueNamespace, $propertyValueLName )
|
||||
->value( $mml, 'http://www.w3.org/1998/Math/MathML' );
|
||||
}
|
||||
}
|
|
@ -2,8 +2,12 @@
|
|||
|
||||
use ValueFormatters\FormatterOptions;
|
||||
use ValueParsers\StringParser;
|
||||
use Wikibase\Rdf\DedupeBag;
|
||||
use Wikibase\Rdf\EntityMentionListener;
|
||||
use Wikibase\Rdf\RdfVocabulary;
|
||||
use Wikibase\Repo\Parsers\WikibaseStringValueNormalizer;
|
||||
use Wikibase\Repo\WikibaseRepo;
|
||||
use Wikimedia\Purtle\RdfWriter;
|
||||
|
||||
class MathWikidataHook {
|
||||
|
||||
|
@ -41,6 +45,15 @@ class MathWikidataHook {
|
|||
$wgOut->addModuleStyles( $styles );
|
||||
return new MathFormatter( $format );
|
||||
},
|
||||
'rdf-builder-factory-callback' => function (
|
||||
$mode,
|
||||
RdfVocabulary $vocab,
|
||||
RdfWriter $writer,
|
||||
EntityMentionListener $tracker,
|
||||
DedupeBag $dedupe
|
||||
) {
|
||||
return new MathMLRdfBuilder();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@
|
|||
"SpecialMathStatus": "SpecialMathStatus.php",
|
||||
"MathValidator": "MathValidator.php",
|
||||
"MathFormatter": "MathFormatter.php",
|
||||
"MathWikidataHook": "MathWikidataHook.php"
|
||||
"MathWikidataHook": "MathWikidataHook.php",
|
||||
"MathMLRdfBuilder": "MathMLRdfBuilder.php"
|
||||
},
|
||||
"DefaultUserOptions": {
|
||||
"math": "png"
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
/**
|
||||
* Test the MathML RDF formatter
|
||||
*
|
||||
* @group Math
|
||||
* @covers MathMLRdfBuilder
|
||||
* @author Moritz Schubotz (physikerwelt)
|
||||
*/
|
||||
|
||||
use DataValues\StringValue;
|
||||
use Wikibase\DataModel\Entity\PropertyId;
|
||||
use Wikibase\DataModel\Snak\PropertyValueSnak;
|
||||
use Wikimedia\Purtle\NTriplesRdfWriter;
|
||||
|
||||
class MathMLRdfBuilderTest extends MediaWikiTestCase {
|
||||
const ACME_PREFIX_URL = 'http://acme/';
|
||||
const ACME_REF = 'testing';
|
||||
protected static $hasRestbase;
|
||||
|
||||
public static function setUpBeforeClass() {
|
||||
$rbi = new MathRestbaseInterface();
|
||||
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." );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $test
|
||||
* @return string
|
||||
*/
|
||||
private function makeCase( $test ) {
|
||||
$builder = new MathMLRdfBuilder();
|
||||
$writer = new NTriplesRdfWriter();
|
||||
$writer->prefix( 'www', "http://www/" );
|
||||
$writer->prefix( 'acme', self::ACME_PREFIX_URL );
|
||||
|
||||
$writer->start();
|
||||
$writer->about( 'www', 'Q1' );
|
||||
|
||||
$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( $test ) );
|
||||
$builder->addValue( $writer, 'acme', self::ACME_REF, 'DUMMY', $snak );
|
||||
|
||||
return trim( $writer->drain() );
|
||||
}
|
||||
|
||||
public function testValidInput() {
|
||||
$triples = $this->makeCase( 'a^2' );
|
||||
$this->assertContains( self::ACME_PREFIX_URL . self::ACME_REF . '> "<math', $triples );
|
||||
$this->assertContains( '<mi>a</mi>\n', $triples );
|
||||
$this->assertContains( '<mn>2</mn>\n', $triples );
|
||||
$this->assertContains( 'a^{2}', $triples );
|
||||
$this->assertContains( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
||||
}
|
||||
|
||||
public function testInvalidInput() {
|
||||
$triples = $this->makeCase( '\notExists' );
|
||||
$this->assertContains( '<math', $triples );
|
||||
$this->assertContains( 'unknown function', $triples );
|
||||
$this->assertContains( 'notExists', $triples );
|
||||
$this->assertContains( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue