| 1 |
<?php |
<?php |
| 2 |
// $Id: import.inc,v 1.5.4.5.4.1 2008/07/18 03:04:50 pyutaros Exp $ |
// $Id: import.inc,v 1.5.4.5.4.2 2008/09/11 15:19:11 pyutaros Exp $ |
| 3 |
require_once "includes/common.inc"; |
require_once "includes/common.inc"; |
| 4 |
// import.inc |
// import.inc |
| 5 |
// Functions for importing GEDCOM files to database |
// Functions for importing GEDCOM files to database |
| 134 |
$gedline = fgets( $fp, 1024 ); |
$gedline = fgets( $fp, 1024 ); |
| 135 |
$lnum++; |
$lnum++; |
| 136 |
|
|
| 137 |
if (preg_match("/^\s*(\d+)\s*(?:@([^@]+)@)?\s*(\S+)\s*(.*\S)?\s*$/i", $gedline , $matches)) |
if (preg_match("/^\s*(\d+)\s*(?:@([^@]+)@)?\s*(\S+)\s*(.*\S)?\s*$/i", $gedline , $matches)) { |
|
{ |
|
| 138 |
$level=$matches[1]; |
$level=$matches[1]; |
| 139 |
if ($level == 0) ++$rnum; |
if ($level == 0) ++$rnum; |
| 140 |
if ($rnum < $rmin) continue; |
if ($rnum < $rmin) continue; |
| 147 |
$gedcom_source=$gedline; |
$gedcom_source=$gedline; |
| 148 |
$parent=$gedcom_hier[$level-1]; |
$parent=$gedcom_hier[$level-1]; |
| 149 |
|
|
| 150 |
//NEW IMPORT CODE STARTS HERE |
//new ged file evaluation code |
| 151 |
//import creates a temp DB to help map relations |
switch($level){ |
| 152 |
|
case 0: |
| 153 |
if (strpos(";HUSB;WIFE;CHIL",$fact_code)) |
switch($current0record){ |
| 154 |
{ |
case 'INDI': |
| 155 |
// |
//create title_format variable that has not yet been set. |
| 156 |
// Lines that define only a relation |
$title_format = $firstname . " " . $middlename . " " . $lastname; //Will change with the implementation of tokens. |
| 157 |
// |
//Create family_individual node |
| 158 |
$gedcom_hier[$level]=NULL; |
unset($node); |
| 159 |
preg_match("/@\s*([^@\s]+)\s*@/i", $gedline , $matches); |
$node->type = "family_individual"; |
| 160 |
$target_xref=$matches[1]; |
$node->uid = $user->uid; |
| 161 |
$fid = db_result(db_query("SELECT fid FROM {family_facts} WHERE xref = '%s'", $target_xref)); |
$node->title = $title_format; |
| 162 |
$relation=$fact_code; |
$node->status = 1; |
| 163 |
} |
$node->moderate = 0; |
| 164 |
else |
$node->comment = 2; |
| 165 |
{ |
$node->revision = 0; |
| 166 |
// |
node_validate($node, $error); |
| 167 |
// Lines that define a fact |
if (!node_access("create", $node)) { |
| 168 |
// Every INDI fact gets a node |
$error['access'] = message_access(); |
| 169 |
// |
} |
| 170 |
$fid = db_next_id('{family_facts}_fid'); |
if ($error) { |
| 171 |
$nid=NULL; |
drupal_set_message( |
| 172 |
$relation="FACT"; |
t('Error at line @lnum of GED (@line): @error.', |
| 173 |
|
array('@lnum' => $lnum, '@line' => $gedline, '@error' => print_r($error,true)) |
| 174 |
if ($fact_code == 'INDI') { |
) |
| 175 |
unset($node); |
); |
| 176 |
$node->type = "family_individual"; |
} |
| 177 |
$node->uid = $user->uid; |
else { |
| 178 |
$node->title = "Unknown"; |
node_save($node); |
| 179 |
$node->status = 1; |
$vid=$node->vid; |
| 180 |
$node->moderate = 0; |
$nid=$node->nid; |
| 181 |
$node->comment = 2; |
} |
| 182 |
$node->revision = 0; |
unset($node); |
| 183 |
$node->fid = $fid; |
//Insert relationship variables into temporary database |
| 184 |
$node->xref = $xref; |
db_query_temporary("INSERT INTO {family_relations_temp} (nid, famc_xref, fams_xref) VALUES (%d, '%s', '%s')", $nid, $famc_xref, $fams_xref); |
| 185 |
$node->value = $value; |
//insert data into family_individual table |
| 186 |
$node->gedcom_source = $gedcom_source; |
db_query("INSERT INTO {family_individual} (vid, nid, title_format, firstname, middlename, lastname, gender, birthdate, birthplace, deathdate, deathplace, children_num) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s')", $vid, $nid, $title_format, $firstname, $middlename, $lastname, $gender, $birthdate, $birthplace, $deathdate, $deathplace, $children_num); |
| 187 |
node_validate($node, $error); |
//unset all INDI variables |
| 188 |
if (!node_access("create", $node)) { |
unset ($current0record); |
| 189 |
$error['access'] = message_access(); |
unset ($famc_xref); |
| 190 |
} |
unset ($fams_xref); |
| 191 |
if ($error) { |
unset ($vid); |
| 192 |
drupal_set_message( |
unset ($nid); |
| 193 |
t( |
unset ($title_format); |
| 194 |
'Error at line @lnum of GED (@line): @error.', |
unset ($firstname); |
| 195 |
array('@lnum' => $lnum, '@line' => $gedline, '@error' => print_r($error,true)) |
unset ($middlename); |
| 196 |
) |
unset ($lastname); |
| 197 |
); |
unset ($gender); |
| 198 |
} |
unset ($birthdate); |
| 199 |
else { |
unset ($birthplace); |
| 200 |
node_save($node); |
unset ($deathdate); |
| 201 |
$nid=$node->nid; |
unset ($deathplace); |
| 202 |
} |
unset ($children_num); |
| 203 |
unset($node); |
break; |
| 204 |
} |
case 'FAM' |
| 205 |
else |
//Find the group surname shared by the children of the group. - This may be incorrect in cases where the name has not been passed to the children. |
| 206 |
{ |
$result = db_query_temporary("SELECT nid FROM {family_relations_temp} WHERE famc_xref = '%s'", $current0xref); |
| 207 |
db_query("INSERT INTO {family_facts} (fid, nid, xref, fact_code, fact_value, gedcom_source) |
$child_ref_nid = db_fetch_object($result); |
| 208 |
VALUES (%d, %d, '%s', '%s', '%s', '%s')", |
$result = db_query("SELECT lastname FROM {family_individual} WHERE nid = %d", $child_ref_nid); |
| 209 |
$fid, $nid, $xref,$fact_code,$value,$gedcom_source); |
$group_surname = db_fetch_object($result); |
| 210 |
|
//Find Parents of group |
| 211 |
|
$result = db_query_temporary("SELECT nid FROM {family_relations_temp} WHERE fams_xref = '%s'", $current0xref); |
| 212 |
|
while ($parent_nid = db_fetch_object($result)) { |
| 213 |
|
$result2 = db_query("SELECT lastname FROM {family_individual} WHERE nid = %d", $parent_nid); |
| 214 |
|
$parent_surname = db_fetch_object($result2); |
| 215 |
|
if ($parent_surname = $group_surname){ |
| 216 |
|
$parent1 = $parent_nid; |
| 217 |
|
} else { |
| 218 |
|
$parent2 = $parent_nid; |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
|
//Set title format |
| 222 |
|
$result = db_query("SELECT firstname FROM {family_individual} WHERE nid = %d", $parent1); |
| 223 |
|
$parent1_firstname = db_fetch_object($result); |
| 224 |
|
$result = db_query("SELECT firstname FROM {family_individual} WHERE nid = %d", $parent2); |
| 225 |
|
$parent2_firstname = db_fetch_object($result); |
| 226 |
|
$title_format = $parent1_firstname . " and " . $parent2_firstname . " " . $group_surname; //Will change with the implementation of tokens. |
| 227 |
|
//create group node |
| 228 |
|
unset($node); |
| 229 |
|
$node->type = "family_group"; |
| 230 |
|
$node->uid = $user->uid; |
| 231 |
|
$node->title = $title_format; |
| 232 |
|
$node->status = 1; |
| 233 |
|
$node->moderate = 0; |
| 234 |
|
$node->comment = 2; |
| 235 |
|
$node->revision = 0; |
| 236 |
|
node_validate($node, $error); |
| 237 |
|
if (!node_access("create", $node)) { |
| 238 |
|
$error['access'] = message_access(); |
| 239 |
|
} |
| 240 |
|
if ($error) { |
| 241 |
|
drupal_set_message( |
| 242 |
|
t('Error at line @lnum of GED (@line): @error.', |
| 243 |
|
array('@lnum' => $lnum, '@line' => $gedline, '@error' => print_r($error,true)) |
| 244 |
|
) |
| 245 |
|
); |
| 246 |
|
} |
| 247 |
|
else { |
| 248 |
|
node_save($node); |
| 249 |
|
$vid=$node->vid; |
| 250 |
|
$nid=$node->nid; |
| 251 |
|
} |
| 252 |
|
unset($node); |
| 253 |
|
//insert ancestor group value into INDI nodes related to this group |
| 254 |
|
db_query("UPDATE {family_individual} SET ancestor_group='%d' WHERE lastname='%s'", $nid, $group_surname); |
| 255 |
|
//insert variables into family_group table |
| 256 |
|
db_query("INSERT INTO {family_group} (vid, nid, title_format, marr_type, marr_date, marr_plac, div_date, div_plac, parent1, parent2) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d')", $vid, $nid, $title_format, $marr_type, $marr_date, $marr_plac, $div_date, $div_plac, $parent1, $parent2); |
| 257 |
|
//unset all FAM variables |
| 258 |
|
unset ($current0record); |
| 259 |
|
unset ($current0xref); |
| 260 |
|
unset ($child_ref_nid); |
| 261 |
|
unset ($group_surname); |
| 262 |
|
unset ($parent_surname); |
| 263 |
|
unset ($parent1_firstname); |
| 264 |
|
unset ($parent2_firstname); |
| 265 |
|
unset ($vid); |
| 266 |
|
unset ($nid); |
| 267 |
|
unset ($title_format); |
| 268 |
|
unset ($marr_type); |
| 269 |
|
unset ($marr_date); |
| 270 |
|
unset ($marr_plac); |
| 271 |
|
unset ($div_date); |
| 272 |
|
unset ($div_plac); |
| 273 |
|
unset ($parent1); |
| 274 |
|
unset ($parent2); |
| 275 |
|
break; |
| 276 |
|
} |
| 277 |
|
$current0record = NULL; |
| 278 |
|
|
| 279 |
|
switch($value){ |
| 280 |
|
case 'FAM': |
| 281 |
|
case 'INDI': |
| 282 |
|
$current0record = $value; |
| 283 |
|
$current0xref = $xref; |
| 284 |
|
break; |
| 285 |
|
} |
| 286 |
|
break; |
| 287 |
|
case 1: |
| 288 |
|
switch($current0record){ |
| 289 |
|
case 'INDI': |
| 290 |
|
switch($fact_code){ |
| 291 |
|
case 'SEX': |
| 292 |
|
$gender = $value; |
| 293 |
|
$current1record = $fact_code; |
| 294 |
|
break; |
| 295 |
|
case 'NCHI': |
| 296 |
|
$children_num = $value; |
| 297 |
|
$current1record = $fact_code; |
| 298 |
|
break; |
| 299 |
|
case 'NAME': |
| 300 |
|
//split name value by / to separate surname |
| 301 |
|
$splitName1 = explode("/", $value); |
| 302 |
|
$lastname = $splitName1[1]; |
| 303 |
|
// split name by spaces |
| 304 |
|
$splitName2 = explode(" ", $splitName1[0]); |
| 305 |
|
// take the first name to be firstname |
| 306 |
|
$firstname = $splitName2[0]; |
| 307 |
|
// add all the other names together in a string |
| 308 |
|
$middlename = $splitName2[1] . " " . $splitName2[2] . " " . $splitName2[3] . " " . $splitName2[4] . " " . $splitName2[5] . " " . $splitName2[6] . " " . $splitName2[7]; |
| 309 |
|
$current1record = $fact_code; |
| 310 |
|
break; |
| 311 |
|
case 'DEAT': |
| 312 |
|
case 'BIRT': |
| 313 |
|
$current1record = $fact_code; |
| 314 |
|
break; |
| 315 |
|
case 'FAMS': |
| 316 |
|
$fams_xref = $value; |
| 317 |
|
break; |
| 318 |
|
case 'FAMC': |
| 319 |
|
$famc_xref = $value; |
| 320 |
|
break; |
| 321 |
|
} |
| 322 |
|
break; |
| 323 |
|
case 'FAM': |
| 324 |
|
switch($fact_code){ |
| 325 |
|
case 'MARR': |
| 326 |
|
case 'DIV': |
| 327 |
|
$current1record = $fact_code; |
| 328 |
|
break; |
| 329 |
|
} |
| 330 |
|
break; |
| 331 |
|
} |
| 332 |
|
break; |
| 333 |
|
case 2: |
| 334 |
|
switch($current1record){ |
| 335 |
|
case 'BIRT': |
| 336 |
|
switch($fact_code){ |
| 337 |
|
case 'DATE': |
| 338 |
|
$birthdate = family_changeDateFormat($value) |
| 339 |
|
break; |
| 340 |
|
case 'PLAC': |
| 341 |
|
$birthplace = $value; |
| 342 |
|
break; |
| 343 |
|
} |
| 344 |
|
break; |
| 345 |
|
case 'DEAT': |
| 346 |
|
switch($fact_code){ |
| 347 |
|
case 'DATE': |
| 348 |
|
$deathdate = family_changeDateFormat($value) |
| 349 |
|
break; |
| 350 |
|
case 'PLAC': |
| 351 |
|
$deathplace = $value; |
| 352 |
|
break; |
| 353 |
|
} |
| 354 |
|
break; |
| 355 |
|
case 'MARR': |
| 356 |
|
switch($fact_code){ |
| 357 |
|
case 'TYPE': |
| 358 |
|
$marr_type = $value |
| 359 |
|
break; |
| 360 |
|
case 'DATE': |
| 361 |
|
$marr_date = family_changeDateFormat($value) |
| 362 |
|
break; |
| 363 |
|
case 'PLAC': |
| 364 |
|
$marr_plac = $value |
| 365 |
|
break; |
| 366 |
|
} |
| 367 |
|
break; |
| 368 |
|
case 'DIV': |
| 369 |
|
switch($fact_code){ |
| 370 |
|
case 'DATE': |
| 371 |
|
$div_date = family_changeDateFormat($value) |
| 372 |
|
break; |
| 373 |
|
case 'PLAC': |
| 374 |
|
$div_plac = $value |
| 375 |
|
break; |
| 376 |
|
} |
| 377 |
|
break; |
| 378 |
|
} |
| 379 |
|
break; |
| 380 |
|
} |
| 381 |
} |
} |
|
$gedcom_hier[$level] = $fid; |
|
|
$gedcom_source=null; // If the source is in fact table, no need to put it again in relation table |
|
|
$nchanged++; |
|
|
} |
|
|
|
|
|
if ($level>0) { |
|
|
$rid = db_next_id('{family_relations}_fid'); |
|
|
db_query("INSERT INTO {family_relations} (rid, fid1, fid2, relation_description, |
|
|
gedcom_source) VALUES (%d, %d, %d, '%s', '%s')", |
|
|
$rid,$fid, $parent, $relation, $gedcom_source); |
|
|
} |
|
|
} |
|
| 382 |
} |
} |
| 383 |
fclose ($fp); |
fclose ($fp); |
| 384 |
|
|
| 402 |
|
|
| 403 |
//UPDATE NAME |
//UPDATE NAME |
| 404 |
$node=node_load($row['nid']); |
$node=node_load($row['nid']); |
| 405 |
if ($node->title != $name) |
if ($node->title != $name) { |
|
{ |
|
| 406 |
$node->title = $name; |
$node->title = $name; |
| 407 |
node_save($node); |
node_save($node); |
| 408 |
} |
} |