xref: /webtrees/app/Services/AdminService.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Registry;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\Header;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Media;
30use Fisharebest\Webtrees\Site;
31use Fisharebest\Webtrees\Source;
32use Fisharebest\Webtrees\Tree;
33use Illuminate\Database\Capsule\Manager as DB;
34use Illuminate\Database\Query\Expression;
35use Illuminate\Database\Query\JoinClause;
36use Illuminate\Support\Collection;
37use League\Flysystem\FilesystemException;
38use League\Flysystem\FilesystemOperator;
39use League\Flysystem\StorageAttributes;
40
41use function array_map;
42use function explode;
43use function fclose;
44use function fread;
45use function preg_match;
46
47/**
48 * Utilities for the control panel.
49 */
50class AdminService
51{
52    // Show a reduced page when there are more than a certain number of trees
53    private const MULTIPLE_TREE_THRESHOLD = '500';
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'))
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('GROUP_CONCAT(s_id) AS xrefs')])
124            ->pluck('xrefs')
125            ->map(static function (string $xrefs) use ($tree): array {
126                return array_map(static function (string $xref) use ($tree): Source {
127                    return Registry::sourceFactory()->make($xref, $tree);
128                }, explode(',', $xrefs));
129            })
130            ->all();
131
132        $individuals = DB::table('dates')
133            ->join('name', static function (JoinClause $join): void {
134                $join
135                    ->on('d_file', '=', 'n_file')
136                    ->on('d_gid', '=', 'n_id');
137            })
138            ->where('d_file', '=', $tree->id())
139            ->whereIn('d_fact', ['BIRT', 'CHR', 'BAPM', 'DEAT', 'BURI'])
140            ->groupBy(['d_year', 'd_month', 'd_day', 'd_type', 'd_fact', 'n_type', 'n_full'])
141            ->having(new Expression('COUNT(DISTINCT d_gid)'), '>', '1')
142            ->select([new Expression('GROUP_CONCAT(DISTINCT d_gid ORDER BY d_gid) AS xrefs')])
143            ->distinct()
144            ->pluck('xrefs')
145            ->map(static function (string $xrefs) use ($tree): array {
146                return array_map(static function (string $xref) use ($tree): Individual {
147                    return Registry::individualFactory()->make($xref, $tree);
148                }, explode(',', $xrefs));
149            })
150            ->all();
151
152        $families = DB::table('families')
153            ->where('f_file', '=', $tree->id())
154            ->groupBy([new Expression('LEAST(f_husb, f_wife)')])
155            ->groupBy([new Expression('GREATEST(f_husb, f_wife)')])
156            ->having(new Expression('COUNT(f_id)'), '>', '1')
157            ->select([new Expression('GROUP_CONCAT(f_id) AS xrefs')])
158            ->pluck('xrefs')
159            ->map(static function (string $xrefs) use ($tree): array {
160                return array_map(static function (string $xref) use ($tree): Family {
161                    return Registry::familyFactory()->make($xref, $tree);
162                }, explode(',', $xrefs));
163            })
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(m_id)'), '>', '1')
171            ->select([new Expression('GROUP_CONCAT(m_id) AS xrefs')])
172            ->pluck('xrefs')
173            ->map(static function (string $xrefs) use ($tree): array {
174                return array_map(static function (string $xref) use ($tree): Media {
175                    return Registry::mediaFactory()->make($xref, $tree);
176                }, explode(',', $xrefs));
177            })
178            ->all();
179
180        return [
181            I18N::translate('Repositories')  => $repositories,
182            I18N::translate('Sources')       => $sources,
183            I18N::translate('Individuals')   => $individuals,
184            I18N::translate('Families')      => $families,
185            I18N::translate('Media objects') => $media,
186        ];
187    }
188
189    /**
190     * Every XREF used by this tree and also used by some other tree
191     *
192     * @param Tree $tree
193     *
194     * @return array<string>
195     */
196    public function duplicateXrefs(Tree $tree): array
197    {
198        $subquery1 = DB::table('individuals')
199            ->where('i_file', '=', $tree->id())
200            ->select(['i_id AS xref', new Expression("'INDI' AS type")])
201            ->union(DB::table('families')
202                ->where('f_file', '=', $tree->id())
203                ->select(['f_id AS xref', new Expression("'FAM' AS type")]))
204            ->union(DB::table('sources')
205                ->where('s_file', '=', $tree->id())
206                ->select(['s_id AS xref', new Expression("'SOUR' AS type")]))
207            ->union(DB::table('media')
208                ->where('m_file', '=', $tree->id())
209                ->select(['m_id AS xref', new Expression("'OBJE' AS type")]))
210            ->union(DB::table('other')
211                ->where('o_file', '=', $tree->id())
212                ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
213                ->select(['o_id AS xref', 'o_type AS type']));
214
215        $subquery2 = DB::table('change')
216            ->where('gedcom_id', '<>', $tree->id())
217            ->select(['xref AS other_xref'])
218            ->union(DB::table('individuals')
219                ->where('i_file', '<>', $tree->id())
220                ->select(['i_id AS xref']))
221            ->union(DB::table('families')
222                ->where('f_file', '<>', $tree->id())
223                ->select(['f_id AS xref']))
224            ->union(DB::table('sources')
225                ->where('s_file', '<>', $tree->id())
226                ->select(['s_id AS xref']))
227            ->union(DB::table('media')
228                ->where('m_file', '<>', $tree->id())
229                ->select(['m_id AS xref']))
230            ->union(DB::table('other')
231                ->where('o_file', '<>', $tree->id())
232                ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
233                ->select(['o_id AS xref']));
234
235        return DB::table(new Expression('(' . $subquery1->toSql() . ') AS sub1'))
236            ->mergeBindings($subquery1)
237            ->joinSub($subquery2, 'sub2', 'other_xref', '=', 'xref')
238            ->pluck('type', 'xref')
239            ->all();
240    }
241
242    /**
243     * A list of GEDCOM files in the data folder.
244     *
245     * @param FilesystemOperator $filesystem
246     *
247     * @return Collection<string>
248     */
249    public function gedcomFiles(FilesystemOperator $filesystem): Collection
250    {
251        try {
252            $files = $filesystem->listContents('')
253                ->filter(static function (StorageAttributes $attributes) use ($filesystem) {
254                    if (!$attributes->isFile()) {
255                        return false;
256                    }
257
258                    $stream = $filesystem->readStream($attributes->path());
259
260                    $header = fread($stream, 10);
261                    fclose($stream);
262
263                    return preg_match('/^(' . Gedcom::UTF8_BOM . ')?0 HEAD/', $header) > 0;
264                })
265                ->map(function (StorageAttributes $attributes) {
266                    return $attributes->path();
267                })
268                ->toArray();
269        } catch (FilesystemException $ex) {
270            $files = [];
271        }
272
273        return Collection::make($files)->sort();
274    }
275
276    /**
277     * Change the behaviour a little, when there are a lot of trees.
278     *
279     * @return int
280     */
281    public function multipleTreeThreshold(): int
282    {
283        return (int) Site::getPreference('MULTIPLE_TREE_THRESHOLD', self::MULTIPLE_TREE_THRESHOLD);
284    }
285}
286