1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2022 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 <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Factories; |
21
|
|
|
|
22
|
|
|
use Closure; |
23
|
|
|
use Fisharebest\Webtrees\Contracts\SharedNoteFactoryInterface; |
24
|
|
|
use Fisharebest\Webtrees\SharedNote; |
25
|
|
|
use Fisharebest\Webtrees\Registry; |
26
|
|
|
use Fisharebest\Webtrees\Tree; |
27
|
|
|
use Illuminate\Database\Capsule\Manager as DB; |
28
|
|
|
|
29
|
|
|
use function assert; |
30
|
|
|
use function preg_match; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Make a SharedNote object. |
34
|
|
|
*/ |
35
|
|
|
class SharedNoteFactory extends AbstractGedcomRecordFactory implements SharedNoteFactoryInterface |
36
|
|
|
{ |
37
|
|
|
private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . SharedNote::RECORD_TYPE . '/'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a note. |
41
|
|
|
* |
42
|
|
|
* @param string $xref |
43
|
|
|
* @param Tree $tree |
44
|
|
|
* @param string|null $gedcom |
45
|
|
|
* |
46
|
|
|
* @return SharedNote|null |
47
|
|
|
*/ |
48
|
|
|
public function make(string $xref, Tree $tree, string $gedcom = null): ?SharedNote |
49
|
|
|
{ |
50
|
|
|
return Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { |
51
|
|
|
$gedcom = $gedcom ?? $this->gedcom($xref, $tree); |
52
|
|
|
$pending = $this->pendingChanges($tree)->get($xref); |
53
|
|
|
|
54
|
|
|
if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$xref = $this->extractXref($gedcom ?? $pending, $xref); |
59
|
|
|
|
60
|
|
|
return new SharedNote($xref, $gedcom ?? '', $pending, $tree); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a note from a row in the database. |
66
|
|
|
* |
67
|
|
|
* @param Tree $tree |
68
|
|
|
* |
69
|
|
|
* @return Closure |
70
|
|
|
*/ |
71
|
|
|
public function mapper(Tree $tree): Closure |
72
|
|
|
{ |
73
|
|
|
return function (object $row) use ($tree): SharedNote { |
74
|
|
|
$note = $this->make($row->o_id, $tree, $row->o_gedcom); |
75
|
|
|
assert($note instanceof SharedNote); |
76
|
|
|
|
77
|
|
|
return $note; |
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a note from raw GEDCOM data. |
83
|
|
|
* |
84
|
|
|
* @param string $xref |
85
|
|
|
* @param string $gedcom an empty string for new/pending records |
86
|
|
|
* @param string|null $pending null for a record with no pending edits, |
87
|
|
|
* empty string for records with pending deletions |
88
|
|
|
* @param Tree $tree |
89
|
|
|
* |
90
|
|
|
* @return SharedNote |
91
|
|
|
*/ |
92
|
|
|
public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): SharedNote |
93
|
|
|
{ |
94
|
|
|
return new SharedNote($xref, $gedcom, $pending, $tree); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Fetch GEDCOM data from the database. |
99
|
|
|
* |
100
|
|
|
* @param string $xref |
101
|
|
|
* @param Tree $tree |
102
|
|
|
* |
103
|
|
|
* @return string|null |
104
|
|
|
*/ |
105
|
|
|
protected function gedcom(string $xref, Tree $tree): ?string |
106
|
|
|
{ |
107
|
|
|
return DB::table('other') |
108
|
|
|
->where('o_id', '=', $xref) |
109
|
|
|
->where('o_file', '=', $tree->id()) |
110
|
|
|
->where('o_type', '=', SharedNote::RECORD_TYPE) |
111
|
|
|
->value('o_gedcom'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|