xref: /webtrees/app/Module/FixPlaceNames.php (revision 29a51599e39417638370fe4c2c4d77201726c9ff)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5ce42304aSGreg Roach * Copyright (C) 2019 webtrees development team
6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify
7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by
8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ce42304aSGreg Roach * (at your option) any later version.
10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful,
11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ce42304aSGreg Roach * GNU General Public License for more details.
14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License
15ce42304aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Module;
21ce42304aSGreg Roach
22*29a51599SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
24*29a51599SGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
26*29a51599SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27ce42304aSGreg Roachuse Illuminate\Support\Collection;
28*29a51599SGreg Roach
29*29a51599SGreg Roachuse function addcslashes;
30*29a51599SGreg Roachuse function preg_match;
31*29a51599SGreg Roachuse function preg_quote;
32*29a51599SGreg Roachuse function preg_replace;
33*29a51599SGreg Roachuse function view;
34ce42304aSGreg Roach
35ce42304aSGreg Roach/**
36ce42304aSGreg Roach * Class FixPlaceNames
37ce42304aSGreg Roach */
38ce42304aSGreg Roachclass FixPlaceNames extends AbstractModule implements ModuleDataFixInterface
39ce42304aSGreg Roach{
40ce42304aSGreg Roach    use ModuleDataFixTrait;
41ce42304aSGreg Roach
42*29a51599SGreg Roach    /** @var DataFixService */
43*29a51599SGreg Roach    private $data_fix_service;
44*29a51599SGreg Roach
45*29a51599SGreg Roach    /**
46*29a51599SGreg Roach     * FixMissingDeaths constructor.
47*29a51599SGreg Roach     *
48*29a51599SGreg Roach     * @param DataFixService $data_fix_service
49*29a51599SGreg Roach     */
50*29a51599SGreg Roach    public function __construct(DataFixService $data_fix_service)
51*29a51599SGreg Roach    {
52*29a51599SGreg Roach        $this->data_fix_service = $data_fix_service;
53*29a51599SGreg Roach    }
54*29a51599SGreg Roach
55ce42304aSGreg Roach    /**
56ce42304aSGreg Roach     * How should this module be identified in the control panel, etc.?
57ce42304aSGreg Roach     *
58ce42304aSGreg Roach     * @return string
59ce42304aSGreg Roach     */
60ce42304aSGreg Roach    public function title(): string
61ce42304aSGreg Roach    {
62ce42304aSGreg Roach        /* I18N: Name of a module */
63ce42304aSGreg Roach        return I18N::translate('Update place names');
64ce42304aSGreg Roach    }
65ce42304aSGreg Roach
66ce42304aSGreg Roach    /**
67ce42304aSGreg Roach     * A sentence describing what this module does.
68ce42304aSGreg Roach     *
69ce42304aSGreg Roach     * @return string
70ce42304aSGreg Roach     */
71ce42304aSGreg Roach    public function description(): string
72ce42304aSGreg Roach    {
73ce42304aSGreg Roach        /* I18N: Description of a “Data fix” module */
74ce42304aSGreg Roach        return I18N::translate('Update the higher-level parts of place names, while keeping the lower-level parts.');
75ce42304aSGreg Roach    }
76ce42304aSGreg Roach
77ce42304aSGreg Roach    /**
78*29a51599SGreg Roach     * Options form.
79*29a51599SGreg Roach     *
80*29a51599SGreg Roach     * @param Tree $tree
81*29a51599SGreg Roach     *
82*29a51599SGreg Roach     * @return string
83*29a51599SGreg Roach     */
84*29a51599SGreg Roach    public function fixOptions(Tree $tree): string
85*29a51599SGreg Roach    {
86*29a51599SGreg Roach        return view('modules/fix-place-names/options', []);
87*29a51599SGreg Roach    }
88*29a51599SGreg Roach
89*29a51599SGreg Roach    /**
90ce42304aSGreg Roach     * A list of all records that need examining.  This may include records
91ce42304aSGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
92ce42304aSGreg Roach     *
93ce42304aSGreg Roach     * @param Tree                 $tree
94ce42304aSGreg Roach     * @param array<string,string> $params
95ce42304aSGreg Roach     *
96*29a51599SGreg Roach     * @return Collection<string>|null
97ce42304aSGreg Roach     */
98*29a51599SGreg Roach    protected function familiesToFix(Tree $tree, array $params): ?Collection
99ce42304aSGreg Roach    {
100*29a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
101*29a51599SGreg Roach            return null;
102*29a51599SGreg Roach        }
103*29a51599SGreg Roach
104*29a51599SGreg Roach        $search = '%' . addcslashes($params['search'], '_%\\') . '%';
105*29a51599SGreg Roach
106*29a51599SGreg Roach        return  DB::table('families')
107*29a51599SGreg Roach            ->where('f_file', '=', $tree->id())
108*29a51599SGreg Roach            ->where('f_gedcom', 'LIKE', $search)
109*29a51599SGreg Roach            ->pluck('f_id');
110*29a51599SGreg Roach    }
111*29a51599SGreg Roach
112*29a51599SGreg Roach    /**
113*29a51599SGreg Roach     * A list of all records that need examining.  This may include records
114*29a51599SGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
115*29a51599SGreg Roach     *
116*29a51599SGreg Roach     * @param Tree                 $tree
117*29a51599SGreg Roach     * @param array<string,string> $params
118*29a51599SGreg Roach     *
119*29a51599SGreg Roach     * @return Collection<string>|null
120*29a51599SGreg Roach     */
121*29a51599SGreg Roach    protected function individualsToFix(Tree $tree, array $params): ?Collection
122*29a51599SGreg Roach    {
123*29a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
124*29a51599SGreg Roach            return null;
125*29a51599SGreg Roach        }
126*29a51599SGreg Roach
127*29a51599SGreg Roach        $search = '%' . addcslashes($params['search'], '_%\\') . '%';
128*29a51599SGreg Roach
129*29a51599SGreg Roach        return  DB::table('individuals')
130*29a51599SGreg Roach            ->where('i_file', '=', $tree->id())
131*29a51599SGreg Roach            ->where('i_gedcom', 'LIKE', $search)
132*29a51599SGreg Roach            ->pluck('i_id');
133*29a51599SGreg Roach    }
134*29a51599SGreg Roach
135*29a51599SGreg Roach    /**
136*29a51599SGreg Roach     * Does a record need updating?
137*29a51599SGreg Roach     *
138*29a51599SGreg Roach     * @param GedcomRecord         $record
139*29a51599SGreg Roach     * @param array<string,string> $params
140*29a51599SGreg Roach     *
141*29a51599SGreg Roach     * @return bool
142*29a51599SGreg Roach     */
143*29a51599SGreg Roach    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
144*29a51599SGreg Roach    {
145*29a51599SGreg Roach        $search = preg_quote($params['search'], '/');
146*29a51599SGreg Roach        $regex  = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/';
147*29a51599SGreg Roach
148*29a51599SGreg Roach        return preg_match($regex, $record->gedcom()) === 1;
149*29a51599SGreg Roach    }
150*29a51599SGreg Roach
151*29a51599SGreg Roach    /**
152*29a51599SGreg Roach     * Show the changes we would make
153*29a51599SGreg Roach     *
154*29a51599SGreg Roach     * @param GedcomRecord         $record
155*29a51599SGreg Roach     * @param array<string,string> $params
156*29a51599SGreg Roach     *
157*29a51599SGreg Roach     * @return string
158*29a51599SGreg Roach     */
159*29a51599SGreg Roach    public function previewUpdate(GedcomRecord $record, array $params): string
160*29a51599SGreg Roach    {
161*29a51599SGreg Roach        $old = $record->gedcom();
162*29a51599SGreg Roach        $new = $this->updateGedcom($record, $params);
163*29a51599SGreg Roach
164*29a51599SGreg Roach        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
165*29a51599SGreg Roach    }
166*29a51599SGreg Roach
167*29a51599SGreg Roach    /**
168*29a51599SGreg Roach     * Fix a record
169*29a51599SGreg Roach     *
170*29a51599SGreg Roach     * @param GedcomRecord         $record
171*29a51599SGreg Roach     * @param array<string,string> $params
172*29a51599SGreg Roach     *
173*29a51599SGreg Roach     * @return void
174*29a51599SGreg Roach     */
175*29a51599SGreg Roach    public function updateRecord(GedcomRecord $record, array $params): void
176*29a51599SGreg Roach    {
177*29a51599SGreg Roach        $record->updateRecord($this->updateGedcom($record, $params), false);
178*29a51599SGreg Roach    }
179*29a51599SGreg Roach
180*29a51599SGreg Roach    /**
181*29a51599SGreg Roach     * @param GedcomRecord         $record
182*29a51599SGreg Roach     * @param array<string,string> $params
183*29a51599SGreg Roach     *
184*29a51599SGreg Roach     * @return string
185*29a51599SGreg Roach     */
186*29a51599SGreg Roach    private function updateGedcom(GedcomRecord $record, array $params): string
187*29a51599SGreg Roach    {
188*29a51599SGreg Roach        $search  = preg_quote($params['search'], '/');
189*29a51599SGreg Roach        $regex   = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/';
190*29a51599SGreg Roach        $replace = '$1' . addcslashes($params['replace'], '$\\') . '$2';
191*29a51599SGreg Roach
192*29a51599SGreg Roach        return preg_replace($regex, $replace, $record->gedcom());
193ce42304aSGreg Roach    }
194ce42304aSGreg Roach}
195