1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2026 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Module;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Services\DataFixService;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submitter;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Throwable;
use function addcslashes;
use function asort;
use function preg_match;
use function preg_quote;
use function preg_replace;
use function view;
class FixSearchAndReplace extends AbstractModule implements ModuleDataFixInterface
{
use ModuleDataFixTrait;
// A regular expression that never matches.
private const string INVALID_REGEX = '/(?!)/';
private DataFixService $data_fix_service;
/**
* @param DataFixService $data_fix_service
*/
public function __construct(DataFixService $data_fix_service)
{
$this->data_fix_service = $data_fix_service;
}
public function title(): string
{
/* I18N: Name of a module */
return I18N::translate('Search and replace');
}
public function description(): string
{
/* I18N: Description of a “Data fix” module */
return I18N::translate('Search and replace text, using simple searches or advanced pattern matching.');
}
/**
* Options form.
*
* @param Tree $tree
*
* @return string
*/
public function fixOptions(Tree $tree): string
{
$methods = [
'exact' => I18N::translate('Match the exact text, even if it occurs in the middle of a word.'),
'words' => I18N::translate('Match the exact text, unless it occurs in the middle of a word.'),
'wildcards' => I18N::translate('Use a “?” to match a single character, use “*” to match zero or more characters.'),
/* I18N: https://en.wikipedia.org/wiki/Regular_expression */
'regex' => I18N::translate('Regular expression'),
];
$types = [
Family::RECORD_TYPE => I18N::translate('Families'),
Individual::RECORD_TYPE => I18N::translate('Individuals'),
Location::RECORD_TYPE => I18N::translate('Locations'),
Media::RECORD_TYPE => I18N::translate('Media objects'),
Note::RECORD_TYPE => I18N::translate('Notes'),
Repository::RECORD_TYPE => I18N::translate('Repositories'),
Source::RECORD_TYPE => I18N::translate('Sources'),
Submitter::RECORD_TYPE => I18N::translate('Submitters'),
];
asort($types);
return view('modules/fix-search-and-replace/options', [
'default_method' => 'exact',
'default_type' => Individual::RECORD_TYPE,
'methods' => $methods,
'types' => $types,
]);
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function familiesToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Family::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('families')->where('f_file', '=', $tree->id());
$this->recordQuery($query, 'f_gedcom', $params);
return $query->pluck('f_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function individualsToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Individual::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('individuals')
->where('i_file', '=', $tree->id());
$this->recordQuery($query, 'i_gedcom', $params);
return $query->pluck('i_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function locationsToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Location::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Location::RECORD_TYPE);
$this->recordQuery($query, 'o_gedcom', $params);
return $query->pluck('o_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function mediaToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Media::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('media')
->where('m_file', '=', $tree->id());
$this->recordQuery($query, 'm_gedcom', $params);
return $query->pluck('m_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function notesToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Note::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Note::RECORD_TYPE);
$this->recordQuery($query, 'o_gedcom', $params);
return $query->pluck('o_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function repositoriesToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Repository::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Repository::RECORD_TYPE);
$this->recordQuery($query, 'o_gedcom', $params);
return $query->pluck('o_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function sourcesToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Source::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = $this->sourcesToFixQuery($tree, $params);
$this->recordQuery($query, 's_gedcom', $params);
return $query->pluck('s_id');
}
/**
* A list of all records that need examining. This may include records
* that do not need updating, if we can't detect this quickly using SQL.
*
* @param Tree $tree
* @param array<string,string> $params
*
* @return Collection<int,string>|null
*/
protected function submittersToFix(Tree $tree, array $params): Collection|null
{
if ($params['type'] !== Submitter::RECORD_TYPE || $params['search-for'] === '') {
return null;
}
$query = $this->submittersToFixQuery($tree, $params);
$this->recordQuery($query, 'o_gedcom', $params);
return $query->pluck('o_id');
}
/**
* Does a record need updating?
*
* @param GedcomRecord $record
* @param array<string,string> $params
*
* @return bool
*/
public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
{
return preg_match($this->createRegex($params), $record->gedcom()) === 1;
}
/**
* Show the changes we would make
*
* @param GedcomRecord $record
* @param array<string,string> $params
*
* @return string
*/
public function previewUpdate(GedcomRecord $record, array $params): string
{
$old = $record->gedcom();
$new = $this->updateGedcom($record, $params);
return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
}
/**
* Fix a record
*
* @param GedcomRecord $record
* @param array<string,string> $params
*
* @return void
*/
public function updateRecord(GedcomRecord $record, array $params): void
{
$record->updateRecord($this->updateGedcom($record, $params), false);
}
/**
* @param GedcomRecord $record
* @param array<string,string> $params
*
* @return string
*/
private function updateGedcom(GedcomRecord $record, array $params): string
{
// Allow "\n" to indicate a line-feed in replacement text.
// Back-references such as $1, $2 are handled automatically.
$replace = strtr($params['replace-with'], ['\n' => "\n"]);
$regex = $this->createRegex($params);
return preg_replace($regex, $replace, $record->gedcom());
}
/**
* Create a regular expression from the search pattern.
*
* @param array<string,string> $params
*
* @return string
*/
private function createRegex(array $params): string
{
$search = $params['search-for'];
$method = $params['method'];
$case = $params['case'];
switch ($method) {
case 'exact':
return '/' . preg_quote($search, '/') . '/u' . $case;
case 'words':
return '/\b' . preg_quote($search, '/') . '\b/u' . $case;
case 'wildcards':
return '/\b' . strtr(preg_quote($search, '/'), ['\*' => '.*', '\?' => '.']) . '\b/u' . $case;
case 'regex':
$regex = '/' . addcslashes($search, '/') . '/u' . $case;
try {
// A valid regex on an empty string returns zero.
// An invalid regex on an empty string returns false and throws a warning.
preg_match($regex, '');
} catch (Throwable) {
$regex = self::INVALID_REGEX;
}
return $regex;
}
throw new HttpNotFoundException();
}
/**
* Create a regular expression from the search pattern.
*
* @param Builder $query
* @param string $column
* @param array<string,string> $params
*
* @return void
*/
private function recordQuery(Builder $query, string $column, array $params): void
{
$search = $params['search-for'];
$method = $params['method'];
$like = '%' . addcslashes($search, '\\%_') . '%';
switch ($method) {
case 'exact':
case 'words':
$query->where($column, 'LIKE', $like);
break;
case 'wildcards':
$like = strtr($like, ['?' => '_', '*' => '%']);
$query->where($column, 'LIKE', $like);
break;
case 'regex':
// Substituting newlines seems to be necessary on *some* versions
// of MySQL (e.g. 5.7), and harmless on others (e.g. 8.0).
$search = strtr($search, ['\n' => "\n"]);
$query->where($column, DB::regexOperator(), $search);
break;
}
}
}
|