1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore; |
4
|
|
|
|
5
|
|
|
use BatchingIterator\BatchingFetcher; |
6
|
|
|
use Wikibase\DataModel\Entity\Entity; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
8
|
|
|
use Wikibase\Lib\Store\EntityLookup; |
9
|
|
|
use Wikibase\Lib\Store\StorageException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @since 0.1 |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
*/ |
16
|
|
|
class BatchingEntityFetcher implements BatchingFetcher { |
17
|
|
|
|
18
|
|
|
private $entityIdFetcher; |
19
|
|
|
private $entityLookup; |
20
|
|
|
private $onEntitySkipped; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param BatchingFetcher $entityIdFetcher |
24
|
|
|
* @param EntityLookup $entityLookup |
25
|
|
|
* @param callable|null $onEntitySkipped with two parameters, EntityId $entityId and string $reasonMessage |
26
|
|
|
*/ |
27
|
|
|
public function __construct( BatchingFetcher $entityIdFetcher, EntityLookup $entityLookup, $onEntitySkipped = null ) { |
28
|
|
|
$this->entityIdFetcher = $entityIdFetcher; |
29
|
|
|
$this->entityLookup = $entityLookup; |
30
|
|
|
$this->onEntitySkipped = $onEntitySkipped; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @see BatchingFetcher::fetchNext |
35
|
|
|
* |
36
|
|
|
* @param int $maxFetchCount |
37
|
|
|
* |
38
|
|
|
* @return Entity[] |
39
|
|
|
*/ |
40
|
|
|
public function fetchNext( $maxFetchCount ) { |
41
|
|
|
do { |
42
|
|
|
$entitiesToFetch = $this->entityIdFetcher->fetchNext( $maxFetchCount ); |
43
|
|
|
$entities = $this->retrieveEntities( $entitiesToFetch ); |
44
|
|
|
} |
45
|
|
|
while ( !empty( $entitiesToFetch ) && empty( $entities ) ); |
46
|
|
|
|
47
|
|
|
return $entities; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function retrieveEntities( array $entityIds ) { |
51
|
|
|
$entities = array(); |
52
|
|
|
|
53
|
|
|
foreach ( $entityIds as $entityId ) { |
54
|
|
|
$entity = $this->retrieveEntity( $entityId ); |
55
|
|
|
|
56
|
|
|
if ( $entity !== null ) { |
57
|
|
|
$entities[] = $entity; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $entities; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function retrieveEntity( EntityId $entityId ) { |
65
|
|
|
try { |
66
|
|
|
$entity = $this->entityLookup->getEntity( $entityId ); |
67
|
|
|
} |
68
|
|
|
catch ( StorageException $ex ) { |
69
|
|
|
$this->reportSkippedEntity( $entityId, $ex->getMessage() ); |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ( $entity === null ) { |
74
|
|
|
$this->reportSkippedEntity( $entityId, 'No such entity in the store' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $entity; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function reportSkippedEntity( EntityId $entityId, $reasonMessage ) { |
81
|
|
|
if ( $this->onEntitySkipped !== null ) { |
82
|
|
|
call_user_func( $this->onEntitySkipped, $entityId, $reasonMessage ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @see BatchingFetcher::rewind |
88
|
|
|
*/ |
89
|
|
|
public function rewind() { |
90
|
|
|
$this->entityIdFetcher->rewind(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|