|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
6
|
|
|
use Wikibase\EntityStore\EntityDocumentLookup; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Internal class |
|
10
|
|
|
* |
|
11
|
|
|
* @licence GPLv2+ |
|
12
|
|
|
* @author Thomas Pellissier Tanon |
|
13
|
|
|
*/ |
|
14
|
|
|
class CachedEntityDocumentLookup implements EntityDocumentLookup { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var EntityDocumentLookup |
|
18
|
|
|
*/ |
|
19
|
|
|
private $entityLookup; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EntityDocumentCache |
|
23
|
|
|
*/ |
|
24
|
|
|
private $entityCache; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param EntityDocumentLookup $entityLookup |
|
28
|
|
|
* @param EntityDocumentCache $entityCache |
|
29
|
|
|
*/ |
|
30
|
7 |
|
public function __construct( EntityDocumentLookup $entityLookup, EntityDocumentCache $entityCache ) { |
|
31
|
7 |
|
$this->entityLookup = $entityLookup; |
|
32
|
7 |
|
$this->entityCache = $entityCache; |
|
33
|
7 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @see EntityDocumentLookup::getEntityDocumentForId |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function getEntityDocumentForId( EntityId $entityId ) { |
|
39
|
3 |
|
$entity = $this->entityCache->fetch( $entityId ); |
|
40
|
|
|
|
|
41
|
3 |
|
if( $entity === null ) { |
|
42
|
2 |
|
$entity = $this->entityLookup->getEntityDocumentForId( $entityId ); |
|
43
|
2 |
|
if( $entity !== null ) { |
|
44
|
1 |
|
$this->entityCache->save( $entity ); |
|
45
|
1 |
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
return $entity; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @see EntityDocumentLookup::getEntityDocumentsForIds |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function getEntityDocumentsForIds( array $entityIds ) { |
|
55
|
4 |
|
$entities = []; |
|
56
|
4 |
|
$entityIdsToRetrieve = []; |
|
57
|
|
|
|
|
58
|
4 |
|
foreach( $entityIds as $entityId ) { |
|
59
|
4 |
|
$entity = $this->entityCache->fetch( $entityId ); |
|
60
|
|
|
|
|
61
|
4 |
|
if( $entity === null ) { |
|
62
|
3 |
|
$entityIdsToRetrieve[] = $entityId; |
|
63
|
3 |
|
} else { |
|
64
|
2 |
|
$entities[] = $entity; |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
$additionalEntities = []; |
|
69
|
4 |
|
if( !empty( $entityIdsToRetrieve ) ) { |
|
70
|
3 |
|
$additionalEntities = $this->entityLookup->getEntityDocumentsForIds( $entityIdsToRetrieve ); |
|
71
|
3 |
|
} |
|
72
|
4 |
|
foreach( $additionalEntities as $entity ) { |
|
73
|
1 |
|
$this->entityCache->save( $entity ); |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
return array_merge( $entities, $additionalEntities ); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|