1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2021 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Http\RequestHandlers; |
21
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Auth; |
23
|
|
|
use Fisharebest\Webtrees\Contracts\UserInterface; |
24
|
|
|
use Fisharebest\Webtrees\FlashMessages; |
25
|
|
|
use Fisharebest\Webtrees\I18N; |
26
|
|
|
use Fisharebest\Webtrees\Registry; |
27
|
|
|
use Fisharebest\Webtrees\Services\LinkedRecordService; |
28
|
|
|
use Fisharebest\Webtrees\Validator; |
29
|
|
|
use Illuminate\Database\Capsule\Manager as DB; |
30
|
|
|
use Illuminate\Database\Query\Expression; |
31
|
|
|
use Psr\Http\Message\ResponseInterface; |
32
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
33
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
34
|
|
|
|
35
|
|
|
use function e; |
36
|
|
|
use function in_array; |
37
|
|
|
use function preg_replace; |
38
|
|
|
use function str_replace; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Merge records |
42
|
|
|
*/ |
43
|
|
|
class MergeFactsAction implements RequestHandlerInterface |
44
|
|
|
{ |
45
|
|
|
private LinkedRecordService $linked_record_service; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param LinkedRecordService $linked_record_service |
49
|
|
|
*/ |
50
|
|
|
public function __construct(LinkedRecordService $linked_record_service) |
51
|
|
|
{ |
52
|
|
|
$this->linked_record_service = $linked_record_service; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ServerRequestInterface $request |
57
|
|
|
* |
58
|
|
|
* @return ResponseInterface |
59
|
|
|
*/ |
60
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
61
|
|
|
{ |
62
|
|
|
$tree = Validator::attributes($request)->tree(); |
63
|
|
|
|
64
|
|
|
$params = (array) $request->getParsedBody(); |
65
|
|
|
|
66
|
|
|
$xref1 = $params['xref1'] ?? ''; |
67
|
|
|
$xref2 = $params['xref2'] ?? ''; |
68
|
|
|
|
69
|
|
|
$keep1 = $params['keep1'] ?? []; |
70
|
|
|
$keep2 = $params['keep2'] ?? []; |
71
|
|
|
|
72
|
|
|
// Merge record2 into record1 |
73
|
|
|
$record1 = Registry::gedcomRecordFactory()->make($xref1, $tree); |
74
|
|
|
$record2 = Registry::gedcomRecordFactory()->make($xref2, $tree); |
75
|
|
|
|
76
|
|
|
if ( |
77
|
|
|
$record1 === null || |
78
|
|
|
$record2 === null || |
79
|
|
|
$record1 === $record2 || |
80
|
|
|
$record1->tag() !== $record2->tag() || |
81
|
|
|
$record1->isPendingDeletion() || |
82
|
|
|
$record2->isPendingDeletion() |
83
|
|
|
) { |
84
|
|
|
return Registry::responseFactory()->redirect(MergeRecordsPage::class, [ |
85
|
|
|
'tree' => $tree->name(), |
86
|
|
|
'xref1' => $xref1, |
87
|
|
|
'xref2' => $xref2, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// If we are not auto-accepting, then we can show a link to the pending deletion |
92
|
|
|
if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { |
93
|
|
|
$record2_name = $record2->fullName(); |
94
|
|
|
} else { |
95
|
|
|
$record2_name = '<a class="alert-link" href="' . e($record2->url()) . '">' . $record2->fullName() . '</a>'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Update records that link to the one we will be removing. |
99
|
|
|
$linking_records = $this->linked_record_service->allLinkedRecords($record2); |
100
|
|
|
|
101
|
|
|
foreach ($linking_records as $record) { |
102
|
|
|
if (!$record->isPendingDeletion()) { |
103
|
|
|
/* I18N: The placeholders are the names of individuals, sources, etc. */ |
104
|
|
|
FlashMessages::addMessage(I18N::translate( |
105
|
|
|
'The link from “%1$s” to “%2$s” has been updated.', |
106
|
|
|
'<a class="alert-link" href="' . e($record->url()) . '">' . $record->fullName() . '</a>', |
107
|
|
|
$record2_name |
108
|
|
|
), 'info'); |
109
|
|
|
$gedcom = str_replace('@' . $xref2 . '@', '@' . $xref1 . '@', $record->gedcom()); |
110
|
|
|
$gedcom = preg_replace( |
111
|
|
|
'/(\n1.*@.+@.*(?:\n[2-9].*)*)((?:\n1.*(?:\n[2-9].*)*)*\1)/', |
112
|
|
|
'$2', |
113
|
|
|
$gedcom |
114
|
|
|
); |
115
|
|
|
$record->updateRecord($gedcom, true); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Update any linked user-accounts |
120
|
|
|
DB::table('user_gedcom_setting') |
121
|
|
|
->where('gedcom_id', '=', $tree->id()) |
122
|
|
|
->whereIn('setting_name', [UserInterface::PREF_TREE_ACCOUNT_XREF, UserInterface::PREF_TREE_DEFAULT_XREF]) |
123
|
|
|
->where('setting_value', '=', $xref2) |
124
|
|
|
->update(['setting_value' => $xref1]); |
125
|
|
|
|
126
|
|
|
// Merge stories, etc. |
127
|
|
|
DB::table('block') |
128
|
|
|
->where('gedcom_id', '=', $tree->id()) |
129
|
|
|
->where('xref', '=', $xref2) |
130
|
|
|
->update(['xref' => $xref1]); |
131
|
|
|
|
132
|
|
|
// Merge hit counters |
133
|
|
|
$hits = DB::table('hit_counter') |
134
|
|
|
->where('gedcom_id', '=', $tree->id()) |
135
|
|
|
->whereIn('page_parameter', [$xref1, $xref2]) |
136
|
|
|
->groupBy(['page_name']) |
137
|
|
|
->pluck(new Expression('SUM(page_count)'), 'page_name'); |
138
|
|
|
|
139
|
|
|
foreach ($hits as $page_name => $page_count) { |
140
|
|
|
DB::table('hit_counter') |
141
|
|
|
->where('gedcom_id', '=', $tree->id()) |
142
|
|
|
->where('page_name', '=', $page_name) |
143
|
|
|
->where('page_parameter', '=', $xref1) |
144
|
|
|
->update(['page_count' => $page_count]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
DB::table('hit_counter') |
148
|
|
|
->where('gedcom_id', '=', $tree->id()) |
149
|
|
|
->where('page_parameter', '=', $xref2) |
150
|
|
|
->delete(); |
151
|
|
|
|
152
|
|
|
$gedcom = '0 @' . $record1->xref() . '@ ' . $record1->tag(); |
153
|
|
|
|
154
|
|
|
foreach ($record1->facts() as $fact) { |
155
|
|
|
if (in_array($fact->id(), $keep1, true)) { |
156
|
|
|
$gedcom .= "\n" . $fact->gedcom(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ($record2->facts() as $fact) { |
161
|
|
|
if (in_array($fact->id(), $keep2, true)) { |
162
|
|
|
$gedcom .= "\n" . $fact->gedcom(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
DB::table('favorite') |
167
|
|
|
->where('gedcom_id', '=', $tree->id()) |
168
|
|
|
->where('xref', '=', $xref2) |
169
|
|
|
->update(['xref' => $xref1]); |
170
|
|
|
|
171
|
|
|
$record1->updateRecord($gedcom, true); |
172
|
|
|
$record2->deleteRecord(); |
173
|
|
|
|
174
|
|
|
/* I18N: Records are individuals, sources, etc. */ |
175
|
|
|
FlashMessages::addMessage(I18N::translate( |
176
|
|
|
'The records “%1$s” and “%2$s” have been merged.', |
177
|
|
|
'<a class="alert-link" href="' . e($record1->url()) . '">' . $record1->fullName() . '</a>', |
178
|
|
|
$record2_name |
179
|
|
|
), 'success'); |
180
|
|
|
|
181
|
|
|
return Registry::responseFactory()->redirect(ManageTrees::class, ['tree' => $tree->name()]); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|