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