1 | <?php |
||
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 ) { |
|
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() { |
|
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() { |
|
109 | |||
110 | /** |
||
111 | * @param int $position |
||
112 | * @throws DumpReadingException |
||
113 | */ |
||
114 | 6 | public function seekToPosition( $position ) { |
|
122 | |||
123 | } |
||
124 |