1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 addcslashes; 29use function preg_match; 30use function preg_quote; 31use function preg_replace; 32use function view; 33 34/** 35 * Class FixPlaceNames 36 */ 37class FixPlaceNames extends AbstractModule implements ModuleDataFixInterface 38{ 39 use ModuleDataFixTrait; 40 41 private DataFixService $data_fix_service; 42 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('Update place names'); 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('Update the higher-level parts of place names, while keeping the lower-level parts.'); 71 } 72 73 /** 74 * Options form. 75 * 76 * @param Tree $tree 77 * 78 * @return string 79 */ 80 public function fixOptions(Tree $tree): string 81 { 82 return view('modules/fix-place-names/options', []); 83 } 84 85 /** 86 * A list of all records that need examining. This may include records 87 * that do not need updating, if we can't detect this quickly using SQL. 88 * 89 * @param Tree $tree 90 * @param array<string,string> $params 91 * 92 * @return Collection<int,string>|null 93 */ 94 protected function familiesToFix(Tree $tree, array $params): Collection|null 95 { 96 if ($params['search-for'] === '' || $params['replace-with'] === '') { 97 return null; 98 } 99 100 $search = '%' . addcslashes($params['search-for'], '\\%_') . '%'; 101 102 return $this->familiesToFixQuery($tree, $params) 103 ->where('f_gedcom', 'LIKE', $search) 104 ->pluck('f_id'); 105 } 106 107 /** 108 * A list of all records that need examining. This may include records 109 * that do not need updating, if we can't detect this quickly using SQL. 110 * 111 * @param Tree $tree 112 * @param array<string,string> $params 113 * 114 * @return Collection<int,string>|null 115 */ 116 protected function individualsToFix(Tree $tree, array $params): Collection|null 117 { 118 if ($params['search-for'] === '' || $params['replace-with'] === '') { 119 return null; 120 } 121 122 $search = '%' . addcslashes($params['search-for'], '\\%_') . '%'; 123 124 return $this->individualsToFixQuery($tree, $params) 125 ->where('i_file', '=', $tree->id()) 126 ->where('i_gedcom', 'LIKE', $search) 127 ->pluck('i_id'); 128 } 129 130 /** 131 * Does a record need updating? 132 * 133 * @param GedcomRecord $record 134 * @param array<string,string> $params 135 * 136 * @return bool 137 */ 138 public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 139 { 140 $search = preg_quote($params['search-for'], '/'); 141 $regex = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/'; 142 143 return preg_match($regex, $record->gedcom()) === 1; 144 } 145 146 /** 147 * Show the changes we would make 148 * 149 * @param GedcomRecord $record 150 * @param array<string,string> $params 151 * 152 * @return string 153 */ 154 public function previewUpdate(GedcomRecord $record, array $params): string 155 { 156 $old = $record->gedcom(); 157 $new = $this->updateGedcom($record, $params); 158 159 return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 160 } 161 162 /** 163 * Fix a record 164 * 165 * @param GedcomRecord $record 166 * @param array<string,string> $params 167 * 168 * @return void 169 */ 170 public function updateRecord(GedcomRecord $record, array $params): void 171 { 172 $record->updateRecord($this->updateGedcom($record, $params), false); 173 } 174 175 /** 176 * @param GedcomRecord $record 177 * @param array<string,string> $params 178 * 179 * @return string 180 */ 181 private function updateGedcom(GedcomRecord $record, array $params): string 182 { 183 $search = preg_quote($params['search-for'], '/'); 184 $regex = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/'; 185 $replace = '$1' . addcslashes($params['replace-with'], '$\\') . '$2'; 186 187 return preg_replace($regex, $replace, $record->gedcom()); 188 } 189} 190