summaryrefslogtreecommitdiff
path: root/app/Tree.php
blob: ea9c671998ddbf75bb17c5fc29df3f675cbf3b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2017 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees;

use Fisharebest\Webtrees\Functions\FunctionsExport;
use Fisharebest\Webtrees\Functions\FunctionsImport;
use PDOException;

/**
 * Provide an interface to the wt_gedcom table.
 */
class Tree {
	/** @var int The tree's ID number */
	private $tree_id;

	/** @var string The tree's name */
	private $name;

	/** @var string The tree's title */
	private $title;

	/** @var int[] Default access rules for facts in this tree */
	private $fact_privacy;

	/** @var int[] Default access rules for individuals in this tree */
	private $individual_privacy;

	/** @var integer[][] Default access rules for individual facts in this tree */
	private $individual_fact_privacy;

	/** @var Tree[] All trees that we have permission to see. */
	private static $trees;

	/** @var string[] Cached copy of the wt_gedcom_setting table. */
	private $preferences = [];

	/** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
	private $user_preferences = [];

	/**
	 * Create a tree object. This is a private constructor - it can only
	 * be called from Tree::getAll() to ensure proper initialisation.
	 *
	 * @param int    $tree_id
	 * @param string $tree_name
	 * @param string $tree_title
	 */
	private function __construct($tree_id, $tree_name, $tree_title) {
		$this->tree_id                 = $tree_id;
		$this->name                    = $tree_name;
		$this->title                   = $tree_title;
		$this->fact_privacy            = [];
		$this->individual_privacy      = [];
		$this->individual_fact_privacy = [];

		// Load the privacy settings for this tree
		$rows = Database::prepare(
			"SELECT SQL_CACHE xref, tag_type, CASE resn WHEN 'none' THEN :priv_public WHEN 'privacy' THEN :priv_user WHEN 'confidential' THEN :priv_none WHEN 'hidden' THEN :priv_hide END AS resn" .
			" FROM `##default_resn` WHERE gedcom_id = :tree_id"
		)->execute([
			'priv_public' => Auth::PRIV_PRIVATE,
			'priv_user'   => Auth::PRIV_USER,
			'priv_none'   => Auth::PRIV_NONE,
			'priv_hide'   => Auth::PRIV_HIDE,
			'tree_id'     => $this->tree_id,
		])->fetchAll();

		foreach ($rows as $row) {
			if ($row->xref !== null) {
				if ($row->tag_type !== null) {
					$this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;
				} else {
					$this->individual_privacy[$row->xref] = (int) $row->resn;
				}
			} else {
				$this->fact_privacy[$row->tag_type] = (int) $row->resn;
			}
		}

	}

	/**
	 * The ID of this tree
	 *
	 * @return int
	 */
	public function getTreeId() {
		return $this->tree_id;
	}

	/**
	 * The name of this tree
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * The name of this tree
	 *
	 * @return string
	 */
	public function getNameHtml() {
		return Html::escape($this->name);
	}

	/**
	 * The name of this tree
	 *
	 * @return string
	 */
	public function getNameUrl() {
		return Filter::escapeUrl($this->name);
	}

	/**
	 * The title of this tree
	 *
	 * @return string
	 */
	public function getTitle() {
		return $this->title;
	}

	/**
	 * The title of this tree, with HTML markup
	 *
	 * @return string
	 */
	public function getTitleHtml() {
		return '<span dir="auto">' . Html::escape($this->title) . '</span>';
	}

	/**
	 * The fact-level privacy for this tree.
	 *
	 * @return int[]
	 */
	public function getFactPrivacy() {
		return $this->fact_privacy;
	}

	/**
	 * The individual-level privacy for this tree.
	 *
	 * @return int[]
	 */
	public function getIndividualPrivacy() {
		return $this->individual_privacy;
	}

	/**
	 * The individual-fact-level privacy for this tree.
	 *
	 * @return integer[][]
	 */
	public function getIndividualFactPrivacy() {
		return $this->individual_fact_privacy;
	}

	/**
	 * Get the tree’s configuration settings.
	 *
	 * @param string $setting_name
	 * @param string $default
	 *
	 * @return string
	 */
	public function getPreference($setting_name, $default = '') {
		if (empty($this->preferences)) {
			$this->preferences = Database::prepare(
				"SELECT SQL_CACHE setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?"
			)->execute([$this->tree_id])->fetchAssoc();
		}

		if (array_key_exists($setting_name, $this->preferences)) {
			return $this->preferences[$setting_name];
		} else {
			return $default;
		}
	}

	/**
	 * Set the tree’s configuration settings.
	 *
	 * @param string $setting_name
	 * @param string $setting_value
	 *
	 * @return $this
	 */
	public function setPreference($setting_name, $setting_value) {
		if ($setting_value !== $this->getPreference($setting_name)) {
			Database::prepare(
				"REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .
				" VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))"
			)->execute([
				'tree_id'       => $this->tree_id,
				'setting_name'  => $setting_name,
				'setting_value' => $setting_value,
			]);

			$this->preferences[$setting_name] = $setting_value;

			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this);
		}

		return $this;
	}

	/**
	 * Get the tree’s user-configuration settings.
	 *
	 * @param User        $user
	 * @param string      $setting_name
	 * @param string|null $default
	 *
	 * @return string
	 */
	public function getUserPreference(User $user, $setting_name, $default = null) {
		// There are lots of settings, and we need to fetch lots of them on every page
		// so it is quicker to fetch them all in one go.
		if (!array_key_exists($user->getUserId(), $this->user_preferences)) {
			$this->user_preferences[$user->getUserId()] = Database::prepare(
				"SELECT SQL_CACHE setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?"
			)->execute([$user->getUserId(), $this->tree_id])->fetchAssoc();
		}

		if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) {
			return $this->user_preferences[$user->getUserId()][$setting_name];
		} else {
			return $default;
		}
	}

	/**
	 * Set the tree’s user-configuration settings.
	 *
	 * @param User    $user
	 * @param string  $setting_name
	 * @param string  $setting_value
	 *
	 * @return $this
	 */
	public function setUserPreference(User $user, $setting_name, $setting_value) {
		if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
			// Update the database
			if ($setting_value === null) {
				Database::prepare(
					"DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name"
				)->execute([
					'tree_id'      => $this->tree_id,
					'user_id'      => $user->getUserId(),
					'setting_name' => $setting_name,
				]);
			} else {
				Database::prepare(
					"REPLACE INTO `##user_gedcom_setting` (user_id, gedcom_id, setting_name, setting_value) VALUES (:user_id, :tree_id, :setting_name, LEFT(:setting_value, 255))"
				)->execute([
					'user_id'       => $user->getUserId(),
					'tree_id'       => $this->tree_id,
					'setting_name'  => $setting_name,
					'setting_value' => $setting_value,
				]);
			}
			// Update our cache
			$this->user_preferences[$user->getUserId()][$setting_name] = $setting_value;
			// Audit log of changes
			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this);
		}

		return $this;
	}

	/**
	 * Can a user accept changes for this tree?
	 *
	 * @param User $user
	 *
	 * @return bool
	 */
	public function canAcceptChanges(User $user) {
		return Auth::isModerator($this, $user);
	}

	/**
	 * Fetch all the trees that we have permission to access.
	 *
	 * @return Tree[]
	 */
	public static function getAll() {
		if (self::$trees === null) {
			self::$trees = [];
			$rows        = Database::prepare(
				"SELECT SQL_CACHE g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" .
				" FROM `##gedcom` g" .
				" LEFT JOIN `##gedcom_setting`      gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" .
				" LEFT JOIN `##gedcom_setting`      gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" .
				" LEFT JOIN `##gedcom_setting`      gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" .
				" LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" .
				" WHERE " .
				"  g.gedcom_id>0 AND (" . // exclude the "template" tree
				"    EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all
				"   ) OR (" .
				"    (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either:
				"     gs3.setting_value <> 1 OR" . // visitor access
				"     IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access
				"   )" .
				"  )" .
				" ORDER BY g.sort_order, 3"
			)->execute([Auth::id(), Auth::id()])->fetchAll();
			foreach ($rows as $row) {
				self::$trees[] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title);
			}
		}

		return self::$trees;
	}

	/**
	 * Find the tree with a specific ID.
	 *
	 * @param int $tree_id
	 *
	 * @throws \DomainException
	 *
	 * @return Tree
	 */
	public static function findById($tree_id) {
		foreach (self::getAll() as $tree) {
			if ($tree->tree_id == $tree_id) {
				return $tree;
			}
		}
		throw new \DomainException;
	}

	/**
	 * Find the tree with a specific name.
	 *
	 * @param string $tree_name
	 *
	 * @return Tree|null
	 */
	public static function findByName($tree_name) {
		foreach (self::getAll() as $tree) {
			if ($tree->name === $tree_name) {
				return $tree;
			}
		}

		return null;
	}

	/**
	 * Create arguments to select_edit_control()
	 * Note - these will be escaped later
	 *
	 * @return string[]
	 */
	public static function getIdList() {
		$list = [];
		foreach (self::getAll() as $tree) {
			$list[$tree->tree_id] = $tree->title;
		}

		return $list;
	}

	/**
	 * Create arguments to select_edit_control()
	 * Note - these will be escaped later
	 *
	 * @return string[]
	 */
	public static function getNameList() {
		$list = [];
		foreach (self::getAll() as $tree) {
			$list[$tree->name] = $tree->title;
		}

		return $list;
	}

	/**
	 * Create a new tree
	 *
	 * @param string $tree_name
	 * @param string $tree_title
	 *
	 * @return Tree
	 */
	public static function create($tree_name, $tree_title) {
		try {
			// Create a new tree
			Database::prepare(
				"INSERT INTO `##gedcom` (gedcom_name) VALUES (?)"
			)->execute([$tree_name]);
			$tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne();
		} catch (PDOException $ex) {
			// A tree with that name already exists?
			return self::findByName($tree_name);
		}

		// Update the list of trees - to include this new one
		self::$trees = null;
		$tree        = self::findById($tree_id);

		$tree->setPreference('imported', '0');
		$tree->setPreference('title', $tree_title);

		// Module privacy
		Module::setDefaultAccess($tree_id);

		// Set preferences from default tree
		Database::prepare(
			"INSERT INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .
			" SELECT :tree_id, setting_name, setting_value" .
			" FROM `##gedcom_setting` WHERE gedcom_id = -1"
		)->execute([
			'tree_id' => $tree_id,
		]);

		Database::prepare(
			"INSERT INTO `##default_resn` (gedcom_id, tag_type, resn)" .
			" SELECT :tree_id, tag_type, resn" .
			" FROM `##default_resn` WHERE gedcom_id = -1"
		)->execute([
			'tree_id' => $tree_id,
		]);

		Database::prepare(
			"INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" .
			" SELECT :tree_id, location, block_order, module_name" .
			" FROM `##block` WHERE gedcom_id = -1"
		)->execute([
			'tree_id' => $tree_id,
		]);

		// Gedcom and privacy settings
		$tree->setPreference('CONTACT_USER_ID', Auth::id());
		$tree->setPreference('WEBMASTER_USER_ID', Auth::id());
		$tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
		switch (WT_LOCALE) {
		case 'es':
			$tree->setPreference('SURNAME_TRADITION', 'spanish');
			break;
		case 'is':
			$tree->setPreference('SURNAME_TRADITION', 'icelandic');
			break;
		case 'lt':
			$tree->setPreference('SURNAME_TRADITION', 'lithuanian');
			break;
		case 'pl':
			$tree->setPreference('SURNAME_TRADITION', 'polish');
			break;
		case 'pt':
		case 'pt-BR':
			$tree->setPreference('SURNAME_TRADITION', 'portuguese');
			break;
		default:
			$tree->setPreference('SURNAME_TRADITION', 'paternal');
			break;
		}

		// Genealogy data
		// It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
		$john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */
			I18N::translate('John /DOE/');
		$note     = I18N::translate('Edit this individual and replace their details with your own.');
		Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute([
			$tree_id,
			"0 HEAD\n1 CHAR UTF-8\n0 @I1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n",
		]);

		// Update our cache
		self::$trees[$tree->tree_id] = $tree;

		return $tree;
	}

	/**
	 * Are there any pending edits for this tree, than need reviewing by a moderator.
	 *
	 * @return bool
	 */
	public function hasPendingEdit() {
		return (bool) Database::prepare(
			"SELECT 1 FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id"
		)->execute([
			'tree_id' => $this->tree_id,
		])->fetchOne();
	}

	/**
	 * Delete all the genealogy data from a tree - in preparation for importing
	 * new data. Optionally retain the media data, for when the user has been
	 * editing their data offline using an application which deletes (or does not
	 * support) media data.
	 *
	 * @param bool $keep_media
	 */
	public function deleteGenealogyData($keep_media) {
		Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##individuals`  WHERE i_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##families`     WHERE f_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##sources`      WHERE s_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##other`        WHERE o_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##places`       WHERE p_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##placelinks`   WHERE pl_file   = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##name`         WHERE n_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##dates`        WHERE d_file    = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##change`       WHERE gedcom_id = ?")->execute([$this->tree_id]);

		if ($keep_media) {
			Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->tree_id]);
		} else {
			Database::prepare("DELETE FROM `##link`  WHERE l_file =?")->execute([$this->tree_id]);
			Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->tree_id]);
		}
	}

	/**
	 * Delete everything relating to a tree
	 */
	public function delete() {
		// If this is the default tree, then unset it
		if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) {
			Site::setPreference('DEFAULT_GEDCOM', '');
		}

		$this->deleteGenealogyData(false);

		Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##block`               WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##gedcom_setting`      WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##module_privacy`      WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##next_id`             WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##hit_counter`         WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##default_resn`        WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##gedcom_chunk`        WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##log`                 WHERE gedcom_id = ?")->execute([$this->tree_id]);
		Database::prepare("DELETE FROM `##gedcom`              WHERE gedcom_id = ?")->execute([$this->tree_id]);

		// After updating the database, we need to fetch a new (sorted) copy
		self::$trees = null;
	}

	/**
	 * Export the tree to a GEDCOM file
	 *
	 * @param resource $stream
	 */
	public function exportGedcom($stream) {
		$stmt = Database::prepare(
			"SELECT i_gedcom AS gedcom, i_id AS xref, 1 AS n FROM `##individuals` WHERE i_file = :tree_id_1" .
			" UNION ALL " .
			"SELECT f_gedcom AS gedcom, f_id AS xref, 2 AS n FROM `##families`    WHERE f_file = :tree_id_2" .
			" UNION ALL " .
			"SELECT s_gedcom AS gedcom, s_id AS xref, 3 AS n FROM `##sources`     WHERE s_file = :tree_id_3" .
			" UNION ALL " .
			"SELECT o_gedcom AS gedcom, o_id AS xref, 4 AS n FROM `##other`       WHERE o_file = :tree_id_4 AND o_type NOT IN ('HEAD', 'TRLR')" .
			" UNION ALL " .
			"SELECT m_gedcom AS gedcom, m_id AS xref, 5 AS n FROM `##media`       WHERE m_file = :tree_id_5" .
			" ORDER BY n, LENGTH(xref), xref"
		)->execute([
			'tree_id_1' => $this->tree_id,
			'tree_id_2' => $this->tree_id,
			'tree_id_3' => $this->tree_id,
			'tree_id_4' => $this->tree_id,
			'tree_id_5' => $this->tree_id,
		]);

		$buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this));
		while ($row = $stmt->fetch()) {
			$buffer .= FunctionsExport::reformatRecord($row->gedcom);
			if (strlen($buffer) > 65535) {
				fwrite($stream, $buffer);
				$buffer = '';
			}
		}
		fwrite($stream, $buffer . '0 TRLR' . WT_EOL);
		$stmt->closeCursor();
	}

	/**
	 * Import data from a gedcom file into this tree.
	 *
	 * @param string  $path       The full path to the (possibly temporary) file.
	 * @param string  $filename   The preferred filename, for export/download.
	 *
	 * @throws \Exception
	 */
	public function importGedcomFile($path, $filename) {
		// Read the file in blocks of roughly 64K. Ensure that each block
		// contains complete gedcom records. This will ensure we don’t split
		// multi-byte characters, as well as simplifying the code to import
		// each block.

		$file_data = '';
		$fp        = fopen($path, 'rb');

		// Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction.
		ignore_user_abort(true);

		Database::beginTransaction();
		$this->deleteGenealogyData($this->getPreference('keep_media'));
		$this->setPreference('gedcom_filename', $filename);
		$this->setPreference('imported', '0');

		while (!feof($fp)) {
			$file_data .= fread($fp, 65536);
			// There is no strrpos() function that searches for substrings :-(
			for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
				if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
					// We’ve found the last record boundary in this chunk of data
					break;
				}
			}
			if ($pos) {
				Database::prepare(
					"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
				)->execute([$this->tree_id, substr($file_data, 0, $pos)]);
				$file_data = substr($file_data, $pos);
			}
		}
		Database::prepare(
			"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
		)->execute([$this->tree_id, $file_data]);

		Database::commit();
		fclose($fp);
	}

	/**
	 * Generate a new XREF, unique across all family trees
	 *
	 * @param string $type
	 *
	 * @return string
	 */
	public function getNewXref($type = 'INDI') {
		/** @var string[] Which tree preference is used for which record type */
		static $type_to_preference = [
			'INDI' => 'GEDCOM_ID_PREFIX',
			'FAM'  => 'FAM_ID_PREFIX',
			'OBJE' => 'MEDIA_ID_PREFIX',
			'NOTE' => 'NOTE_ID_PREFIX',
			'SOUR' => 'SOURCE_ID_PREFIX',
			'REPO' => 'REPO_ID_PREFIX',
		];

		if (array_key_exists($type, $type_to_preference)) {
			$prefix = $this->getPreference($type_to_preference[$type]);
		} else {
			// Use the first non-underscore character
			$prefix = substr(trim($type, '_'), 0, 1);
		}

		$increment = 1.0;
		do {
			// Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See
			// http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
			$statement = Database::prepare(
				"UPDATE `##next_id` SET next_id = LAST_INSERT_ID(next_id + :increment) WHERE record_type = :record_type AND gedcom_id = :tree_id"
			);
			$statement->execute([
				'increment'   => (int) $increment,
				'record_type' => $type,
				'tree_id'     => $this->tree_id,
			]);

			if ($statement->rowCount() === 0) {
				// First time we've used this record type.
				Database::prepare(
					"INSERT INTO `##next_id` (gedcom_id, record_type, next_id) VALUES(:tree_id, :record_type, 1)"
				)->execute([
					'record_type' => $type,
					'tree_id'     => $this->tree_id,
				]);
				$num = 1;
			} else {
				$num = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne();
			}

			// Records may already exist with this sequence number.
			$already_used = Database::prepare(
				"SELECT i_id FROM `##individuals` WHERE i_id = :i_id" .
				" UNION ALL " .
				"SELECT f_id FROM `##families` WHERE f_id = :f_id" .
				" UNION ALL " .
				"SELECT s_id FROM `##sources` WHERE s_id = :s_id" .
				" UNION ALL " .
				"SELECT m_id FROM `##media` WHERE m_id = :m_id" .
				" UNION ALL " .
				"SELECT o_id FROM `##other` WHERE o_id = :o_id" .
				" UNION ALL " .
				"SELECT xref FROM `##change` WHERE xref = :xref"
			)->execute([
				'i_id' => $prefix . $num,
				'f_id' => $prefix . $num,
				's_id' => $prefix . $num,
				'm_id' => $prefix . $num,
				'o_id' => $prefix . $num,
				'xref' => $prefix . $num,
			])->fetchOne();

			// This exponential increment allows us to scan over large blocks of
			// existing data in a reasonable time.
			$increment *= 1.01;
		} while ($already_used);

		return $prefix . $num;
	}

	/**
	 * Create a new record from GEDCOM data.
	 *
	 * @param string $gedcom
	 *
	 * @throws \Exception
	 *
	 * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media
	 */
	public function createRecord($gedcom) {
		if (preg_match('/^0 @(' . WT_REGEX_XREF . ')@ (' . WT_REGEX_TAG . ')/', $gedcom, $match)) {
			$xref = $match[1];
			$type = $match[2];
		} else {
			throw new \Exception('Invalid argument to GedcomRecord::createRecord(' . $gedcom . ')');
		}
		if (strpos("\r", $gedcom) !== false) {
			// MSDOS line endings will break things in horrible ways
			throw new \Exception('Evil line endings found in GedcomRecord::createRecord(' . $gedcom . ')');
		}

		// webtrees creates XREFs containing digits. Anything else (e.g. “new”) is just a placeholder.
		if (!preg_match('/\d/', $xref)) {
			$xref   = $this->getNewXref($type);
			$gedcom = preg_replace('/^0 @(' . WT_REGEX_XREF . ')@/', '0 @' . $xref . '@', $gedcom);
		}

		// Create a change record, if not already present
		if (!preg_match('/\n1 CHAN/', $gedcom)) {
			$gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName();
		}

		// Create a pending change
		Database::prepare(
			"INSERT INTO `##change` (gedcom_id, xref, old_gedcom, new_gedcom, user_id) VALUES (?, ?, '', ?, ?)"
		)->execute([
			$this->tree_id,
			$xref,
			$gedcom,
			Auth::id(),
		]);

		Log::addEditLog('Create: ' . $type . ' ' . $xref);

		// Accept this pending change
		if (Auth::user()->getPreference('auto_accept')) {
			FunctionsImport::acceptAllChanges($xref, $this->tree_id);
		}
		// Return the newly created record. Note that since GedcomRecord
		// has a cache of pending changes, we cannot use it to create a
		// record with a newly created pending change.
		return GedcomRecord::getInstance($xref, $this, $gedcom);
	}
}