xref: /webtrees/app/GedcomRecord.php (revision ca50fb84724cb523ca8f4be8abd453a667296dcf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Exception;
24use Fisharebest\Localization\Locale\LocaleInterface;
25use Fisharebest\Webtrees\Functions\FunctionsPrint;
26use Fisharebest\Webtrees\Services\PendingChangesService;
27use Illuminate\Database\Capsule\Manager as DB;
28use Illuminate\Database\Query\Builder;
29use Illuminate\Database\Query\Expression;
30use Illuminate\Database\Query\JoinClause;
31use Illuminate\Support\Collection;
32use Psr\Http\Message\ServerRequestInterface;
33use stdClass;
34
35use function app;
36use function assert;
37
38/**
39 * A GEDCOM object.
40 */
41class GedcomRecord
42{
43    public const RECORD_TYPE = 'UNKNOWN';
44
45    protected const ROUTE_NAME = 'record';
46    /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */
47    public static $gedcom_record_cache;
48    /** @var stdClass[][] Fetch all pending edits in one database query */
49    public static $pending_record_cache;
50    /** @var string The record identifier */
51    protected $xref;
52    /** @var Tree  The family tree to which this record belongs */
53    protected $tree;
54    /** @var string  GEDCOM data (before any pending edits) */
55    protected $gedcom;
56    /** @var string|null  GEDCOM data (after any pending edits) */
57    protected $pending;
58    /** @var Fact[] facts extracted from $gedcom/$pending */
59    protected $facts;
60    /** @var string[][] All the names of this individual */
61    protected $getAllNames;
62    /** @var int|null Cached result */
63    protected $getPrimaryName;
64    /** @var int|null Cached result */
65    protected $getSecondaryName;
66
67    /**
68     * Create a GedcomRecord object from raw GEDCOM data.
69     *
70     * @param string      $xref
71     * @param string      $gedcom  an empty string for new/pending records
72     * @param string|null $pending null for a record with no pending edits,
73     *                             empty string for records with pending deletions
74     * @param Tree        $tree
75     */
76    public function __construct(string $xref, string $gedcom, ?string $pending, Tree $tree)
77    {
78        $this->xref    = $xref;
79        $this->gedcom  = $gedcom;
80        $this->pending = $pending;
81        $this->tree    = $tree;
82
83        $this->parseFacts();
84    }
85
86    /**
87     * A closure which will create a record from a database row.
88     *
89     * @return Closure
90     */
91    public static function rowMapper(): Closure
92    {
93        return static function (stdClass $row): GedcomRecord {
94            return GedcomRecord::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
95        };
96    }
97
98    /**
99     * A closure which will filter out private records.
100     *
101     * @return Closure
102     */
103    public static function accessFilter(): Closure
104    {
105        return static function (GedcomRecord $record): bool {
106            return $record->canShow();
107        };
108    }
109
110    /**
111     * A closure which will compare records by name.
112     *
113     * @return Closure
114     */
115    public static function nameComparator(): Closure
116    {
117        return static function (GedcomRecord $x, GedcomRecord $y): int {
118            if ($x->canShowName()) {
119                if ($y->canShowName()) {
120                    return I18N::strcasecmp($x->sortName(), $y->sortName());
121                }
122
123                return -1; // only $y is private
124            }
125
126            if ($y->canShowName()) {
127                return 1; // only $x is private
128            }
129
130            return 0; // both $x and $y private
131        };
132    }
133
134    /**
135     * A closure which will compare records by change time.
136     *
137     * @param int $direction +1 to sort ascending, -1 to sort descending
138     *
139     * @return Closure
140     */
141    public static function lastChangeComparator(int $direction = 1): Closure
142    {
143        return static function (GedcomRecord $x, GedcomRecord $y) use ($direction): int {
144            return $direction * ($x->lastChangeTimestamp() <=> $y->lastChangeTimestamp());
145        };
146    }
147
148    /**
149     * Get an instance of a GedcomRecord object. For single records,
150     * we just receive the XREF. For bulk records (such as lists
151     * and search results) we can receive the GEDCOM data as well.
152     *
153     * @param string      $xref
154     * @param Tree        $tree
155     * @param string|null $gedcom
156     *
157     * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null
158     * @throws Exception
159     */
160    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
161    {
162        $tree_id = $tree->id();
163
164        // Is this record already in the cache?
165        if (isset(self::$gedcom_record_cache[$xref][$tree_id])) {
166            return self::$gedcom_record_cache[$xref][$tree_id];
167        }
168
169        // Do we need to fetch the record from the database?
170        if ($gedcom === null) {
171            $gedcom = static::fetchGedcomRecord($xref, $tree_id);
172        }
173
174        // If we can edit, then we also need to be able to see pending records.
175        if (Auth::isEditor($tree)) {
176            if (!isset(self::$pending_record_cache[$tree_id])) {
177                // Fetch all pending records in one database query
178                self::$pending_record_cache[$tree_id] = [];
179                $rows                                 = DB::table('change')
180                    ->where('gedcom_id', '=', $tree_id)
181                    ->where('status', '=', 'pending')
182                    ->orderBy('change_id')
183                    ->select(['xref', 'new_gedcom'])
184                    ->get();
185
186                foreach ($rows as $row) {
187                    self::$pending_record_cache[$tree_id][$row->xref] = $row->new_gedcom;
188                }
189            }
190
191            $pending = self::$pending_record_cache[$tree_id][$xref] ?? null;
192        } else {
193            // There are no pending changes for this record
194            $pending = null;
195        }
196
197        // No such record exists
198        if ($gedcom === null && $pending === null) {
199            return null;
200        }
201
202        // No such record, but a pending creation exists
203        if ($gedcom === null) {
204            $gedcom = '';
205        }
206
207        // Create the object
208        if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ (' . Gedcom::REGEX_TAG . ')/', $gedcom . $pending, $match)) {
209            $xref = $match[1]; // Collation - we may have requested I123 and found i123
210            $type = $match[2];
211        } elseif (preg_match('/^0 (HEAD|TRLR)/', $gedcom . $pending, $match)) {
212            $xref = $match[1];
213            $type = $match[1];
214        } elseif ($gedcom . $pending) {
215            throw new Exception('Unrecognized GEDCOM record: ' . $gedcom);
216        } else {
217            // A record with both pending creation and pending deletion
218            $type = static::RECORD_TYPE;
219        }
220
221        switch ($type) {
222            case 'INDI':
223                $record = new Individual($xref, $gedcom, $pending, $tree);
224                break;
225            case 'FAM':
226                $record = new Family($xref, $gedcom, $pending, $tree);
227                break;
228            case 'SOUR':
229                $record = new Source($xref, $gedcom, $pending, $tree);
230                break;
231            case 'OBJE':
232                $record = new Media($xref, $gedcom, $pending, $tree);
233                break;
234            case 'REPO':
235                $record = new Repository($xref, $gedcom, $pending, $tree);
236                break;
237            case 'NOTE':
238                $record = new Note($xref, $gedcom, $pending, $tree);
239                break;
240            default:
241                $record = new self($xref, $gedcom, $pending, $tree);
242                break;
243        }
244
245        // Store it in the cache
246        self::$gedcom_record_cache[$xref][$tree_id] = $record;
247
248        return $record;
249    }
250
251    /**
252     * Fetch data from the database
253     *
254     * @param string $xref
255     * @param int    $tree_id
256     *
257     * @return string|null
258     */
259    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
260    {
261        // We don't know what type of object this is. Try each one in turn.
262        $data = Individual::fetchGedcomRecord($xref, $tree_id);
263        if ($data !== null) {
264            return $data;
265        }
266        $data = Family::fetchGedcomRecord($xref, $tree_id);
267        if ($data !== null) {
268            return $data;
269        }
270        $data = Source::fetchGedcomRecord($xref, $tree_id);
271        if ($data !== null) {
272            return $data;
273        }
274        $data = Repository::fetchGedcomRecord($xref, $tree_id);
275        if ($data !== null) {
276            return $data;
277        }
278        $data = Media::fetchGedcomRecord($xref, $tree_id);
279        if ($data !== null) {
280            return $data;
281        }
282        $data = Note::fetchGedcomRecord($xref, $tree_id);
283        if ($data !== null) {
284            return $data;
285        }
286
287        // Some other type of record...
288        return DB::table('other')
289            ->where('o_file', '=', $tree_id)
290            ->where('o_id', '=', $xref)
291            ->value('o_gedcom');
292    }
293
294    /**
295     * Get the XREF for this record
296     *
297     * @return string
298     */
299    public function xref(): string
300    {
301        return $this->xref;
302    }
303
304    /**
305     * Get the tree to which this record belongs
306     *
307     * @return Tree
308     */
309    public function tree(): Tree
310    {
311        return $this->tree;
312    }
313
314    /**
315     * Application code should access data via Fact objects.
316     * This function exists to support old code.
317     *
318     * @return string
319     */
320    public function gedcom(): string
321    {
322        return $this->pending ?? $this->gedcom;
323    }
324
325    /**
326     * Does this record have a pending change?
327     *
328     * @return bool
329     */
330    public function isPendingAddition(): bool
331    {
332        return $this->pending !== null;
333    }
334
335    /**
336     * Does this record have a pending deletion?
337     *
338     * @return bool
339     */
340    public function isPendingDeletion(): bool
341    {
342        return $this->pending === '';
343    }
344
345    /**
346     * Generate a "slug" to use in pretty URLs.
347     *
348     * @return string
349     */
350    public function slug(): string
351    {
352        return strip_tags($this->fullName());
353    }
354
355    /**
356     * Generate a URL to this record.
357     *
358     * @return string
359     */
360    public function url(): string
361    {
362        return route(static::ROUTE_NAME, [
363            'xref' => $this->xref(),
364            'tree' => $this->tree->name(),
365            'slug' => $this->slug(),
366        ]);
367    }
368
369    /**
370     * Can the details of this record be shown?
371     *
372     * @param int|null $access_level
373     *
374     * @return bool
375     */
376    public function canShow(int $access_level = null): bool
377    {
378        $access_level = $access_level ?? Auth::accessLevel($this->tree);
379
380        // We use this value to bypass privacy checks. For example,
381        // when downloading data or when calculating privacy itself.
382        if ($access_level === Auth::PRIV_HIDE) {
383            return true;
384        }
385
386        $cache_key = 'canShow' . $this->xref . ':' . $this->tree->id() . ':' . $access_level;
387
388        return app('cache.array')->rememberForever($cache_key, function () use ($access_level) {
389            return $this->canShowRecord($access_level);
390        });
391    }
392
393    /**
394     * Can the name of this record be shown?
395     *
396     * @param int|null $access_level
397     *
398     * @return bool
399     */
400    public function canShowName(int $access_level = null): bool
401    {
402        return $this->canShow($access_level);
403    }
404
405    /**
406     * Can we edit this record?
407     *
408     * @return bool
409     */
410    public function canEdit(): bool
411    {
412        if ($this->isPendingDeletion()) {
413            return false;
414        }
415
416        if (Auth::isManager($this->tree)) {
417            return true;
418        }
419
420        return Auth::isEditor($this->tree) && strpos($this->gedcom, "\n1 RESN locked") === false;
421    }
422
423    /**
424     * Remove private data from the raw gedcom record.
425     * Return both the visible and invisible data. We need the invisible data when editing.
426     *
427     * @param int $access_level
428     *
429     * @return string
430     */
431    public function privatizeGedcom(int $access_level): string
432    {
433        if ($access_level === Auth::PRIV_HIDE) {
434            // We may need the original record, for example when downloading a GEDCOM or clippings cart
435            return $this->gedcom;
436        }
437
438        if ($this->canShow($access_level)) {
439            // The record is not private, but the individual facts may be.
440
441            // Include the entire first line (for NOTE records)
442            [$gedrec] = explode("\n", $this->gedcom, 2);
443
444            // Check each of the facts for access
445            foreach ($this->facts([], false, $access_level) as $fact) {
446                $gedrec .= "\n" . $fact->gedcom();
447            }
448
449            return $gedrec;
450        }
451
452        // We cannot display the details, but we may be able to display
453        // limited data, such as links to other records.
454        return $this->createPrivateGedcomRecord($access_level);
455    }
456
457    /**
458     * Default for "other" object types
459     *
460     * @return void
461     */
462    public function extractNames(): void
463    {
464        $this->addName(static::RECORD_TYPE, $this->getFallBackName(), '');
465    }
466
467    /**
468     * Derived classes should redefine this function, otherwise the object will have no name
469     *
470     * @return string[][]
471     */
472    public function getAllNames(): array
473    {
474        if ($this->getAllNames === null) {
475            $this->getAllNames = [];
476            if ($this->canShowName()) {
477                // Ask the record to extract its names
478                $this->extractNames();
479                // No name found? Use a fallback.
480                if (!$this->getAllNames) {
481                    $this->addName(static::RECORD_TYPE, $this->getFallBackName(), '');
482                }
483            } else {
484                $this->addName(static::RECORD_TYPE, I18N::translate('Private'), '');
485            }
486        }
487
488        return $this->getAllNames;
489    }
490
491    /**
492     * If this object has no name, what do we call it?
493     *
494     * @return string
495     */
496    public function getFallBackName(): string
497    {
498        return e($this->xref());
499    }
500
501    /**
502     * Which of the (possibly several) names of this record is the primary one.
503     *
504     * @return int
505     */
506    public function getPrimaryName(): int
507    {
508        static $language_script;
509
510        if ($language_script === null) {
511            $locale = app(ServerRequestInterface::class)->getAttribute('locale');
512            assert($locale instanceof LocaleInterface);
513
514            $language_script = $locale->script()->code();
515        }
516
517        if ($this->getPrimaryName === null) {
518            // Generally, the first name is the primary one....
519            $this->getPrimaryName = 0;
520            // ...except when the language/name use different character sets
521            foreach ($this->getAllNames() as $n => $name) {
522                if (I18N::textScript($name['sort']) === $language_script) {
523                    $this->getPrimaryName = $n;
524                    break;
525                }
526            }
527        }
528
529        return $this->getPrimaryName;
530    }
531
532    /**
533     * Which of the (possibly several) names of this record is the secondary one.
534     *
535     * @return int
536     */
537    public function getSecondaryName(): int
538    {
539        if ($this->getSecondaryName === null) {
540            // Generally, the primary and secondary names are the same
541            $this->getSecondaryName = $this->getPrimaryName();
542            // ....except when there are names with different character sets
543            $all_names = $this->getAllNames();
544            if (count($all_names) > 1) {
545                $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']);
546                foreach ($all_names as $n => $name) {
547                    if ($n !== $this->getPrimaryName() && $name['type'] !== '_MARNM' && I18N::textScript($name['sort']) !== $primary_script) {
548                        $this->getSecondaryName = $n;
549                        break;
550                    }
551                }
552            }
553        }
554
555        return $this->getSecondaryName;
556    }
557
558    /**
559     * Allow the choice of primary name to be overidden, e.g. in a search result
560     *
561     * @param int|null $n
562     *
563     * @return void
564     */
565    public function setPrimaryName(int $n = null): void
566    {
567        $this->getPrimaryName   = $n;
568        $this->getSecondaryName = null;
569    }
570
571    /**
572     * Allow native PHP functions such as array_unique() to work with objects
573     *
574     * @return string
575     */
576    public function __toString()
577    {
578        return $this->xref . '@' . $this->tree->id();
579    }
580
581    /**
582     * /**
583     * Get variants of the name
584     *
585     * @return string
586     */
587    public function fullName(): string
588    {
589        if ($this->canShowName()) {
590            $tmp = $this->getAllNames();
591
592            return $tmp[$this->getPrimaryName()]['full'];
593        }
594
595        return I18N::translate('Private');
596    }
597
598    /**
599     * Get a sortable version of the name. Do not display this!
600     *
601     * @return string
602     */
603    public function sortName(): string
604    {
605        // The sortable name is never displayed, no need to call canShowName()
606        $tmp = $this->getAllNames();
607
608        return $tmp[$this->getPrimaryName()]['sort'];
609    }
610
611    /**
612     * Get the full name in an alternative character set
613     *
614     * @return string|null
615     */
616    public function alternateName(): ?string
617    {
618        if ($this->canShowName() && $this->getPrimaryName() !== $this->getSecondaryName()) {
619            $all_names = $this->getAllNames();
620
621            return $all_names[$this->getSecondaryName()]['full'];
622        }
623
624        return null;
625    }
626
627    /**
628     * Format this object for display in a list
629     *
630     * @return string
631     */
632    public function formatList(): string
633    {
634        $html = '<a href="' . e($this->url()) . '" class="list_item">';
635        $html .= '<b>' . $this->fullName() . '</b>';
636        $html .= $this->formatListDetails();
637        $html .= '</a>';
638
639        return $html;
640    }
641
642    /**
643     * This function should be redefined in derived classes to show any major
644     * identifying characteristics of this record.
645     *
646     * @return string
647     */
648    public function formatListDetails(): string
649    {
650        return '';
651    }
652
653    /**
654     * Extract/format the first fact from a list of facts.
655     *
656     * @param string[] $facts
657     * @param int      $style
658     *
659     * @return string
660     */
661    public function formatFirstMajorFact(array $facts, int $style): string
662    {
663        foreach ($this->facts($facts, true) as $event) {
664            // Only display if it has a date or place (or both)
665            if ($event->date()->isOK() && $event->place()->gedcomName() !== '') {
666                $joiner = ' — ';
667            } else {
668                $joiner = '';
669            }
670            if ($event->date()->isOK() || $event->place()->gedcomName() !== '') {
671                switch ($style) {
672                    case 1:
673                        return '<br><em>' . $event->label() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>';
674                    case 2:
675                        return '<dl><dt class="label">' . $event->label() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>';
676                }
677            }
678        }
679
680        return '';
681    }
682
683    /**
684     * Find individuals linked to this record.
685     *
686     * @param string $link
687     *
688     * @return Collection
689     */
690    public function linkedIndividuals(string $link): Collection
691    {
692        return DB::table('individuals')
693            ->join('link', static function (JoinClause $join): void {
694                $join
695                    ->on('l_file', '=', 'i_file')
696                    ->on('l_from', '=', 'i_id');
697            })
698            ->where('i_file', '=', $this->tree->id())
699            ->where('l_type', '=', $link)
700            ->where('l_to', '=', $this->xref)
701            ->select(['individuals.*'])
702            ->get()
703            ->map(Individual::rowMapper())
704            ->filter(self::accessFilter());
705    }
706
707    /**
708     * Find families linked to this record.
709     *
710     * @param string $link
711     *
712     * @return Collection
713     */
714    public function linkedFamilies(string $link): Collection
715    {
716        return DB::table('families')
717            ->join('link', static function (JoinClause $join): void {
718                $join
719                    ->on('l_file', '=', 'f_file')
720                    ->on('l_from', '=', 'f_id');
721            })
722            ->where('f_file', '=', $this->tree->id())
723            ->where('l_type', '=', $link)
724            ->where('l_to', '=', $this->xref)
725            ->select(['families.*'])
726            ->get()
727            ->map(Family::rowMapper())
728            ->filter(self::accessFilter());
729    }
730
731    /**
732     * Find sources linked to this record.
733     *
734     * @param string $link
735     *
736     * @return Collection
737     */
738    public function linkedSources(string $link): Collection
739    {
740        return DB::table('sources')
741            ->join('link', static function (JoinClause $join): void {
742                $join
743                    ->on('l_file', '=', 's_file')
744                    ->on('l_from', '=', 's_id');
745            })
746            ->where('s_file', '=', $this->tree->id())
747            ->where('l_type', '=', $link)
748            ->where('l_to', '=', $this->xref)
749            ->select(['sources.*'])
750            ->get()
751            ->map(Source::rowMapper())
752            ->filter(self::accessFilter());
753    }
754
755    /**
756     * Find media objects linked to this record.
757     *
758     * @param string $link
759     *
760     * @return Collection
761     */
762    public function linkedMedia(string $link): Collection
763    {
764        return DB::table('media')
765            ->join('link', static function (JoinClause $join): void {
766                $join
767                    ->on('l_file', '=', 'm_file')
768                    ->on('l_from', '=', 'm_id');
769            })
770            ->where('m_file', '=', $this->tree->id())
771            ->where('l_type', '=', $link)
772            ->where('l_to', '=', $this->xref)
773            ->select(['media.*'])
774            ->get()
775            ->map(Media::rowMapper())
776            ->filter(self::accessFilter());
777    }
778
779    /**
780     * Find notes linked to this record.
781     *
782     * @param string $link
783     *
784     * @return Collection
785     */
786    public function linkedNotes(string $link): Collection
787    {
788        return DB::table('other')
789            ->join('link', static function (JoinClause $join): void {
790                $join
791                    ->on('l_file', '=', 'o_file')
792                    ->on('l_from', '=', 'o_id');
793            })
794            ->where('o_file', '=', $this->tree->id())
795            ->where('o_type', '=', 'NOTE')
796            ->where('l_type', '=', $link)
797            ->where('l_to', '=', $this->xref)
798            ->select(['other.*'])
799            ->get()
800            ->map(Note::rowMapper())
801            ->filter(self::accessFilter());
802    }
803
804    /**
805     * Find repositories linked to this record.
806     *
807     * @param string $link
808     *
809     * @return Collection
810     */
811    public function linkedRepositories(string $link): Collection
812    {
813        return DB::table('other')
814            ->join('link', static function (JoinClause $join): void {
815                $join
816                    ->on('l_file', '=', 'o_file')
817                    ->on('l_from', '=', 'o_id');
818            })
819            ->where('o_file', '=', $this->tree->id())
820            ->where('o_type', '=', 'REPO')
821            ->where('l_type', '=', $link)
822            ->where('l_to', '=', $this->xref)
823            ->select(['other.*'])
824            ->get()
825            ->map(Individual::rowMapper())
826            ->filter(self::accessFilter());
827    }
828
829    /**
830     * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR).
831     * This is used to display multiple events on the individual/family lists.
832     * Multiple events can exist because of uncertainty in dates, dates in different
833     * calendars, place-names in both latin and hebrew character sets, etc.
834     * It also allows us to combine dates/places from different events in the summaries.
835     *
836     * @param string[] $events
837     *
838     * @return Date[]
839     */
840    public function getAllEventDates(array $events): array
841    {
842        $dates = [];
843        foreach ($this->facts($events) as $event) {
844            if ($event->date()->isOK()) {
845                $dates[] = $event->date();
846            }
847        }
848
849        return $dates;
850    }
851
852    /**
853     * Get all the places for a particular type of event
854     *
855     * @param string[] $events
856     *
857     * @return Place[]
858     */
859    public function getAllEventPlaces(array $events): array
860    {
861        $places = [];
862        foreach ($this->facts($events) as $event) {
863            if (preg_match_all('/\n(?:2 PLAC|3 (?:ROMN|FONE|_HEB)) +(.+)/', $event->gedcom(), $ged_places)) {
864                foreach ($ged_places[1] as $ged_place) {
865                    $places[] = new Place($ged_place, $this->tree);
866                }
867            }
868        }
869
870        return $places;
871    }
872
873    /**
874     * The facts and events for this record.
875     *
876     * @param string[] $filter
877     * @param bool     $sort
878     * @param int|null $access_level
879     * @param bool     $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES.
880     *
881     * @return Collection
882     */
883    public function facts(array $filter = [], bool $sort = false, int $access_level = null, bool $override = false): Collection
884    {
885        if ($access_level === null) {
886            $access_level = Auth::accessLevel($this->tree);
887        }
888
889        $facts = new Collection();
890        if ($this->canShow($access_level) || $override) {
891            foreach ($this->facts as $fact) {
892                if (($filter === [] || in_array($fact->getTag(), $filter, true)) && $fact->canShow($access_level)) {
893                    $facts->push($fact);
894                }
895            }
896        }
897
898        if ($sort) {
899            $facts = Fact::sortFacts($facts);
900        }
901
902        return new Collection($facts);
903    }
904
905    /**
906     * Get the last-change timestamp for this record
907     *
908     * @return Carbon
909     */
910    public function lastChangeTimestamp(): Carbon
911    {
912        /** @var Fact|null $chan */
913        $chan = $this->facts(['CHAN'])->first();
914
915        if ($chan instanceof Fact) {
916            // The record does have a CHAN event
917            $d = $chan->date()->minimumDate();
918
919            if (preg_match('/\n3 TIME (\d\d):(\d\d):(\d\d)/', $chan->gedcom(), $match)) {
920                return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2], (int) $match[3]);
921            }
922
923            if (preg_match('/\n3 TIME (\d\d):(\d\d)/', $chan->gedcom(), $match)) {
924                return Carbon::create($d->year(), $d->month(), $d->day(), (int) $match[1], (int) $match[2]);
925            }
926
927            return Carbon::create($d->year(), $d->month(), $d->day());
928        }
929
930        // The record does not have a CHAN event
931        return Carbon::createFromTimestamp(0);
932    }
933
934    /**
935     * Get the last-change user for this record
936     *
937     * @return string
938     */
939    public function lastChangeUser(): string
940    {
941        $chan = $this->facts(['CHAN'])->first();
942
943        if ($chan === null) {
944            return I18N::translate('Unknown');
945        }
946
947        $chan_user = $chan->attribute('_WT_USER');
948        if ($chan_user === '') {
949            return I18N::translate('Unknown');
950        }
951
952        return $chan_user;
953    }
954
955    /**
956     * Add a new fact to this record
957     *
958     * @param string $gedcom
959     * @param bool   $update_chan
960     *
961     * @return void
962     */
963    public function createFact(string $gedcom, bool $update_chan): void
964    {
965        $this->updateFact('', $gedcom, $update_chan);
966    }
967
968    /**
969     * Delete a fact from this record
970     *
971     * @param string $fact_id
972     * @param bool   $update_chan
973     *
974     * @return void
975     */
976    public function deleteFact(string $fact_id, bool $update_chan): void
977    {
978        $this->updateFact($fact_id, '', $update_chan);
979    }
980
981    /**
982     * Replace a fact with a new gedcom data.
983     *
984     * @param string $fact_id
985     * @param string $gedcom
986     * @param bool   $update_chan
987     *
988     * @return void
989     * @throws Exception
990     */
991    public function updateFact(string $fact_id, string $gedcom, bool $update_chan): void
992    {
993        // MSDOS line endings will break things in horrible ways
994        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
995        $gedcom = trim($gedcom);
996
997        if ($this->pending === '') {
998            throw new Exception('Cannot edit a deleted record');
999        }
1000        if ($gedcom !== '' && !preg_match('/^1 ' . Gedcom::REGEX_TAG . '/', $gedcom)) {
1001            throw new Exception('Invalid GEDCOM data passed to GedcomRecord::updateFact(' . $gedcom . ')');
1002        }
1003
1004        if ($this->pending) {
1005            $old_gedcom = $this->pending;
1006        } else {
1007            $old_gedcom = $this->gedcom;
1008        }
1009
1010        // First line of record may contain data - e.g. NOTE records.
1011        [$new_gedcom] = explode("\n", $old_gedcom, 2);
1012
1013        // Replacing (or deleting) an existing fact
1014        foreach ($this->facts([], false, Auth::PRIV_HIDE) as $fact) {
1015            if (!$fact->isPendingDeletion()) {
1016                if ($fact->id() === $fact_id) {
1017                    if ($gedcom !== '') {
1018                        $new_gedcom .= "\n" . $gedcom;
1019                    }
1020                    $fact_id = 'NOT A VALID FACT ID'; // Only replace/delete one copy of a duplicate fact
1021                } elseif ($fact->getTag() !== 'CHAN' || !$update_chan) {
1022                    $new_gedcom .= "\n" . $fact->gedcom();
1023                }
1024            }
1025        }
1026        if ($update_chan) {
1027            $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
1028        }
1029
1030        // Adding a new fact
1031        if ($fact_id === '') {
1032            $new_gedcom .= "\n" . $gedcom;
1033        }
1034
1035        if ($new_gedcom !== $old_gedcom) {
1036            // Save the changes
1037            DB::table('change')->insert([
1038                'gedcom_id'  => $this->tree->id(),
1039                'xref'       => $this->xref,
1040                'old_gedcom' => $old_gedcom,
1041                'new_gedcom' => $new_gedcom,
1042                'user_id'    => Auth::id(),
1043            ]);
1044
1045            $this->pending = $new_gedcom;
1046
1047            if (Auth::user()->getPreference('auto_accept')) {
1048                app(PendingChangesService::class)->acceptRecord($this);
1049                $this->gedcom  = $new_gedcom;
1050                $this->pending = null;
1051            }
1052        }
1053        $this->parseFacts();
1054    }
1055
1056    /**
1057     * Update this record
1058     *
1059     * @param string $gedcom
1060     * @param bool   $update_chan
1061     *
1062     * @return void
1063     */
1064    public function updateRecord(string $gedcom, bool $update_chan): void
1065    {
1066        // MSDOS line endings will break things in horrible ways
1067        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1068        $gedcom = trim($gedcom);
1069
1070        // Update the CHAN record
1071        if ($update_chan) {
1072            $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom);
1073            $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
1074        }
1075
1076        // Create a pending change
1077        DB::table('change')->insert([
1078            'gedcom_id'  => $this->tree->id(),
1079            'xref'       => $this->xref,
1080            'old_gedcom' => $this->gedcom(),
1081            'new_gedcom' => $gedcom,
1082            'user_id'    => Auth::id(),
1083        ]);
1084
1085        // Clear the cache
1086        $this->pending = $gedcom;
1087
1088        // Accept this pending change
1089        if (Auth::user()->getPreference('auto_accept')) {
1090            app(PendingChangesService::class)->acceptRecord($this);
1091            $this->gedcom  = $gedcom;
1092            $this->pending = null;
1093        }
1094
1095        $this->parseFacts();
1096
1097        Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree);
1098    }
1099
1100    /**
1101     * Delete this record
1102     *
1103     * @return void
1104     */
1105    public function deleteRecord(): void
1106    {
1107        // Create a pending change
1108        if (!$this->isPendingDeletion()) {
1109            DB::table('change')->insert([
1110                'gedcom_id'  => $this->tree->id(),
1111                'xref'       => $this->xref,
1112                'old_gedcom' => $this->gedcom(),
1113                'new_gedcom' => '',
1114                'user_id'    => Auth::id(),
1115            ]);
1116        }
1117
1118        // Auto-accept this pending change
1119        if (Auth::user()->getPreference('auto_accept')) {
1120            app(PendingChangesService::class)->acceptRecord($this);
1121        }
1122
1123        // Clear the cache
1124        self::$gedcom_record_cache  = [];
1125        self::$pending_record_cache = [];
1126
1127        Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree);
1128    }
1129
1130    /**
1131     * Remove all links from this record to $xref
1132     *
1133     * @param string $xref
1134     * @param bool   $update_chan
1135     *
1136     * @return void
1137     */
1138    public function removeLinks(string $xref, bool $update_chan): void
1139    {
1140        $value = '@' . $xref . '@';
1141
1142        foreach ($this->facts() as $fact) {
1143            if ($fact->value() === $value) {
1144                $this->deleteFact($fact->id(), $update_chan);
1145            } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) {
1146                $gedcom = $fact->gedcom();
1147                foreach ($matches as $match) {
1148                    $next_level  = $match[1] + 1;
1149                    $next_levels = '[' . $next_level . '-9]';
1150                    $gedcom      = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom);
1151                }
1152                $this->updateFact($fact->id(), $gedcom, $update_chan);
1153            }
1154        }
1155    }
1156
1157    /**
1158     * Fetch XREFs of all records linked to a record - when deleting an object, we must
1159     * also delete all links to it.
1160     *
1161     * @return GedcomRecord[]
1162     */
1163    public function linkingRecords(): array
1164    {
1165        $union = DB::table('change')
1166            ->where('gedcom_id', '=', $this->tree()->id())
1167            ->whereContains('new_gedcom', '@' . $this->xref() . '@')
1168            ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%')
1169            ->whereIn('change_id', function (Builder $query): void {
1170                $query->select(new Expression('MAX(change_id)'))
1171                    ->from('change')
1172                    ->where('gedcom_id', '=', $this->tree->id())
1173                    ->where('status', '=', 'pending')
1174                    ->groupBy(['xref']);
1175            })
1176            ->select(['xref']);
1177
1178        $xrefs = DB::table('link')
1179            ->where('l_file', '=', $this->tree()->id())
1180            ->where('l_to', '=', $this->xref())
1181            ->select(['l_from'])
1182            ->union($union)
1183            ->pluck('l_from');
1184
1185        return $xrefs->map(function (string $xref): GedcomRecord {
1186            return GedcomRecord::getInstance($xref, $this->tree);
1187        })->all();
1188    }
1189
1190    /**
1191     * Each object type may have its own special rules, and re-implement this function.
1192     *
1193     * @param int $access_level
1194     *
1195     * @return bool
1196     */
1197    protected function canShowByType(int $access_level): bool
1198    {
1199        $fact_privacy = $this->tree->getFactPrivacy();
1200
1201        if (isset($fact_privacy[static::RECORD_TYPE])) {
1202            // Restriction found
1203            return $fact_privacy[static::RECORD_TYPE] >= $access_level;
1204        }
1205
1206        // No restriction found - must be public:
1207        return true;
1208    }
1209
1210    /**
1211     * Generate a private version of this record
1212     *
1213     * @param int $access_level
1214     *
1215     * @return string
1216     */
1217    protected function createPrivateGedcomRecord(int $access_level): string
1218    {
1219        return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private');
1220    }
1221
1222    /**
1223     * Convert a name record into sortable and full/display versions. This default
1224     * should be OK for simple record types. INDI/FAM records will need to redefine it.
1225     *
1226     * @param string $type
1227     * @param string $value
1228     * @param string $gedcom
1229     *
1230     * @return void
1231     */
1232    protected function addName(string $type, string $value, string $gedcom): void
1233    {
1234        $this->getAllNames[] = [
1235            'type'   => $type,
1236            'sort'   => preg_replace_callback('/([0-9]+)/', static function (array $matches): string {
1237                return str_pad($matches[0], 10, '0', STR_PAD_LEFT);
1238            }, $value),
1239            'full'   => '<span dir="auto">' . e($value) . '</span>',
1240            // This is used for display
1241            'fullNN' => $value,
1242            // This goes into the database
1243        ];
1244    }
1245
1246    /**
1247     * Get all the names of a record, including ROMN, FONE and _HEB alternatives.
1248     * Records without a name (e.g. FAM) will need to redefine this function.
1249     * Parameters: the level 1 fact containing the name.
1250     * Return value: an array of name structures, each containing
1251     * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc.
1252     * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown'
1253     * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John'
1254     *
1255     * @param int        $level
1256     * @param string     $fact_type
1257     * @param Collection $facts
1258     *
1259     * @return void
1260     */
1261    protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void
1262    {
1263        $sublevel    = $level + 1;
1264        $subsublevel = $sublevel + 1;
1265        foreach ($facts as $fact) {
1266            if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) {
1267                foreach ($matches as $match) {
1268                    // Treat 1 NAME / 2 TYPE married the same as _MARNM
1269                    if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) {
1270                        $this->addName('_MARNM', $match[2], $fact->gedcom());
1271                    } else {
1272                        $this->addName($match[1], $match[2], $fact->gedcom());
1273                    }
1274                    if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) {
1275                        foreach ($submatches as $submatch) {
1276                            $this->addName($submatch[1], $submatch[2], $match[3]);
1277                        }
1278                    }
1279                }
1280            }
1281        }
1282    }
1283
1284    /**
1285     * Split the record into facts
1286     *
1287     * @return void
1288     */
1289    private function parseFacts(): void
1290    {
1291        // Split the record into facts
1292        if ($this->gedcom) {
1293            $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom);
1294            array_shift($gedcom_facts);
1295        } else {
1296            $gedcom_facts = [];
1297        }
1298        if ($this->pending) {
1299            $pending_facts = preg_split('/\n(?=1)/s', $this->pending);
1300            array_shift($pending_facts);
1301        } else {
1302            $pending_facts = [];
1303        }
1304
1305        $this->facts = [];
1306
1307        foreach ($gedcom_facts as $gedcom_fact) {
1308            $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact));
1309            if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) {
1310                $fact->setPendingDeletion();
1311            }
1312            $this->facts[] = $fact;
1313        }
1314        foreach ($pending_facts as $pending_fact) {
1315            if (!in_array($pending_fact, $gedcom_facts, true)) {
1316                $fact = new Fact($pending_fact, $this, md5($pending_fact));
1317                $fact->setPendingAddition();
1318                $this->facts[] = $fact;
1319            }
1320        }
1321    }
1322
1323    /**
1324     * Work out whether this record can be shown to a user with a given access level
1325     *
1326     * @param int $access_level
1327     *
1328     * @return bool
1329     */
1330    private function canShowRecord(int $access_level): bool
1331    {
1332        // This setting would better be called "$ENABLE_PRIVACY"
1333        if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) {
1334            return true;
1335        }
1336
1337        // We should always be able to see our own record (unless an admin is applying download restrictions)
1338        if ($this->xref() === $this->tree->getUserPreference(Auth::user(), 'gedcomid') && $access_level === Auth::accessLevel($this->tree)) {
1339            return true;
1340        }
1341
1342        // Does this record have a RESN?
1343        if (strpos($this->gedcom, "\n1 RESN confidential") !== false) {
1344            return Auth::PRIV_NONE >= $access_level;
1345        }
1346        if (strpos($this->gedcom, "\n1 RESN privacy") !== false) {
1347            return Auth::PRIV_USER >= $access_level;
1348        }
1349        if (strpos($this->gedcom, "\n1 RESN none") !== false) {
1350            return true;
1351        }
1352
1353        // Does this record have a default RESN?
1354        $individual_privacy = $this->tree->getIndividualPrivacy();
1355        if (isset($individual_privacy[$this->xref()])) {
1356            return $individual_privacy[$this->xref()] >= $access_level;
1357        }
1358
1359        // Privacy rules do not apply to admins
1360        if (Auth::PRIV_NONE >= $access_level) {
1361            return true;
1362        }
1363
1364        // Different types of record have different privacy rules
1365        return $this->canShowByType($access_level);
1366    }
1367}
1368