|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* php-gedcom. |
|
4
|
|
|
* |
|
5
|
|
|
* php-gedcom is a library for parsing, manipulating, importing and exporting |
|
6
|
|
|
* GEDCOM 5.5 files in PHP 5.3+. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Kristopher Wilson <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) 2010-2013, Kristopher Wilson |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://github.com/mrkrstphr/php-gedcom |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace PhpGedcom; |
|
16
|
|
|
|
|
17
|
|
|
class Parser |
|
18
|
|
|
{ |
|
19
|
|
|
protected $_file = null; |
|
20
|
|
|
|
|
21
|
|
|
protected $_gedcom = null; |
|
22
|
|
|
|
|
23
|
|
|
protected $_errorLog = []; |
|
24
|
|
|
|
|
25
|
|
|
protected $_linesParsed = 0; |
|
26
|
|
|
|
|
27
|
|
|
protected $_line = ''; |
|
28
|
|
|
|
|
29
|
|
|
protected $_lineRecord = null; |
|
30
|
|
|
|
|
31
|
|
|
protected $_linePieces = 0; |
|
32
|
|
|
|
|
33
|
|
|
protected $_returnedLine = ''; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(\PhpGedcom\Gedcom $gedcom = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!is_null($gedcom)) { |
|
38
|
|
|
$this->_gedcom = $gedcom; |
|
39
|
|
|
} else { |
|
40
|
|
|
$this->_gedcom = new \PhpGedcom\Gedcom(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function forward() |
|
45
|
|
|
{ |
|
46
|
|
|
// if there was a returned line by back(), set that as our current |
|
47
|
|
|
// line and blank out the returnedLine variable, otherwise grab |
|
48
|
|
|
// the next line from the file |
|
49
|
|
|
|
|
50
|
|
|
if (!empty($this->_returnedLine)) { |
|
51
|
|
|
$this->_line = $this->_returnedLine; |
|
52
|
|
|
$this->_returnedLine = ''; |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->_line = fgets($this->_file); |
|
55
|
|
|
$this->_lineRecord = null; |
|
56
|
|
|
$this->_linesParsed++; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function back() |
|
63
|
|
|
{ |
|
64
|
|
|
// our parser object encountered a line it wasn't meant to parse |
|
65
|
|
|
// store this line for the previous parser to analyze |
|
66
|
|
|
|
|
67
|
|
|
$this->_returnedLine = $this->_line; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Jump to the next level in the GEDCOM that is <= $level. This will leave the parser at the line above |
|
74
|
|
|
* this level, such that calling $parser->forward() will result in landing at the correct level. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $level |
|
77
|
|
|
*/ |
|
78
|
|
|
public function skipToNextLevel($level) |
|
79
|
|
|
{ |
|
80
|
|
|
$currentDepth = 999; |
|
81
|
|
|
|
|
82
|
|
|
while ($currentDepth > $level) { |
|
83
|
|
|
$this->forward(); |
|
84
|
|
|
$record = $this->getCurrentLineRecord(); |
|
85
|
|
|
$currentDepth = (int) $record[0]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->back(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getGedcom() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->_gedcom; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function eof() |
|
97
|
|
|
{ |
|
98
|
|
|
return feof($this->_file); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function parseMultiLineRecord() |
|
105
|
|
|
{ |
|
106
|
|
|
$record = $this->getCurrentLineRecord(); |
|
107
|
|
|
|
|
108
|
|
|
$depth = (int) $record[0]; |
|
109
|
|
|
$data = isset($record[2]) ? trim($record[2]) : ''; |
|
110
|
|
|
|
|
111
|
|
|
$this->forward(); |
|
112
|
|
|
|
|
113
|
|
|
while (!$this->eof()) { |
|
114
|
|
|
$record = $this->getCurrentLineRecord(); |
|
115
|
|
|
$recordType = strtoupper(trim($record[1])); |
|
116
|
|
|
$currentDepth = (int) $record[0]; |
|
117
|
|
|
|
|
118
|
|
|
if ($currentDepth <= $depth) { |
|
119
|
|
|
$this->back(); |
|
120
|
|
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
switch ($recordType) { |
|
124
|
|
|
case 'CONT': |
|
125
|
|
|
$data .= "\n"; |
|
126
|
|
|
|
|
127
|
|
|
if (isset($record[2])) { |
|
128
|
|
|
$data .= trim($record[2]); |
|
129
|
|
|
} |
|
130
|
|
|
break; |
|
131
|
|
|
case 'CONC': |
|
132
|
|
|
if (isset($record[2])) { |
|
133
|
|
|
$data .= ' '.trim($record[2]); |
|
134
|
|
|
} |
|
135
|
|
|
break; |
|
136
|
|
|
default: |
|
137
|
|
|
$this->back(); |
|
138
|
|
|
break 2; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->forward(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $data; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string The current line |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getCurrentLine() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->_line; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getCurrentLineRecord($pieces = 3) |
|
156
|
|
|
{ |
|
157
|
|
|
if (!is_null($this->_lineRecord)) { |
|
158
|
|
|
if ($this->_linePieces == $pieces) { |
|
159
|
|
|
return $this->_lineRecord; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (empty($this->_line)) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$line = trim($this->_line); |
|
168
|
|
|
|
|
169
|
|
|
$this->_lineRecord = explode(' ', $line, $pieces); |
|
170
|
|
|
$this->_linePieces = $pieces; |
|
171
|
|
|
|
|
172
|
|
|
return $this->_lineRecord; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
protected function logError($error) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->_errorLog[] = $error; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function logUnhandledRecord($additionalInfo = '') |
|
181
|
|
|
{ |
|
182
|
|
|
$this->logError( |
|
183
|
|
|
$this->_linesParsed.': (Unhandled) '.trim(implode('|', $this->getCurrentLineRecord())). |
|
|
|
|
|
|
184
|
|
|
(!empty($additionalInfo) ? ' - '.$additionalInfo : '') |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function logSkippedRecord($additionalInfo = '') |
|
189
|
|
|
{ |
|
190
|
|
|
$this->logError( |
|
191
|
|
|
$this->_linesParsed.': (Skipping) '.trim(implode('|', $this->getCurrentLineRecord())). |
|
|
|
|
|
|
192
|
|
|
(!empty($additionalInfo) ? ' - '.$additionalInfo : '') |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function getErrors() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->_errorLog; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function normalizeIdentifier($identifier) |
|
202
|
|
|
{ |
|
203
|
|
|
$identifier = trim($identifier); |
|
204
|
|
|
$identifier = trim($identifier, '@'); |
|
205
|
|
|
|
|
206
|
|
|
return $identifier; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $fileName |
|
211
|
|
|
* |
|
212
|
|
|
* @return Gedcom |
|
213
|
|
|
*/ |
|
214
|
|
|
public function parse($fileName) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->_file = fopen($fileName, 'r'); //explode("\n", mb_convert_encoding($contents, 'UTF-8')); |
|
217
|
|
|
|
|
218
|
|
|
if (!$this->_file) { |
|
219
|
|
|
return null; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$this->forward(); |
|
223
|
|
|
|
|
224
|
|
|
while (!$this->eof()) { |
|
225
|
|
|
$record = $this->getCurrentLineRecord(); |
|
226
|
|
|
|
|
227
|
|
|
if ($record === false) { |
|
228
|
|
|
continue; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$depth = (int) $record[0]; |
|
232
|
|
|
|
|
233
|
|
|
// We only process 0 level records here. Sub levels are processed |
|
234
|
|
|
// in methods for those data types (individuals, sources, etc) |
|
235
|
|
|
|
|
236
|
|
|
if ($depth == 0) { |
|
237
|
|
|
// Although not always an identifier (HEAD,TRLR): |
|
238
|
|
|
if (isset($record[1])) { |
|
239
|
|
|
$identifier = $this->normalizeIdentifier($record[1]); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if (isset($record[1]) && trim($record[1]) == 'HEAD') { |
|
243
|
|
|
Parser\Head::parse($this); |
|
244
|
|
|
} elseif (isset($record[2]) && trim($record[2]) == 'SUBN') { |
|
245
|
|
|
Parser\Subn::parse($this); |
|
246
|
|
|
} elseif (isset($record[2]) && trim($record[2]) == 'SUBM') { |
|
247
|
|
|
Parser\Subm::parse($this); |
|
248
|
|
|
} elseif (isset($record[2]) && $record[2] == 'SOUR') { |
|
249
|
|
|
Parser\Sour::parse($this); |
|
250
|
|
|
} elseif (isset($record[2]) && $record[2] == 'INDI') { |
|
251
|
|
|
Parser\Indi::parse($this); |
|
252
|
|
|
} elseif (isset($record[2]) && $record[2] == 'FAM') { |
|
253
|
|
|
Parser\Fam::parse($this); |
|
254
|
|
|
} elseif (isset($record[2]) && substr(trim($record[2]), 0, 4) == 'NOTE') { |
|
255
|
|
|
Parser\Note::parse($this); |
|
256
|
|
|
} elseif (isset($record[2]) && $record[2] == 'REPO') { |
|
257
|
|
|
Parser\Repo::parse($this); |
|
258
|
|
|
} elseif (isset($record[2]) && $record[2] == 'OBJE') { |
|
259
|
|
|
Parser\Obje::parse($this); |
|
260
|
|
|
} elseif (isset($record[1]) && trim($record[1]) == 'TRLR') { |
|
261
|
|
|
// EOF |
|
262
|
|
|
break; |
|
263
|
|
|
} else { |
|
264
|
|
|
$this->logUnhandledRecord(get_class().' @ '.__LINE__); |
|
265
|
|
|
} |
|
266
|
|
|
} else { |
|
267
|
|
|
$this->logUnhandledRecord(get_class().' @ '.__LINE__); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
$this->forward(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return $this->getGedcom(); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|