TermsRdfBuilder::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Wikibase\Repo\Rdf;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Term\AliasesProvider;
7
use Wikibase\DataModel\Term\AliasGroup;
8
use Wikibase\DataModel\Term\AliasGroupList;
9
use Wikibase\DataModel\Term\DescriptionsProvider;
10
use Wikibase\DataModel\Term\LabelsProvider;
11
use Wikibase\DataModel\Term\TermList;
12
use Wikimedia\Purtle\RdfWriter;
13
14
/**
15
 * RDF mapping for entity terms.
16
 *
17
 * @license GPL-2.0-or-later
18
 */
19
class TermsRdfBuilder implements EntityRdfBuilder {
20
21
	/**
22
	 * @var RdfVocabulary
23
	 */
24
	private $vocabulary;
25
26
	/**
27
	 * @var RdfWriter
28
	 */
29
	private $writer;
30
31
	/**
32
	 * @var string[]|null a list of desired languages, or null for all languages.
33
	 */
34
	private $languages;
35
36
	/**
37
	 * @var string[][][] Map of type to array of [ ns, local ] for each label predicate
38
	 */
39
	private $labelPredicates;
40
41
	/**
42
	 * @param RdfVocabulary $vocabulary
43
	 * @param RdfWriter $writer
44
	 * @param string[][][] $labelPredicates Map of type to array of [ ns, local ] for each label predicate
45
	 * @param string[]|null $languages
46
	 */
47
	public function __construct(
48
		RdfVocabulary $vocabulary,
49
		RdfWriter $writer,
50
		array $labelPredicates = [],
51
		array $languages = null
52
	) {
53
		$this->vocabulary = $vocabulary;
54
		$this->writer = $writer;
55
		$this->languages = $languages === null ? null : array_flip( $languages );
56
		$this->labelPredicates = $labelPredicates;
57
	}
58
59
	/**
60
	 * Get predicates that will be used for labels.
61
	 * @param EntityDocument $entity
62
	 * @return string[][] array of [ ns, local ] for each label predicate
63
	 */
64
	private function getLabelPredicates( EntityDocument $entity ) {
65
		return $this->labelPredicates[$entity->getType()] ?? [
66
				[ 'rdfs', 'label' ],
67
				[ RdfVocabulary::NS_SKOS, 'prefLabel' ],
68
				[ RdfVocabulary::NS_SCHEMA_ORG, 'name' ],
69
			];
70
	}
71
72
	/**
73
	 * Adds the labels of the given entity to the RDF graph
74
	 *
75
	 * @param string $entityNamespace
76
	 * @param string $entityLName
77
	 * @param TermList $labels
78
	 * @param string[][] $labelPredicates array of [ ns, local ] for each label predicate
79
	 */
80
	private function addLabels( $entityNamespace, $entityLName, TermList $labels, array $labelPredicates ) {
81
		if ( empty( $labelPredicates ) ) {
82
			// If we want no predicates, no need to bother with the rest.
83
			return;
84
		}
85
		foreach ( $labels->toTextArray() as $languageCode => $labelText ) {
86
			if ( $this->languages !== null && !isset( $this->languages[$languageCode] ) ) {
87
				continue;
88
			}
89
90
			$this->writer->about( $entityNamespace, $entityLName );
91
			foreach ( $labelPredicates as $predicate ) {
92
				$this->writer->say( $predicate[0], $predicate[1] )->text( $labelText, $languageCode );
93
			}
94
		}
95
	}
96
97
	/**
98
	 * Adds the descriptions of the given entity to the RDF graph.
99
	 *
100
	 * @param string $entityNamespace
101
	 * @param string $entityLName
102
	 * @param TermList $descriptions
103
	 */
104
	private function addDescriptions( $entityNamespace, $entityLName, TermList $descriptions ) {
105
		foreach ( $descriptions->toTextArray() as $languageCode => $description ) {
106
			if ( $this->languages !== null && !isset( $this->languages[$languageCode] ) ) {
107
				continue;
108
			}
109
110
			$this->writer->about( $entityNamespace, $entityLName )
111
				->say( RdfVocabulary::NS_SCHEMA_ORG, 'description' )->text( $description, $languageCode );
112
		}
113
	}
114
115
	/**
116
	 * Adds the aliases of the given entity to the RDF graph.
117
	 *
118
	 * @param string $entityNamespace
119
	 * @param string $entityLName
120
	 * @param AliasGroupList $aliases
121
	 */
122
	private function addAliases( $entityNamespace, $entityLName, AliasGroupList $aliases ) {
123
		/** @var AliasGroup $aliasGroup */
124
		foreach ( $aliases as $aliasGroup ) {
125
			$languageCode = $aliasGroup->getLanguageCode();
126
			if ( $this->languages !== null && !isset( $this->languages[$languageCode] ) ) {
127
				continue;
128
			}
129
130
			foreach ( $aliasGroup->getAliases() as $alias ) {
131
				$this->writer->about(
132
					$entityNamespace,
133
					$entityLName
134
				)->say( RdfVocabulary::NS_SKOS, 'altLabel' )->text( $alias, $languageCode );
135
			}
136
		}
137
	}
138
139
	/**
140
	 * Add the entity's labels, descriptions, and aliases to the RDF graph.
141
	 *
142
	 * @see EntityRdfBuilder::addEntity
143
	 *
144
	 * @param EntityDocument $entity the entity to output.
145
	 * @suppress PhanTypeMismatchArgument
146
	 */
147
	public function addEntity( EntityDocument $entity ) {
148
		$entityId = $entity->getId();
149
		$entityLName = $this->vocabulary->getEntityLName( $entityId );
0 ignored issues
show
Bug introduced by
It seems like $entityId defined by $entity->getId() on line 148 can be null; however, Wikibase\Repo\Rdf\RdfVocabulary::getEntityLName() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
150
		$entityRepoName = $this->vocabulary->getEntityRepositoryName( $entityId );
0 ignored issues
show
Bug introduced by
It seems like $entityId defined by $entity->getId() on line 148 can be null; however, Wikibase\Repo\Rdf\RdfVoc...tEntityRepositoryName() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
151
		$entityNamespace = $this->vocabulary->entityNamespaceNames[$entityRepoName];
152
153
		if ( $entity instanceof LabelsProvider ) {
154
			$this->addLabels( $entityNamespace, $entityLName, $entity->getLabels(),
155
				$this->getLabelPredicates( $entity ) );
156
		}
157
158
		if ( $entity instanceof DescriptionsProvider ) {
159
			$this->addDescriptions( $entityNamespace, $entityLName, $entity->getDescriptions() );
160
		}
161
162
		if ( $entity instanceof AliasesProvider ) {
163
			$this->addAliases( $entityNamespace, $entityLName, $entity->getAliasGroups() );
164
		}
165
	}
166
167
	/**
168
	 * Add the entity's labels and descriptions to the RDF graph.
169
	 *
170
	 * @see EntityRdfBuilder::addEntityStub
171
	 *
172
	 * @param EntityDocument $entity the entity to output.
173
	 * @suppress PhanTypeMismatchArgument
174
	 */
175
	public function addEntityStub( EntityDocument $entity ) {
176
		$entityId = $entity->getId();
177
		$entityLName = $this->vocabulary->getEntityLName( $entityId );
0 ignored issues
show
Bug introduced by
It seems like $entityId defined by $entity->getId() on line 176 can be null; however, Wikibase\Repo\Rdf\RdfVocabulary::getEntityLName() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
178
		$entityRepoName = $this->vocabulary->getEntityRepositoryName( $entityId );
0 ignored issues
show
Bug introduced by
It seems like $entityId defined by $entity->getId() on line 176 can be null; however, Wikibase\Repo\Rdf\RdfVoc...tEntityRepositoryName() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
179
		$entityNamespace = $this->vocabulary->entityNamespaceNames[$entityRepoName];
180
181
		if ( $entity instanceof LabelsProvider ) {
182
			$this->addLabels( $entityNamespace, $entityLName, $entity->getLabels(),
183
				$this->getLabelPredicates( $entity ) );
184
		}
185
186
		if ( $entity instanceof DescriptionsProvider ) {
187
			$this->addDescriptions( $entityNamespace, $entityLName, $entity->getDescriptions() );
188
		}
189
	}
190
191
}
192