Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
52 / 52 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
LexemeStubRdfBuilder | |
100.00% |
52 / 52 |
|
100.00% |
12 / 12 |
18 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
addPrefixes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addEntityStub | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
addLexemeStub | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
addFormStub | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
addSenseStub | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
addFormTypes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addRepresentations | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
addLexemeTypes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addSenseTypes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addGlosses | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
addLemmas | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace Wikibase\Lexeme\Presentation\Rdf; |
6 | |
7 | use Wikibase\DataModel\Entity\EntityId; |
8 | use Wikibase\DataModel\Services\Lookup\EntityLookup; |
9 | use Wikibase\DataModel\Term\TermList; |
10 | use Wikibase\Lexeme\Domain\Model\Form; |
11 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
12 | use Wikibase\Lexeme\Domain\Model\Sense; |
13 | use Wikibase\Repo\Rdf\EntityStubRdfBuilder; |
14 | use Wikibase\Repo\Rdf\RdfVocabulary; |
15 | use Wikimedia\Assert\Assert; |
16 | use Wikimedia\Purtle\RdfWriter; |
17 | |
18 | /** |
19 | * @license GPL-2.0-or-later |
20 | */ |
21 | class LexemeStubRdfBuilder implements EntityStubRdfBuilder { |
22 | |
23 | private const NS_ONTOLEX = 'ontolex'; |
24 | |
25 | /** |
26 | * @var RdfVocabulary |
27 | */ |
28 | private $vocabulary; |
29 | |
30 | /** |
31 | * @var RdfWriter |
32 | */ |
33 | private $writer; |
34 | |
35 | /** |
36 | * @var EntityLookup |
37 | */ |
38 | private $entityLookup; |
39 | |
40 | public function __construct( |
41 | RdfVocabulary $vocabulary, |
42 | RdfWriter $writer, |
43 | EntityLookup $entityLookup |
44 | ) { |
45 | $this->vocabulary = $vocabulary; |
46 | $this->writer = $writer; |
47 | $this->entityLookup = $entityLookup; |
48 | } |
49 | |
50 | /** |
51 | * Adds the prefixes used by the lexeme RDF mapping to the writer |
52 | * It should be executed before the writer starts |
53 | */ |
54 | public function addPrefixes(): void { |
55 | $this->writer->prefix( self::NS_ONTOLEX, 'http://www.w3.org/ns/lemon/ontolex#' ); |
56 | } |
57 | |
58 | /** |
59 | * Map some aspect of an entity to the RDF graph, as it should appear in the stub |
60 | * representation of the entity. |
61 | * |
62 | * @param EntityId $entityId |
63 | */ |
64 | public function addEntityStub( EntityId $entityId ): void { |
65 | |
66 | $entity = $this->entityLookup->getEntity( $entityId ); |
67 | |
68 | if ( $entity instanceof Lexeme ) { |
69 | $this->addLexemeStub( $entity ); |
70 | } |
71 | if ( $entity instanceof Form ) { |
72 | $this->addFormStub( $entity ); |
73 | } |
74 | if ( $entity instanceof Sense ) { |
75 | $this->addSenseStub( $entity ); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * Map some aspect of a Lexeme to the RDF graph, as it should appear in the stub |
81 | * representation of the lexeme. |
82 | * |
83 | * @param Lexeme $lexeme |
84 | */ |
85 | private function addLexemeStub( Lexeme $lexeme ): void { |
86 | $lexemeId = $lexeme->getId(); |
87 | Assert::parameter( $lexemeId !== null, '$lexeme', 'must have a lexeme ID' ); |
88 | $lexemeLName = $this->vocabulary->getEntityLName( $lexemeId ); |
89 | $repositoryName = $this->vocabulary->getEntityRepositoryName( $lexemeId ); |
90 | $lexemePrefix = $this->vocabulary->entityNamespaceNames[ $repositoryName ]; |
91 | |
92 | $this->addLexemeTypes( $lexemePrefix, $lexemeLName ); |
93 | |
94 | $this->addLemmas( $lexemePrefix, $lexemeLName, $lexeme->getLemmas() ); |
95 | } |
96 | |
97 | /** |
98 | * Map some aspect of a Form to the RDF graph, as it should appear in the stub |
99 | * representation of the form. |
100 | * |
101 | * @param Form $form |
102 | */ |
103 | private function addFormStub( Form $form ): void { |
104 | $formLName = $this->vocabulary->getEntityLName( $form->getId() ); |
105 | $repositoryName = $this->vocabulary->getEntityRepositoryName( $form->getId() ); |
106 | $lexemePrefix = $this->vocabulary->entityNamespaceNames[$repositoryName]; |
107 | |
108 | $this->addFormTypes( $lexemePrefix, $formLName ); |
109 | $this->addRepresentations( $lexemePrefix, $formLName, $form->getRepresentations() ); |
110 | } |
111 | |
112 | /** |
113 | * Map some aspect of a Sense to the RDF graph, as it should appear in the stub |
114 | * representation of the sense. |
115 | * |
116 | * @param Sense $sense |
117 | */ |
118 | private function addSenseStub( Sense $sense ): void { |
119 | $senseLName = $this->vocabulary->getEntityLName( $sense->getId() ); |
120 | $repositoryName = $this->vocabulary->getEntityRepositoryName( $sense->getId() ); |
121 | $lexemePrefix = $this->vocabulary->entityNamespaceNames[$repositoryName]; |
122 | |
123 | $this->addSenseTypes( $lexemePrefix, $senseLName ); |
124 | $this->addGlosses( $lexemePrefix, $senseLName, $sense->getGlosses() ); |
125 | } |
126 | |
127 | /** |
128 | * Adds the types of the given form to the RDF graph |
129 | * |
130 | * @param string $lexemePrefix |
131 | * @param string $formLName |
132 | */ |
133 | private function addFormTypes( string $lexemePrefix, string $formLName ): void { |
134 | $this->writer->about( $lexemePrefix, $formLName ) |
135 | ->a( self::NS_ONTOLEX, 'Form' ); |
136 | } |
137 | |
138 | /** |
139 | * Adds the representations of the given form to the RDF graph |
140 | * |
141 | * @param string $lexemePrefix |
142 | * @param string $formLName |
143 | * @param TermList $representations |
144 | */ |
145 | private function addRepresentations( string $lexemePrefix, string $formLName, TermList $representations ): void { |
146 | foreach ( $representations->toTextArray() as $representationCode => $representationText ) { |
147 | $this->writer->about( $lexemePrefix, $formLName ) |
148 | ->say( 'rdfs', 'label' ) |
149 | ->text( $representationText, $representationCode ) |
150 | ->say( self::NS_ONTOLEX, 'representation' ) |
151 | ->text( $representationText, $representationCode ); |
152 | } |
153 | } |
154 | |
155 | /** |
156 | * Adds the types of the given lexeme to the RDF graph |
157 | * |
158 | * @param string $lexemePrefix |
159 | * @param string $lexemeLName |
160 | */ |
161 | private function addLexemeTypes( string $lexemePrefix, string $lexemeLName ): void { |
162 | $this->writer->about( $lexemePrefix, $lexemeLName ) |
163 | ->a( self::NS_ONTOLEX, 'LexicalEntry' ); |
164 | } |
165 | |
166 | /** |
167 | * Adds the types of the given sense to the RDF graph |
168 | * |
169 | * @param string $lexemePrefix |
170 | * @param string $senseLName |
171 | */ |
172 | private function addSenseTypes( string $lexemePrefix, string $senseLName ): void { |
173 | $this->writer->about( $lexemePrefix, $senseLName ) |
174 | ->a( self::NS_ONTOLEX, 'LexicalSense' ); |
175 | } |
176 | |
177 | /** |
178 | * Adds the glosses of the given sense to the RDF graph |
179 | * |
180 | * @param string $lexemePrefix |
181 | * @param string $senseLName |
182 | * @param TermList $glosses |
183 | */ |
184 | private function addGlosses( string $lexemePrefix, string $senseLName, TermList $glosses ): void { |
185 | foreach ( $glosses->toTextArray() as $glossCode => $glossText ) { |
186 | $this->writer->about( $lexemePrefix, $senseLName ) |
187 | ->say( 'rdfs', 'label' ) |
188 | ->text( $glossText, $glossCode ) |
189 | ->say( RdfVocabulary::NS_SKOS, 'definition' ) |
190 | ->text( $glossText, $glossCode ); |
191 | } |
192 | } |
193 | |
194 | /** |
195 | * Adds the lemmas of the given lexeme to the RDF graph |
196 | * |
197 | * @param string $lexemePrefix |
198 | * @param string $lexemeLName |
199 | * @param TermList $lemmas |
200 | */ |
201 | private function addLemmas( string $lexemePrefix, string $lexemeLName, TermList $lemmas ): void { |
202 | foreach ( $lemmas->toTextArray() as $lemmaCode => $lemmaText ) { |
203 | $this->writer->about( $lexemePrefix, $lexemeLName ) |
204 | ->say( 'rdfs', 'label' ) |
205 | ->text( $lemmaText, $lemmaCode ) |
206 | ->say( RdfVocabulary::NS_ONTOLOGY, 'lemma' ) |
207 | ->text( $lemmaText, $lemmaCode ); |
208 | } |
209 | } |
210 | |
211 | } |