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