xref: /webtrees/app/GedcomRecord.php (revision 68136c4f6e10efb8b69e797562a7efe2d96fde70)
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, 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            $new_gedcom .= "\n1 CHAN\n2 DATE " . strtoupper(date('d M Y')) . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
1078        }
1079
1080        // Adding a new fact
1081        if ($fact_id === '') {
1082            $new_gedcom .= "\n" . $gedcom;
1083        }
1084
1085        if ($new_gedcom !== $old_gedcom) {
1086            // Save the changes
1087            DB::table('change')->insert([
1088                'gedcom_id'  => $this->tree->id(),
1089                'xref'       => $this->xref,
1090                'old_gedcom' => $old_gedcom,
1091                'new_gedcom' => $new_gedcom,
1092                'user_id'    => Auth::id(),
1093            ]);
1094
1095            $this->pending = $new_gedcom;
1096
1097            if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') {
1098                app(PendingChangesService::class)->acceptRecord($this);
1099                $this->gedcom  = $new_gedcom;
1100                $this->pending = null;
1101            }
1102        }
1103        $this->parseFacts();
1104    }
1105
1106    /**
1107     * Update this record
1108     *
1109     * @param string $gedcom
1110     * @param bool   $update_chan
1111     *
1112     * @return void
1113     */
1114    public function updateRecord(string $gedcom, bool $update_chan): void
1115    {
1116        // Not all record types allow a CHAN event.
1117        $update_chan = $update_chan && in_array(static::RECORD_TYPE, Gedcom::RECORDS_WITH_CHAN, true);
1118
1119        // MSDOS line endings will break things in horrible ways
1120        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
1121        $gedcom = trim($gedcom);
1122
1123        // Update the CHAN record
1124        if ($update_chan) {
1125            $gedcom = preg_replace('/\n1 CHAN(\n[2-9].*)*/', '', $gedcom);
1126            $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
1127        }
1128
1129        // Create a pending change
1130        DB::table('change')->insert([
1131            'gedcom_id'  => $this->tree->id(),
1132            'xref'       => $this->xref,
1133            'old_gedcom' => $this->gedcom(),
1134            'new_gedcom' => $gedcom,
1135            'user_id'    => Auth::id(),
1136        ]);
1137
1138        // Clear the cache
1139        $this->pending = $gedcom;
1140
1141        // Accept this pending change
1142        if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') {
1143            app(PendingChangesService::class)->acceptRecord($this);
1144            $this->gedcom  = $gedcom;
1145            $this->pending = null;
1146        }
1147
1148        $this->parseFacts();
1149
1150        Log::addEditLog('Update: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree);
1151    }
1152
1153    /**
1154     * Delete this record
1155     *
1156     * @return void
1157     */
1158    public function deleteRecord(): void
1159    {
1160        // Create a pending change
1161        if (!$this->isPendingDeletion()) {
1162            DB::table('change')->insert([
1163                'gedcom_id'  => $this->tree->id(),
1164                'xref'       => $this->xref,
1165                'old_gedcom' => $this->gedcom(),
1166                'new_gedcom' => '',
1167                'user_id'    => Auth::id(),
1168            ]);
1169        }
1170
1171        // Auto-accept this pending change
1172        if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') {
1173            app(PendingChangesService::class)->acceptRecord($this);
1174        }
1175
1176        // Clear the cache
1177        self::$gedcom_record_cache  = [];
1178        self::$pending_record_cache = [];
1179
1180        Log::addEditLog('Delete: ' . static::RECORD_TYPE . ' ' . $this->xref, $this->tree);
1181    }
1182
1183    /**
1184     * Remove all links from this record to $xref
1185     *
1186     * @param string $xref
1187     * @param bool   $update_chan
1188     *
1189     * @return void
1190     */
1191    public function removeLinks(string $xref, bool $update_chan): void
1192    {
1193        $value = '@' . $xref . '@';
1194
1195        foreach ($this->facts() as $fact) {
1196            if ($fact->value() === $value) {
1197                $this->deleteFact($fact->id(), $update_chan);
1198            } elseif (preg_match_all('/\n(\d) ' . Gedcom::REGEX_TAG . ' ' . $value . '/', $fact->gedcom(), $matches, PREG_SET_ORDER)) {
1199                $gedcom = $fact->gedcom();
1200                foreach ($matches as $match) {
1201                    $next_level  = $match[1] + 1;
1202                    $next_levels = '[' . $next_level . '-9]';
1203                    $gedcom      = preg_replace('/' . $match[0] . '(\n' . $next_levels . '.*)*/', '', $gedcom);
1204                }
1205                $this->updateFact($fact->id(), $gedcom, $update_chan);
1206            }
1207        }
1208    }
1209
1210    /**
1211     * Fetch XREFs of all records linked to a record - when deleting an object, we must
1212     * also delete all links to it.
1213     *
1214     * @return GedcomRecord[]
1215     */
1216    public function linkingRecords(): array
1217    {
1218        $union = DB::table('change')
1219            ->where('gedcom_id', '=', $this->tree()->id())
1220            ->whereContains('new_gedcom', '@' . $this->xref() . '@')
1221            ->where('new_gedcom', 'NOT LIKE', '0 @' . $this->xref() . '@%')
1222            ->whereIn('change_id', function (Builder $query): void {
1223                $query->select(new Expression('MAX(change_id)'))
1224                    ->from('change')
1225                    ->where('gedcom_id', '=', $this->tree->id())
1226                    ->where('status', '=', 'pending')
1227                    ->groupBy(['xref']);
1228            })
1229            ->select(['xref']);
1230
1231        $xrefs = DB::table('link')
1232            ->where('l_file', '=', $this->tree()->id())
1233            ->where('l_to', '=', $this->xref())
1234            ->select(['l_from'])
1235            ->union($union)
1236            ->pluck('l_from');
1237
1238        return $xrefs->map(function (string $xref): GedcomRecord {
1239            $record = GedcomRecord::getInstance($xref, $this->tree);
1240            assert($record instanceof GedcomRecord);
1241
1242            return $record;
1243        })->all();
1244    }
1245
1246    /**
1247     * Each object type may have its own special rules, and re-implement this function.
1248     *
1249     * @param int $access_level
1250     *
1251     * @return bool
1252     */
1253    protected function canShowByType(int $access_level): bool
1254    {
1255        $fact_privacy = $this->tree->getFactPrivacy();
1256
1257        if (isset($fact_privacy[static::RECORD_TYPE])) {
1258            // Restriction found
1259            return $fact_privacy[static::RECORD_TYPE] >= $access_level;
1260        }
1261
1262        // No restriction found - must be public:
1263        return true;
1264    }
1265
1266    /**
1267     * Generate a private version of this record
1268     *
1269     * @param int $access_level
1270     *
1271     * @return string
1272     */
1273    protected function createPrivateGedcomRecord(int $access_level): string
1274    {
1275        return '0 @' . $this->xref . '@ ' . static::RECORD_TYPE . "\n1 NOTE " . I18N::translate('Private');
1276    }
1277
1278    /**
1279     * Convert a name record into sortable and full/display versions. This default
1280     * should be OK for simple record types. INDI/FAM records will need to redefine it.
1281     *
1282     * @param string $type
1283     * @param string $value
1284     * @param string $gedcom
1285     *
1286     * @return void
1287     */
1288    protected function addName(string $type, string $value, string $gedcom): void
1289    {
1290        $this->getAllNames[] = [
1291            'type'   => $type,
1292            'sort'   => preg_replace_callback('/([0-9]+)/', static function (array $matches): string {
1293                return str_pad($matches[0], 10, '0', STR_PAD_LEFT);
1294            }, $value),
1295            'full'   => '<span dir="auto">' . e($value) . '</span>',
1296            // This is used for display
1297            'fullNN' => $value,
1298            // This goes into the database
1299        ];
1300    }
1301
1302    /**
1303     * Get all the names of a record, including ROMN, FONE and _HEB alternatives.
1304     * Records without a name (e.g. FAM) will need to redefine this function.
1305     * Parameters: the level 1 fact containing the name.
1306     * Return value: an array of name structures, each containing
1307     * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc.
1308     * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown'
1309     * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John'
1310     *
1311     * @param int        $level
1312     * @param string     $fact_type
1313     * @param Collection $facts
1314     *
1315     * @return void
1316     */
1317    protected function extractNamesFromFacts(int $level, string $fact_type, Collection $facts): void
1318    {
1319        $sublevel    = $level + 1;
1320        $subsublevel = $sublevel + 1;
1321        foreach ($facts as $fact) {
1322            if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->gedcom(), $matches, PREG_SET_ORDER)) {
1323                foreach ($matches as $match) {
1324                    // Treat 1 NAME / 2 TYPE married the same as _MARNM
1325                    if ($match[1] === 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) {
1326                        $this->addName('_MARNM', $match[2], $fact->gedcom());
1327                    } else {
1328                        $this->addName($match[1], $match[2], $fact->gedcom());
1329                    }
1330                    if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) {
1331                        foreach ($submatches as $submatch) {
1332                            $this->addName($submatch[1], $submatch[2], $match[3]);
1333                        }
1334                    }
1335                }
1336            }
1337        }
1338    }
1339
1340    /**
1341     * Split the record into facts
1342     *
1343     * @return void
1344     */
1345    private function parseFacts(): void
1346    {
1347        // Split the record into facts
1348        if ($this->gedcom) {
1349            $gedcom_facts = preg_split('/\n(?=1)/s', $this->gedcom);
1350            array_shift($gedcom_facts);
1351        } else {
1352            $gedcom_facts = [];
1353        }
1354        if ($this->pending) {
1355            $pending_facts = preg_split('/\n(?=1)/s', $this->pending);
1356            array_shift($pending_facts);
1357        } else {
1358            $pending_facts = [];
1359        }
1360
1361        $this->facts = [];
1362
1363        foreach ($gedcom_facts as $gedcom_fact) {
1364            $fact = new Fact($gedcom_fact, $this, md5($gedcom_fact));
1365            if ($this->pending !== null && !in_array($gedcom_fact, $pending_facts, true)) {
1366                $fact->setPendingDeletion();
1367            }
1368            $this->facts[] = $fact;
1369        }
1370        foreach ($pending_facts as $pending_fact) {
1371            if (!in_array($pending_fact, $gedcom_facts, true)) {
1372                $fact = new Fact($pending_fact, $this, md5($pending_fact));
1373                $fact->setPendingAddition();
1374                $this->facts[] = $fact;
1375            }
1376        }
1377    }
1378
1379    /**
1380     * Work out whether this record can be shown to a user with a given access level
1381     *
1382     * @param int $access_level
1383     *
1384     * @return bool
1385     */
1386    private function canShowRecord(int $access_level): bool
1387    {
1388        // This setting would better be called "$ENABLE_PRIVACY"
1389        if (!$this->tree->getPreference('HIDE_LIVE_PEOPLE')) {
1390            return true;
1391        }
1392
1393        // We should always be able to see our own record (unless an admin is applying download restrictions)
1394        if ($this->xref() === $this->tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF) && $access_level === Auth::accessLevel($this->tree)) {
1395            return true;
1396        }
1397
1398        // Does this record have a RESN?
1399        if (strpos($this->gedcom, "\n1 RESN confidential") !== false) {
1400            return Auth::PRIV_NONE >= $access_level;
1401        }
1402        if (strpos($this->gedcom, "\n1 RESN privacy") !== false) {
1403            return Auth::PRIV_USER >= $access_level;
1404        }
1405        if (strpos($this->gedcom, "\n1 RESN none") !== false) {
1406            return true;
1407        }
1408
1409        // Does this record have a default RESN?
1410        $individual_privacy = $this->tree->getIndividualPrivacy();
1411        if (isset($individual_privacy[$this->xref()])) {
1412            return $individual_privacy[$this->xref()] >= $access_level;
1413        }
1414
1415        // Privacy rules do not apply to admins
1416        if (Auth::PRIV_NONE >= $access_level) {
1417            return true;
1418        }
1419
1420        // Different types of record have different privacy rules
1421        return $this->canShowByType($access_level);
1422    }
1423}
1424