summaryrefslogtreecommitdiff
path: root/admin_trees_merge.php
diff options
context:
space:
mode:
authorGreg Roach <fisharebest@gmail.com>2014-07-06 22:31:07 +0100
committerGreg Roach <fisharebest@gmail.com>2014-07-06 22:31:07 +0100
commit6cee866ed270d9dfa96732d0321f317af617a318 (patch)
tree95f98eca3d7452c84a70fed8a0ba82ac67522605 /admin_trees_merge.php
parentfb4f4da5f04908850f05f9388887224e42b37c99 (diff)
downloadwebtrees-6cee866ed270d9dfa96732d0321f317af617a318.tar.gz
webtrees-6cee866ed270d9dfa96732d0321f317af617a318.tar.bz2
webtrees-6cee866ed270d9dfa96732d0321f317af617a318.zip
Renumber/merge family trees
Diffstat (limited to 'admin_trees_merge.php')
-rw-r--r--admin_trees_merge.php189
1 files changed, 142 insertions, 47 deletions
diff --git a/admin_trees_merge.php b/admin_trees_merge.php
index 4ae3273e33..5cf50d8b47 100644
--- a/admin_trees_merge.php
+++ b/admin_trees_merge.php
@@ -1,5 +1,5 @@
<?php
-// Check a family tree for structural errors.
+// Merge two trees, by copying the records from one into another.
//
// webtrees: Web based Family History software
// Copyright (C) 2014 Greg Roach
@@ -18,66 +18,161 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-define('WT_SCRIPT_NAME', 'admin_trees_renumber.php');
+define('WT_SCRIPT_NAME', 'admin_trees_merge.php');
require './includes/session.php';
+require WT_ROOT . 'includes/functions/functions_edit.php';
$controller=new WT_Controller_Page();
$controller
->restrictAccess(\WT\Auth::isManager())
- ->setPageTitle(WT_I18N::translate('Merge family tree'))
+ ->setPageTitle(WT_I18N::translate('Merge family trees'))
->pageHeader();
-// Every XREF used by this tree and also any other tree
-$xrefs = WT_DB::prepare(
- "SELECT xref, type FROM (" .
- " SELECT i_id AS xref, 'INDI' AS type FROM `##individuals` WHERE i_file = ?" .
- " UNION " .
- " SELECT f_id AS xref, 'FAM' AS type FROM `##families` WHERE f_file = ?" .
- " UNION " .
- " SELECT s_id AS xref, 'SOUR' AS type FROM `##sources` WHERE s_file = ?" .
- " UNION " .
- " SELECT m_id AS xref, 'OBJE' AS type FROM `##media` WHERE m_file = ?" .
- " UNION " .
- " SELECT o_id AS xref, o_type AS type FROM `##other` WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" .
- ") AS this_tree JOIN (".
- " SELECT xref FROM `##change` WHERE gedcom_id <> ?" .
- " UNION " .
- " SELECT i_id AS xref FROM `##individuals` WHERE i_file <> ?" .
- " UNION " .
- " SELECT f_id AS xref FROM `##families` WHERE f_file <> ?" .
- " UNION " .
- " SELECT s_id AS xref FROM `##sources` WHERE s_file <> ?" .
- " UNION " .
- " SELECT m_id AS xref FROM `##media` WHERE m_file <> ?" .
- " UNION " .
- " SELECT o_id AS xref FROM `##other` WHERE o_file <> ? AND o_type NOT IN ('HEAD', 'TRLR')" .
- ") AS other_trees USING (xref)"
-)->execute(array(
- WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID,
- WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID
- ))->fetchAssoc();
+echo '<h2>', $controller->getPageTitle(), '</h2>';
-echo '<h2>', $WT_TREE->tree_title_html, '</h2>';
+$ged2_id = WT_Filter::post('ged2_id');
-echo WT_I18N::translate(
- /* I18N: Copy all the records from [family tree 1] into [family tree 2] */ 'Copy all the records from %1$s into %2$s.',
- WT_Filter::escapeHtml(WT_GEDCOM),
- WT_Filter::escapeHtml(WT_GEDCOM)
-);
+if ($ged2_id && $ged2_id != WT_GED_ID ) {
+ // Every XREF used by both trees
+ $xrefs = WT_DB::prepare(
+ "SELECT xref, type FROM (" .
+ " SELECT i_id AS xref, 'INDI' AS type FROM `##individuals` WHERE i_file = ?" .
+ " UNION " .
+ " SELECT f_id AS xref, 'FAM' AS type FROM `##families` WHERE f_file = ?" .
+ " UNION " .
+ " SELECT s_id AS xref, 'SOUR' AS type FROM `##sources` WHERE s_file = ?" .
+ " UNION " .
+ " SELECT m_id AS xref, 'OBJE' AS type FROM `##media` WHERE m_file = ?" .
+ " UNION " .
+ " SELECT o_id AS xref, o_type AS type FROM `##other` WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" .
+ ") AS this_tree JOIN (".
+ " SELECT xref FROM `##change` WHERE gedcom_id = ?" .
+ " UNION " .
+ " SELECT i_id AS xref FROM `##individuals` WHERE i_file = ?" .
+ " UNION " .
+ " SELECT f_id AS xref FROM `##families` WHERE f_file = ?" .
+ " UNION " .
+ " SELECT s_id AS xref FROM `##sources` WHERE s_file = ?" .
+ " UNION " .
+ " SELECT m_id AS xref FROM `##media` WHERE m_file = ?" .
+ " UNION " .
+ " SELECT o_id AS xref FROM `##other` WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" .
+ ") AS other_trees USING (xref)"
+ )->execute(array(
+ WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID, WT_GED_ID,
+ $ged2_id, $ged2_id, $ged2_id, $ged2_id, $ged2_id, $ged2_id
+ ))->fetchAssoc();
-if ($xrefs) {
- echo
- '<p>',
+ if ($xrefs) {
+ echo
+ '<p class="error">',
WT_I18N::plural(
- 'This family tree has %s record which uses the same “XREF” as another family tree.',
- 'This family tree has %s records which use the same “XREF” as another family tree.',
+ 'The two family trees have %1$s record which uses the same “XREF”.',
+ 'The two family trees have %1$s records which uses the same “XREF”.',
count($xrefs), count($xrefs)
),
'</p>',
- '<p>',
- WT_Translate('You must renumber one of the trees before you can merge them.'),
+ '<p class="error">',
+ WT_I18N::translate('You must renumber the records in one of the trees before you can merge them.'),
'</p>';
+ } else {
+ WT_DB::exec("START TRANSACTION");
+ WT_DB::exec(
+ "LOCK TABLE" .
+ " `##individuals` WRITE," .
+ " `##individuals` AS individuals2 READ," .
+ " `##families` WRITE," .
+ " `##families` AS families2 READ," .
+ " `##sources` WRITE," .
+ " `##sources` AS sources2 READ," .
+ " `##media` WRITE," .
+ " `##media` AS media2 READ," .
+ " `##other` WRITE," .
+ " `##other` AS other2 READ," .
+ " `##name` WRITE," .
+ " `##name` AS name2 READ," .
+ " `##placelinks` WRITE," .
+ " `##placelinks` AS placelinks2 READ," .
+ " `##change` WRITE," .
+ " `##change` AS change2 READ," .
+ " `##dates` WRITE," .
+ " `##dates` AS dates2 READ," .
+ " `##default_resn` WRITE," .
+ " `##default_resn` AS default_resn2 READ," .
+ " `##hit_counter` WRITE," .
+ " `##hit_counter` AS hit_counter2 READ," .
+ " `##link` WRITE," .
+ " `##link` AS link2 READ"
+ );
+ try {
+ WT_DB::prepare(
+ "INSERT INTO `##individuals` (i_id, i_file, i_rin, i_sex, i_gedcom)" .
+ " SELECT i_id, ?, i_rin, i_sex, i_gedcom FROM `##individuals` AS individuals2 WHERE i_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##families` (f_id, f_file, f_husb, f_wife, f_gedcom, f_numchil)" .
+ " SELECT f_id, ?, f_husb, f_wife, f_gedcom, f_numchil FROM `##families` AS families2 WHERE f_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##sources` (s_id, s_file, s_name, s_gedcom)" .
+ " SELECT s_id, ?, s_name, s_gedcom FROM `##sources` AS sources2 WHERE s_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##media` (m_id, m_ext, m_type, m_titl, m_filename, m_file, m_gedcom)" .
+ " SELECT m_id, m_ext, m_type, m_titl, m_filename, ?, m_gedcom FROM `##media` AS media2 WHERE m_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##other` (o_id, o_file, o_type, o_gedcom)" .
+ " SELECT o_id, ?, o_type, o_gedcom FROM `##other` AS other2 WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##name` (n_file, n_id, n_num, n_type, n_sort, n_full, n_surname, n_surn, n_givn, n_soundex_givn_std, n_soundex_surn_std, n_soundex_givn_dm, n_soundex_surn_dm)" .
+ " SELECT ?, n_id, n_num, n_type, n_sort, n_full, n_surname, n_surn, n_givn, n_soundex_givn_std, n_soundex_surn_std, n_soundex_givn_dm, n_soundex_surn_dm FROM `##name` AS name2 WHERE n_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##placelinks` (pl_p_id, pl_gid, pl_file)" .
+ " SELECT pl_p_id, pl_gid, ? FROM `##placelinks` AS placelinks2 WHERE pl_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##dates` (d_day, d_month, d_mon, d_year, d_julianday1, d_julianday2, d_fact, d_gid, d_file, d_type)" .
+ " SELECT d_day, d_month, d_mon, d_year, d_julianday1, d_julianday2, d_fact, d_gid, ?, d_type FROM `##dates` AS dates2 WHERE d_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##default_resn` (gedcom_id, xref, tag_type, resn, comment, updated)" .
+ " SELECT ?, xref, tag_type, resn, comment, updated FROM `##default_resn` AS default_resn2 WHERE gedcom_id = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ WT_DB::prepare(
+ "INSERT INTO `##link` (l_file, l_from, l_type, l_to)" .
+ " SELECT ?, l_from, l_type, l_to FROM `##link` AS link2 WHERE l_file = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ // This table may contain old (deleted) references, which could clash. IGNORE these.
+ WT_DB::prepare(
+ "INSERT IGNORE INTO `##change` (change_time, status, gedcom_id, xref, old_gedcom, new_gedcom, user_id)" .
+ " SELECT change_time, status, ?, xref, old_gedcom, new_gedcom, user_id FROM `##change` AS change2 WHERE gedcom_id = ?"
+ )->execute(array($ged2_id, WT_GED_ID));
+ // This table may contain old (deleted) references, which could clash. IGNORE these.
+ WT_DB::prepare(
+ "INSERT IGNORE INTO `##hit_counter` (gedcom_id, page_name, page_parameter, page_count)" .
+ " SELECT ?, page_name, page_parameter, page_count FROM `##hit_counter` AS hit_counter2 WHERE gedcom_id = ? AND page_name <> 'index.php'"
+ )->execute(array($ged2_id, WT_GED_ID));
+ echo '<p>', WT_I18N::translate('The family trees were merged successfully.'), '</p>';
+ } catch (Exception $ex) {
+ WT_DB::exec("ROLLBACK");
+ echo '<p>', WT_I18N::translate('Oops! An unexpected database error occurred.'), '</p>';
+ echo '<pre>', $ex, '</pre>';
+ }
+ WT_DB::exec("UNLOCK TABLES");
+ WT_DB::exec("COMMIT");
+ }
}
-if (WT_Filter::get('go')) {
-} \ No newline at end of file
+echo '<form method="POST" action="', WT_SCRIPT_NAME, '">';
+echo '<input type="hidden" name="go" value="1">';
+echo '<p>', WT_I18N::translate(
+/* I18N: Copy all the records from [family tree 1] into [family tree 2] */ 'Copy all the records from %1$s into %2$s.',
+ WT_Filter::escapeHtml($WT_TREE->tree_title),
+ select_edit_control('ged2_id', WT_Tree::getIdList(), null, $ged2_id)
+), '</p>';
+
+echo '<input type="submit" value="', WT_I18N::translate('continue'), '">';
+echo '</form>'; \ No newline at end of file