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