Issues (2560)

app/Module/FixPlaceNames.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\GedcomRecord;
0 ignored issues
show
The type Fisharebest\Webtrees\GedcomRecord was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Services\DataFixService;
25
use Fisharebest\Webtrees\Tree;
26
use Illuminate\Support\Collection;
27
28
use function addcslashes;
29
use function preg_match;
30
use function preg_quote;
31
use function preg_replace;
32
use function view;
33
34
class FixPlaceNames 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
    public function title(): string
49
    {
50
        /* I18N: Name of a module */
51
        return I18N::translate('Update place names');
52
    }
53
54
    public function description(): string
55
    {
56
        /* I18N: Description of a “Data fix” module */
57
        return I18N::translate('Update the higher-level parts of place names, while keeping the lower-level parts.');
58
    }
59
60
    /**
61
     * Options form.
62
     *
63
     * @param Tree $tree
64
     *
65
     * @return string
66
     */
67
    public function fixOptions(Tree $tree): string
68
    {
69
        return view('modules/fix-place-names/options', []);
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<int,string>|null
80
     */
81
    protected function familiesToFix(Tree $tree, array $params): Collection|null
82
    {
83
        if ($params['search-for'] === '' || $params['replace-with'] === '') {
84
            return null;
85
        }
86
87
        $search = '%' . addcslashes($params['search-for'], '\\%_') . '%';
88
89
        return  $this->familiesToFixQuery($tree, $params)
90
            ->where('f_gedcom', 'LIKE', $search)
91
            ->pluck('f_id');
92
    }
93
94
    /**
95
     * A list of all records that need examining.  This may include records
96
     * that do not need updating, if we can't detect this quickly using SQL.
97
     *
98
     * @param Tree                 $tree
99
     * @param array<string,string> $params
100
     *
101
     * @return Collection<int,string>|null
102
     */
103
    protected function individualsToFix(Tree $tree, array $params): Collection|null
104
    {
105
        if ($params['search-for'] === '' || $params['replace-with'] === '') {
106
            return null;
107
        }
108
109
        $search = '%' . addcslashes($params['search-for'], '\\%_') . '%';
110
111
        return  $this->individualsToFixQuery($tree, $params)
112
            ->where('i_file', '=', $tree->id())
113
            ->where('i_gedcom', 'LIKE', $search)
114
            ->pluck('i_id');
115
    }
116
117
    /**
118
     * Does a record need updating?
119
     *
120
     * @param GedcomRecord         $record
121
     * @param array<string,string> $params
122
     *
123
     * @return bool
124
     */
125
    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
126
    {
127
        $search = preg_quote($params['search-for'], '/');
128
        $regex  = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/';
129
130
        return preg_match($regex, $record->gedcom()) === 1;
131
    }
132
133
    /**
134
     * Show the changes we would make
135
     *
136
     * @param GedcomRecord         $record
137
     * @param array<string,string> $params
138
     *
139
     * @return string
140
     */
141
    public function previewUpdate(GedcomRecord $record, array $params): string
142
    {
143
        $old = $record->gedcom();
144
        $new = $this->updateGedcom($record, $params);
145
146
        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
147
    }
148
149
    /**
150
     * Fix a record
151
     *
152
     * @param GedcomRecord         $record
153
     * @param array<string,string> $params
154
     *
155
     * @return void
156
     */
157
    public function updateRecord(GedcomRecord $record, array $params): void
158
    {
159
        $record->updateRecord($this->updateGedcom($record, $params), false);
160
    }
161
162
    /**
163
     * @param GedcomRecord         $record
164
     * @param array<string,string> $params
165
     *
166
     * @return string
167
     */
168
    private function updateGedcom(GedcomRecord $record, array $params): string
169
    {
170
        $search  = preg_quote($params['search-for'], '/');
171
        $regex   = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/';
172
        $replace = '$1' . addcslashes($params['replace-with'], '$\\') . '$2';
173
174
        return preg_replace($regex, $replace, $record->gedcom());
175
    }
176
}
177