xref: /webtrees/app/Tree.php (revision 3eae54c8a20b98985facfcff8d58c982338bbf85)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Fisharebest\Webtrees\Contracts\UserInterface;
21use Fisharebest\Webtrees\Functions\FunctionsExport;
22use Fisharebest\Webtrees\Functions\FunctionsImport;
23use Illuminate\Database\Capsule\Manager as DB;
24use Illuminate\Database\Query\Builder;
25use Illuminate\Database\Query\Expression;
26use Illuminate\Database\Query\JoinClause;
27use Illuminate\Support\Collection;
28use Illuminate\Support\Str;
29use InvalidArgumentException;
30use PDOException;
31use Psr\Http\Message\StreamInterface;
32use stdClass;
33
34/**
35 * Provide an interface to the wt_gedcom table.
36 */
37class Tree
38{
39    private const RESN_PRIVACY = [
40        'none'         => Auth::PRIV_PRIVATE,
41        'privacy'      => Auth::PRIV_USER,
42        'confidential' => Auth::PRIV_NONE,
43        'hidden'       => Auth::PRIV_HIDE,
44    ];
45    /** @var Tree[] All trees that we have permission to see, indexed by ID. */
46    public static $trees = [];
47    /** @var int The tree's ID number */
48    private $id;
49    /** @var string The tree's name */
50    private $name;
51    /** @var string The tree's title */
52    private $title;
53    /** @var int[] Default access rules for facts in this tree */
54    private $fact_privacy;
55    /** @var int[] Default access rules for individuals in this tree */
56    private $individual_privacy;
57    /** @var integer[][] Default access rules for individual facts in this tree */
58    private $individual_fact_privacy;
59    /** @var string[] Cached copy of the wt_gedcom_setting table. */
60    private $preferences = [];
61    /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
62    private $user_preferences = [];
63
64    /**
65     * Create a tree object. This is a private constructor - it can only
66     * be called from Tree::getAll() to ensure proper initialisation.
67     *
68     * @param int    $id
69     * @param string $name
70     * @param string $title
71     */
72    private function __construct($id, $name, $title)
73    {
74        $this->id                      = $id;
75        $this->name                    = $name;
76        $this->title                   = $title;
77        $this->fact_privacy            = [];
78        $this->individual_privacy      = [];
79        $this->individual_fact_privacy = [];
80
81        // Load the privacy settings for this tree
82        $rows = DB::table('default_resn')
83            ->where('gedcom_id', '=', $this->id)
84            ->get();
85
86        foreach ($rows as $row) {
87            // Convert GEDCOM privacy restriction to a webtrees access level.
88            $row->resn = self::RESN_PRIVACY[$row->resn];
89
90            if ($row->xref !== null) {
91                if ($row->tag_type !== null) {
92                    $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;
93                } else {
94                    $this->individual_privacy[$row->xref] = (int) $row->resn;
95                }
96            } else {
97                $this->fact_privacy[$row->tag_type] = (int) $row->resn;
98            }
99        }
100    }
101
102    /**
103     * Find the tree with a specific ID.
104     *
105     * @param int $tree_id
106     *
107     * @return Tree
108     */
109    public static function findById(int $tree_id): Tree
110    {
111        return self::getAll()[$tree_id];
112    }
113
114    /**
115     * Fetch all the trees that we have permission to access.
116     *
117     * @return Tree[]
118     */
119    public static function getAll(): array
120    {
121        if (empty(self::$trees)) {
122            self::$trees = self::all()->all();
123        }
124
125        return self::$trees;
126    }
127
128    /**
129     * All the trees that we have permission to access.
130     *
131     * @return Collection
132     */
133    public static function all(): Collection
134    {
135        return app('cache.array')->rememberForever(__CLASS__, static function (): Collection {
136            // Admins see all trees
137            $query = DB::table('gedcom')
138                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
139                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
140                        ->where('gedcom_setting.setting_name', '=', 'title');
141                })
142                ->where('gedcom.gedcom_id', '>', 0)
143                ->select([
144                    'gedcom.gedcom_id AS tree_id',
145                    'gedcom.gedcom_name AS tree_name',
146                    'gedcom_setting.setting_value AS tree_title',
147                ])
148                ->orderBy('gedcom.sort_order')
149                ->orderBy('gedcom_setting.setting_value');
150
151            // Non-admins may not see all trees
152            if (!Auth::isAdmin()) {
153                $query
154                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
155                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
156                            ->where('gs2.setting_name', '=', 'imported');
157                    })
158                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
159                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
160                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
161                    })
162                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
163                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
164                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
165                            ->where('user_gedcom_setting.setting_name', '=', 'canedit');
166                    })
167                    ->where(static function (Builder $query): void {
168                        $query
169                            // Managers
170                            ->where('user_gedcom_setting.setting_value', '=', 'admin')
171                            // Members
172                            ->orWhere(static function (Builder $query): void {
173                                $query
174                                    ->where('gs2.setting_value', '=', '1')
175                                    ->where('gs3.setting_value', '=', '1')
176                                    ->where('user_gedcom_setting.setting_value', '<>', 'none');
177                            })
178                            // Public trees
179                            ->orWhere(static function (Builder $query): void {
180                                $query
181                                    ->where('gs2.setting_value', '=', '1')
182                                    ->where('gs3.setting_value', '<>', '1');
183                            });
184                    });
185            }
186
187            return $query
188                ->get()
189                ->mapWithKeys(static function (stdClass $row): array {
190                    return [$row->tree_id => new self((int) $row->tree_id, $row->tree_name, $row->tree_title)];
191                });
192        });
193    }
194
195    /**
196     * Create arguments to select_edit_control()
197     * Note - these will be escaped later
198     *
199     * @return string[]
200     */
201    public static function getIdList(): array
202    {
203        $list = [];
204        foreach (self::getAll() as $tree) {
205            $list[$tree->id] = $tree->title;
206        }
207
208        return $list;
209    }
210
211    /**
212     * Create arguments to select_edit_control()
213     * Note - these will be escaped later
214     *
215     * @return string[]
216     */
217    public static function getNameList(): array
218    {
219        $list = [];
220        foreach (self::getAll() as $tree) {
221            $list[$tree->name] = $tree->title;
222        }
223
224        return $list;
225    }
226
227    /**
228     * Create a new tree
229     *
230     * @param string $tree_name
231     * @param string $tree_title
232     *
233     * @return Tree
234     */
235    public static function create(string $tree_name, string $tree_title): Tree
236    {
237        try {
238            // Create a new tree
239            DB::table('gedcom')->insert([
240                'gedcom_name' => $tree_name,
241            ]);
242
243            $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
244
245            $tree = new self($tree_id, $tree_name, $tree_title);
246        } catch (PDOException $ex) {
247            // A tree with that name already exists?
248            return self::findByName($tree_name);
249        }
250
251        $tree->setPreference('imported', '0');
252        $tree->setPreference('title', $tree_title);
253
254        // Set preferences from default tree
255        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
256            ['gedcom_id', 'setting_name', 'setting_value'],
257            static function (Builder $query) use ($tree_id): void {
258                $query
259                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
260                    ->from('gedcom_setting')
261                    ->where('gedcom_id', '=', -1);
262            }
263        );
264
265        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
266            ['gedcom_id', 'tag_type', 'resn'],
267            static function (Builder $query) use ($tree_id): void {
268                $query
269                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
270                    ->from('default_resn')
271                    ->where('gedcom_id', '=', -1);
272            }
273        );
274
275        // Gedcom and privacy settings
276        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
277        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
278        $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
279
280        switch (WT_LOCALE) {
281            case 'es':
282                $tree->setPreference('SURNAME_TRADITION', 'spanish');
283                break;
284            case 'is':
285                $tree->setPreference('SURNAME_TRADITION', 'icelandic');
286                break;
287            case 'lt':
288                $tree->setPreference('SURNAME_TRADITION', 'lithuanian');
289                break;
290            case 'pl':
291                $tree->setPreference('SURNAME_TRADITION', 'polish');
292                break;
293            case 'pt':
294            case 'pt-BR':
295                $tree->setPreference('SURNAME_TRADITION', 'portuguese');
296                break;
297            default:
298                $tree->setPreference('SURNAME_TRADITION', 'paternal');
299                break;
300        }
301
302        // Genealogy data
303        // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
304        /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */
305        $john_doe = I18N::translate('John /DOE/');
306        $note     = I18N::translate('Edit this individual and replace their details with your own.');
307        $gedcom   = "0 HEAD\n1 CHAR UTF-8\n0 @X1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n";
308
309        DB::table('gedcom_chunk')->insert([
310            'gedcom_id'  => $tree_id,
311            'chunk_data' => $gedcom,
312        ]);
313
314        // Update our cache
315        self::$trees[$tree->id] = $tree;
316
317        return $tree;
318    }
319
320    /**
321     * Find the tree with a specific name.
322     *
323     * @param string $tree_name
324     *
325     * @return Tree|null
326     */
327    public static function findByName($tree_name): ?Tree
328    {
329        foreach (self::getAll() as $tree) {
330            if ($tree->name === $tree_name) {
331                return $tree;
332            }
333        }
334
335        return null;
336    }
337
338    /**
339     * Set the tree’s configuration settings.
340     *
341     * @param string $setting_name
342     * @param string $setting_value
343     *
344     * @return $this
345     */
346    public function setPreference(string $setting_name, string $setting_value): Tree
347    {
348        if ($setting_value !== $this->getPreference($setting_name)) {
349            DB::table('gedcom_setting')->updateOrInsert([
350                'gedcom_id'    => $this->id,
351                'setting_name' => $setting_name,
352            ], [
353                'setting_value' => $setting_value,
354            ]);
355
356            $this->preferences[$setting_name] = $setting_value;
357
358            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
359        }
360
361        return $this;
362    }
363
364    /**
365     * Get the tree’s configuration settings.
366     *
367     * @param string $setting_name
368     * @param string $default
369     *
370     * @return string
371     */
372    public function getPreference(string $setting_name, string $default = ''): string
373    {
374        if (empty($this->preferences)) {
375            $this->preferences = DB::table('gedcom_setting')
376                ->where('gedcom_id', '=', $this->id)
377                ->pluck('setting_value', 'setting_name')
378                ->all();
379        }
380
381        return $this->preferences[$setting_name] ?? $default;
382    }
383
384    /**
385     * The name of this tree
386     *
387     * @return string
388     */
389    public function name(): string
390    {
391        return $this->name;
392    }
393
394    /**
395     * The title of this tree
396     *
397     * @return string
398     */
399    public function title(): string
400    {
401        return $this->title;
402    }
403
404    /**
405     * The fact-level privacy for this tree.
406     *
407     * @return int[]
408     */
409    public function getFactPrivacy(): array
410    {
411        return $this->fact_privacy;
412    }
413
414    /**
415     * The individual-level privacy for this tree.
416     *
417     * @return int[]
418     */
419    public function getIndividualPrivacy(): array
420    {
421        return $this->individual_privacy;
422    }
423
424    /**
425     * The individual-fact-level privacy for this tree.
426     *
427     * @return int[][]
428     */
429    public function getIndividualFactPrivacy(): array
430    {
431        return $this->individual_fact_privacy;
432    }
433
434    /**
435     * Set the tree’s user-configuration settings.
436     *
437     * @param UserInterface $user
438     * @param string        $setting_name
439     * @param string        $setting_value
440     *
441     * @return $this
442     */
443    public function setUserPreference(UserInterface $user, string $setting_name, string $setting_value): Tree
444    {
445        if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
446            // Update the database
447            DB::table('user_gedcom_setting')->updateOrInsert([
448                'gedcom_id'    => $this->id(),
449                'user_id'      => $user->id(),
450                'setting_name' => $setting_name,
451            ], [
452                'setting_value' => $setting_value,
453            ]);
454
455            // Update the cache
456            $this->user_preferences[$user->id()][$setting_name] = $setting_value;
457            // Audit log of changes
458            Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->userName() . '"', $this);
459        }
460
461        return $this;
462    }
463
464    /**
465     * Get the tree’s user-configuration settings.
466     *
467     * @param UserInterface $user
468     * @param string        $setting_name
469     * @param string        $default
470     *
471     * @return string
472     */
473    public function getUserPreference(UserInterface $user, string $setting_name, string $default = ''): string
474    {
475        // There are lots of settings, and we need to fetch lots of them on every page
476        // so it is quicker to fetch them all in one go.
477        if (!array_key_exists($user->id(), $this->user_preferences)) {
478            $this->user_preferences[$user->id()] = DB::table('user_gedcom_setting')
479                ->where('user_id', '=', $user->id())
480                ->where('gedcom_id', '=', $this->id)
481                ->pluck('setting_value', 'setting_name')
482                ->all();
483        }
484
485        return $this->user_preferences[$user->id()][$setting_name] ?? $default;
486    }
487
488    /**
489     * The ID of this tree
490     *
491     * @return int
492     */
493    public function id(): int
494    {
495        return $this->id;
496    }
497
498    /**
499     * Can a user accept changes for this tree?
500     *
501     * @param UserInterface $user
502     *
503     * @return bool
504     */
505    public function canAcceptChanges(UserInterface $user): bool
506    {
507        return Auth::isModerator($this, $user);
508    }
509
510    /**
511     * Are there any pending edits for this tree, than need reviewing by a moderator.
512     *
513     * @return bool
514     */
515    public function hasPendingEdit(): bool
516    {
517        return DB::table('change')
518            ->where('gedcom_id', '=', $this->id)
519            ->where('status', '=', 'pending')
520            ->exists();
521    }
522
523    /**
524     * Delete everything relating to a tree
525     *
526     * @return void
527     */
528    public function delete(): void
529    {
530        // If this is the default tree, then unset it
531        if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
532            Site::setPreference('DEFAULT_GEDCOM', '');
533        }
534
535        $this->deleteGenealogyData(false);
536
537        DB::table('block_setting')
538            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
539            ->where('gedcom_id', '=', $this->id)
540            ->delete();
541        DB::table('block')->where('gedcom_id', '=', $this->id)->delete();
542        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
543        DB::table('gedcom_setting')->where('gedcom_id', '=', $this->id)->delete();
544        DB::table('module_privacy')->where('gedcom_id', '=', $this->id)->delete();
545        DB::table('hit_counter')->where('gedcom_id', '=', $this->id)->delete();
546        DB::table('default_resn')->where('gedcom_id', '=', $this->id)->delete();
547        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
548        DB::table('log')->where('gedcom_id', '=', $this->id)->delete();
549        DB::table('gedcom')->where('gedcom_id', '=', $this->id)->delete();
550
551        // After updating the database, we need to fetch a new (sorted) copy
552        self::$trees = [];
553    }
554
555    /**
556     * Delete all the genealogy data from a tree - in preparation for importing
557     * new data. Optionally retain the media data, for when the user has been
558     * editing their data offline using an application which deletes (or does not
559     * support) media data.
560     *
561     * @param bool $keep_media
562     *
563     * @return void
564     */
565    public function deleteGenealogyData(bool $keep_media): void
566    {
567        DB::table('gedcom_chunk')->where('gedcom_id', '=', $this->id)->delete();
568        DB::table('individuals')->where('i_file', '=', $this->id)->delete();
569        DB::table('families')->where('f_file', '=', $this->id)->delete();
570        DB::table('sources')->where('s_file', '=', $this->id)->delete();
571        DB::table('other')->where('o_file', '=', $this->id)->delete();
572        DB::table('places')->where('p_file', '=', $this->id)->delete();
573        DB::table('placelinks')->where('pl_file', '=', $this->id)->delete();
574        DB::table('name')->where('n_file', '=', $this->id)->delete();
575        DB::table('dates')->where('d_file', '=', $this->id)->delete();
576        DB::table('change')->where('gedcom_id', '=', $this->id)->delete();
577
578        if ($keep_media) {
579            DB::table('link')->where('l_file', '=', $this->id)
580                ->where('l_type', '<>', 'OBJE')
581                ->delete();
582        } else {
583            DB::table('link')->where('l_file', '=', $this->id)->delete();
584            DB::table('media_file')->where('m_file', '=', $this->id)->delete();
585            DB::table('media')->where('m_file', '=', $this->id)->delete();
586        }
587    }
588
589    /**
590     * Export the tree to a GEDCOM file
591     *
592     * @param resource $stream
593     *
594     * @return void
595     */
596    public function exportGedcom($stream): void
597    {
598        $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8'));
599
600        $union_families = DB::table('families')
601            ->where('f_file', '=', $this->id)
602            ->select(['f_gedcom AS gedcom', 'f_id AS xref', new Expression('LENGTH(f_id) AS len'), new Expression('2 AS n')]);
603
604        $union_sources = DB::table('sources')
605            ->where('s_file', '=', $this->id)
606            ->select(['s_gedcom AS gedcom', 's_id AS xref', new Expression('LENGTH(s_id) AS len'), new Expression('3 AS n')]);
607
608        $union_other = DB::table('other')
609            ->where('o_file', '=', $this->id)
610            ->whereNotIn('o_type', ['HEAD', 'TRLR'])
611            ->select(['o_gedcom AS gedcom', 'o_id AS xref', new Expression('LENGTH(o_id) AS len'), new Expression('4 AS n')]);
612
613        $union_media = DB::table('media')
614            ->where('m_file', '=', $this->id)
615            ->select(['m_gedcom AS gedcom', 'm_id AS xref', new Expression('LENGTH(m_id) AS len'), new Expression('5 AS n')]);
616
617        DB::table('individuals')
618            ->where('i_file', '=', $this->id)
619            ->select(['i_gedcom AS gedcom', 'i_id AS xref', new Expression('LENGTH(i_id) AS len'), new Expression('1 AS n')])
620            ->union($union_families)
621            ->union($union_sources)
622            ->union($union_other)
623            ->union($union_media)
624            ->orderBy('n')
625            ->orderBy('len')
626            ->orderBy('xref')
627            ->chunk(100, static function (Collection $rows) use ($stream, &$buffer): void {
628                foreach ($rows as $row) {
629                    $buffer .= FunctionsExport::reformatRecord($row->gedcom);
630                    if (strlen($buffer) > 65535) {
631                        fwrite($stream, $buffer);
632                        $buffer = '';
633                    }
634                }
635            });
636
637        fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
638    }
639
640    /**
641     * Import data from a gedcom file into this tree.
642     *
643     * @param StreamInterface $stream   The GEDCOM file.
644     * @param string          $filename The preferred filename, for export/download.
645     *
646     * @return void
647     */
648    public function importGedcomFile(StreamInterface $stream, string $filename): void
649    {
650        // Read the file in blocks of roughly 64K. Ensure that each block
651        // contains complete gedcom records. This will ensure we don’t split
652        // multi-byte characters, as well as simplifying the code to import
653        // each block.
654
655        $file_data = '';
656
657        $this->deleteGenealogyData((bool) $this->getPreference('keep_media'));
658        $this->setPreference('gedcom_filename', $filename);
659        $this->setPreference('imported', '0');
660
661        while (!$stream->eof()) {
662            $file_data .= $stream->read(65536);
663            // There is no strrpos() function that searches for substrings :-(
664            for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
665                if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
666                    // We’ve found the last record boundary in this chunk of data
667                    break;
668                }
669            }
670            if ($pos) {
671                DB::table('gedcom_chunk')->insert([
672                    'gedcom_id'  => $this->id,
673                    'chunk_data' => substr($file_data, 0, $pos),
674                ]);
675
676                $file_data = substr($file_data, $pos);
677            }
678        }
679        DB::table('gedcom_chunk')->insert([
680            'gedcom_id'  => $this->id,
681            'chunk_data' => $file_data,
682        ]);
683
684        $stream->close();
685    }
686
687    /**
688     * Create a new record from GEDCOM data.
689     *
690     * @param string $gedcom
691     *
692     * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
693     * @throws InvalidArgumentException
694     */
695    public function createRecord(string $gedcom): GedcomRecord
696    {
697        if (!Str::startsWith($gedcom, '0 @@ ')) {
698            throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@');
699        }
700
701        $xref   = $this->getNewXref();
702        $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ ');
703
704        // Create a change record
705        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
706
707        // Create a pending change
708        DB::table('change')->insert([
709            'gedcom_id'  => $this->id,
710            'xref'       => $xref,
711            'old_gedcom' => '',
712            'new_gedcom' => $gedcom,
713            'user_id'    => Auth::id(),
714        ]);
715
716        // Accept this pending change
717        if (Auth::user()->getPreference('auto_accept')) {
718            FunctionsImport::acceptAllChanges($xref, $this);
719
720            return new GedcomRecord($xref, $gedcom, null, $this);
721        }
722
723        return GedcomRecord::getInstance($xref, $this, $gedcom);
724    }
725
726    /**
727     * Generate a new XREF, unique across all family trees
728     *
729     * @return string
730     */
731    public function getNewXref(): string
732    {
733        // Lock the row, so that only one new XREF may be generated at a time.
734        DB::table('site_setting')
735            ->where('setting_name', '=', 'next_xref')
736            ->lockForUpdate()
737            ->get();
738
739        $prefix = 'X';
740
741        $increment = 1.0;
742        do {
743            $num = (int) Site::getPreference('next_xref') + (int) $increment;
744
745            // This exponential increment allows us to scan over large blocks of
746            // existing data in a reasonable time.
747            $increment *= 1.01;
748
749            $xref = $prefix . $num;
750
751            // Records may already exist with this sequence number.
752            $already_used =
753                DB::table('individuals')->where('i_id', '=', $xref)->exists() ||
754                DB::table('families')->where('f_id', '=', $xref)->exists() ||
755                DB::table('sources')->where('s_id', '=', $xref)->exists() ||
756                DB::table('media')->where('m_id', '=', $xref)->exists() ||
757                DB::table('other')->where('o_id', '=', $xref)->exists() ||
758                DB::table('change')->where('xref', '=', $xref)->exists();
759        } while ($already_used);
760
761        Site::setPreference('next_xref', (string) $num);
762
763        return $xref;
764    }
765
766    /**
767     * Create a new family from GEDCOM data.
768     *
769     * @param string $gedcom
770     *
771     * @return Family
772     * @throws InvalidArgumentException
773     */
774    public function createFamily(string $gedcom): GedcomRecord
775    {
776        if (!Str::startsWith($gedcom, '0 @@ FAM')) {
777            throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM');
778        }
779
780        $xref   = $this->getNewXref();
781        $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM');
782
783        // Create a change record
784        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
785
786        // Create a pending change
787        DB::table('change')->insert([
788            'gedcom_id'  => $this->id,
789            'xref'       => $xref,
790            'old_gedcom' => '',
791            'new_gedcom' => $gedcom,
792            'user_id'    => Auth::id(),
793        ]);
794
795        // Accept this pending change
796        if (Auth::user()->getPreference('auto_accept')) {
797            FunctionsImport::acceptAllChanges($xref, $this);
798
799            return new Family($xref, $gedcom, null, $this);
800        }
801
802        return new Family($xref, '', $gedcom, $this);
803    }
804
805    /**
806     * Create a new individual from GEDCOM data.
807     *
808     * @param string $gedcom
809     *
810     * @return Individual
811     * @throws InvalidArgumentException
812     */
813    public function createIndividual(string $gedcom): GedcomRecord
814    {
815        if (!Str::startsWith($gedcom, '0 @@ INDI')) {
816            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI');
817        }
818
819        $xref   = $this->getNewXref();
820        $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI');
821
822        // Create a change record
823        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
824
825        // Create a pending change
826        DB::table('change')->insert([
827            'gedcom_id'  => $this->id,
828            'xref'       => $xref,
829            'old_gedcom' => '',
830            'new_gedcom' => $gedcom,
831            'user_id'    => Auth::id(),
832        ]);
833
834        // Accept this pending change
835        if (Auth::user()->getPreference('auto_accept')) {
836            FunctionsImport::acceptAllChanges($xref, $this);
837
838            return new Individual($xref, $gedcom, null, $this);
839        }
840
841        return new Individual($xref, '', $gedcom, $this);
842    }
843
844    /**
845     * Create a new media object from GEDCOM data.
846     *
847     * @param string $gedcom
848     *
849     * @return Media
850     * @throws InvalidArgumentException
851     */
852    public function createMediaObject(string $gedcom): Media
853    {
854        if (!Str::startsWith($gedcom, '0 @@ OBJE')) {
855            throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE');
856        }
857
858        $xref   = $this->getNewXref();
859        $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE');
860
861        // Create a change record
862        $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->userName();
863
864        // Create a pending change
865        DB::table('change')->insert([
866            'gedcom_id'  => $this->id,
867            'xref'       => $xref,
868            'old_gedcom' => '',
869            'new_gedcom' => $gedcom,
870            'user_id'    => Auth::id(),
871        ]);
872
873        // Accept this pending change
874        if (Auth::user()->getPreference('auto_accept')) {
875            FunctionsImport::acceptAllChanges($xref, $this);
876
877            return new Media($xref, $gedcom, null, $this);
878        }
879
880        return new Media($xref, '', $gedcom, $this);
881    }
882
883    /**
884     * What is the most significant individual in this tree.
885     *
886     * @param UserInterface $user
887     *
888     * @return Individual
889     */
890    public function significantIndividual(UserInterface $user): Individual
891    {
892        $individual = null;
893
894        if ($this->getUserPreference($user, 'rootid') !== '') {
895            $individual = Individual::getInstance($this->getUserPreference($user, 'rootid'), $this);
896        }
897
898        if ($individual === null && $this->getUserPreference($user, 'gedcomid') !== '') {
899            $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this);
900        }
901
902        if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') {
903            $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this);
904        }
905        if ($individual === null) {
906            $xref = (string) DB::table('individuals')
907                ->where('i_file', '=', $this->id())
908                ->min('i_id');
909
910            $individual = Individual::getInstance($xref, $this);
911        }
912        if ($individual === null) {
913            // always return a record
914            $individual = new Individual('I', '0 @I@ INDI', null, $this);
915        }
916
917        return $individual;
918    }
919}
920