1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\Replicator\EntitySource; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
7
|
|
|
use Iterator; |
8
|
|
|
use Queryr\Replicator\Model\EntityPage; |
9
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
*/ |
16
|
|
|
class ReferencedEntityPageIterator implements Iterator { |
17
|
|
|
|
18
|
|
|
private $innerIterator; |
19
|
|
|
private $fetcher; |
20
|
|
|
private $entityDeserializer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ReferencedEntitiesFinder |
24
|
|
|
*/ |
25
|
|
|
private $referencedEntitiesFinder; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $extraEntitiesToFetch = []; |
31
|
|
|
|
32
|
|
|
public function __construct( Iterator $entityPageIterator, BatchingEntityPageFetcher $fetcher, |
33
|
|
|
Deserializer $entityDeserializer ) { |
34
|
|
|
|
35
|
|
|
$this->innerIterator = $entityPageIterator; |
36
|
|
|
$this->fetcher = $fetcher; |
37
|
|
|
$this->entityDeserializer = $entityDeserializer; |
38
|
|
|
|
39
|
|
|
$this->referencedEntitiesFinder = new ReferencedEntitiesFinder(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return EntityPage|null |
44
|
|
|
*/ |
45
|
|
|
public function current() { |
46
|
|
|
/** |
47
|
|
|
* @var EntityPage|null $page |
48
|
|
|
*/ |
49
|
|
|
$page = $this->innerIterator->current(); |
50
|
|
|
|
51
|
|
|
if ( $page !== null ) { |
52
|
|
|
$this->addEntitiesReferencedByPage( $page ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $page; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function addEntitiesReferencedByPage( EntityPage $page ) { |
59
|
|
|
try { |
60
|
|
|
/** |
61
|
|
|
* @var EntityDocument $entity |
62
|
|
|
*/ |
63
|
|
|
$entity = $this->entityDeserializer->deserialize( @json_decode( $page->getEntityJson(), true ) ); |
64
|
|
|
} |
65
|
|
|
catch ( DeserializationException $ex ) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( !$this->entityWasFetchedDueToReference( $entity->getId() ) ) { |
|
|
|
|
70
|
|
|
$this->addEntitiesReferencedByEntity( $entity ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function entityWasFetchedDueToReference( EntityId $entityId ) { |
75
|
|
|
return in_array( $entityId->getSerialization(), $this->extraEntitiesToFetch ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function addEntitiesReferencedByEntity( EntityDocument $entity ) { |
79
|
|
|
if ( $entity->getType() !== 'item' ) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$referencedEntities = $this->referencedEntitiesFinder->findForItem( $entity ); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$extraEntitiesToFetch = []; |
86
|
|
|
|
87
|
|
|
foreach ( $referencedEntities as $referencedEntityId ) { |
88
|
|
|
$extraEntitiesToFetch[] = $referencedEntityId->getSerialization(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->extraEntitiesToFetch = array_merge( $this->extraEntitiesToFetch, $extraEntitiesToFetch ); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->fetcher->addPagesToFetch( $extraEntitiesToFetch ); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function next() { |
98
|
|
|
$this->innerIterator->next(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function key() { |
102
|
|
|
return $this->innerIterator->key(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function valid() { |
106
|
|
|
return $this->innerIterator->valid(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function rewind() { |
110
|
|
|
$this->next(); |
111
|
|
|
$this->extraEntitiesToFetch = []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
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: