jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/342202 )
Change subject: Start a very basic LexemeRdfBuilder
......................................................................
Start a very basic LexemeRdfBuilder
Bug: T157791
Change-Id: I4a8d528ce058dc0acbddc57cf04ee48f28a38c22
---
M WikibaseLexeme.entitytypes.php
A src/Rdf/LexemeRdfBuilder.php
A tests/phpunit/data/rdf/LexemeRdfBuilder/L2_all.nt
A tests/phpunit/data/rdf/LexemeRdfBuilder/L2_lemmas.nt
A tests/phpunit/data/rdf/LexemeRdfBuilder/L2_stubs.nt
A tests/phpunit/data/rdf/entities/L2.json
A tests/phpunit/mediawiki/Rdf/LexemeRdfBuilderTest.php
7 files changed, 275 insertions(+), 0 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/WikibaseLexeme.entitytypes.php b/WikibaseLexeme.entitytypes.php
index 5623ace..f5df89e 100644
--- a/WikibaseLexeme.entitytypes.php
+++ b/WikibaseLexeme.entitytypes.php
@@ -16,7 +16,9 @@
use Wikibase\Lexeme\ChangeOp\Deserialization\LemmaChangeOpDeserializer;
use Wikibase\Lexeme\ChangeOp\Deserialization\LanguageChangeOpDeserializer;
use
Wikibase\Lexeme\ChangeOp\Deserialization\LexicalCategoryChangeOpDeserializer;
+use Wikibase\Lexeme\Rdf\LexemeRdfBuilder;
use Wikibase\Lexeme\Validators\LexemeValidatorFactory;
+use Wikibase\Rdf\RdfVocabulary;
use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator;
use Wikibase\Repo\MediaWikiLanguageDirectionalityLookup;
use Wikibase\Repo\ParserOutput\FallbackHintHtmlTermRenderer;
@@ -39,6 +41,7 @@
use Wikibase\View\EditSectionGenerator;
use Wikibase\View\EntityTermsView;
use Wikibase\View\Template\TemplateFactory;
+use Wikimedia\Purtle\RdfWriter;
return [
'lexeme' => [
@@ -148,5 +151,17 @@
)
);
},
+ 'rdf-builder-factory-callback' => function(
+ $flavorFlags,
+ RdfVocabulary $vocabulary,
+ RdfWriter $writer,
+ $mentionedEntityTracker,
+ $dedupe
+ ) {
+ return new LexemeRdfBuilder(
+ $vocabulary,
+ $writer
+ );
+ }
]
];
diff --git a/src/Rdf/LexemeRdfBuilder.php b/src/Rdf/LexemeRdfBuilder.php
new file mode 100755
index 0000000..0b48afe
--- /dev/null
+++ b/src/Rdf/LexemeRdfBuilder.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace Wikibase\Lexeme\Rdf;
+
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Rdf\EntityRdfBuilder;
+use Wikibase\Rdf\RdfVocabulary;
+use Wikimedia\Purtle\RdfWriter;
+
+/**
+ * Factory to return Rdf builders for parts of lexeme
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <[email protected]>
+ */
+class LexemeRdfBuilder implements EntityRdfBuilder {
+
+ /**
+ * @var RdfVocabulary
+ */
+ private $vocabulary;
+
+ /**
+ * @var RdfWriter
+ */
+ private $writer;
+
+ /**
+ * @param RdfVocabulary $vocabulary
+ * @param RdfWriter $writer
+ */
+ public function __construct(
+ RdfVocabulary $vocabulary,
+ RdfWriter $writer
+ ) {
+ $this->vocabulary = $vocabulary;
+ $this->writer = $writer;
+ }
+
+ /**
+ * Adds the lemmas of the given entity to the RDF graph
+ *
+ * @param string $entityLName
+ * @param TermList $lemmas
+ */
+ public function addLemmas( $entityLName, TermList $lemmas ) {
+ foreach ( $lemmas->toTextArray() as $lemmaCode => $lemmaText ) {
+
+ $this->writer->about( RdfVocabulary::NS_ENTITY,
$entityLName )
+ ->say( 'rdfs', 'label' )
+ ->text( $lemmaText, $lemmaCode )
+ ->say( RdfVocabulary::NS_SKOS, 'prefLabel' )
+ ->text( $lemmaText, $lemmaCode )
+ ->say( RdfVocabulary::NS_SCHEMA_ORG, 'name' )
+ ->text( $lemmaText, $lemmaCode );
+ }
+ }
+
+ /**
+ * Map a Lexeme to the RDF graph
+ *
+ * @param EntityDocument $entity
+ */
+ public function addEntity(
+ EntityDocument $entity
+ ) {
+ if ( !$entity instanceof Lexeme ) {
+ return;
+ }
+ $lexemeLName = $this->vocabulary->getEntityLName(
$entity->getId() );
+
+ $this->addLemmas( $lexemeLName, $entity->getLemmas() );
+ // TODO: Implement other parts.
+ }
+
+ /**
+ * Map some aspect of a Lexeme to the RDF graph, as it should appear in
the stub
+ * representation of the lexeme.
+ *
+ * @param EntityDocument $entity
+ */
+ public function addEntityStub( EntityDocument $entity ) {
+ if ( !$entity instanceof Lexeme ) {
+ return;
+ }
+ $lexemeLName = $this->vocabulary->getEntityLName(
$entity->getId() );
+
+ $this->addLemmas( $lexemeLName, $entity->getLemmas() );
+ }
+
+}
diff --git a/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_all.nt
b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_all.nt
new file mode 100755
index 0000000..2e3efcb
--- /dev/null
+++ b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_all.nt
@@ -0,0 +1,6 @@
+<http://acme.test/L2> <http://schema.org/name> "Color"@en-us .
+<http://acme.test/L2> <http://schema.org/name> "Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Colour"@en-gb .
diff --git a/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_lemmas.nt
b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_lemmas.nt
new file mode 100644
index 0000000..2e3efcb
--- /dev/null
+++ b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_lemmas.nt
@@ -0,0 +1,6 @@
+<http://acme.test/L2> <http://schema.org/name> "Color"@en-us .
+<http://acme.test/L2> <http://schema.org/name> "Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Colour"@en-gb .
diff --git a/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_stubs.nt
b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_stubs.nt
new file mode 100644
index 0000000..2e3efcb
--- /dev/null
+++ b/tests/phpunit/data/rdf/LexemeRdfBuilder/L2_stubs.nt
@@ -0,0 +1,6 @@
+<http://acme.test/L2> <http://schema.org/name> "Color"@en-us .
+<http://acme.test/L2> <http://schema.org/name> "Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2000/01/rdf-schema#label>
"Colour"@en-gb .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Color"@en-us .
+<http://acme.test/L2> <http://www.w3.org/2004/02/skos/core#prefLabel>
"Colour"@en-gb .
diff --git a/tests/phpunit/data/rdf/entities/L2.json
b/tests/phpunit/data/rdf/entities/L2.json
new file mode 100755
index 0000000..a408836
--- /dev/null
+++ b/tests/phpunit/data/rdf/entities/L2.json
@@ -0,0 +1,14 @@
+{
+ "id": "L2",
+ "type": "lexeme",
+ "lemmas": {
+ "en-us": {
+ "language": "en-us",
+ "value": "Color"
+ },
+ "en-gb": {
+ "language": "en-gb",
+ "value": "Colour"
+ }
+ }
+}
diff --git a/tests/phpunit/mediawiki/Rdf/LexemeRdfBuilderTest.php
b/tests/phpunit/mediawiki/Rdf/LexemeRdfBuilderTest.php
new file mode 100644
index 0000000..deb56eb
--- /dev/null
+++ b/tests/phpunit/mediawiki/Rdf/LexemeRdfBuilderTest.php
@@ -0,0 +1,135 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\Rdf;
+
+use Wikibase\Lexeme\Rdf\LexemeRdfBuilder;
+use Wikibase\Repo\Tests\Rdf\NTriplesRdfTestHelper;
+use Wikibase\Repo\Tests\Rdf\RdfBuilderTestData;
+use Wikimedia\Purtle\RdfWriter;
+
+/**
+ * @covers Wikibase\Lexeme\Rdf\LexemeRdfBuilder
+ *
+ * @group Wikibase
+ * @group WikibaseRdf
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <[email protected]>
+ */
+class LexemeRdfBuilderTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @var NTriplesRdfTestHelper
+ */
+ private $helper;
+
+ /**
+ * @var RdfBuilderTestData|null
+ */
+ private $testData = null;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->helper = new NTriplesRdfTestHelper();
+ }
+
+ /**
+ * Initialize repository data
+ *
+ * @return RdfBuilderTestData
+ */
+ private function getTestData() {
+ if ( $this->testData === null ) {
+ $this->testData = new RdfBuilderTestData(
+ __DIR__ . '/../../data/rdf/entities',
+ __DIR__ . '/../../data/rdf/LexemeRdfBuilder'
+ );
+ }
+
+ return $this->testData;
+ }
+
+ /**
+ * @param RdfWriter $writer
+ *
+ * @return LexemeRdfBuilder
+ */
+ private function newBuilder( RdfWriter $writer ) {
+ $vocabulary = $this->getTestData()->getVocabulary();
+
+ return new LexemeRdfBuilder(
+ $vocabulary,
+ $writer
+ );
+ }
+
+ private function assertOrCreateNTriples( $dataSetName, RdfWriter
$writer ) {
+ $actual = $writer->drain();
+ $expected = $this->getTestData()->getNTriples( $dataSetName );
+
+ if ( $expected === null ) {
+ $this->getTestData()->putTestData( $dataSetName,
$actual, '.actual' );
+ $this->fail( "Data set $dataSetName not found! Created
file with the current data using"
+ . " the suffix .actual" );
+ }
+
+ $this->helper->assertNTriplesEquals( $expected, $actual, "Data
set $dataSetName" );
+ }
+
+ public function provideAddEntity() {
+ return [
+ [ 'L2', 'L2_all' ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideAddEntity
+ */
+ public function testAddEntity( $entityName, $dataSetName ) {
+ $entity = $this->getTestData()->getEntity( $entityName );
+
+ $writer = $this->getTestData()->getNTriplesWriter();
+ $this->newBuilder( $writer )->addEntity( $entity );
+
+ $this->assertOrCreateNTriples( $dataSetName, $writer );
+ }
+
+ public function provideAddEntityStub() {
+ return [
+ [ 'L2', 'L2_stubs' ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideAddEntityStub
+ */
+ public function testAddEntityStub( $entityName, $dataSetName ) {
+ $entity = $this->getTestData()->getEntity( $entityName );
+
+ $writer = $this->getTestData()->getNTriplesWriter();
+ $this->newBuilder( $writer )->addEntityStub( $entity );
+
+ $this->assertOrCreateNTriples( $dataSetName, $writer );
+ }
+
+ public function provideAddLemmas() {
+ return [
+ [ 'L2', 'L2_lemmas' ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideAddLemmas
+ */
+ public function testAddLemmas( $entityName, $dataSetName ) {
+ $entity = $this->getTestData()->getEntity( $entityName );
+
+ $writer = $this->getTestData()->getNTriplesWriter();
+ $this->newBuilder( $writer )
+ ->addLemmas( $entity->getId(), $entity->getLemmas() );
+
+ $this->assertOrCreateNTriples( $dataSetName, $writer );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/342202
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4a8d528ce058dc0acbddc57cf04ee48f28a38c22
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits