1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\IO; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityIdParsingException; |
8
|
|
|
use Wikibase\DataModel\Services\EntityId\EntityIdPager; |
9
|
|
|
use Wikibase\Lib\Reporting\ExceptionHandler; |
10
|
|
|
use Wikibase\Lib\Reporting\RethrowingExceptionHandler; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* EntityIdReader reads entity IDs from a file, one per line. |
14
|
|
|
* |
15
|
|
|
* @license GPL-2.0-or-later |
16
|
|
|
* @author Daniel Kinzler |
17
|
|
|
*/ |
18
|
|
|
class EntityIdReader implements EntityIdPager { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var LineReader |
22
|
|
|
*/ |
23
|
|
|
private $reader; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var EntityIdParser |
27
|
|
|
*/ |
28
|
|
|
private $parser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
private $entityType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ExceptionHandler |
37
|
|
|
*/ |
38
|
|
|
private $exceptionHandler; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param LineReader $reader |
42
|
|
|
* @param EntityIdParser $parser |
43
|
|
|
* @param null|string $entityType The desired entity type, or null for any type. |
44
|
|
|
*/ |
45
|
|
|
public function __construct( LineReader $reader, EntityIdParser $parser, $entityType = null ) { |
46
|
|
|
$this->reader = $reader; |
47
|
|
|
$this->parser = $parser; |
48
|
|
|
$this->entityType = $entityType; |
49
|
|
|
|
50
|
|
|
$this->exceptionHandler = new RethrowingExceptionHandler(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setExceptionHandler( ExceptionHandler $exceptionHandler ) { |
54
|
|
|
$this->exceptionHandler = $exceptionHandler; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return ExceptionHandler |
59
|
|
|
*/ |
60
|
|
|
public function getExceptionHandler() { |
61
|
|
|
return $this->exceptionHandler; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $line |
66
|
|
|
* @return EntityId|null |
67
|
|
|
*/ |
68
|
|
|
protected function lineToId( $line ) { |
69
|
|
|
$line = trim( $line ); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$id = $this->parser->parse( $line ); |
73
|
|
|
} catch ( EntityIdParsingException $ex ) { |
74
|
|
|
$this->exceptionHandler->handleException( $ex, 'bad-entity-id', "Failed to parse Entity ID $line" ); |
75
|
|
|
$id = null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Closes the underlying input stream |
83
|
|
|
*/ |
84
|
|
|
public function dispose() { |
85
|
|
|
$this->reader->dispose(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the next ID (or null if there are no more ids). |
90
|
|
|
* |
91
|
|
|
* @return EntityId|null |
92
|
|
|
*/ |
93
|
|
|
protected function next() { |
94
|
|
|
$id = null; |
95
|
|
|
|
96
|
|
|
while ( $id === null ) { |
97
|
|
|
$this->reader->next(); |
98
|
|
|
|
99
|
|
|
if ( !$this->reader->valid() ) { |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$line = trim( $this->reader->current() ); |
104
|
|
|
|
105
|
|
|
if ( $line === '' ) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$id = $this->lineToId( $line ); |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ( !$id ) { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( $this->entityType !== null && $id->getEntityType() !== $this->entityType ) { |
116
|
|
|
$id = null; |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $id; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Fetches the next batch of IDs. Calling this has the side effect of advancing the |
126
|
|
|
* internal state of the page, typically implemented by some underlying resource |
127
|
|
|
* such as a file pointer or a database connection. |
128
|
|
|
* |
129
|
|
|
* @note After some finite number of calls, this method should eventually return |
130
|
|
|
* an empty list of IDs, indicating that no more IDs are available. |
131
|
|
|
* |
132
|
|
|
* @param int $limit The maximum number of IDs to return. |
133
|
|
|
* |
134
|
|
|
* @return EntityId[] A list of EntityIds matching the given parameters. Will |
135
|
|
|
* be empty if there are no more entities to list from the given offset. |
136
|
|
|
*/ |
137
|
|
|
public function fetchIds( $limit ) { |
138
|
|
|
$ids = []; |
139
|
|
|
while ( $limit > 0 ) { |
140
|
|
|
$id = $this->next(); |
141
|
|
|
|
142
|
|
|
if ( $id === null ) { |
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$ids[] = $id; |
147
|
|
|
$limit--; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $ids; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.