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