1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Wikibase\JsonDumpReader\Reader; |
6
|
|
|
|
7
|
|
|
use Wikibase\JsonDumpReader\DumpReadingException; |
8
|
|
|
use Wikibase\JsonDumpReader\SeekableDumpReader; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Package private |
12
|
|
|
* Was public in 1.0.x and 1.1.x. No breaking changes before 2.0. |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class ExtractedDumpReader implements SeekableDumpReader { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $dumpFile; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $initialPosition; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var resource |
31
|
|
|
*/ |
32
|
|
|
private $handle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $dumpFilePath |
36
|
|
|
* @param int $initialPosition |
37
|
|
|
*/ |
38
|
6 |
|
public function __construct( $dumpFilePath, $initialPosition = 0 ) { |
39
|
6 |
|
$this->dumpFile = $dumpFilePath; |
40
|
6 |
|
$this->initialPosition = $initialPosition; |
41
|
6 |
|
} |
42
|
|
|
|
43
|
6 |
|
private function initReader() { |
44
|
6 |
|
if ( $this->handle === null ) { |
45
|
6 |
|
$this->handle = @fopen( $this->dumpFile, 'r' ); |
46
|
|
|
|
47
|
6 |
|
if ( !is_resource( $this->handle ) ) { |
48
|
|
|
throw new DumpReadingException( 'Could not open file: ' . $this->dumpFile ); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
$this->seekToPosition( $this->initialPosition ); |
52
|
|
|
} |
53
|
6 |
|
} |
54
|
|
|
|
55
|
6 |
|
public function __destruct() { |
56
|
6 |
|
$this->closeReader(); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
6 |
|
private function closeReader() { |
60
|
6 |
|
if ( is_resource( $this->handle ) ) { |
61
|
6 |
|
fclose( $this->handle ); |
62
|
|
|
} |
63
|
6 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function rewind(): void { |
66
|
1 |
|
if ( $this->handle !== null ) { |
67
|
1 |
|
fseek( $this->handle, $this->initialPosition ); |
68
|
|
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string|null |
73
|
|
|
* @throws DumpReadingException |
74
|
|
|
*/ |
75
|
6 |
|
public function nextJsonLine(): ?string { |
76
|
6 |
|
$this->initReader(); |
77
|
|
|
|
78
|
6 |
|
while ( true ) { |
79
|
6 |
|
$line = fgets( $this->handle ); |
80
|
|
|
|
81
|
6 |
|
if ( $line === false ) { |
82
|
5 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
if ( $line{0} === '{' ) { |
86
|
5 |
|
return rtrim( $line, ",\n\r" ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
* @throws DumpReadingException |
96
|
|
|
*/ |
97
|
1 |
|
public function getPosition(): int { |
98
|
1 |
|
if ( PHP_INT_SIZE < 8 ) { |
99
|
|
|
throw new DumpReadingException( 'Cannot reliably get the file position on 32bit PHP' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$this->initReader(); |
103
|
1 |
|
$position = @ftell( $this->handle ); |
104
|
|
|
|
105
|
1 |
|
if ( !is_int( $position ) ) { |
106
|
|
|
throw new DumpReadingException( 'Could not tell the position of the file handle' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $position; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param int $position |
114
|
|
|
* @throws DumpReadingException |
115
|
|
|
*/ |
116
|
6 |
|
public function seekToPosition( int $position ): void { |
117
|
6 |
|
$this->initReader(); |
118
|
6 |
|
$seekResult = @fseek( $this->handle, $position ); |
119
|
|
|
|
120
|
6 |
|
if ( $seekResult !== 0 ) { |
121
|
|
|
throw new DumpReadingException( 'Seeking to position failed' ); |
122
|
|
|
} |
123
|
6 |
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|