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