1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\MongoDB; |
4
|
|
|
|
5
|
|
|
use Doctrine\MongoDB\Database; |
6
|
|
|
use Doctrine\MongoDB\Query\Expr; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
9
|
|
|
use Wikibase\EntityStore\EntityDocumentLookup; |
10
|
|
|
use Wikibase\EntityStore\EntityDocumentSaver; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Internal class |
14
|
|
|
* |
15
|
|
|
* @licence GPLv2+ |
16
|
|
|
* @author Thomas Pellissier Tanon |
17
|
|
|
*/ |
18
|
|
|
class MongoDBEntityDatabase implements EntityDocumentLookup, EntityDocumentSaver { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Database |
22
|
|
|
*/ |
23
|
|
|
private $database; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var MongoDBDocumentBuilder |
27
|
|
|
*/ |
28
|
|
|
private $documentBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Database $database |
32
|
|
|
* @param MongoDBDocumentBuilder $documentBuilder |
33
|
|
|
*/ |
34
|
5 |
|
public function __construct( Database $database, MongoDBDocumentBuilder $documentBuilder ) { |
35
|
5 |
|
$this->database = $database; |
36
|
5 |
|
$this->documentBuilder = $documentBuilder; |
37
|
5 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @see EntityDocumentLookup::getEntityDocumentForId |
41
|
|
|
*/ |
42
|
3 |
|
public function getEntityDocumentForId( EntityId $entityId ) { |
43
|
3 |
|
$document = $this->database |
44
|
3 |
|
->selectCollection( $entityId->getEntityType() ) |
45
|
3 |
|
->findOne( $this->buildGetEntityForIdQuery( $entityId ) ); |
|
|
|
|
46
|
|
|
|
47
|
3 |
|
return ( $document === null ) ? null : $this->documentBuilder->buildEntityForDocument( $document ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see EntityDocumentLookup::getEntityDocumentsForIds |
52
|
|
|
*/ |
53
|
2 |
|
public function getEntityDocumentsForIds( array $entityIds ) { |
54
|
2 |
|
$entities = []; |
55
|
|
|
|
56
|
2 |
|
foreach( $this->splitEntityIdsPerType( $entityIds ) as $type => $entityIdsForType ) { |
57
|
2 |
|
$entities = array_merge( |
58
|
2 |
|
$entities, |
59
|
2 |
|
$this->getEntitiesInCollection( $entityIdsForType, $type ) |
60
|
2 |
|
); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
2 |
|
return $entities; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
private function splitEntityIdsPerType( array $entityIds ) { |
67
|
2 |
|
$entityIdsPerType = []; |
68
|
|
|
|
69
|
|
|
/** @var EntityId $entityId */ |
70
|
2 |
|
foreach( $entityIds as $entityId ) { |
71
|
2 |
|
$entityIdsPerType[$entityId->getEntityType()][] = $entityId; |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
return $entityIdsPerType; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
private function getEntitiesInCollection( array $entityIds, $collectionName ) { |
78
|
2 |
|
$documents = $this->database |
79
|
2 |
|
->selectCollection( $collectionName ) |
80
|
2 |
|
->find( $this->buildGetEntitiesForIdsQuery( $entityIds ) ); |
|
|
|
|
81
|
|
|
|
82
|
2 |
|
$entities = []; |
83
|
2 |
|
foreach( $documents as $document ) { |
84
|
1 |
|
$entities[] = $this->documentBuilder->buildEntityForDocument( $document ); |
85
|
2 |
|
} |
86
|
2 |
|
return $entities; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see EntityDocumentSaver::saveEntityDocument |
91
|
|
|
*/ |
92
|
2 |
|
public function saveEntityDocument( EntityDocument $entityDocument ) { |
93
|
2 |
|
$this->database->selectCollection( $entityDocument->getType() )->upsert( |
94
|
2 |
|
$this->buildGetEntityForIdQuery( $entityDocument->getId() ), |
|
|
|
|
95
|
2 |
|
$this->documentBuilder->buildDocumentForEntity( $entityDocument ) |
96
|
2 |
|
); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
4 |
|
private function buildGetEntityForIdQuery( EntityId $entityId ) { |
100
|
4 |
|
$expr = new Expr(); |
101
|
4 |
|
return $expr->field( '_id' )->equals( $entityId->getSerialization() )->getQuery(); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
private function buildGetEntitiesForIdsQuery( array $entityIds ) { |
105
|
2 |
|
$expr = new Expr(); |
106
|
2 |
|
return $expr->field( '_id' )->in( $this->serializeEntityIds( $entityIds ) )->getQuery(); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
private function serializeEntityIds( array $entityIds ) { |
110
|
2 |
|
$serializations = []; |
111
|
|
|
|
112
|
|
|
/** @var EntityId $entityId */ |
113
|
2 |
|
foreach( $entityIds as $entityId ) { |
114
|
2 |
|
$serializations[] = $entityId->getSerialization(); |
115
|
2 |
|
} |
116
|
|
|
|
117
|
2 |
|
return $serializations; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: