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