xref: /webtrees/app/Module/FixDuplicateLinks.php (revision a9b613b19a7bebd6295716a08ffb33e94a76bd34)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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
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 preg_match;
29use function preg_replace;
30
31/**
32 * Class FixDuplicateLinks
33 */
34class FixDuplicateLinks 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    /**
49     * How should this module be identified in the control panel, etc.?
50     *
51     * @return string
52     */
53    public function title(): string
54    {
55        /* I18N: Name of a module */
56        return I18N::translate('Remove duplicate links');
57    }
58
59    /**
60     * A sentence describing what this module does.
61     *
62     * @return string
63     */
64    public function description(): string
65    {
66        /* I18N: Description of a “Data fix” module */
67        return I18N::translate('A common error is to have multiple links to the same record, for example listing the same child more than once in a family record.');
68    }
69
70    /**
71     * A list of all records that need examining.  This may include records
72     * that do not need updating, if we can't detect this quickly using SQL.
73     *
74     * @param Tree          $tree
75     * @param array<string> $params
76     *
77     * @return Collection<int,string>
78     */
79    protected function familiesToFix(Tree $tree, array $params): Collection
80    {
81        // No DB querying possible?  Select all.
82        return $this->familiesToFixQuery($tree, $params)
83            ->pluck('f_id');
84    }
85
86    /**
87     * A list of all records that need examining.  This may include records
88     * that do not need updating, if we can't detect this quickly using SQL.
89     *
90     * @param Tree                 $tree
91     * @param array<string,string> $params
92     *
93     * @return Collection<int,string>|null
94     */
95    protected function individualsToFix(Tree $tree, array $params): ?Collection
96    {
97        // No DB querying possible?  Select all.
98        return $this->individualsToFixQuery($tree, $params)
99            ->pluck('i_id');
100    }
101
102    /**
103     * A list of all records that need examining.  This may include records
104     * that do not need updating, if we can't detect this quickly using SQL.
105     *
106     * @param Tree                 $tree
107     * @param array<string,string> $params
108     *
109     * @return Collection<int,string>
110     */
111    protected function mediaToFix(Tree $tree, array $params): Collection
112    {
113        // No DB querying possible?  Select all.
114        return $this->mediaToFixQuery($tree, $params)
115            ->pluck('m_id');
116    }
117
118    /**
119     * A list of all records that need examining.  This may include records
120     * that do not need updating, if we can't detect this quickly using SQL.
121     *
122     * @param Tree                 $tree
123     * @param array<string,string> $params
124     *
125     * @return Collection<int,string>
126     */
127    protected function notesToFix(Tree $tree, array $params): Collection
128    {
129        // No DB querying possible?  Select all.
130        return $this->notesToFixQuery($tree, $params)
131            ->pluck('o_id');
132    }
133
134    /**
135     * A list of all records that need examining.  This may include records
136     * that do not need updating, if we can't detect this quickly using SQL.
137     *
138     * @param Tree                 $tree
139     * @param array<string,string> $params
140     *
141     * @return Collection<int,string>
142     */
143    protected function repositoriesToFix(Tree $tree, array $params): Collection
144    {
145        // No DB querying possible?  Select all.
146        return $this->repositoriesToFixQuery($tree, $params)
147            ->pluck('o_id');
148    }
149
150    /**
151     * A list of all records that need examining.  This may include records
152     * that do not need updating, if we can't detect this quickly using SQL.
153     *
154     * @param Tree                 $tree
155     * @param array<string,string> $params
156     *
157     * @return Collection<int,string>
158     */
159    protected function sourcesToFix(Tree $tree, array $params): Collection
160    {
161        // No DB querying possible?  Select all.
162        return $this->sourcesToFixQuery($tree, $params)
163            ->pluck('s_id');
164    }
165
166    /**
167     * A list of all records that need examining.  This may include records
168     * that do not need updating, if we can't detect this quickly using SQL.
169     *
170     * @param Tree                 $tree
171     * @param array<string,string> $params
172     *
173     * @return Collection<int,string>
174     */
175    protected function submittersToFix(Tree $tree, array $params): Collection
176    {
177        // No DB querying possible?  Select all.
178        return $this->submittersToFixQuery($tree, $params)
179            ->pluck('o_id');
180    }
181
182    /**
183     * Does a record need updating?
184     *
185     * @param GedcomRecord         $record
186     * @param array<string,string> $params
187     *
188     * @return bool
189     */
190    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
191    {
192        $gedcom = $record->gedcom();
193
194        return
195            preg_match('/(\n1.*@.+@.*(?:\n[2-9].*)*)(?:\n1.*(?:\n[2-9].*)*)*\1/', $gedcom) ||
196            preg_match('/(\n2.*@.+@.*(?:\n[3-9].*)*)(?:\n2.*(?:\n[3-9].*)*)*\1/', $gedcom) ||
197            preg_match('/(\n3.*@.+@.*(?:\n[4-9].*)*)(?:\n3.*(?:\n[4-9].*)*)*\1/', $gedcom);
198    }
199
200    /**
201     * Show the changes we would make
202     *
203     * @param GedcomRecord         $record
204     * @param array<string,string> $params
205     *
206     * @return string
207     */
208    public function previewUpdate(GedcomRecord $record, array $params): string
209    {
210        $old = $record->gedcom();
211        $new = $this->updateGedcom($record);
212
213        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
214    }
215
216    /**
217     * Fix a record
218     *
219     * @param GedcomRecord         $record
220     * @param array<string,string> $params
221     *
222     * @return void
223     */
224    public function updateRecord(GedcomRecord $record, array $params): void
225    {
226        $record->updateRecord($this->updateGedcom($record), false);
227    }
228
229    /**
230     * @param GedcomRecord $record
231     *
232     * @return string
233     */
234    private function updateGedcom(GedcomRecord $record): string
235    {
236        $gedcom = $record->gedcom();
237        $gedcom = preg_replace('/(\n1.*@.+@.*(?:\n[2-9].*)*)((?:\n1.*(?:\n[2-9].*)*)*\1)/', '$2', $gedcom);
238        $gedcom = preg_replace('/(\n2.*@.+@.*(?:\n[3-9].*)*)((?:\n2.*(?:\n[3-9].*)*)*\1)/', '$2', $gedcom);
239
240        return preg_replace('/(\n3.*@.+@.*(?:\n[4-9].*)*)((?:\n3.*(?:\n[4-9].*)*)*\1)/', '$2', $gedcom);
241    }
242}
243