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