. */ namespace Fisharebest\Webtrees; /** * Defined in session.php * * @global Tree $WT_TREE */ global $WT_TREE; use Fisharebest\Webtrees\Controller\PageController; use Fisharebest\Webtrees\Functions\FunctionsDb; use Fisharebest\Webtrees\Functions\FunctionsPrint; define('WT_SCRIPT_NAME', 'admin_site_merge.php'); require './includes/session.php'; $controller = new PageController; $controller ->restrictAccess(Auth::isManager($WT_TREE)) ->setPageTitle(I18N::translate('Merge records') . ' — ' . $WT_TREE->getTitleHtml()) ->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL) ->addInlineJavascript('autocomplete();'); $gid1 = Filter::post('gid1', WT_REGEX_XREF, Filter::get('gid1', WT_REGEX_XREF)); $gid2 = Filter::post('gid2', WT_REGEX_XREF, Filter::get('gid2', WT_REGEX_XREF)); $keep1 = Filter::postArray('keep1'); $keep2 = Filter::postArray('keep2'); $rec1 = GedcomRecord::getInstance($gid1, $WT_TREE); $rec2 = GedcomRecord::getInstance($gid2, $WT_TREE); if ($gid1 && !$rec1) { FlashMessages::addMessage(I18N::translate('%1$s does not exist.', $gid1), 'danger'); } if ($gid2 && !$rec2) { FlashMessages::addMessage(I18N::translate('%1$s does not exist.', $gid2), 'danger'); } if ($rec1 && $rec2 && $rec1->getXref() === $rec2->getXref()) { FlashMessages::addMessage(I18N::translate('You entered the same IDs. You cannot merge the same records.'), 'danger'); } if ($rec1 && $rec2 && $rec1::RECORD_TYPE !== $rec2::RECORD_TYPE) { FlashMessages::addMessage(I18N::translate('Records are not the same type. Cannot merge records that are not the same type.'), 'danger'); } // Facts found both records $facts = array(); // Facts found in only one record $facts1 = array(); $facts2 = array(); if ($rec1) { foreach ($rec1->getFacts() as $fact) { if (!$fact->isPendingDeletion() && $fact->getTag() !== 'CHAN') { $facts1[$fact->getFactId()] = $fact; } } } if ($rec2) { foreach ($rec2->getFacts() as $fact) { if (!$fact->isPendingDeletion() && $fact->getTag() !== 'CHAN') { $facts2[$fact->getFactId()] = $fact; } } } foreach ($facts1 as $id1 => $fact1) { foreach ($facts2 as $id2 => $fact2) { if ($fact1->getFactId() === $fact2->getFactId()) { $facts[] = $fact1; unset($facts1[$id1]); unset($facts2[$id2]); } } } if ($rec1 && $rec2 && $rec1->getXref() !== $rec2->getXref() && $rec1::RECORD_TYPE === $rec2::RECORD_TYPE && Filter::post('action') === 'merge' && Filter::checkCsrf()) { // Use the XREF of the record. $gid1 = $rec1->getXref(); $gid2 = $rec2->getXref(); $ids = FunctionsDb::fetchAllLinks($gid2, $WT_TREE->getTreeId()); // If we are not auto-accepting, then we can show a link to the pending deletion if (Auth::user()->getPreference('auto_accept')) { $record2_name = $rec2->getFullName(); } else { $record2_name = '' . $rec2->getFullName() . ''; } foreach ($ids as $id) { $record = GedcomRecord::getInstance($id, $WT_TREE); if (!$record->isPendingDeletion()) { FlashMessages::addMessage(I18N::translate( /* I18N: The placeholders are the names of individuals, sources, etc. */ 'The link from “%1$s” to “%2$s” has been updated.', '' . $record->getFullName() . '', $record2_name ), 'info'); $gedcom = str_replace("@$gid2@", "@$gid1@", $record->getGedcom()); $gedcom = preg_replace( '/(\n1.*@.+@.*(?:(?:\n[2-9].*)*))((?:\n1.*(?:\n[2-9].*)*)*\1)/', '$2', $gedcom ); $record->updateRecord($gedcom, true); } } // Update any linked user-accounts Database::prepare( "UPDATE `##user_gedcom_setting`" . " SET setting_value=?" . " WHERE gedcom_id=? AND setting_name='gedcomid' AND setting_value=?" )->execute(array($gid2, $WT_TREE->getTreeId(), $gid1)); // Merge hit counters $hits = Database::prepare( "SELECT page_name, SUM(page_count)" . " FROM `##hit_counter`" . " WHERE gedcom_id=? AND page_parameter IN (?, ?)" . " GROUP BY page_name" )->execute(array($WT_TREE->getTreeId(), $gid1, $gid2))->fetchAssoc(); foreach ($hits as $page_name => $page_count) { Database::prepare( "UPDATE `##hit_counter` SET page_count=?" . " WHERE gedcom_id=? AND page_name=? AND page_parameter=?" )->execute(array($page_count, $WT_TREE->getTreeId(), $page_name, $gid1)); } Database::prepare( "DELETE FROM `##hit_counter`" . " WHERE gedcom_id=? AND page_parameter=?" )->execute(array($WT_TREE->getTreeId(), $gid2)); $gedcom = "0 @" . $rec1->getXref() . "@ " . $rec1::RECORD_TYPE; foreach ($facts as $fact_id => $fact) { $gedcom .= "\n" . $fact->getGedcom(); } foreach ($facts1 as $fact_id => $fact) { if (in_array($fact_id, $keep1)) { $gedcom .= "\n" . $fact->getGedcom(); } } foreach ($facts2 as $fact_id => $fact) { if (in_array($fact_id, $keep2)) { $gedcom .= "\n" . $fact->getGedcom(); } } $rec1->updateRecord($gedcom, true); $rec2->deleteRecord(); FunctionsDb::updateFavorites($gid2, $gid1, $WT_TREE); FlashMessages::addMessage(I18N::translate( /* I18N: Records are individuals, sources, etc. */ 'The records “%1$s” and “%2$s” have been merged.', '' . $rec1->getFullName() . '', $record2_name ), 'success'); header('Location: ' . WT_BASE_URL . Filter::post('url', 'admin_trees_duplicates\.php', WT_SCRIPT_NAME)); return; } $controller->pageHeader(); ?>

getPageTitle(); ?>

getXref() !== $rec2->getXref() && $rec1::RECORD_TYPE === $rec2::RECORD_TYPE): ?>

$fact): ?>
getGedcom()); ?>
getTarget()): ?> getTarget()->getFullName(); ?>

getHtmlUrl() . '">' . $rec1->getFullName()) . ''; ?>

$fact): ?>
getGedcom()); ?>
getTarget()): ?> getTarget()->getFullName(); ?>

getHtmlUrl() . '">' . $rec2->getFullName()) . ''; ?>

$fact): ?>
getGedcom()); ?>
getTarget()): ?> getTarget()->getFullName(); ?>