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
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2026 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 <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Services;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Encodings\UTF8;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Header;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use function array_map;
use function array_unique;
use function explode;
use function fclose;
use function fread;
use function implode;
use function preg_match;
use function sort;
/**
* Utilities for the control panel.
*/
class AdminService
{
/**
* Count of XREFs used by two trees at the same time.
*
* @param Tree $tree1
* @param Tree $tree2
*
* @return int
*/
public function countCommonXrefs(Tree $tree1, Tree $tree2): int
{
$subquery1 = DB::table('change')
->where('gedcom_id', '=', $tree1->id())
->select(['xref AS xref1'])
->union(DB::table('individuals')
->where('i_file', '=', $tree1->id())
->select(['i_id AS xref']))
->union(DB::table('families')
->where('f_file', '=', $tree1->id())
->select(['f_id AS xref']))
->union(DB::table('sources')
->where('s_file', '=', $tree1->id())
->select(['s_id AS xref']))
->union(DB::table('media')
->where('m_file', '=', $tree1->id())
->select(['m_id AS xref']))
->union(DB::table('other')
->where('o_file', '=', $tree1->id())
->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
->select(['o_id AS xref']));
$subquery2 = DB::table('change')
->where('gedcom_id', '=', $tree2->id())
->select(['xref AS xref2'])
->union(DB::table('individuals')
->where('i_file', '=', $tree2->id())
->select(['i_id AS xref']))
->union(DB::table('families')
->where('f_file', '=', $tree2->id())
->select(['f_id AS xref']))
->union(DB::table('sources')
->where('s_file', '=', $tree2->id())
->select(['s_id AS xref']))
->union(DB::table('media')
->where('m_file', '=', $tree2->id())
->select(['m_id AS xref']))
->union(DB::table('other')
->where('o_file', '=', $tree2->id())
->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
->select(['o_id AS xref']));
return DB::query()
->fromSub($subquery1, 'sub1')
->joinSub($subquery2, 'sub2', 'xref1', '=', 'xref2')
->count();
}
/**
* @param Tree $tree
*
* @return array<string,array<int,array<int,GedcomRecord>>>
*/
public function duplicateRecords(Tree $tree): array
{
// We can't do any reasonable checks using MySQL.
// Will need to wait for a "repositories" table.
$repositories = [];
$sources = DB::table('sources')
->where('s_file', '=', $tree->id())
->groupBy(['s_name'])
->having(new Expression('COUNT(s_id)'), '>', '1')
->select([new Expression(DB::groupConcat('s_id') . ' AS xrefs')])
->orderBy('xrefs')
->pluck('xrefs')
->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Source => Registry::sourceFactory()->make($xref, $tree), explode(',', $xrefs)))
->all();
// Database agnostic way to do GROUP_CONCAT(DISTINCT x ORDER BY x)
$distinct_order_by = static function (string $xrefs): string {
$array = explode(',', $xrefs);
sort($array);
return implode(',', array_unique($array));
};
$individuals = DB::table('dates')
->join('name', static function (JoinClause $join): void {
$join
->on('d_file', '=', 'n_file')
->on('d_gid', '=', 'n_id');
})
->where('d_file', '=', $tree->id())
->whereIn('d_fact', ['BIRT', 'CHR', 'BAPM', 'DEAT', 'BURI'])
->groupBy(['d_year', 'd_month', 'd_day', 'd_type', 'd_fact', 'n_type', 'n_full'])
->having(new Expression('COUNT(DISTINCT d_gid)'), '>', '1')
->select([new Expression(DB::groupConcat('d_gid') . ' AS xrefs')])
->orderBy('xrefs')
->pluck('xrefs')
->map($distinct_order_by)
->unique()
->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Individual => Registry::individualFactory()->make($xref, $tree), explode(',', $xrefs)))
->all();
$families = DB::table('families')
->where('f_file', '=', $tree->id())
->groupBy([new Expression( DB::driverName() === DB::FIREBIRD ? 'MINVALUE(f_husb, f_wife)' : 'LEAST(f_husb, f_wife)' )])
->groupBy([new Expression( DB::driverName() === DB::FIREBIRD ? 'MAXVALUE(f_husb, f_wife)' : 'GREATEST(f_husb, f_wife)' )])
->having(new Expression('COUNT(f_id)'), '>', '1')
->select([new Expression(DB::groupConcat('f_id') . ' AS xrefs')])
->orderBy('xrefs')
->pluck('xrefs')
->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Family => Registry::familyFactory()->make($xref, $tree), explode(',', $xrefs)))
->all();
$media = DB::table('media_file')
->where('m_file', '=', $tree->id())
->where('descriptive_title', '<>', '')
->groupBy(['descriptive_title'])
->having(new Expression('COUNT(DISTINCT m_id)'), '>', '1')
->select([new Expression(DB::groupConcat('m_id') . ' AS xrefs')])
->orderBy('xrefs')
->pluck('xrefs')
->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Media => Registry::mediaFactory()->make($xref, $tree), explode(',', $xrefs)))
->all();
return [
I18N::translate('Repositories') => $repositories,
I18N::translate('Sources') => $sources,
I18N::translate('Individuals') => $individuals,
I18N::translate('Families') => $families,
I18N::translate('Media objects') => $media,
];
}
/**
* Every XREF used by this tree and also used by some other tree
*
* @param Tree $tree
*
* @return array<string>
*/
public function duplicateXrefs(Tree $tree): array
{
$subquery1 = DB::table('individuals')
->where('i_file', '=', $tree->id())
->select(['i_id AS xref', new Expression("'INDI' AS type")])
->union(DB::table('families')
->where('f_file', '=', $tree->id())
->select(['f_id AS xref', new Expression("'FAM' AS type")]))
->union(DB::table('sources')
->where('s_file', '=', $tree->id())
->select(['s_id AS xref', new Expression("'SOUR' AS type")]))
->union(DB::table('media')
->where('m_file', '=', $tree->id())
->select(['m_id AS xref', new Expression("'OBJE' AS type")]))
->union(DB::table('other')
->where('o_file', '=', $tree->id())
->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
->select(['o_id AS xref', 'o_type AS type']));
$subquery2 = DB::table('change')
->where('gedcom_id', '<>', $tree->id())
->select(['xref AS other_xref'])
->union(DB::table('individuals')
->where('i_file', '<>', $tree->id())
->select(['i_id AS xref']))
->union(DB::table('families')
->where('f_file', '<>', $tree->id())
->select(['f_id AS xref']))
->union(DB::table('sources')
->where('s_file', '<>', $tree->id())
->select(['s_id AS xref']))
->union(DB::table('media')
->where('m_file', '<>', $tree->id())
->select(['m_id AS xref']))
->union(DB::table('other')
->where('o_file', '<>', $tree->id())
->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
->select(['o_id AS xref']));
return DB::query()
->fromSub($subquery1, 'sub1')
->joinSub($subquery2, 'sub2', 'other_xref', '=', 'xref')
->pluck('type', 'xref')
->all();
}
/**
* A list of GEDCOM files in the data folder.
*
* @param FilesystemOperator $filesystem
*
* @return Collection<int,string>
*/
public function gedcomFiles(FilesystemOperator $filesystem): Collection
{
try {
$files = $filesystem->listContents('')
->filter(static function (StorageAttributes $attributes) use ($filesystem) {
if (!$attributes->isFile()) {
return false;
}
$stream = $filesystem->readStream($attributes->path());
$header = fread($stream, 10);
fclose($stream);
return preg_match('/^(' . UTF8::BYTE_ORDER_MARK . ')?0 HEAD/', $header) > 0;
})
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
} catch (FilesystemException) {
$files = [];
}
return Collection::make($files)->sort();
}
/**
* Change the behaviour a little, when there are a lot of trees.
*
* @return int
*/
public function multipleTreeThreshold(): int
{
return (int) Site::getPreference('MULTIPLE_TREE_THRESHOLD');
}
}
|