xref: /webtrees/app/Factories/AbstractGedcomRecordFactory.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1bb03c9f0SGreg Roach<?php
2bb03c9f0SGreg Roach
3bb03c9f0SGreg Roach/**
4bb03c9f0SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6bb03c9f0SGreg Roach * This program is free software: you can redistribute it and/or modify
7bb03c9f0SGreg Roach * it under the terms of the GNU General Public License as published by
8bb03c9f0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9bb03c9f0SGreg Roach * (at your option) any later version.
10bb03c9f0SGreg Roach * This program is distributed in the hope that it will be useful,
11bb03c9f0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bb03c9f0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13bb03c9f0SGreg Roach * GNU General Public License for more details.
14bb03c9f0SGreg 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/>.
16bb03c9f0SGreg Roach */
17bb03c9f0SGreg Roach
18bb03c9f0SGreg Roachdeclare(strict_types=1);
19bb03c9f0SGreg Roach
20bb03c9f0SGreg Roachnamespace Fisharebest\Webtrees\Factories;
21bb03c9f0SGreg Roach
222bc34369SGreg Roachuse Fisharebest\Webtrees\Auth;
236f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
24bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree;
27bb03c9f0SGreg Roachuse Illuminate\Support\Collection;
28bb03c9f0SGreg Roach
29b37e31eaSGreg Roachuse function str_starts_with;
30b37e31eaSGreg Roach
31bb03c9f0SGreg Roach/**
32bb03c9f0SGreg Roach * Make a GedcomRecord object.
33bb03c9f0SGreg Roach */
34bb03c9f0SGreg Roachabstract class AbstractGedcomRecordFactory
35bb03c9f0SGreg Roach{
36bb03c9f0SGreg Roach    /**
37bb03c9f0SGreg Roach     * @param Tree $tree
38bb03c9f0SGreg Roach     *
395fef3bfaSGreg Roach     * @return Collection<array-key,string>
40bb03c9f0SGreg Roach     */
41bb03c9f0SGreg Roach    protected function pendingChanges(Tree $tree): Collection
42bb03c9f0SGreg Roach    {
432bc34369SGreg Roach        // Only editors can see pending changes
442bc34369SGreg Roach        if (!Auth::isEditor($tree)) {
452bc34369SGreg Roach            return new Collection();
462bc34369SGreg Roach        }
472bc34369SGreg Roach
4869c05a6eSGreg Roach        // Caution - this cache can be overwritten by GedcomExportService
49*f25fc0f9SGreg Roach        return Registry::cache()
50*f25fc0f9SGreg Roach            ->array()
51*f25fc0f9SGreg Roach            ->remember(self::class . $tree->id(), static fn (): Collection => DB::table('change')
52bb03c9f0SGreg Roach                ->where('gedcom_id', '=', $tree->id())
53bb03c9f0SGreg Roach                ->where('status', '=', 'pending')
54bb03c9f0SGreg Roach                ->orderBy('change_id')
55*f25fc0f9SGreg Roach                ->pluck('new_gedcom', 'xref'));
56bb03c9f0SGreg Roach    }
57bb03c9f0SGreg Roach
58bb03c9f0SGreg Roach    /**
59bb03c9f0SGreg Roach     * We may have searched for X123, but found the record for x123.
60bb03c9f0SGreg Roach     *
61bb03c9f0SGreg Roach     * @param string $gedcom
62bb03c9f0SGreg Roach     * @param string $xref
63bb03c9f0SGreg Roach     *
6424f2a3afSGreg Roach     * @return string
65bb03c9f0SGreg Roach     */
6624f2a3afSGreg Roach    protected function extractXref(string $gedcom, string $xref): string
67bb03c9f0SGreg Roach    {
68bb03c9f0SGreg Roach        if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@/', $gedcom, $match)) {
69bb03c9f0SGreg Roach            return $match[1];
70bb03c9f0SGreg Roach        }
71bb03c9f0SGreg Roach
72b37e31eaSGreg Roach        if (str_starts_with($gedcom, '0 HEAD')) {
73b37e31eaSGreg Roach            return 'HEAD';
74b37e31eaSGreg Roach        }
75b37e31eaSGreg Roach
76bb03c9f0SGreg Roach        return $xref;
77bb03c9f0SGreg Roach    }
78bb03c9f0SGreg Roach}
79