Issues (2560)

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