forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_site_merge.php
398 lines (362 loc) · 13 KB
/
admin_site_merge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2016 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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 = [];
// Facts found in only one record
$facts1 = [];
$facts2 = [];
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 = '<a class="alert-link" href="' . $rec2->getHtmlUrl() . '">' . $rec2->getFullName() . '</a>';
}
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.',
'<a class="alert-link" href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a>',
$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([$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([$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([$page_count, $WT_TREE->getTreeId(), $page_name, $gid1]);
}
Database::prepare(
"DELETE FROM `##hit_counter`" .
" WHERE gedcom_id=? AND page_parameter=?"
)->execute([$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.',
'<a class="alert-link" href="' . $rec1->getHtmlUrl() . '">' . $rec1->getFullName() . '</a>',
$record2_name
), 'success');
header('Location: ' . WT_BASE_URL . Filter::post('url', 'admin_trees_duplicates\.php', WT_SCRIPT_NAME));
return;
}
$controller->pageHeader();
?>
<ol class="breadcrumb small">
<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
<li><a href="admin_trees_manage.php"><?php echo I18N::translate('Manage family trees'); ?></a></li>
<li class="active"><?php echo $controller->getPageTitle(); ?></li>
</ol>
<h1><?php echo $controller->getPageTitle(); ?></h1>
<?php if ($rec1 && $rec2 && $rec1->getXref() !== $rec2->getXref() && $rec1::RECORD_TYPE === $rec2::RECORD_TYPE): ?>
<form method="post">
<input type="hidden" name="action" value="merge">
<input type="hidden" name="ged" value="<?php echo $WT_TREE->getNameHtml(); ?>">
<input type="hidden" name="url" value="<?php echo Filter::get('url', 'admin_trees_duplicates\.php'); ?>">
<?php echo Filter::getCsrf(); ?>
<p>
<?php echo I18N::translate('Select the facts and events to keep from both records.'); ?>
</p>
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">
<?php echo I18N::translate('The following facts and events were found in both records.'); ?>
</h2>
</div>
<div class="panel-body">
<?php if (!empty($facts)): ?>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>
<?php echo I18N::translate('Select'); ?>
</th>
<th>
<?php echo I18N::translate('Details'); ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($facts as $fact_id => $fact): ?>
<tr>
<td>
<input type="checkbox" name="keep1[]" value="<?php echo $fact->getFactId(); ?>" checked>
</td>
<td>
<div class="gedcom-data" dir="ltr"><?php echo Filter::escapeHtml($fact->getGedcom()); ?></div>
<?php if ($fact->getTarget()): ?>
<a href="<?php echo $fact->getTarget()->getHtmlUrl(); ?>">
<?php echo $fact->getTarget()->getFullName(); ?>
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>
<?php echo I18N::translate('No matching facts found'); ?>
</p>
<?php endif; ?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">
<?php echo /* I18N: the name of an individual, source, etc. */ I18N::translate('The following facts and events were only found in the record of %s.', '<a href="' . $rec1->getHtmlUrl() . '">' . $rec1->getFullName()) . '</a>'; ?>
</h2>
</div>
<div class="panel-body">
<?php if (!empty($facts1)): ?>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>
<?php echo I18N::translate('Select'); ?>
</th>
<th>
<?php echo I18N::translate('Details'); ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($facts1 as $fact_id => $fact): ?>
<tr>
<td>
<input type="checkbox" name="keep1[]" value="<?php echo $fact->getFactId(); ?>" checked>
</td>
<td>
<div class="gedcom-data" dir="ltr"><?php echo Filter::escapeHtml($fact->getGedcom()); ?></div>
<?php if ($fact->getTarget()): ?>
<a href="<?php echo $fact->getTarget()->getHtmlUrl(); ?>">
<?php echo $fact->getTarget()->getFullName(); ?>
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>
<?php echo I18N::translate('No matching facts found'); ?>
</p>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">
<?php echo /* I18N: the name of an individual, source, etc. */ I18N::translate('The following facts and events were only found in the record of %s.', '<a href="' . $rec2->getHtmlUrl() . '">' . $rec2->getFullName()) . '</a>'; ?>
</h2>
</div>
<div class="panel-body">
<?php if (!empty($facts2)): ?>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>
<?php echo I18N::translate('Select'); ?>
</th>
<th>
<?php echo I18N::translate('Details'); ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($facts2 as $fact_id => $fact): ?>
<tr>
<td>
<input type="checkbox" name="keep2[]" value="<?php echo $fact->getFactId(); ?>" checked>
</td>
<td>
<div class="gedcom-data" dir="ltr"><?php echo Filter::escapeHtml($fact->getGedcom()); ?></div>
<?php if ($fact->getTarget()): ?>
<a href="<?php echo $fact->getTarget()->getHtmlUrl(); ?>">
<?php echo $fact->getTarget()->getFullName(); ?>
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>
<?php echo I18N::translate('No matching facts found'); ?>
</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">
<i class="fa fa-check"></i>
<?php echo I18N::translate('save'); ?>
</button>
</form>
<?php else: ?>
<form class="form form-horizontal">
<input type="hidden" name="ged" value="<?php echo $WT_TREE->getNameHtml(); ?>">
<p><?php echo /* I18N: Records are indviduals, sources, etc. */ I18N::translate('Select two records to merge.'); ?></p>
<div class="form-group">
<div class="control-label col-sm-3">
<label for="gid1">
<?php echo /* I18N: Record is an indvidual, source, etc. */ I18N::translate('First record'); ?>
</label>
</div>
<div class="col-sm-9">
<input data-autocomplete-type="IFSRO" type="text" name="gid1" id="gid1" maxlength="20" value="<?php echo $gid1; ?>">
<?php echo FunctionsPrint::printFindIndividualLink('gid1'); ?>
<?php echo FunctionsPrint::printFindFamilyLink('gid1'); ?>
<?php echo FunctionsPrint::printFindSourceLink('gid1'); ?>
<?php echo FunctionsPrint::printFindRepositoryLink('gid1'); ?>
<?php echo FunctionsPrint::printFindMediaLink('gid1'); ?>
<?php echo FunctionsPrint::printFindNoteLink('gid1'); ?>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-3">
<label for="gid2">
<?php echo /* I18N: Record is an indvidual, source, etc. */ I18N::translate('Second record'); ?>
</label>
</div>
<div class="col-sm-9">
<input data-autocomplete-type="IFSRO" type="text" name="gid2" id="gid2" maxlength="20" value="<?php echo $gid2; ?>" >
<?php echo FunctionsPrint::printFindIndividualLink('gid2'); ?>
<?php echo FunctionsPrint::printFindFamilyLink('gid2'); ?>
<?php echo FunctionsPrint::printFindSourceLink('gid2'); ?>
<?php echo FunctionsPrint::printFindRepositoryLink('gid2'); ?>
<?php echo FunctionsPrint::printFindMediaLink('gid2'); ?>
<?php echo FunctionsPrint::printFindNoteLink('gid2'); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary">
<?php echo I18N::translate('continue'); ?>
</button>
</div>
</div>
</form>
<?php endif; ?>