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