|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Rdf; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
8
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
9
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
|
10
|
|
|
use Wikibase\DataModel\Statement\StatementListProvider; |
|
11
|
|
|
use Wikimedia\Purtle\RdfWriter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* "Truthy" RDF mapping for wikibase statements, directly mapping properties to "best" values |
|
15
|
|
|
* without modelling statements as identifiable objects. "Best" statements per property are |
|
16
|
|
|
* statements that have the best non-deprecated rank. |
|
17
|
|
|
* |
|
18
|
|
|
* This simple property to value mapping excludes deprecated and non-"best" statements, ranks, |
|
19
|
|
|
* qualifiers, and references. This allows for a much simpler, much easier to query RDF structure |
|
20
|
|
|
* that allows searching for values similar to what would have been shown in infoboxes via Lua. |
|
21
|
|
|
* |
|
22
|
|
|
* If more information is needed, use FullStatementRdfBuilder instead. |
|
23
|
|
|
* |
|
24
|
|
|
* @see FullStatementRdfBuilder |
|
25
|
|
|
* |
|
26
|
|
|
* @license GPL-2.0-or-later |
|
27
|
|
|
* @author Daniel Kinzler |
|
28
|
|
|
* @author Stas Malyshev |
|
29
|
|
|
*/ |
|
30
|
|
|
class TruthyStatementRdfBuilder implements EntityRdfBuilder { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var RdfVocabulary |
|
34
|
|
|
*/ |
|
35
|
|
|
private $vocabulary; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var RdfWriter |
|
39
|
|
|
*/ |
|
40
|
|
|
private $writer; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var SnakRdfBuilder |
|
44
|
|
|
*/ |
|
45
|
|
|
private $snakBuilder; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct( RdfVocabulary $vocabulary, RdfWriter $writer, SnakRdfBuilder $snakBuilder ) { |
|
48
|
|
|
$this->vocabulary = $vocabulary; |
|
49
|
|
|
$this->writer = $writer; |
|
50
|
|
|
$this->snakBuilder = $snakBuilder; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Adds Statements to the RDF graph. |
|
55
|
|
|
* |
|
56
|
|
|
* @param EntityId $entityId |
|
57
|
|
|
* @param StatementList $statementList |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addStatements( EntityId $entityId, StatementList $statementList ) { |
|
60
|
|
|
// FIXME: getBestStatementPerProperty() uis expensive, share the result with FullStatementRdfBuilder! |
|
61
|
|
|
foreach ( $statementList->getPropertyIds() as $propertyId ) { |
|
62
|
|
|
foreach ( $statementList->getByPropertyId( $propertyId )->getBestStatements() as $statement ) { |
|
63
|
|
|
$this->addMainSnak( $entityId, $statement ); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Adds the given Statement's main Snak to the RDF graph. |
|
70
|
|
|
* |
|
71
|
|
|
* @todo share more of this code with FullStatementRdfBuilder |
|
72
|
|
|
* |
|
73
|
|
|
* @param EntityId $entityId |
|
74
|
|
|
* @param Statement $statement |
|
75
|
|
|
* |
|
76
|
|
|
* @throws InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
|
|
private function addMainSnak( EntityId $entityId, Statement $statement ) { |
|
79
|
|
|
$snak = $statement->getMainSnak(); |
|
80
|
|
|
|
|
81
|
|
|
$entityLName = $this->vocabulary->getEntityLName( $entityId ); |
|
82
|
|
|
$entityRepoName = $this->vocabulary->getEntityRepositoryName( $entityId ); |
|
83
|
|
|
|
|
84
|
|
|
$snakNamespace = $this->vocabulary->statementNamespaceNames[$entityRepoName][RdfVocabulary::NS_VALUE]; |
|
85
|
|
|
|
|
86
|
|
|
$this->writer->about( $this->vocabulary->entityNamespaceNames[$entityRepoName], $entityLName ); |
|
87
|
|
|
|
|
88
|
|
|
$propertyRepoName = $this->vocabulary->getEntityRepositoryName( $snak->getPropertyId() ); |
|
89
|
|
|
$this->snakBuilder->addSnak( |
|
90
|
|
|
$this->writer, |
|
91
|
|
|
$snakNamespace, |
|
92
|
|
|
$snak, |
|
93
|
|
|
$this->vocabulary->propertyNamespaceNames[$propertyRepoName][RdfVocabulary::NSP_DIRECT_CLAIM], |
|
94
|
|
|
$this->vocabulary->getStatementLName( $statement ) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add truthy statements for the given entity to the RDF graph. |
|
100
|
|
|
* |
|
101
|
|
|
* @param EntityDocument $entity the entity to output. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function addEntity( EntityDocument $entity ) { |
|
104
|
|
|
$entityId = $entity->getId(); |
|
105
|
|
|
|
|
106
|
|
|
if ( $entity instanceof StatementListProvider ) { |
|
107
|
|
|
$this->addStatements( $entityId, $entity->getStatements() ); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Does nothing, since Statements should not be part of entity stubs. |
|
113
|
|
|
* |
|
114
|
|
|
* @see EntityRdfBuilder::addEntityStub |
|
115
|
|
|
* |
|
116
|
|
|
* @param EntityDocument $entity the entity to output. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function addEntityStub( EntityDocument $entity ) { |
|
119
|
|
|
// noop |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
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: