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