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