xref: /webtrees/app/Module/FixPlaceNames.php (revision 36779af1bd0601de7819554b13a393f6edb92507)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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     * FixMissingDeaths constructor.
4529a51599SGreg Roach     *
4629a51599SGreg Roach     * @param DataFixService $data_fix_service
4729a51599SGreg Roach     */
4829a51599SGreg Roach    public function __construct(DataFixService $data_fix_service)
4929a51599SGreg Roach    {
5029a51599SGreg Roach        $this->data_fix_service = $data_fix_service;
5129a51599SGreg Roach    }
5229a51599SGreg Roach
53ce42304aSGreg Roach    /**
54ce42304aSGreg Roach     * How should this module be identified in the control panel, etc.?
55ce42304aSGreg Roach     *
56ce42304aSGreg Roach     * @return string
57ce42304aSGreg Roach     */
58ce42304aSGreg Roach    public function title(): string
59ce42304aSGreg Roach    {
60ce42304aSGreg Roach        /* I18N: Name of a module */
61ce42304aSGreg Roach        return I18N::translate('Update place names');
62ce42304aSGreg Roach    }
63ce42304aSGreg Roach
64ce42304aSGreg Roach    /**
65ce42304aSGreg Roach     * A sentence describing what this module does.
66ce42304aSGreg Roach     *
67ce42304aSGreg Roach     * @return string
68ce42304aSGreg Roach     */
69ce42304aSGreg Roach    public function description(): string
70ce42304aSGreg Roach    {
71ce42304aSGreg Roach        /* I18N: Description of a “Data fix” module */
72ce42304aSGreg Roach        return I18N::translate('Update the higher-level parts of place names, while keeping the lower-level parts.');
73ce42304aSGreg Roach    }
74ce42304aSGreg Roach
75ce42304aSGreg Roach    /**
7629a51599SGreg Roach     * Options form.
7729a51599SGreg Roach     *
7829a51599SGreg Roach     * @param Tree $tree
7929a51599SGreg Roach     *
8029a51599SGreg Roach     * @return string
8129a51599SGreg Roach     */
8229a51599SGreg Roach    public function fixOptions(Tree $tree): string
8329a51599SGreg Roach    {
8429a51599SGreg Roach        return view('modules/fix-place-names/options', []);
8529a51599SGreg Roach    }
8629a51599SGreg Roach
8729a51599SGreg Roach    /**
88ce42304aSGreg Roach     * A list of all records that need examining.  This may include records
89ce42304aSGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
90ce42304aSGreg Roach     *
91ce42304aSGreg Roach     * @param Tree                 $tree
92ce42304aSGreg Roach     * @param array<string,string> $params
93ce42304aSGreg Roach     *
94*36779af1SGreg Roach     * @return Collection<int,string>|null
95ce42304aSGreg Roach     */
9629a51599SGreg Roach    protected function familiesToFix(Tree $tree, array $params): ?Collection
97ce42304aSGreg Roach    {
9829a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
9929a51599SGreg Roach            return null;
10029a51599SGreg Roach        }
10129a51599SGreg Roach
102b5961194SGreg Roach        $search = '%' . addcslashes($params['search'], '\\%_') . '%';
10329a51599SGreg Roach
1047684867eSGreg Roach        return  $this->familiesToFixQuery($tree, $params)
10529a51599SGreg Roach            ->where('f_gedcom', 'LIKE', $search)
10629a51599SGreg Roach            ->pluck('f_id');
10729a51599SGreg Roach    }
10829a51599SGreg Roach
10929a51599SGreg Roach    /**
11029a51599SGreg Roach     * A list of all records that need examining.  This may include records
11129a51599SGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
11229a51599SGreg Roach     *
11329a51599SGreg Roach     * @param Tree                 $tree
11429a51599SGreg Roach     * @param array<string,string> $params
11529a51599SGreg Roach     *
116*36779af1SGreg Roach     * @return Collection<int,string>|null
11729a51599SGreg Roach     */
11829a51599SGreg Roach    protected function individualsToFix(Tree $tree, array $params): ?Collection
11929a51599SGreg Roach    {
12029a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
12129a51599SGreg Roach            return null;
12229a51599SGreg Roach        }
12329a51599SGreg Roach
124b5961194SGreg Roach        $search = '%' . addcslashes($params['search'], '\\%_') . '%';
12529a51599SGreg Roach
1267684867eSGreg Roach        return  $this->individualsToFixQuery($tree, $params)
12729a51599SGreg Roach            ->where('i_file', '=', $tree->id())
12829a51599SGreg Roach            ->where('i_gedcom', 'LIKE', $search)
12929a51599SGreg Roach            ->pluck('i_id');
13029a51599SGreg Roach    }
13129a51599SGreg Roach
13229a51599SGreg Roach    /**
13329a51599SGreg Roach     * Does a record need updating?
13429a51599SGreg Roach     *
13529a51599SGreg Roach     * @param GedcomRecord         $record
13629a51599SGreg Roach     * @param array<string,string> $params
13729a51599SGreg Roach     *
13829a51599SGreg Roach     * @return bool
13929a51599SGreg Roach     */
14029a51599SGreg Roach    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
14129a51599SGreg Roach    {
14229a51599SGreg Roach        $search = preg_quote($params['search'], '/');
14329a51599SGreg Roach        $regex  = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/';
14429a51599SGreg Roach
14529a51599SGreg Roach        return preg_match($regex, $record->gedcom()) === 1;
14629a51599SGreg Roach    }
14729a51599SGreg Roach
14829a51599SGreg Roach    /**
14929a51599SGreg Roach     * Show the changes we would make
15029a51599SGreg Roach     *
15129a51599SGreg Roach     * @param GedcomRecord         $record
15229a51599SGreg Roach     * @param array<string,string> $params
15329a51599SGreg Roach     *
15429a51599SGreg Roach     * @return string
15529a51599SGreg Roach     */
15629a51599SGreg Roach    public function previewUpdate(GedcomRecord $record, array $params): string
15729a51599SGreg Roach    {
15829a51599SGreg Roach        $old = $record->gedcom();
15929a51599SGreg Roach        $new = $this->updateGedcom($record, $params);
16029a51599SGreg Roach
16129a51599SGreg Roach        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
16229a51599SGreg Roach    }
16329a51599SGreg Roach
16429a51599SGreg Roach    /**
16529a51599SGreg Roach     * Fix a record
16629a51599SGreg Roach     *
16729a51599SGreg Roach     * @param GedcomRecord         $record
16829a51599SGreg Roach     * @param array<string,string> $params
16929a51599SGreg Roach     *
17029a51599SGreg Roach     * @return void
17129a51599SGreg Roach     */
17229a51599SGreg Roach    public function updateRecord(GedcomRecord $record, array $params): void
17329a51599SGreg Roach    {
17429a51599SGreg Roach        $record->updateRecord($this->updateGedcom($record, $params), false);
17529a51599SGreg Roach    }
17629a51599SGreg Roach
17729a51599SGreg Roach    /**
17829a51599SGreg Roach     * @param GedcomRecord         $record
17929a51599SGreg Roach     * @param array<string,string> $params
18029a51599SGreg Roach     *
18129a51599SGreg Roach     * @return string
18229a51599SGreg Roach     */
18329a51599SGreg Roach    private function updateGedcom(GedcomRecord $record, array $params): string
18429a51599SGreg Roach    {
18529a51599SGreg Roach        $search  = preg_quote($params['search'], '/');
18629a51599SGreg Roach        $regex   = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/';
18729a51599SGreg Roach        $replace = '$1' . addcslashes($params['replace'], '$\\') . '$2';
18829a51599SGreg Roach
18929a51599SGreg Roach        return preg_replace($regex, $replace, $record->gedcom());
190ce42304aSGreg Roach    }
191ce42304aSGreg Roach}
192