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