setPageTitle(WT_I18N::translate('Merge records')); require_once WT_ROOT.'includes/functions/functions_edit.php'; require_once WT_ROOT.'includes/functions/functions_import.php'; $ged=$GEDCOM; $gid1=safe_POST_xref('gid1'); $gid2=safe_POST_xref('gid2'); $action=safe_POST('action', WT_REGEX_ALPHA, 'choose'); $ged2=safe_POST('ged2', WT_REGEX_NOSCRIPT, $GEDCOM); $keep1=safe_POST('keep1', WT_REGEX_UNSAFE); $keep2=safe_POST('keep2', WT_REGEX_UNSAFE); if (empty($keep1)) $keep1=array(); if (empty($keep2)) $keep2=array(); $controller->pageHeader(); if (get_gedcom_count()==1) { //Removed becasue it doesn't work here for multiple GEDCOMs. Can be reinstated when fixed (https://bugs.launchpad.net/webtrees/+bug/613235) if ($ENABLE_AUTOCOMPLETE) require WT_ROOT.'js/autocomplete.js.htm'; } //-- make sure they have accept access privileges if (!WT_USER_CAN_ACCEPT) { echo '', WT_I18N::translate('Access Denied
You do not have access to this resource.'), '
'; exit; } if ($action!='choose') { if ($gid1==$gid2 && $GEDCOM==$ged2) { $action='choose'; echo '', WT_I18N::translate('You entered the same IDs. You cannot merge the same records.'), ''; } else { $gedrec1 = find_gedcom_record($gid1, WT_GED_ID, true); $gedrec2 = find_gedcom_record($gid2, get_id_from_gedcom($ged2), true); // Fetch the original XREF - may differ in case from the supplied value $tmp=new WT_Person($gedrec1); $gid1=$tmp->getXref(); $tmp=new WT_Person($gedrec2); $gid2=$tmp->getXref(); if (empty($gedrec1)) { echo '', WT_I18N::translate('Unable to find record with ID'), ': ', $gid1, ', ', $ged; $action='choose'; } elseif (empty($gedrec2)) { echo '', WT_I18N::translate('Unable to find record with ID'), ': ', $gid2, ', ', $ged2; $action='choose'; } else { $type1 = ''; $ct = preg_match("/0 @$gid1@ (.*)/", $gedrec1, $match); if ($ct>0) { $type1 = trim($match[1]); } $type2 = ""; $ct = preg_match("/0 @$gid2@ (.*)/", $gedrec2, $match); if ($ct>0) $type2 = trim($match[1]); if (!empty($type1) && ($type1!=$type2)) { echo '', WT_I18N::translate('Records are not the same type. Cannot merge records that are not the same type.'), ''; $action='choose'; } else { $facts1 = array(); $facts2 = array(); $prev_tags = array(); $ct = preg_match_all('/\n1 (\w+)/', $gedrec1, $match, PREG_SET_ORDER); for ($i=0; $i<$ct; $i++) { $fact = trim($match[$i][1]); if (isset($prev_tags[$fact])) { $prev_tags[$fact]++; } else { $prev_tags[$fact] = 1; } $subrec = get_sub_record(1, "1 $fact", $gedrec1, $prev_tags[$fact]); $facts1[] = array('fact'=>$fact, 'subrec'=>trim($subrec)); } $prev_tags = array(); $ct = preg_match_all('/\n1 (\w+)/', $gedrec2, $match, PREG_SET_ORDER); for ($i=0; $i<$ct; $i++) { $fact = trim($match[$i][1]); if (isset($prev_tags[$fact])) { $prev_tags[$fact]++; } else { $prev_tags[$fact] = 1; } $subrec = get_sub_record(1, "1 $fact", $gedrec2, $prev_tags[$fact]); $facts2[] = array('fact'=>$fact, 'subrec'=>trim($subrec)); } if ($action=='select') { echo '

', WT_I18N::translate('Merge Step 2 of 3'), '

'; echo '
'; echo WT_I18N::translate('The following facts were exactly the same in both records and will be merged automatically.'), '
'; echo ''; echo ''; echo ''; echo ''; echo ''; $equal_count=0; $skip1 = array(); $skip2 = array(); echo ''; foreach ($facts1 as $i=>$fact1) { foreach ($facts2 as $j=>$fact2) { if (utf8_strtoupper($fact1['subrec'])==utf8_strtoupper($fact2['subrec'])) { $skip1[] = $i; $skip2[] = $j; $equal_count++; echo ''; } } } if ($equal_count==0) { echo ''; } echo '
', WT_I18N::translate($fact1['fact']); echo '', nl2br($fact1['subrec']), '
', WT_I18N::translate('No matching facts found'), '

'; echo WT_I18N::translate('The following facts did not match. Select the information you would like to keep.'); echo ''; echo ''; echo ''; echo '
', WT_I18N::translate('Record'), ' ', $gid1, '', WT_I18N::translate('Record'), ' ', $gid2, '
'; echo ''; foreach ($facts1 as $i=>$fact1) { if (($fact1['fact']!='CHAN')&&(!in_array($i, $skip1))) { echo ''; echo ''; } } echo '
', nl2br($fact1['subrec']), '
'; echo '
'; echo ''; foreach ($facts2 as $j=>$fact2) { if (($fact2['fact']!='CHAN')&&(!in_array($j, $skip2))) { echo ''; echo ''; } } echo '
', nl2br($fact2['subrec']), '
'; echo '
'; echo ''; echo '
'; } elseif ($action=='merge') { $manual_save = true; echo '

', WT_I18N::translate('Merge Step 3 of 3'), '

'; if ($GEDCOM==$ged2) { $success = delete_gedrec($gid2, WT_GED_ID); if ($success) { echo '
', WT_I18N::translate('GEDCOM record successfully deleted.'), '
'; } //-- replace all the records that linked to gid2 $ids=fetch_all_links($gid2, WT_GED_ID); foreach ($ids as $id) { $record=find_gedcom_record($id, WT_GED_ID, true); echo WT_I18N::translate('Updating linked record'), ' ', $id, '
'; $newrec=str_replace("@$gid2@", "@$gid1@", $record); $newrec=preg_replace( '/(\n1.*@.+@.*(?:(?:\n[2-9].*)*))((?:\n1.*(?:\n[2-9].*)*)*\1)/', '$2', $newrec ); replace_gedrec($id, WT_GED_ID, $newrec); } // Update any linked user-accounts WT_DB::prepare( "UPDATE `##user_gedcom_setting`". " SET setting_value=?". " WHERE gedcom_id=? AND setting_name='gedcomid' AND setting_value=?" )->execute(array($gid2, WT_GED_ID, $gid1)); // Merge hit counters $hits=WT_DB::prepare( "SELECT page_name, SUM(page_count)". " FROM `##hit_counter`". " WHERE gedcom_id=? AND page_parameter IN (?, ?)". " GROUP BY page_name" )->execute(array(WT_GED_ID, $gid1, $gid2))->fetchAssoc(); foreach ($hits as $page_name=>$page_count) { WT_DB::prepare( "UPDATE `##hit_counter` SET page_count=?". " WHERE gedcom_id=? AND page_name=? AND page_parameter=?" )->execute(array($page_count, WT_GED_ID, $page_name, $gid1)); } WT_DB::prepare( "DELETE FROM `##hit_counter`". " WHERE gedcom_id=? AND page_parameter=?" )->execute(array(WT_GED_ID, $gid2)); } $newgedrec = "0 @$gid1@ $type1\n"; for ($i=0; ($i'; } } if (isset($facts2[$i])) { if (in_array($i, $keep2)) { $newgedrec .= $facts2[$i]['subrec']."\n"; echo WT_I18N::translate('Adding'), ' ', $facts2[$i]['fact'], ' ', WT_I18N::translate('from'), ' ', $gid2, '
'; } } } replace_gedrec($gid1, WT_GED_ID, $newgedrec); $rec=WT_GedcomRecord::getInstance($gid1); echo '
', WT_I18N::translate('Record %s successfully updated.', $rec->getXrefLink()), '
'; $fav_count=update_favorites($gid2, $gid1); if ($fav_count > 0) { echo '
', $fav_count, ' ', WT_I18N::translate('favorites updated.'), '
'; } echo '
', WT_I18N::translate('Merge more records.'), '
'; echo '
'; } } } } } if ($action=='choose') { ?>

', WT_I18N::translate('Merge Step 1 of 3'), '

', WT_I18N::translate('Select two GEDCOM records to merge. The records must be of the same type.'), '

', WT_I18N::translate('Merge To ID:'), ' ', ' ', $inditext, ' ', $famtext, ' ', $sourtext, '
', WT_I18N::translate('Merge From ID:'), '  ', ' ', $inditext, ' ', $famtext, ' ', $sourtext, '
'; }