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