xref: /webtrees/app/Module/FixPlaceNames.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Module;
21ce42304aSGreg Roach
2229a51599SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
2429a51599SGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
26ce42304aSGreg Roachuse Illuminate\Support\Collection;
2729a51599SGreg Roach
2829a51599SGreg Roachuse function addcslashes;
2929a51599SGreg Roachuse function preg_match;
3029a51599SGreg Roachuse function preg_quote;
3129a51599SGreg Roachuse function preg_replace;
3229a51599SGreg Roachuse function view;
33ce42304aSGreg Roach
34ce42304aSGreg Roach/**
35ce42304aSGreg Roach * Class FixPlaceNames
36ce42304aSGreg Roach */
37ce42304aSGreg Roachclass FixPlaceNames extends AbstractModule implements ModuleDataFixInterface
38ce42304aSGreg Roach{
39ce42304aSGreg Roach    use ModuleDataFixTrait;
40ce42304aSGreg Roach
41c4943cffSGreg Roach    private DataFixService $data_fix_service;
4229a51599SGreg Roach
4329a51599SGreg Roach    /**
4429a51599SGreg Roach     * @param DataFixService $data_fix_service
4529a51599SGreg Roach     */
4629a51599SGreg Roach    public function __construct(DataFixService $data_fix_service)
4729a51599SGreg Roach    {
4829a51599SGreg Roach        $this->data_fix_service = $data_fix_service;
4929a51599SGreg Roach    }
5029a51599SGreg Roach
51ce42304aSGreg Roach    /**
52ce42304aSGreg Roach     * How should this module be identified in the control panel, etc.?
53ce42304aSGreg Roach     *
54ce42304aSGreg Roach     * @return string
55ce42304aSGreg Roach     */
56ce42304aSGreg Roach    public function title(): string
57ce42304aSGreg Roach    {
58ce42304aSGreg Roach        /* I18N: Name of a module */
59ce42304aSGreg Roach        return I18N::translate('Update place names');
60ce42304aSGreg Roach    }
61ce42304aSGreg Roach
62ce42304aSGreg Roach    public function description(): string
63ce42304aSGreg Roach    {
64ce42304aSGreg Roach        /* I18N: Description of a “Data fix” module */
65ce42304aSGreg Roach        return I18N::translate('Update the higher-level parts of place names, while keeping the lower-level parts.');
66ce42304aSGreg Roach    }
67ce42304aSGreg Roach
68ce42304aSGreg Roach    /**
6929a51599SGreg Roach     * Options form.
7029a51599SGreg Roach     *
7129a51599SGreg Roach     * @param Tree $tree
7229a51599SGreg Roach     *
7329a51599SGreg Roach     * @return string
7429a51599SGreg Roach     */
7529a51599SGreg Roach    public function fixOptions(Tree $tree): string
7629a51599SGreg Roach    {
7729a51599SGreg Roach        return view('modules/fix-place-names/options', []);
7829a51599SGreg Roach    }
7929a51599SGreg Roach
8029a51599SGreg Roach    /**
81ce42304aSGreg Roach     * A list of all records that need examining.  This may include records
82ce42304aSGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
83ce42304aSGreg Roach     *
84ce42304aSGreg Roach     * @param Tree                 $tree
85ce42304aSGreg Roach     * @param array<string,string> $params
86ce42304aSGreg Roach     *
8736779af1SGreg Roach     * @return Collection<int,string>|null
88ce42304aSGreg Roach     */
89*1ff45046SGreg Roach    protected function familiesToFix(Tree $tree, array $params): Collection|null
90ce42304aSGreg Roach    {
91748dbe15SGreg Roach        if ($params['search-for'] === '' || $params['replace-with'] === '') {
9229a51599SGreg Roach            return null;
9329a51599SGreg Roach        }
9429a51599SGreg Roach
95748dbe15SGreg Roach        $search = '%' . addcslashes($params['search-for'], '\\%_') . '%';
9629a51599SGreg Roach
977684867eSGreg Roach        return  $this->familiesToFixQuery($tree, $params)
9829a51599SGreg Roach            ->where('f_gedcom', 'LIKE', $search)
9929a51599SGreg Roach            ->pluck('f_id');
10029a51599SGreg Roach    }
10129a51599SGreg Roach
10229a51599SGreg Roach    /**
10329a51599SGreg Roach     * A list of all records that need examining.  This may include records
10429a51599SGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
10529a51599SGreg Roach     *
10629a51599SGreg Roach     * @param Tree                 $tree
10729a51599SGreg Roach     * @param array<string,string> $params
10829a51599SGreg Roach     *
10936779af1SGreg Roach     * @return Collection<int,string>|null
11029a51599SGreg Roach     */
111*1ff45046SGreg Roach    protected function individualsToFix(Tree $tree, array $params): Collection|null
11229a51599SGreg Roach    {
113748dbe15SGreg Roach        if ($params['search-for'] === '' || $params['replace-with'] === '') {
11429a51599SGreg Roach            return null;
11529a51599SGreg Roach        }
11629a51599SGreg Roach
117748dbe15SGreg Roach        $search = '%' . addcslashes($params['search-for'], '\\%_') . '%';
11829a51599SGreg Roach
1197684867eSGreg Roach        return  $this->individualsToFixQuery($tree, $params)
12029a51599SGreg Roach            ->where('i_file', '=', $tree->id())
12129a51599SGreg Roach            ->where('i_gedcom', 'LIKE', $search)
12229a51599SGreg Roach            ->pluck('i_id');
12329a51599SGreg Roach    }
12429a51599SGreg Roach
12529a51599SGreg Roach    /**
12629a51599SGreg Roach     * Does a record need updating?
12729a51599SGreg Roach     *
12829a51599SGreg Roach     * @param GedcomRecord         $record
12929a51599SGreg Roach     * @param array<string,string> $params
13029a51599SGreg Roach     *
13129a51599SGreg Roach     * @return bool
13229a51599SGreg Roach     */
13329a51599SGreg Roach    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
13429a51599SGreg Roach    {
135748dbe15SGreg Roach        $search = preg_quote($params['search-for'], '/');
13629a51599SGreg Roach        $regex  = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/';
13729a51599SGreg Roach
13829a51599SGreg Roach        return preg_match($regex, $record->gedcom()) === 1;
13929a51599SGreg Roach    }
14029a51599SGreg Roach
14129a51599SGreg Roach    /**
14229a51599SGreg Roach     * Show the changes we would make
14329a51599SGreg Roach     *
14429a51599SGreg Roach     * @param GedcomRecord         $record
14529a51599SGreg Roach     * @param array<string,string> $params
14629a51599SGreg Roach     *
14729a51599SGreg Roach     * @return string
14829a51599SGreg Roach     */
14929a51599SGreg Roach    public function previewUpdate(GedcomRecord $record, array $params): string
15029a51599SGreg Roach    {
15129a51599SGreg Roach        $old = $record->gedcom();
15229a51599SGreg Roach        $new = $this->updateGedcom($record, $params);
15329a51599SGreg Roach
15429a51599SGreg Roach        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
15529a51599SGreg Roach    }
15629a51599SGreg Roach
15729a51599SGreg Roach    /**
15829a51599SGreg Roach     * Fix a record
15929a51599SGreg Roach     *
16029a51599SGreg Roach     * @param GedcomRecord         $record
16129a51599SGreg Roach     * @param array<string,string> $params
16229a51599SGreg Roach     *
16329a51599SGreg Roach     * @return void
16429a51599SGreg Roach     */
16529a51599SGreg Roach    public function updateRecord(GedcomRecord $record, array $params): void
16629a51599SGreg Roach    {
16729a51599SGreg Roach        $record->updateRecord($this->updateGedcom($record, $params), false);
16829a51599SGreg Roach    }
16929a51599SGreg Roach
17029a51599SGreg Roach    /**
17129a51599SGreg Roach     * @param GedcomRecord         $record
17229a51599SGreg Roach     * @param array<string,string> $params
17329a51599SGreg Roach     *
17429a51599SGreg Roach     * @return string
17529a51599SGreg Roach     */
17629a51599SGreg Roach    private function updateGedcom(GedcomRecord $record, array $params): string
17729a51599SGreg Roach    {
178748dbe15SGreg Roach        $search  = preg_quote($params['search-for'], '/');
17929a51599SGreg Roach        $regex   = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/';
180748dbe15SGreg Roach        $replace = '$1' . addcslashes($params['replace-with'], '$\\') . '$2';
18129a51599SGreg Roach
18229a51599SGreg Roach        return preg_replace($regex, $replace, $record->gedcom());
183ce42304aSGreg Roach    }
184ce42304aSGreg Roach}
185