1ce42304aSGreg Roach<?php 2ce42304aSGreg Roach 3ce42304aSGreg Roach/** 4ce42304aSGreg Roach * webtrees: online genealogy 5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify 7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by 8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9ce42304aSGreg Roach * (at your option) any later version. 10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful, 11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13ce42304aSGreg Roach * GNU General Public License for more details. 14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License 15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16ce42304aSGreg Roach */ 17ce42304aSGreg Roach 18ce42304aSGreg Roachdeclare(strict_types=1); 19ce42304aSGreg Roach 20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Module; 21ce42304aSGreg Roach 2229a51599SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N; 2429a51599SGreg Roachuse Fisharebest\Webtrees\Services\DataFixService; 25ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree; 26ce42304aSGreg Roachuse Illuminate\Support\Collection; 2729a51599SGreg Roach 2829a51599SGreg Roachuse function addcslashes; 2929a51599SGreg Roachuse function preg_match; 3029a51599SGreg Roachuse function preg_quote; 3129a51599SGreg Roachuse function preg_replace; 3229a51599SGreg Roachuse function view; 33ce42304aSGreg Roach 34ce42304aSGreg Roach/** 35ce42304aSGreg Roach * Class FixPlaceNames 36ce42304aSGreg Roach */ 37ce42304aSGreg Roachclass FixPlaceNames extends AbstractModule implements ModuleDataFixInterface 38ce42304aSGreg Roach{ 39ce42304aSGreg Roach use ModuleDataFixTrait; 40ce42304aSGreg Roach 4129a51599SGreg Roach /** @var DataFixService */ 4229a51599SGreg Roach private $data_fix_service; 4329a51599SGreg Roach 4429a51599SGreg Roach /** 4529a51599SGreg Roach * FixMissingDeaths constructor. 4629a51599SGreg Roach * 4729a51599SGreg Roach * @param DataFixService $data_fix_service 4829a51599SGreg Roach */ 4929a51599SGreg Roach public function __construct(DataFixService $data_fix_service) 5029a51599SGreg Roach { 5129a51599SGreg Roach $this->data_fix_service = $data_fix_service; 5229a51599SGreg Roach } 5329a51599SGreg Roach 54ce42304aSGreg Roach /** 55ce42304aSGreg Roach * How should this module be identified in the control panel, etc.? 56ce42304aSGreg Roach * 57ce42304aSGreg Roach * @return string 58ce42304aSGreg Roach */ 59ce42304aSGreg Roach public function title(): string 60ce42304aSGreg Roach { 61ce42304aSGreg Roach /* I18N: Name of a module */ 62ce42304aSGreg Roach return I18N::translate('Update place names'); 63ce42304aSGreg Roach } 64ce42304aSGreg Roach 65ce42304aSGreg Roach /** 66ce42304aSGreg Roach * A sentence describing what this module does. 67ce42304aSGreg Roach * 68ce42304aSGreg Roach * @return string 69ce42304aSGreg Roach */ 70ce42304aSGreg Roach public function description(): string 71ce42304aSGreg Roach { 72ce42304aSGreg Roach /* I18N: Description of a “Data fix” module */ 73ce42304aSGreg Roach return I18N::translate('Update the higher-level parts of place names, while keeping the lower-level parts.'); 74ce42304aSGreg Roach } 75ce42304aSGreg Roach 76ce42304aSGreg Roach /** 7729a51599SGreg Roach * Options form. 7829a51599SGreg Roach * 7929a51599SGreg Roach * @param Tree $tree 8029a51599SGreg Roach * 8129a51599SGreg Roach * @return string 8229a51599SGreg Roach */ 8329a51599SGreg Roach public function fixOptions(Tree $tree): string 8429a51599SGreg Roach { 8529a51599SGreg Roach return view('modules/fix-place-names/options', []); 8629a51599SGreg Roach } 8729a51599SGreg Roach 8829a51599SGreg Roach /** 89ce42304aSGreg Roach * A list of all records that need examining. This may include records 90ce42304aSGreg Roach * that do not need updating, if we can't detect this quickly using SQL. 91ce42304aSGreg Roach * 92ce42304aSGreg Roach * @param Tree $tree 93ce42304aSGreg Roach * @param array<string,string> $params 94ce42304aSGreg Roach * 9529a51599SGreg Roach * @return Collection<string>|null 96ce42304aSGreg Roach */ 9729a51599SGreg Roach protected function familiesToFix(Tree $tree, array $params): ?Collection 98ce42304aSGreg Roach { 9929a51599SGreg Roach if ($params['search'] === '' || $params['replace'] === '') { 10029a51599SGreg Roach return null; 10129a51599SGreg Roach } 10229a51599SGreg Roach 103b5961194SGreg Roach $search = '%' . addcslashes($params['search'], '\\%_') . '%'; 10429a51599SGreg Roach 1057684867eSGreg Roach return $this->familiesToFixQuery($tree, $params) 10629a51599SGreg Roach ->where('f_gedcom', 'LIKE', $search) 10729a51599SGreg Roach ->pluck('f_id'); 10829a51599SGreg Roach } 10929a51599SGreg Roach 11029a51599SGreg Roach /** 11129a51599SGreg Roach * A list of all records that need examining. This may include records 11229a51599SGreg Roach * that do not need updating, if we can't detect this quickly using SQL. 11329a51599SGreg Roach * 11429a51599SGreg Roach * @param Tree $tree 11529a51599SGreg Roach * @param array<string,string> $params 11629a51599SGreg Roach * 11729a51599SGreg Roach * @return Collection<string>|null 11829a51599SGreg Roach */ 11929a51599SGreg Roach protected function individualsToFix(Tree $tree, array $params): ?Collection 12029a51599SGreg Roach { 12129a51599SGreg Roach if ($params['search'] === '' || $params['replace'] === '') { 12229a51599SGreg Roach return null; 12329a51599SGreg Roach } 12429a51599SGreg Roach 125b5961194SGreg Roach $search = '%' . addcslashes($params['search'], '\\%_') . '%'; 12629a51599SGreg Roach 1277684867eSGreg Roach return $this->individualsToFixQuery($tree, $params) 12829a51599SGreg Roach ->where('i_file', '=', $tree->id()) 12929a51599SGreg Roach ->where('i_gedcom', 'LIKE', $search) 13029a51599SGreg Roach ->pluck('i_id'); 13129a51599SGreg Roach } 13229a51599SGreg Roach 13329a51599SGreg Roach /** 13429a51599SGreg Roach * Does a record need updating? 13529a51599SGreg Roach * 13629a51599SGreg Roach * @param GedcomRecord $record 13729a51599SGreg Roach * @param array<string,string> $params 13829a51599SGreg Roach * 13929a51599SGreg Roach * @return bool 14029a51599SGreg Roach */ 14129a51599SGreg Roach public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 14229a51599SGreg Roach { 14329a51599SGreg Roach $search = preg_quote($params['search'], '/'); 14429a51599SGreg Roach $regex = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/'; 14529a51599SGreg Roach 14629a51599SGreg Roach return preg_match($regex, $record->gedcom()) === 1; 14729a51599SGreg Roach } 14829a51599SGreg Roach 14929a51599SGreg Roach /** 15029a51599SGreg Roach * Show the changes we would make 15129a51599SGreg Roach * 15229a51599SGreg Roach * @param GedcomRecord $record 15329a51599SGreg Roach * @param array<string,string> $params 15429a51599SGreg Roach * 15529a51599SGreg Roach * @return string 15629a51599SGreg Roach */ 15729a51599SGreg Roach public function previewUpdate(GedcomRecord $record, array $params): string 15829a51599SGreg Roach { 15929a51599SGreg Roach $old = $record->gedcom(); 16029a51599SGreg Roach $new = $this->updateGedcom($record, $params); 16129a51599SGreg Roach 16229a51599SGreg Roach return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 16329a51599SGreg Roach } 16429a51599SGreg Roach 16529a51599SGreg Roach /** 16629a51599SGreg Roach * Fix a record 16729a51599SGreg Roach * 16829a51599SGreg Roach * @param GedcomRecord $record 16929a51599SGreg Roach * @param array<string,string> $params 17029a51599SGreg Roach * 17129a51599SGreg Roach * @return void 17229a51599SGreg Roach */ 17329a51599SGreg Roach public function updateRecord(GedcomRecord $record, array $params): void 17429a51599SGreg Roach { 17529a51599SGreg Roach $record->updateRecord($this->updateGedcom($record, $params), false); 17629a51599SGreg Roach } 17729a51599SGreg Roach 17829a51599SGreg Roach /** 17929a51599SGreg Roach * @param GedcomRecord $record 18029a51599SGreg Roach * @param array<string,string> $params 18129a51599SGreg Roach * 18229a51599SGreg Roach * @return string 18329a51599SGreg Roach */ 18429a51599SGreg Roach private function updateGedcom(GedcomRecord $record, array $params): string 18529a51599SGreg Roach { 18629a51599SGreg Roach $search = preg_quote($params['search'], '/'); 18729a51599SGreg Roach $regex = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/'; 18829a51599SGreg Roach $replace = '$1' . addcslashes($params['replace'], '$\\') . '$2'; 18929a51599SGreg Roach 19029a51599SGreg Roach return preg_replace($regex, $replace, $record->gedcom()); 191ce42304aSGreg Roach } 192ce42304aSGreg Roach} 193