xref: /webtrees/app/Module/FixPlaceNames.php (revision b5961194694c0b5b2dc4269689207eb972e3b20c)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5*b5961194SGreg Roach * Copyright (C) 2020 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
2229a51599SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
2429a51599SGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
2629a51599SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27ce42304aSGreg Roachuse Illuminate\Support\Collection;
2829a51599SGreg Roach
2929a51599SGreg Roachuse function addcslashes;
3029a51599SGreg Roachuse function preg_match;
3129a51599SGreg Roachuse function preg_quote;
3229a51599SGreg Roachuse function preg_replace;
3329a51599SGreg 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
4229a51599SGreg Roach    /** @var DataFixService */
4329a51599SGreg Roach    private $data_fix_service;
4429a51599SGreg Roach
4529a51599SGreg Roach    /**
4629a51599SGreg Roach     * FixMissingDeaths constructor.
4729a51599SGreg Roach     *
4829a51599SGreg Roach     * @param DataFixService $data_fix_service
4929a51599SGreg Roach     */
5029a51599SGreg Roach    public function __construct(DataFixService $data_fix_service)
5129a51599SGreg Roach    {
5229a51599SGreg Roach        $this->data_fix_service = $data_fix_service;
5329a51599SGreg Roach    }
5429a51599SGreg 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    /**
7829a51599SGreg Roach     * Options form.
7929a51599SGreg Roach     *
8029a51599SGreg Roach     * @param Tree $tree
8129a51599SGreg Roach     *
8229a51599SGreg Roach     * @return string
8329a51599SGreg Roach     */
8429a51599SGreg Roach    public function fixOptions(Tree $tree): string
8529a51599SGreg Roach    {
8629a51599SGreg Roach        return view('modules/fix-place-names/options', []);
8729a51599SGreg Roach    }
8829a51599SGreg Roach
8929a51599SGreg 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     *
9629a51599SGreg Roach     * @return Collection<string>|null
97ce42304aSGreg Roach     */
9829a51599SGreg Roach    protected function familiesToFix(Tree $tree, array $params): ?Collection
99ce42304aSGreg Roach    {
10029a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
10129a51599SGreg Roach            return null;
10229a51599SGreg Roach        }
10329a51599SGreg Roach
104*b5961194SGreg Roach        $search = '%' . addcslashes($params['search'], '\\%_') . '%';
10529a51599SGreg Roach
10629a51599SGreg Roach        return  DB::table('families')
10729a51599SGreg Roach            ->where('f_file', '=', $tree->id())
10829a51599SGreg Roach            ->where('f_gedcom', 'LIKE', $search)
10929a51599SGreg Roach            ->pluck('f_id');
11029a51599SGreg Roach    }
11129a51599SGreg Roach
11229a51599SGreg Roach    /**
11329a51599SGreg Roach     * A list of all records that need examining.  This may include records
11429a51599SGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
11529a51599SGreg Roach     *
11629a51599SGreg Roach     * @param Tree                 $tree
11729a51599SGreg Roach     * @param array<string,string> $params
11829a51599SGreg Roach     *
11929a51599SGreg Roach     * @return Collection<string>|null
12029a51599SGreg Roach     */
12129a51599SGreg Roach    protected function individualsToFix(Tree $tree, array $params): ?Collection
12229a51599SGreg Roach    {
12329a51599SGreg Roach        if ($params['search'] === '' || $params['replace'] === '') {
12429a51599SGreg Roach            return null;
12529a51599SGreg Roach        }
12629a51599SGreg Roach
127*b5961194SGreg Roach        $search = '%' . addcslashes($params['search'], '\\%_') . '%';
12829a51599SGreg Roach
12929a51599SGreg Roach        return  DB::table('individuals')
13029a51599SGreg Roach            ->where('i_file', '=', $tree->id())
13129a51599SGreg Roach            ->where('i_gedcom', 'LIKE', $search)
13229a51599SGreg Roach            ->pluck('i_id');
13329a51599SGreg Roach    }
13429a51599SGreg Roach
13529a51599SGreg Roach    /**
13629a51599SGreg Roach     * Does a record need updating?
13729a51599SGreg Roach     *
13829a51599SGreg Roach     * @param GedcomRecord         $record
13929a51599SGreg Roach     * @param array<string,string> $params
14029a51599SGreg Roach     *
14129a51599SGreg Roach     * @return bool
14229a51599SGreg Roach     */
14329a51599SGreg Roach    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
14429a51599SGreg Roach    {
14529a51599SGreg Roach        $search = preg_quote($params['search'], '/');
14629a51599SGreg Roach        $regex  = '/\n2 PLAC (?:.*, )?' . $search . '(\n|$)/';
14729a51599SGreg Roach
14829a51599SGreg Roach        return preg_match($regex, $record->gedcom()) === 1;
14929a51599SGreg Roach    }
15029a51599SGreg Roach
15129a51599SGreg Roach    /**
15229a51599SGreg Roach     * Show the changes we would make
15329a51599SGreg Roach     *
15429a51599SGreg Roach     * @param GedcomRecord         $record
15529a51599SGreg Roach     * @param array<string,string> $params
15629a51599SGreg Roach     *
15729a51599SGreg Roach     * @return string
15829a51599SGreg Roach     */
15929a51599SGreg Roach    public function previewUpdate(GedcomRecord $record, array $params): string
16029a51599SGreg Roach    {
16129a51599SGreg Roach        $old = $record->gedcom();
16229a51599SGreg Roach        $new = $this->updateGedcom($record, $params);
16329a51599SGreg Roach
16429a51599SGreg Roach        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
16529a51599SGreg Roach    }
16629a51599SGreg Roach
16729a51599SGreg Roach    /**
16829a51599SGreg Roach     * Fix a record
16929a51599SGreg Roach     *
17029a51599SGreg Roach     * @param GedcomRecord         $record
17129a51599SGreg Roach     * @param array<string,string> $params
17229a51599SGreg Roach     *
17329a51599SGreg Roach     * @return void
17429a51599SGreg Roach     */
17529a51599SGreg Roach    public function updateRecord(GedcomRecord $record, array $params): void
17629a51599SGreg Roach    {
17729a51599SGreg Roach        $record->updateRecord($this->updateGedcom($record, $params), false);
17829a51599SGreg Roach    }
17929a51599SGreg Roach
18029a51599SGreg Roach    /**
18129a51599SGreg Roach     * @param GedcomRecord         $record
18229a51599SGreg Roach     * @param array<string,string> $params
18329a51599SGreg Roach     *
18429a51599SGreg Roach     * @return string
18529a51599SGreg Roach     */
18629a51599SGreg Roach    private function updateGedcom(GedcomRecord $record, array $params): string
18729a51599SGreg Roach    {
18829a51599SGreg Roach        $search  = preg_quote($params['search'], '/');
18929a51599SGreg Roach        $regex   = '/(\n2 PLAC (?:.*, )?)' . $search . '(\n|$)/';
19029a51599SGreg Roach        $replace = '$1' . addcslashes($params['replace'], '$\\') . '$2';
19129a51599SGreg Roach
19229a51599SGreg Roach        return preg_replace($regex, $replace, $record->gedcom());
193ce42304aSGreg Roach    }
194ce42304aSGreg Roach}
195