Issues (2560)

app/Module/FixMissingDeaths.php (3 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\Gedcom;
23
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...
24
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...
25
use Fisharebest\Webtrees\Individual;
0 ignored issues
show
The type Fisharebest\Webtrees\Individual 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...
26
use Fisharebest\Webtrees\Services\DataFixService;
27
use Fisharebest\Webtrees\Tree;
28
use Illuminate\Support\Collection;
29
30
class FixMissingDeaths extends AbstractModule implements ModuleDataFixInterface
31
{
32
    use ModuleDataFixTrait;
33
34
    private DataFixService $data_fix_service;
35
36
    /**
37
     * @param DataFixService $data_fix_service
38
     */
39
    public function __construct(DataFixService $data_fix_service)
40
    {
41
        $this->data_fix_service = $data_fix_service;
42
    }
43
44
    public function title(): string
45
    {
46
        /* I18N: Name of a module */
47
        return I18N::translate('Add missing death records');
48
    }
49
50
    public function description(): string
51
    {
52
        /* I18N: Description of a “Data fix” module */
53
        return I18N::translate('You can speed up the privacy calculations by adding a death record to individuals whose death can be inferred from other dates, but who do not have a record of death, burial, cremation, etc.');
54
    }
55
56
    /**
57
     * A list of all records that need examining.  This may include records
58
     * that do not need updating, if we can't detect this quickly using SQL.
59
     *
60
     * @param Tree                 $tree
61
     * @param array<string,string> $params
62
     *
63
     * @return Collection<int,string>|null
64
     */
65
    protected function individualsToFix(Tree $tree, array $params): Collection|null
66
    {
67
        $query = $this->individualsToFixQuery($tree, $params);
68
69
        foreach (Gedcom::DEATH_EVENTS as $event) {
70
            $query->where('i_gedcom', 'NOT LIKE', "%\n1 " . $event . '%');
71
        }
72
73
        return $query->pluck('i_id');
74
    }
75
76
    /**
77
     * Does a record need updating?
78
     *
79
     * @param GedcomRecord         $record
80
     * @param array<string,string> $params
81
     *
82
     * @return bool
83
     */
84
    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
85
    {
86
        return
87
            $record instanceof Individual &&
88
            $record->facts(Gedcom::DEATH_EVENTS, false, null, true)->isEmpty() &&
89
            $record->isDead();
90
    }
91
92
    /**
93
     * Show the changes we would make
94
     *
95
     * @param GedcomRecord         $record
96
     * @param array<string,string> $params
97
     *
98
     * @return string
99
     */
100
    public function previewUpdate(GedcomRecord $record, array $params): string
101
    {
102
        $old = $record->gedcom();
103
        $new = $this->updateGedcom($record);
104
105
        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
106
    }
107
108
    /**
109
     * Fix a record
110
     *
111
     * @param GedcomRecord         $record
112
     * @param array<string,string> $params
113
     *
114
     * @return void
115
     */
116
    public function updateRecord(GedcomRecord $record, array $params): void
117
    {
118
        $record->updateRecord($this->updateGedcom($record), false);
119
    }
120
121
    /**
122
     * @param GedcomRecord $record
123
     *
124
     * @return string
125
     */
126
    private function updateGedcom(GedcomRecord $record): string
127
    {
128
        return $record->gedcom() . "\n1 DEAT Y";
129
    }
130
}
131