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