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\Module; 21 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Services\DataFixService; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Support\Collection; 27 28use function preg_match; 29use function preg_replace; 30 31/** 32 * Class FixDuplicateLinks 33 */ 34class FixDuplicateLinks extends AbstractModule implements ModuleDataFixInterface 35{ 36 use ModuleDataFixTrait; 37 38 /** @var DataFixService */ 39 private $data_fix_service; 40 41 /** 42 * FixMissingDeaths constructor. 43 * 44 * @param DataFixService $data_fix_service 45 */ 46 public function __construct(DataFixService $data_fix_service) 47 { 48 $this->data_fix_service = $data_fix_service; 49 } 50 51 /** 52 * How should this module be identified in the control panel, etc.? 53 * 54 * @return string 55 */ 56 public function title(): string 57 { 58 /* I18N: Name of a module */ 59 return I18N::translate('Remove duplicate links'); 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 /* I18N: Description of a “Data fix” module */ 70 return I18N::translate('A common error is to have multiple links to the same record, for example listing the same child more than once in a family record.'); 71 } 72 73 /** 74 * A list of all records that need examining. This may include records 75 * that do not need updating, if we can't detect this quickly using SQL. 76 * 77 * @param Tree $tree 78 * @param array<string> $params 79 * 80 * @return Collection<string> 81 */ 82 protected function familiesToFix(Tree $tree, array $params): Collection 83 { 84 // No DB querying possible? Select all. 85 return $this->familiesToFixQuery($tree, $params) 86 ->pluck('f_id'); 87 } 88 89 /** 90 * A list of all records that need examining. This may include records 91 * that do not need updating, if we can't detect this quickly using SQL. 92 * 93 * @param Tree $tree 94 * @param array<string,string> $params 95 * 96 * @return Collection<string>|null 97 */ 98 protected function individualsToFix(Tree $tree, array $params): ?Collection 99 { 100 // No DB querying possible? Select all. 101 return $this->individualsToFixQuery($tree, $params) 102 ->pluck('i_id'); 103 } 104 105 /** 106 * A list of all records that need examining. This may include records 107 * that do not need updating, if we can't detect this quickly using SQL. 108 * 109 * @param Tree $tree 110 * @param array<string,string> $params 111 * 112 * @return Collection<string> 113 */ 114 protected function mediaToFix(Tree $tree, array $params): Collection 115 { 116 // No DB querying possible? Select all. 117 return $this->mediaToFixQuery($tree, $params) 118 ->pluck('m_id'); 119 } 120 121 /** 122 * A list of all records that need examining. This may include records 123 * that do not need updating, if we can't detect this quickly using SQL. 124 * 125 * @param Tree $tree 126 * @param array<string,string> $params 127 * 128 * @return Collection<string> 129 */ 130 protected function notesToFix(Tree $tree, array $params): Collection 131 { 132 // No DB querying possible? Select all. 133 return $this->notesToFixQuery($tree, $params) 134 ->pluck('o_id'); 135 } 136 137 /** 138 * A list of all records that need examining. This may include records 139 * that do not need updating, if we can't detect this quickly using SQL. 140 * 141 * @param Tree $tree 142 * @param array<string,string> $params 143 * 144 * @return Collection<string> 145 */ 146 protected function repositoriesToFix(Tree $tree, array $params): Collection 147 { 148 // No DB querying possible? Select all. 149 return $this->repositoriesToFixQuery($tree, $params) 150 ->pluck('o_id'); 151 } 152 153 /** 154 * A list of all records that need examining. This may include records 155 * that do not need updating, if we can't detect this quickly using SQL. 156 * 157 * @param Tree $tree 158 * @param array<string,string> $params 159 * 160 * @return Collection<string> 161 */ 162 protected function sourcesToFix(Tree $tree, array $params): Collection 163 { 164 // No DB querying possible? Select all. 165 return $this->sourcesToFixQuery($tree, $params) 166 ->pluck('s_id'); 167 } 168 169 /** 170 * A list of all records that need examining. This may include records 171 * that do not need updating, if we can't detect this quickly using SQL. 172 * 173 * @param Tree $tree 174 * @param array<string,string> $params 175 * 176 * @return Collection<string> 177 */ 178 protected function submittersToFix(Tree $tree, array $params): Collection 179 { 180 // No DB querying possible? Select all. 181 return $this->submittersToFixQuery($tree, $params) 182 ->pluck('o_id'); 183 } 184 185 /** 186 * Does a record need updating? 187 * 188 * @param GedcomRecord $record 189 * @param array<string,string> $params 190 * 191 * @return bool 192 */ 193 public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 194 { 195 $gedcom = $record->gedcom(); 196 197 return 198 preg_match('/(\n1.*@.+@.*(?:(?:\n[2-9].*)*))(?:\n1.*(?:\n[2-9].*)*)*\1/', $gedcom) || 199 preg_match('/(\n2.*@.+@.*(?:(?:\n[3-9].*)*))(?:\n2.*(?:\n[3-9].*)*)*\1/', $gedcom) || 200 preg_match('/(\n3.*@.+@.*(?:(?:\n[4-9].*)*))(?:\n3.*(?:\n[4-9].*)*)*\1/', $gedcom); 201 } 202 203 /** 204 * Show the changes we would make 205 * 206 * @param GedcomRecord $record 207 * @param array<string,string> $params 208 * 209 * @return string 210 */ 211 public function previewUpdate(GedcomRecord $record, array $params): string 212 { 213 $old = $record->gedcom(); 214 $new = $this->updateGedcom($record); 215 216 return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 217 } 218 219 /** 220 * Fix a record 221 * 222 * @param GedcomRecord $record 223 * @param array<string,string> $params 224 * 225 * @return void 226 */ 227 public function updateRecord(GedcomRecord $record, array $params): void 228 { 229 $record->updateRecord($this->updateGedcom($record), false); 230 } 231 232 /** 233 * @param GedcomRecord $record 234 * 235 * @return string 236 */ 237 private function updateGedcom(GedcomRecord $record): string 238 { 239 return preg_replace([ 240 '/(\n1.*@.+@.*(?:(?:\n[2-9].*)*))((?:\n1.*(?:\n[2-9].*)*)*\1)/', 241 '/(\n2.*@.+@.*(?:(?:\n[3-9].*)*))((?:\n2.*(?:\n[3-9].*)*)*\1)/', 242 '/(\n3.*@.+@.*(?:(?:\n[4-9].*)*))((?:\n3.*(?:\n[4-9].*)*)*\1)/', 243 ], '$2', $record->gedcom()); 244 } 245} 246