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 FixNameSlashesAndSpaces 33 */ 34class FixNameSlashesAndSpaces extends AbstractModule implements ModuleDataFixInterface 35{ 36 use ModuleDataFixTrait; 37 38 private DataFixService $data_fix_service; 39 40 /** 41 * FixMissingDeaths constructor. 42 * 43 * @param DataFixService $data_fix_service 44 */ 45 public function __construct(DataFixService $data_fix_service) 46 { 47 $this->data_fix_service = $data_fix_service; 48 } 49 50 /** 51 * How should this module be identified in the control panel, etc.? 52 * 53 * @return string 54 */ 55 public function title(): string 56 { 57 /* I18N: Name of a module */ 58 return I18N::translate('Fix name slashes and spaces'); 59 } 60 61 /** 62 * A sentence describing what this module does. 63 * 64 * @return string 65 */ 66 public function description(): string 67 { 68 /* I18N: Description of a “Data fix” module */ 69 return I18N::translate('Correct NAME records of the form “John/DOE/” or “John /DOE”, as produced by older genealogy programs.'); 70 } 71 72 /** 73 * A list of all records that need examining. This may include records 74 * that do not need updating, if we can't detect this quickly using SQL. 75 * 76 * @param Tree $tree 77 * @param array<string,string> $params 78 * 79 * @return Collection<string>|null 80 */ 81 protected function individualsToFix(Tree $tree, array $params): ?Collection 82 { 83 // No DB querying possible? Select all. 84 return $this->individualsToFixQuery($tree, $params) 85 ->pluck('i_id'); 86 } 87 88 /** 89 * Does a record need updating? 90 * 91 * @param GedcomRecord $record 92 * @param array<string,string> $params 93 * 94 * @return bool 95 */ 96 public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 97 { 98 $gedcom = $record->gedcom(); 99 100 return 101 preg_match('/^(?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*\/[^\/\n]*$/m', $gedcom) || 102 preg_match('/^(?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*[^\/ ]\//m', $gedcom); 103 } 104 105 /** 106 * Show the changes we would make 107 * 108 * @param GedcomRecord $record 109 * @param array<string,string> $params 110 * 111 * @return string 112 */ 113 public function previewUpdate(GedcomRecord $record, array $params): string 114 { 115 $old = $record->gedcom(); 116 $new = $this->updateGedcom($record); 117 118 return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 119 } 120 121 /** 122 * Fix a record 123 * 124 * @param GedcomRecord $record 125 * @param array<string,string> $params 126 * 127 * @return void 128 */ 129 public function updateRecord(GedcomRecord $record, array $params): void 130 { 131 $record->updateRecord($this->updateGedcom($record), false); 132 } 133 134 /** 135 * @param GedcomRecord $record 136 * 137 * @return string 138 */ 139 private function updateGedcom(GedcomRecord $record): string 140 { 141 $gedcom = $record->gedcom(); 142 $gedcom = preg_replace('/^((?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*\/[^\/\n]*)$/m', '$1/', $gedcom); 143 144 return preg_replace('/^((?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*[^\/ ])(\/)/m', '$1 $2', $gedcom); 145 } 146} 147