xref: /webtrees/app/Http/RequestHandlers/RenumberTreeAction.php (revision 1fe542e96f8f7eedeebc278fae1e0ab0d9e74d95)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5*1fe542e9SGreg Roach * Copyright (C) 2021 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
156fd01894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
22*1fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
236fd01894SGreg Roachuse Fisharebest\Webtrees\Family;
246fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
256fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
266fd01894SGreg Roachuse Fisharebest\Webtrees\Individual;
276fd01894SGreg Roachuse Fisharebest\Webtrees\Media;
286fd01894SGreg Roachuse Fisharebest\Webtrees\Note;
296b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
306fd01894SGreg Roachuse Fisharebest\Webtrees\Repository;
316fd01894SGreg Roachuse Fisharebest\Webtrees\Services\AdminService;
326fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
336fd01894SGreg Roachuse Fisharebest\Webtrees\Source;
346fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
356fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
366fd01894SGreg Roachuse Illuminate\Database\Query\Expression;
376fd01894SGreg Roachuse Illuminate\Database\Query\JoinClause;
386fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
396fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
406fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
416fd01894SGreg Roach
426fd01894SGreg Roachuse function assert;
436fd01894SGreg Roachuse function redirect;
446fd01894SGreg Roachuse function route;
456fd01894SGreg Roach
466fd01894SGreg Roach/**
476fd01894SGreg Roach * Renumber the XREFs in a family tree.
486fd01894SGreg Roach */
496fd01894SGreg Roachclass RenumberTreeAction implements RequestHandlerInterface
506fd01894SGreg Roach{
516fd01894SGreg Roach    /** @var AdminService */
526fd01894SGreg Roach    private $admin_service;
536fd01894SGreg Roach
546fd01894SGreg Roach    /** @var TimeoutService */
556fd01894SGreg Roach    private $timeout_service;
566fd01894SGreg Roach
576fd01894SGreg Roach    /**
586fd01894SGreg Roach     * @param AdminService   $admin_service
596fd01894SGreg Roach     * @param TimeoutService $timeout_service
606fd01894SGreg Roach     */
616fd01894SGreg Roach    public function __construct(AdminService $admin_service, TimeoutService $timeout_service)
626fd01894SGreg Roach    {
636fd01894SGreg Roach        $this->admin_service   = $admin_service;
646fd01894SGreg Roach        $this->timeout_service = $timeout_service;
656fd01894SGreg Roach    }
666fd01894SGreg Roach
676fd01894SGreg Roach    /**
686fd01894SGreg Roach     * @param ServerRequestInterface $request
696fd01894SGreg Roach     *
706fd01894SGreg Roach     * @return ResponseInterface
716fd01894SGreg Roach     */
726fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
736fd01894SGreg Roach    {
746fd01894SGreg Roach        $tree = $request->getAttribute('tree');
756fd01894SGreg Roach        assert($tree instanceof Tree);
766fd01894SGreg Roach
776fd01894SGreg Roach        $xrefs = $this->admin_service->duplicateXrefs($tree);
786fd01894SGreg Roach
796fd01894SGreg Roach        foreach ($xrefs as $old_xref => $type) {
806b9cb339SGreg Roach            $new_xref = Registry::xrefFactory()->make($type);
816fd01894SGreg Roach            switch ($type) {
826fd01894SGreg Roach                case Individual::RECORD_TYPE:
836fd01894SGreg Roach                    DB::table('individuals')
846fd01894SGreg Roach                        ->where('i_file', '=', $tree->id())
856fd01894SGreg Roach                        ->where('i_id', '=', $old_xref)
866fd01894SGreg Roach                        ->update([
876fd01894SGreg Roach                            'i_id'     => $new_xref,
886fd01894SGreg Roach                            'i_gedcom' => new Expression("REPLACE(i_gedcom, '0 @$old_xref@ INDI', '0 @$new_xref@ INDI')"),
896fd01894SGreg Roach                        ]);
906fd01894SGreg Roach
916fd01894SGreg Roach                    DB::table('families')
926fd01894SGreg Roach                        ->where('f_husb', '=', $old_xref)
936fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
946fd01894SGreg Roach                        ->update([
956fd01894SGreg Roach                            'f_husb'   => $new_xref,
966fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' HUSB @$old_xref@', ' HUSB @$new_xref@')"),
976fd01894SGreg Roach                        ]);
986fd01894SGreg Roach
996fd01894SGreg Roach                    DB::table('families')
1006fd01894SGreg Roach                        ->where('f_wife', '=', $old_xref)
1016fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
1026fd01894SGreg Roach                        ->update([
1036fd01894SGreg Roach                            'f_wife'   => $new_xref,
1046fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' WIFE @$old_xref@', ' WIFE @$new_xref@')"),
1056fd01894SGreg Roach                        ]);
1066fd01894SGreg Roach
1076fd01894SGreg Roach                    // Other links from families to individuals
1086fd01894SGreg Roach                    foreach (['CHIL', 'ASSO', '_ASSO'] as $tag) {
1096fd01894SGreg Roach                        DB::table('families')
1106fd01894SGreg Roach                            ->join('link', static function (JoinClause $join): void {
1116fd01894SGreg Roach                                $join
1126fd01894SGreg Roach                                    ->on('l_file', '=', 'f_file')
1136fd01894SGreg Roach                                    ->on('l_from', '=', 'f_id');
1146fd01894SGreg Roach                            })
1156fd01894SGreg Roach                            ->where('l_to', '=', $old_xref)
1166fd01894SGreg Roach                            ->where('l_type', '=', $tag)
1176fd01894SGreg Roach                            ->where('f_file', '=', $tree->id())
1186fd01894SGreg Roach                            ->update([
1196fd01894SGreg Roach                                'f_gedcom' => new Expression("REPLACE(f_gedcom, ' $tag @$old_xref@', ' $tag @$new_xref@')"),
1206fd01894SGreg Roach                            ]);
1216fd01894SGreg Roach                    }
1226fd01894SGreg Roach
1236fd01894SGreg Roach                    // Links from individuals to individuals
1246fd01894SGreg Roach                    foreach (['ALIA', 'ASSO', '_ASSO'] as $tag) {
1256fd01894SGreg Roach                        DB::table('individuals')
1266fd01894SGreg Roach                            ->join('link', static function (JoinClause $join): void {
1276fd01894SGreg Roach                                $join
1286fd01894SGreg Roach                                    ->on('l_file', '=', 'i_file')
1296fd01894SGreg Roach                                    ->on('l_from', '=', 'i_id');
1306fd01894SGreg Roach                            })
1316fd01894SGreg Roach                            ->where('link.l_to', '=', $old_xref)
1326fd01894SGreg Roach                            ->where('link.l_type', '=', $tag)
1336fd01894SGreg Roach                            ->where('i_file', '=', $tree->id())
1346fd01894SGreg Roach                            ->update([
1356fd01894SGreg Roach                                'i_gedcom' => new Expression("REPLACE(i_gedcom, ' $tag @$old_xref@', ' $tag @$new_xref@')"),
1366fd01894SGreg Roach                            ]);
1376fd01894SGreg Roach                    }
1386fd01894SGreg Roach
1396fd01894SGreg Roach                    DB::table('placelinks')
1406fd01894SGreg Roach                        ->where('pl_file', '=', $tree->id())
1416fd01894SGreg Roach                        ->where('pl_gid', '=', $old_xref)
1426fd01894SGreg Roach                        ->update([
1436fd01894SGreg Roach                            'pl_gid' => $new_xref,
1446fd01894SGreg Roach                        ]);
1456fd01894SGreg Roach
1466fd01894SGreg Roach                    DB::table('dates')
1476fd01894SGreg Roach                        ->where('d_file', '=', $tree->id())
1486fd01894SGreg Roach                        ->where('d_gid', '=', $old_xref)
1496fd01894SGreg Roach                        ->update([
1506fd01894SGreg Roach                            'd_gid' => $new_xref,
1516fd01894SGreg Roach                        ]);
1526fd01894SGreg Roach
1536fd01894SGreg Roach                    DB::table('user_gedcom_setting')
1546fd01894SGreg Roach                        ->where('gedcom_id', '=', $tree->id())
1556fd01894SGreg Roach                        ->where('setting_value', '=', $old_xref)
156*1fe542e9SGreg Roach                        ->whereIn('setting_name', [UserInterface::PREF_TREE_ACCOUNT_XREF, UserInterface::PREF_TREE_DEFAULT_XREF])
1576fd01894SGreg Roach                        ->update([
1586fd01894SGreg Roach                            'setting_value' => $new_xref,
1596fd01894SGreg Roach                        ]);
1606fd01894SGreg Roach                    break;
1616fd01894SGreg Roach
1626fd01894SGreg Roach                case Family::RECORD_TYPE:
1636fd01894SGreg Roach                    DB::table('families')
1646fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
1656fd01894SGreg Roach                        ->where('f_id', '=', $old_xref)
1666fd01894SGreg Roach                        ->update([
1676fd01894SGreg Roach                            'f_id'     => $new_xref,
1686fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, '0 @$old_xref@ FAM', '0 @$new_xref@ FAM')"),
1696fd01894SGreg Roach                        ]);
1706fd01894SGreg Roach
1716fd01894SGreg Roach                    // Links from individuals to families
1726fd01894SGreg Roach                    foreach (['FAMC', 'FAMS'] as $tag) {
1736fd01894SGreg Roach                        DB::table('individuals')
1746fd01894SGreg Roach                            ->join('link', static function (JoinClause $join): void {
1756fd01894SGreg Roach                                $join
1766fd01894SGreg Roach                                    ->on('l_file', '=', 'i_file')
1776fd01894SGreg Roach                                    ->on('l_from', '=', 'i_id');
1786fd01894SGreg Roach                            })
1796fd01894SGreg Roach                            ->where('l_to', '=', $old_xref)
1806fd01894SGreg Roach                            ->where('l_type', '=', $tag)
1816fd01894SGreg Roach                            ->where('i_file', '=', $tree->id())
1826fd01894SGreg Roach                            ->update([
1836fd01894SGreg Roach                                'i_gedcom' => new Expression("REPLACE(i_gedcom, ' $tag @$old_xref@', ' $tag @$new_xref@')"),
1846fd01894SGreg Roach                            ]);
1856fd01894SGreg Roach                    }
1866fd01894SGreg Roach
1876fd01894SGreg Roach                    DB::table('placelinks')
1886fd01894SGreg Roach                        ->where('pl_file', '=', $tree->id())
1896fd01894SGreg Roach                        ->where('pl_gid', '=', $old_xref)
1906fd01894SGreg Roach                        ->update([
1916fd01894SGreg Roach                            'pl_gid' => $new_xref,
1926fd01894SGreg Roach                        ]);
1936fd01894SGreg Roach
1946fd01894SGreg Roach                    DB::table('dates')
1956fd01894SGreg Roach                        ->where('d_file', '=', $tree->id())
1966fd01894SGreg Roach                        ->where('d_gid', '=', $old_xref)
1976fd01894SGreg Roach                        ->update([
1986fd01894SGreg Roach                            'd_gid' => $new_xref,
1996fd01894SGreg Roach                        ]);
2006fd01894SGreg Roach                    break;
2016fd01894SGreg Roach
2026fd01894SGreg Roach                case Source::RECORD_TYPE:
2036fd01894SGreg Roach                    DB::table('sources')
2046fd01894SGreg Roach                        ->where('s_file', '=', $tree->id())
2056fd01894SGreg Roach                        ->where('s_id', '=', $old_xref)
2066fd01894SGreg Roach                        ->update([
2076fd01894SGreg Roach                            's_id'     => $new_xref,
2086fd01894SGreg Roach                            's_gedcom' => new Expression("REPLACE(s_gedcom, '0 @$old_xref@ SOUR', '0 @$new_xref@ SOUR')"),
2096fd01894SGreg Roach                        ]);
2106fd01894SGreg Roach
2116fd01894SGreg Roach                    DB::table('individuals')
2126fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
2136fd01894SGreg Roach                            $join
2146fd01894SGreg Roach                                ->on('l_file', '=', 'i_file')
2156fd01894SGreg Roach                                ->on('l_from', '=', 'i_id');
2166fd01894SGreg Roach                        })
2176fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
2186fd01894SGreg Roach                        ->where('l_type', '=', 'SOUR')
2196fd01894SGreg Roach                        ->where('i_file', '=', $tree->id())
2206fd01894SGreg Roach                        ->update([
2216fd01894SGreg Roach                            'i_gedcom' => new Expression("REPLACE(i_gedcom, ' SOUR @$old_xref@', ' SOUR @$new_xref@')"),
2226fd01894SGreg Roach                        ]);
2236fd01894SGreg Roach
2246fd01894SGreg Roach                    DB::table('families')
2256fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
2266fd01894SGreg Roach                            $join
2276fd01894SGreg Roach                                ->on('l_file', '=', 'f_file')
2286fd01894SGreg Roach                                ->on('l_from', '=', 'f_id');
2296fd01894SGreg Roach                        })
2306fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
2316fd01894SGreg Roach                        ->where('l_type', '=', 'SOUR')
2326fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
2336fd01894SGreg Roach                        ->update([
2346fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' SOUR @$old_xref@', ' SOUR @$new_xref@')"),
2356fd01894SGreg Roach                        ]);
2366fd01894SGreg Roach
2376fd01894SGreg Roach                    DB::table('media')
2386fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
2396fd01894SGreg Roach                            $join
2406fd01894SGreg Roach                                ->on('l_file', '=', 'm_file')
2416fd01894SGreg Roach                                ->on('l_from', '=', 'm_id');
2426fd01894SGreg Roach                        })
2436fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
2446fd01894SGreg Roach                        ->where('l_type', '=', 'SOUR')
2456fd01894SGreg Roach                        ->where('m_file', '=', $tree->id())
2466fd01894SGreg Roach                        ->update([
2476fd01894SGreg Roach                            'm_gedcom' => new Expression("REPLACE(m_gedcom, ' SOUR @$old_xref@', ' SOUR @$new_xref@')"),
2486fd01894SGreg Roach                        ]);
2496fd01894SGreg Roach
2506fd01894SGreg Roach                    DB::table('other')
2516fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
2526fd01894SGreg Roach                            $join
2536fd01894SGreg Roach                                ->on('l_file', '=', 'o_file')
2546fd01894SGreg Roach                                ->on('l_from', '=', 'o_id');
2556fd01894SGreg Roach                        })
2566fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
2576fd01894SGreg Roach                        ->where('l_type', '=', 'SOUR')
2586fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
2596fd01894SGreg Roach                        ->update([
2606fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, ' SOUR @$old_xref@', ' SOUR @$new_xref@')"),
2616fd01894SGreg Roach                        ]);
2626fd01894SGreg Roach                    break;
2636fd01894SGreg Roach
2646fd01894SGreg Roach                case Repository::RECORD_TYPE:
2656fd01894SGreg Roach                    DB::table('other')
2666fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
2676fd01894SGreg Roach                        ->where('o_id', '=', $old_xref)
2686fd01894SGreg Roach                        ->where('o_type', '=', 'REPO')
2696fd01894SGreg Roach                        ->update([
2706fd01894SGreg Roach                            'o_id'     => $new_xref,
2716fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, '0 @$old_xref@ REPO', '0 @$new_xref@ REPO')"),
2726fd01894SGreg Roach                        ]);
2736fd01894SGreg Roach
2746fd01894SGreg Roach                    DB::table('sources')
2756fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
2766fd01894SGreg Roach                            $join
2776fd01894SGreg Roach                                ->on('l_file', '=', 's_file')
2786fd01894SGreg Roach                                ->on('l_from', '=', 's_id');
2796fd01894SGreg Roach                        })
2806fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
2816fd01894SGreg Roach                        ->where('l_type', '=', 'REPO')
2826fd01894SGreg Roach                        ->where('s_file', '=', $tree->id())
2836fd01894SGreg Roach                        ->update([
2846fd01894SGreg Roach                            's_gedcom' => new Expression("REPLACE(s_gedcom, ' REPO @$old_xref@', ' REPO @$new_xref@')"),
2856fd01894SGreg Roach                        ]);
2866fd01894SGreg Roach                    break;
2876fd01894SGreg Roach
2886fd01894SGreg Roach                case Note::RECORD_TYPE:
2896fd01894SGreg Roach                    DB::table('other')
2906fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
2916fd01894SGreg Roach                        ->where('o_id', '=', $old_xref)
2926fd01894SGreg Roach                        ->where('o_type', '=', 'NOTE')
2936fd01894SGreg Roach                        ->update([
2946fd01894SGreg Roach                            'o_id'     => $new_xref,
2956fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, '0 @$old_xref@ NOTE', '0 @$new_xref@ NOTE')"),
2966fd01894SGreg Roach                        ]);
2976fd01894SGreg Roach
2986fd01894SGreg Roach                    DB::table('individuals')
2996fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3006fd01894SGreg Roach                            $join
3016fd01894SGreg Roach                                ->on('l_file', '=', 'i_file')
3026fd01894SGreg Roach                                ->on('l_from', '=', 'i_id');
3036fd01894SGreg Roach                        })
3046fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3056fd01894SGreg Roach                        ->where('l_type', '=', 'NOTE')
3066fd01894SGreg Roach                        ->where('i_file', '=', $tree->id())
3076fd01894SGreg Roach                        ->update([
3086fd01894SGreg Roach                            'i_gedcom' => new Expression("REPLACE(i_gedcom, ' NOTE @$old_xref@', ' NOTE @$new_xref@')"),
3096fd01894SGreg Roach                        ]);
3106fd01894SGreg Roach
3116fd01894SGreg Roach                    DB::table('families')
3126fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3136fd01894SGreg Roach                            $join
3146fd01894SGreg Roach                                ->on('l_file', '=', 'f_file')
3156fd01894SGreg Roach                                ->on('l_from', '=', 'f_id');
3166fd01894SGreg Roach                        })
3176fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3186fd01894SGreg Roach                        ->where('l_type', '=', 'NOTE')
3196fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
3206fd01894SGreg Roach                        ->update([
3216fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' NOTE @$old_xref@', ' NOTE @$new_xref@')"),
3226fd01894SGreg Roach                        ]);
3236fd01894SGreg Roach
3246fd01894SGreg Roach                    DB::table('media')
3256fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3266fd01894SGreg Roach                            $join
3276fd01894SGreg Roach                                ->on('l_file', '=', 'm_file')
3286fd01894SGreg Roach                                ->on('l_from', '=', 'm_id');
3296fd01894SGreg Roach                        })
3306fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3316fd01894SGreg Roach                        ->where('l_type', '=', 'NOTE')
3326fd01894SGreg Roach                        ->where('m_file', '=', $tree->id())
3336fd01894SGreg Roach                        ->update([
3346fd01894SGreg Roach                            'm_gedcom' => new Expression("REPLACE(m_gedcom, ' NOTE @$old_xref@', ' NOTE @$new_xref@')"),
3356fd01894SGreg Roach                        ]);
3366fd01894SGreg Roach
3376fd01894SGreg Roach                    DB::table('sources')
3386fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3396fd01894SGreg Roach                            $join
3406fd01894SGreg Roach                                ->on('l_file', '=', 's_file')
3416fd01894SGreg Roach                                ->on('l_from', '=', 's_id');
3426fd01894SGreg Roach                        })
3436fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3446fd01894SGreg Roach                        ->where('l_type', '=', 'NOTE')
3456fd01894SGreg Roach                        ->where('s_file', '=', $tree->id())
3466fd01894SGreg Roach                        ->update([
3476fd01894SGreg Roach                            's_gedcom' => new Expression("REPLACE(s_gedcom, ' NOTE @$old_xref@', ' NOTE @$new_xref@')"),
3486fd01894SGreg Roach                        ]);
3496fd01894SGreg Roach
3506fd01894SGreg Roach                    DB::table('other')
3516fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3526fd01894SGreg Roach                            $join
3536fd01894SGreg Roach                                ->on('l_file', '=', 'o_file')
3546fd01894SGreg Roach                                ->on('l_from', '=', 'o_id');
3556fd01894SGreg Roach                        })
3566fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3576fd01894SGreg Roach                        ->where('l_type', '=', 'NOTE')
3586fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
3596fd01894SGreg Roach                        ->update([
3606fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, ' NOTE @$old_xref@', ' NOTE @$new_xref@')"),
3616fd01894SGreg Roach                        ]);
3626fd01894SGreg Roach                    break;
3636fd01894SGreg Roach
3646fd01894SGreg Roach                case Media::RECORD_TYPE:
3656fd01894SGreg Roach                    DB::table('media')
3666fd01894SGreg Roach                        ->where('m_file', '=', $tree->id())
3676fd01894SGreg Roach                        ->where('m_id', '=', $old_xref)
3686fd01894SGreg Roach                        ->update([
3696fd01894SGreg Roach                            'm_id'     => $new_xref,
3706fd01894SGreg Roach                            'm_gedcom' => new Expression("REPLACE(m_gedcom, '0 @$old_xref@ OBJE', '0 @$new_xref@ OBJE')"),
3716fd01894SGreg Roach                        ]);
3726fd01894SGreg Roach
3736fd01894SGreg Roach                    DB::table('media_file')
3746fd01894SGreg Roach                        ->where('m_file', '=', $tree->id())
3756fd01894SGreg Roach                        ->where('m_id', '=', $old_xref)
3766fd01894SGreg Roach                        ->update([
3776fd01894SGreg Roach                            'm_id' => $new_xref,
3786fd01894SGreg Roach                        ]);
3796fd01894SGreg Roach
3806fd01894SGreg Roach                    DB::table('individuals')
3816fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3826fd01894SGreg Roach                            $join
3836fd01894SGreg Roach                                ->on('l_file', '=', 'i_file')
3846fd01894SGreg Roach                                ->on('l_from', '=', 'i_id');
3856fd01894SGreg Roach                        })
3866fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
3876fd01894SGreg Roach                        ->where('l_type', '=', 'OBJE')
3886fd01894SGreg Roach                        ->where('i_file', '=', $tree->id())
3896fd01894SGreg Roach                        ->update([
3906fd01894SGreg Roach                            'i_gedcom' => new Expression("REPLACE(i_gedcom, ' OBJE @$old_xref@', ' OBJE @$new_xref@')"),
3916fd01894SGreg Roach                        ]);
3926fd01894SGreg Roach
3936fd01894SGreg Roach                    DB::table('families')
3946fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
3956fd01894SGreg Roach                            $join
3966fd01894SGreg Roach                                ->on('l_file', '=', 'f_file')
3976fd01894SGreg Roach                                ->on('l_from', '=', 'f_id');
3986fd01894SGreg Roach                        })
3996fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4006fd01894SGreg Roach                        ->where('l_type', '=', 'OBJE')
4016fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
4026fd01894SGreg Roach                        ->update([
4036fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' OBJE @$old_xref@', ' OBJE @$new_xref@')"),
4046fd01894SGreg Roach                        ]);
4056fd01894SGreg Roach
4066fd01894SGreg Roach                    DB::table('sources')
4076fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4086fd01894SGreg Roach                            $join
4096fd01894SGreg Roach                                ->on('l_file', '=', 's_file')
4106fd01894SGreg Roach                                ->on('l_from', '=', 's_id');
4116fd01894SGreg Roach                        })
4126fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4136fd01894SGreg Roach                        ->where('l_type', '=', 'OBJE')
4146fd01894SGreg Roach                        ->where('s_file', '=', $tree->id())
4156fd01894SGreg Roach                        ->update([
4166fd01894SGreg Roach                            's_gedcom' => new Expression("REPLACE(s_gedcom, ' OBJE @$old_xref@', ' OBJE @$new_xref@')"),
4176fd01894SGreg Roach                        ]);
4186fd01894SGreg Roach
4196fd01894SGreg Roach                    DB::table('other')
4206fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4216fd01894SGreg Roach                            $join
4226fd01894SGreg Roach                                ->on('l_file', '=', 'o_file')
4236fd01894SGreg Roach                                ->on('l_from', '=', 'o_id');
4246fd01894SGreg Roach                        })
4256fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4266fd01894SGreg Roach                        ->where('l_type', '=', 'OBJE')
4276fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
4286fd01894SGreg Roach                        ->update([
4296fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, ' OBJE @$old_xref@', ' OBJE @$new_xref@')"),
4306fd01894SGreg Roach                        ]);
4316fd01894SGreg Roach                    break;
4326fd01894SGreg Roach
4336fd01894SGreg Roach                default:
4346fd01894SGreg Roach                    DB::table('other')
4356fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
4366fd01894SGreg Roach                        ->where('o_id', '=', $old_xref)
4376fd01894SGreg Roach                        ->where('o_type', '=', $type)
4386fd01894SGreg Roach                        ->update([
4396fd01894SGreg Roach                            'o_id'     => $new_xref,
4406fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, '0 @$old_xref@ $type', '0 @$new_xref@ $type')"),
4416fd01894SGreg Roach                        ]);
4426fd01894SGreg Roach
4436fd01894SGreg Roach                    DB::table('individuals')
4446fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4456fd01894SGreg Roach                            $join
4466fd01894SGreg Roach                                ->on('l_file', '=', 'i_file')
4476fd01894SGreg Roach                                ->on('l_from', '=', 'i_id');
4486fd01894SGreg Roach                        })
4496fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4506fd01894SGreg Roach                        ->where('l_type', '=', $type)
4516fd01894SGreg Roach                        ->where('i_file', '=', $tree->id())
4526fd01894SGreg Roach                        ->update([
4536fd01894SGreg Roach                            'i_gedcom' => new Expression("REPLACE(i_gedcom, ' $type @$old_xref@', ' $type @$new_xref@')"),
4546fd01894SGreg Roach                        ]);
4556fd01894SGreg Roach
4566fd01894SGreg Roach                    DB::table('families')
4576fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4586fd01894SGreg Roach                            $join
4596fd01894SGreg Roach                                ->on('l_file', '=', 'f_file')
4606fd01894SGreg Roach                                ->on('l_from', '=', 'f_id');
4616fd01894SGreg Roach                        })
4626fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4636fd01894SGreg Roach                        ->where('l_type', '=', $type)
4646fd01894SGreg Roach                        ->where('f_file', '=', $tree->id())
4656fd01894SGreg Roach                        ->update([
4666fd01894SGreg Roach                            'f_gedcom' => new Expression("REPLACE(f_gedcom, ' $type @$old_xref@', ' $type @$new_xref@')"),
4676fd01894SGreg Roach                        ]);
4686fd01894SGreg Roach
4696fd01894SGreg Roach                    DB::table('media')
4706fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4716fd01894SGreg Roach                            $join
4726fd01894SGreg Roach                                ->on('l_file', '=', 'm_file')
4736fd01894SGreg Roach                                ->on('l_from', '=', 'm_id');
4746fd01894SGreg Roach                        })
4756fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4766fd01894SGreg Roach                        ->where('l_type', '=', $type)
4776fd01894SGreg Roach                        ->where('m_file', '=', $tree->id())
4786fd01894SGreg Roach                        ->update([
4796fd01894SGreg Roach                            'm_gedcom' => new Expression("REPLACE(m_gedcom, ' $type @$old_xref@', ' $type @$new_xref@')"),
4806fd01894SGreg Roach                        ]);
4816fd01894SGreg Roach
4826fd01894SGreg Roach                    DB::table('sources')
4836fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4846fd01894SGreg Roach                            $join
4856fd01894SGreg Roach                                ->on('l_file', '=', 's_file')
4866fd01894SGreg Roach                                ->on('l_from', '=', 's_id');
4876fd01894SGreg Roach                        })
4886fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
4896fd01894SGreg Roach                        ->where('l_type', '=', $type)
4906fd01894SGreg Roach                        ->where('s_file', '=', $tree->id())
4916fd01894SGreg Roach                        ->update([
4926fd01894SGreg Roach                            's_gedcom' => new Expression("REPLACE(s_gedcom, ' $type @$old_xref@', ' $type @$new_xref@')"),
4936fd01894SGreg Roach                        ]);
4946fd01894SGreg Roach
4956fd01894SGreg Roach                    DB::table('other')
4966fd01894SGreg Roach                        ->join('link', static function (JoinClause $join): void {
4976fd01894SGreg Roach                            $join
4986fd01894SGreg Roach                                ->on('l_file', '=', 'o_file')
4996fd01894SGreg Roach                                ->on('l_from', '=', 'o_id');
5006fd01894SGreg Roach                        })
5016fd01894SGreg Roach                        ->where('l_to', '=', $old_xref)
5026fd01894SGreg Roach                        ->where('l_type', '=', $type)
5036fd01894SGreg Roach                        ->where('o_file', '=', $tree->id())
5046fd01894SGreg Roach                        ->update([
5056fd01894SGreg Roach                            'o_gedcom' => new Expression("REPLACE(o_gedcom, ' $type @$old_xref@', ' $type @$new_xref@')"),
5066fd01894SGreg Roach                        ]);
5076fd01894SGreg Roach                    break;
5086fd01894SGreg Roach            }
5096fd01894SGreg Roach
5106fd01894SGreg Roach            DB::table('name')
5116fd01894SGreg Roach                ->where('n_file', '=', $tree->id())
5126fd01894SGreg Roach                ->where('n_id', '=', $old_xref)
5136fd01894SGreg Roach                ->update([
5146fd01894SGreg Roach                    'n_id' => $new_xref,
5156fd01894SGreg Roach                ]);
5166fd01894SGreg Roach
5176fd01894SGreg Roach            DB::table('default_resn')
5186fd01894SGreg Roach                ->where('gedcom_id', '=', $tree->id())
5196fd01894SGreg Roach                ->where('xref', '=', $old_xref)
5206fd01894SGreg Roach                ->update([
5216fd01894SGreg Roach                    'xref' => $new_xref,
5226fd01894SGreg Roach                ]);
5236fd01894SGreg Roach
5246fd01894SGreg Roach            DB::table('hit_counter')
5256fd01894SGreg Roach                ->where('gedcom_id', '=', $tree->id())
5266fd01894SGreg Roach                ->where('page_parameter', '=', $old_xref)
5276fd01894SGreg Roach                ->update([
5286fd01894SGreg Roach                    'page_parameter' => $new_xref,
5296fd01894SGreg Roach                ]);
5306fd01894SGreg Roach
5316fd01894SGreg Roach            DB::table('link')
5326fd01894SGreg Roach                ->where('l_file', '=', $tree->id())
5336fd01894SGreg Roach                ->where('l_from', '=', $old_xref)
5346fd01894SGreg Roach                ->update([
5356fd01894SGreg Roach                    'l_from' => $new_xref,
5366fd01894SGreg Roach                ]);
5376fd01894SGreg Roach
5386fd01894SGreg Roach            DB::table('link')
5396fd01894SGreg Roach                ->where('l_file', '=', $tree->id())
5406fd01894SGreg Roach                ->where('l_to', '=', $old_xref)
5416fd01894SGreg Roach                ->update([
5426fd01894SGreg Roach                    'l_to' => $new_xref,
5436fd01894SGreg Roach                ]);
5446fd01894SGreg Roach
5456fd01894SGreg Roach            DB::table('favorite')
5466fd01894SGreg Roach                ->where('gedcom_id', '=', $tree->id())
5476fd01894SGreg Roach                ->where('xref', '=', $old_xref)
5486fd01894SGreg Roach                ->update([
5496fd01894SGreg Roach                    'xref' => $new_xref,
5506fd01894SGreg Roach                ]);
5516fd01894SGreg Roach
5526fd01894SGreg Roach            unset($xrefs[$old_xref]);
5536fd01894SGreg Roach
5546fd01894SGreg Roach            // How much time do we have left?
5556fd01894SGreg Roach            if ($this->timeout_service->isTimeNearlyUp()) {
5566fd01894SGreg Roach                FlashMessages::addMessage(I18N::translate('The server’s time limit has been reached.'), 'warning');
5576fd01894SGreg Roach                break;
5586fd01894SGreg Roach            }
5596fd01894SGreg Roach        }
5606fd01894SGreg Roach
5616fd01894SGreg Roach        $url = route(RenumberTreePage::class, ['tree' => $tree->name()]);
5626fd01894SGreg Roach
5636fd01894SGreg Roach        return redirect($url);
5646fd01894SGreg Roach    }
5656fd01894SGreg Roach}
566