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