xref: /webtrees/app/Module/FixNameSlashesAndSpaces.php (revision 627d7dbed7a67fd8982bad3a124c1f2d688138bc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Database\Capsule\Manager as DB;
27use Illuminate\Support\Collection;
28
29use function preg_match;
30use function preg_replace;
31
32/**
33 * Class FixNameSlashesAndSpaces
34 */
35class FixNameSlashesAndSpaces extends AbstractModule implements ModuleDataFixInterface
36{
37    use ModuleDataFixTrait;
38
39    /** @var DataFixService */
40    private $data_fix_service;
41
42    /**
43     * FixMissingDeaths constructor.
44     *
45     * @param DataFixService $data_fix_service
46     */
47    public function __construct(DataFixService $data_fix_service)
48    {
49        $this->data_fix_service = $data_fix_service;
50    }
51
52    /**
53     * How should this module be identified in the control panel, etc.?
54     *
55     * @return string
56     */
57    public function title(): string
58    {
59        /* I18N: Name of a module */
60        return I18N::translate('Fix name slashes and spaces');
61    }
62
63    /**
64     * A sentence describing what this module does.
65     *
66     * @return string
67     */
68    public function description(): string
69    {
70        /* I18N: Description of a “Data fix” module */
71        return I18N::translate('Correct NAME records of the form “John/DOE/” or “John /DOE”, as produced by older genealogy programs.');
72    }
73
74    /**
75     * A list of all records that need examining.  This may include records
76     * that do not need updating, if we can't detect this quickly using SQL.
77     *
78     * @param Tree                 $tree
79     * @param array<string,string> $params
80     *
81     * @return Collection<string>|null
82     */
83    protected function individualsToFix(Tree $tree, array $params): ?Collection
84    {
85        // No DB querying possible?  Select all.
86        return DB::table('individuals')
87            ->where('i_file', '=', $tree->id())
88            ->pluck('i_id');
89    }
90
91    /**
92     * Does a record need updating?
93     *
94     * @param GedcomRecord         $record
95     * @param array<string,string> $params
96     *
97     * @return bool
98     */
99    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
100    {
101        $gedcom = $record->gedcom();
102
103        return
104            preg_match('/^(?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*\/[^\/\n]*$/m', $gedcom) ||
105            preg_match('/^(?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*[^\/ ]\//m', $gedcom);
106    }
107
108    /**
109     * Show the changes we would make
110     *
111     * @param GedcomRecord         $record
112     * @param array<string,string> $params
113     *
114     * @return string
115     */
116    public function previewUpdate(GedcomRecord $record, array $params): string
117    {
118        $old = $record->gedcom();
119        $new = $this->updateGedcom($record);
120
121        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
122    }
123
124    /**
125     * Fix a record
126     *
127     * @param GedcomRecord         $record
128     * @param array<string,string> $params
129     *
130     * @return void
131     */
132    public function updateRecord(GedcomRecord $record, array $params): void
133    {
134        $record->updateRecord($this->updateGedcom($record), false);
135    }
136
137    /**
138     * @param GedcomRecord $record
139     *
140     * @return string
141     */
142    private function updateGedcom(GedcomRecord $record): string
143    {
144        return preg_replace([
145            '/^((?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*\/[^\/\n]*)$/m',
146            '/^((?:1 NAME|2 (?:FONE|ROMN|_MARNM|_AKA|_HEB)) [^\/\n]*[^\/ ])(\/)/m',
147        ], [
148            '$1/',
149            '$1 $2',
150        ], $record->gedcom());
151    }
152}
153