1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * webtrees: online genealogy |
||||
5 | * Copyright (C) 2023 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\Services; |
||||
21 | |||||
22 | use Fisharebest\Webtrees\DB; |
||||
23 | use Fisharebest\Webtrees\Encodings\UTF8; |
||||
24 | use Fisharebest\Webtrees\Family; |
||||
25 | use Fisharebest\Webtrees\GedcomRecord; |
||||
26 | use Fisharebest\Webtrees\Header; |
||||
27 | use Fisharebest\Webtrees\I18N; |
||||
28 | use Fisharebest\Webtrees\Individual; |
||||
29 | use Fisharebest\Webtrees\Media; |
||||
30 | use Fisharebest\Webtrees\Registry; |
||||
31 | use Fisharebest\Webtrees\Site; |
||||
32 | use Fisharebest\Webtrees\Source; |
||||
33 | use Fisharebest\Webtrees\Tree; |
||||
34 | use Illuminate\Database\Query\Expression; |
||||
35 | use Illuminate\Database\Query\JoinClause; |
||||
36 | use Illuminate\Support\Collection; |
||||
37 | use League\Flysystem\FilesystemException; |
||||
38 | use League\Flysystem\FilesystemOperator; |
||||
39 | use League\Flysystem\StorageAttributes; |
||||
40 | |||||
41 | use function array_map; |
||||
42 | use function array_unique; |
||||
43 | use function explode; |
||||
44 | use function fclose; |
||||
45 | use function fread; |
||||
46 | use function implode; |
||||
47 | use function preg_match; |
||||
48 | use function sort; |
||||
49 | |||||
50 | /** |
||||
51 | * Utilities for the control panel. |
||||
52 | */ |
||||
53 | class AdminService |
||||
54 | { |
||||
55 | /** |
||||
56 | * Count of XREFs used by two trees at the same time. |
||||
57 | * |
||||
58 | * @param Tree $tree1 |
||||
59 | * @param Tree $tree2 |
||||
60 | * |
||||
61 | * @return int |
||||
62 | */ |
||||
63 | public function countCommonXrefs(Tree $tree1, Tree $tree2): int |
||||
64 | { |
||||
65 | $subquery1 = DB::table('individuals') |
||||
66 | ->where('i_file', '=', $tree1->id()) |
||||
67 | ->select(['i_id AS xref']) |
||||
68 | ->union(DB::table('families') |
||||
69 | ->where('f_file', '=', $tree1->id()) |
||||
70 | ->select(['f_id AS xref'])) |
||||
71 | ->union(DB::table('sources') |
||||
72 | ->where('s_file', '=', $tree1->id()) |
||||
73 | ->select(['s_id AS xref'])) |
||||
74 | ->union(DB::table('media') |
||||
75 | ->where('m_file', '=', $tree1->id()) |
||||
76 | ->select(['m_id AS xref'])) |
||||
77 | ->union(DB::table('other') |
||||
78 | ->where('o_file', '=', $tree1->id()) |
||||
79 | ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR']) |
||||
80 | ->select(['o_id AS xref'])); |
||||
81 | |||||
82 | $subquery2 = DB::table('change') |
||||
83 | ->where('gedcom_id', '=', $tree2->id()) |
||||
84 | ->select(['xref AS other_xref']) |
||||
85 | ->union(DB::table('individuals') |
||||
86 | ->where('i_file', '=', $tree2->id()) |
||||
87 | ->select(['i_id AS xref'])) |
||||
88 | ->union(DB::table('families') |
||||
89 | ->where('f_file', '=', $tree2->id()) |
||||
90 | ->select(['f_id AS xref'])) |
||||
91 | ->union(DB::table('sources') |
||||
92 | ->where('s_file', '=', $tree2->id()) |
||||
93 | ->select(['s_id AS xref'])) |
||||
94 | ->union(DB::table('media') |
||||
95 | ->where('m_file', '=', $tree2->id()) |
||||
96 | ->select(['m_id AS xref'])) |
||||
97 | ->union(DB::table('other') |
||||
98 | ->where('o_file', '=', $tree2->id()) |
||||
99 | ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR']) |
||||
100 | ->select(['o_id AS xref'])); |
||||
101 | |||||
102 | return DB::table(new Expression('(' . $subquery1->toSql() . ') AS sub1')) |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
103 | ->mergeBindings($subquery1) |
||||
104 | ->joinSub($subquery2, 'sub2', 'other_xref', '=', 'xref') |
||||
105 | ->count(); |
||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * @param Tree $tree |
||||
110 | * |
||||
111 | * @return array<string,array<int,array<int,GedcomRecord>>> |
||||
112 | */ |
||||
113 | public function duplicateRecords(Tree $tree): array |
||||
114 | { |
||||
115 | // We can't do any reasonable checks using MySQL. |
||||
116 | // Will need to wait for a "repositories" table. |
||||
117 | $repositories = []; |
||||
118 | |||||
119 | $sources = DB::table('sources') |
||||
120 | ->where('s_file', '=', $tree->id()) |
||||
121 | ->groupBy(['s_name']) |
||||
122 | ->having(new Expression('COUNT(s_id)'), '>', '1') |
||||
123 | ->select([new Expression(DB::groupConcat('s_id') . ' AS xrefs')]) |
||||
124 | ->orderBy('xrefs') |
||||
125 | ->pluck('xrefs') |
||||
126 | ->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Source => Registry::sourceFactory()->make($xref, $tree), explode(',', $xrefs))) |
||||
127 | ->all(); |
||||
128 | |||||
129 | // Database agnostic way to do GROUP_CONCAT(DISTINCT x ORDER BY x) |
||||
130 | $distinct_order_by = static function (string $xrefs): string { |
||||
131 | $array = explode(',', $xrefs); |
||||
132 | sort($array); |
||||
133 | |||||
134 | return implode(',', array_unique($array)); |
||||
135 | }; |
||||
136 | |||||
137 | $individuals = DB::table('dates') |
||||
138 | ->join('name', static function (JoinClause $join): void { |
||||
139 | $join |
||||
140 | ->on('d_file', '=', 'n_file') |
||||
141 | ->on('d_gid', '=', 'n_id'); |
||||
142 | }) |
||||
143 | ->where('d_file', '=', $tree->id()) |
||||
144 | ->whereIn('d_fact', ['BIRT', 'CHR', 'BAPM', 'DEAT', 'BURI']) |
||||
145 | ->groupBy(['d_year', 'd_month', 'd_day', 'd_type', 'd_fact', 'n_type', 'n_full']) |
||||
146 | ->having(new Expression('COUNT(DISTINCT d_gid)'), '>', '1') |
||||
147 | ->select([new Expression(DB::groupConcat('d_gid') . ' AS xrefs')]) |
||||
148 | ->orderBy('xrefs') |
||||
149 | ->pluck('xrefs') |
||||
150 | ->map($distinct_order_by) |
||||
151 | ->unique() |
||||
152 | ->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Individual => Registry::individualFactory()->make($xref, $tree), explode(',', $xrefs))) |
||||
153 | ->all(); |
||||
154 | |||||
155 | $families = DB::table('families') |
||||
156 | ->where('f_file', '=', $tree->id()) |
||||
157 | ->groupBy([new Expression('LEAST(f_husb, f_wife)')]) |
||||
158 | ->groupBy([new Expression('GREATEST(f_husb, f_wife)')]) |
||||
159 | ->having(new Expression('COUNT(f_id)'), '>', '1') |
||||
160 | ->select([new Expression(DB::groupConcat('f_id') . ' AS xrefs')]) |
||||
161 | ->orderBy('xrefs') |
||||
162 | ->pluck('xrefs') |
||||
163 | ->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Family => Registry::familyFactory()->make($xref, $tree), explode(',', $xrefs))) |
||||
164 | ->all(); |
||||
165 | |||||
166 | $media = DB::table('media_file') |
||||
167 | ->where('m_file', '=', $tree->id()) |
||||
168 | ->where('descriptive_title', '<>', '') |
||||
169 | ->groupBy(['descriptive_title']) |
||||
170 | ->having(new Expression('COUNT(DISTINCT m_id)'), '>', '1') |
||||
171 | ->select([new Expression(DB::groupConcat('m_id') . ' AS xrefs')]) |
||||
172 | ->orderBy('xrefs') |
||||
173 | ->pluck('xrefs') |
||||
174 | ->map(static fn (string $xrefs): array => array_map(static fn (string $xref): Media => Registry::mediaFactory()->make($xref, $tree), explode(',', $xrefs))) |
||||
175 | ->all(); |
||||
176 | |||||
177 | return [ |
||||
178 | I18N::translate('Repositories') => $repositories, |
||||
179 | I18N::translate('Sources') => $sources, |
||||
180 | I18N::translate('Individuals') => $individuals, |
||||
181 | I18N::translate('Families') => $families, |
||||
182 | I18N::translate('Media objects') => $media, |
||||
183 | ]; |
||||
184 | } |
||||
185 | |||||
186 | /** |
||||
187 | * Every XREF used by this tree and also used by some other tree |
||||
188 | * |
||||
189 | * @param Tree $tree |
||||
190 | * |
||||
191 | * @return array<string> |
||||
192 | */ |
||||
193 | public function duplicateXrefs(Tree $tree): array |
||||
194 | { |
||||
195 | $subquery1 = DB::table('individuals') |
||||
196 | ->where('i_file', '=', $tree->id()) |
||||
197 | ->select(['i_id AS xref', new Expression("'INDI' AS type")]) |
||||
198 | ->union(DB::table('families') |
||||
199 | ->where('f_file', '=', $tree->id()) |
||||
200 | ->select(['f_id AS xref', new Expression("'FAM' AS type")])) |
||||
201 | ->union(DB::table('sources') |
||||
202 | ->where('s_file', '=', $tree->id()) |
||||
203 | ->select(['s_id AS xref', new Expression("'SOUR' AS type")])) |
||||
204 | ->union(DB::table('media') |
||||
205 | ->where('m_file', '=', $tree->id()) |
||||
206 | ->select(['m_id AS xref', new Expression("'OBJE' AS type")])) |
||||
207 | ->union(DB::table('other') |
||||
208 | ->where('o_file', '=', $tree->id()) |
||||
209 | ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR']) |
||||
210 | ->select(['o_id AS xref', 'o_type AS type'])); |
||||
211 | |||||
212 | $subquery2 = DB::table('change') |
||||
213 | ->where('gedcom_id', '<>', $tree->id()) |
||||
214 | ->select(['xref AS other_xref']) |
||||
215 | ->union(DB::table('individuals') |
||||
216 | ->where('i_file', '<>', $tree->id()) |
||||
217 | ->select(['i_id AS xref'])) |
||||
218 | ->union(DB::table('families') |
||||
219 | ->where('f_file', '<>', $tree->id()) |
||||
220 | ->select(['f_id AS xref'])) |
||||
221 | ->union(DB::table('sources') |
||||
222 | ->where('s_file', '<>', $tree->id()) |
||||
223 | ->select(['s_id AS xref'])) |
||||
224 | ->union(DB::table('media') |
||||
225 | ->where('m_file', '<>', $tree->id()) |
||||
226 | ->select(['m_id AS xref'])) |
||||
227 | ->union(DB::table('other') |
||||
228 | ->where('o_file', '<>', $tree->id()) |
||||
229 | ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR']) |
||||
230 | ->select(['o_id AS xref'])); |
||||
231 | |||||
232 | return DB::query() |
||||
233 | ->fromSub($subquery1, 'sub1') |
||||
234 | ->joinSub($subquery2, 'sub2', 'other_xref', '=', 'xref') |
||||
235 | ->pluck('type', 'xref') |
||||
236 | ->all(); |
||||
237 | } |
||||
238 | |||||
239 | /** |
||||
240 | * A list of GEDCOM files in the data folder. |
||||
241 | * |
||||
242 | * @param FilesystemOperator $filesystem |
||||
243 | * |
||||
244 | * @return Collection<int,string> |
||||
245 | */ |
||||
246 | public function gedcomFiles(FilesystemOperator $filesystem): Collection |
||||
247 | { |
||||
248 | try { |
||||
249 | $files = $filesystem->listContents('') |
||||
250 | ->filter(static function (StorageAttributes $attributes) use ($filesystem) { |
||||
251 | if (!$attributes->isFile()) { |
||||
252 | return false; |
||||
253 | } |
||||
254 | |||||
255 | $stream = $filesystem->readStream($attributes->path()); |
||||
256 | |||||
257 | $header = fread($stream, 10); |
||||
258 | fclose($stream); |
||||
259 | |||||
260 | return preg_match('/^(' . UTF8::BYTE_ORDER_MARK . ')?0 HEAD/', $header) > 0; |
||||
261 | }) |
||||
262 | ->map(fn (StorageAttributes $attributes) => $attributes->path()) |
||||
263 | ->toArray(); |
||||
264 | } catch (FilesystemException) { |
||||
265 | $files = []; |
||||
266 | } |
||||
267 | |||||
268 | return Collection::make($files)->sort(); |
||||
0 ignored issues
–
show
$files of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
269 | } |
||||
270 | |||||
271 | /** |
||||
272 | * Change the behaviour a little, when there are a lot of trees. |
||||
273 | * |
||||
274 | * @return int |
||||
275 | */ |
||||
276 | public function multipleTreeThreshold(): int |
||||
277 | { |
||||
278 | return (int) Site::getPreference('MULTIPLE_TREE_THRESHOLD'); |
||||
279 | } |
||||
280 | } |
||||
281 |