xref: /webtrees/app/Module/SiteMapModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
21a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
22a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23b1b85189SGreg Roachuse Fisharebest\Webtrees\Html;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
31a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
32a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request;
33a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response;
34a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
358c2e8227SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class SiteMapModule
388c2e8227SGreg Roach */
39c1010edaSGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface
40c1010edaSGreg Roach{
418c2e8227SGreg Roach    const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
428c2e8227SGreg Roach    const CACHE_LIFE         = 1209600; // Two weeks
438c2e8227SGreg Roach
44a5f7ed67SGreg Roach    /**
45a5f7ed67SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
46a5f7ed67SGreg Roach     *
47a5f7ed67SGreg Roach     * @return string
48a5f7ed67SGreg Roach     */
498f53f488SRico Sonntag    public function getTitle(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
52bbb76c12SGreg Roach        return I18N::translate('Sitemaps');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
55a5f7ed67SGreg Roach    /**
56a5f7ed67SGreg Roach     * A sentence describing what this module does.
57a5f7ed67SGreg Roach     *
58a5f7ed67SGreg Roach     * @return string
59a5f7ed67SGreg Roach     */
608f53f488SRico Sonntag    public function getDescription(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
63bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
67a5f7ed67SGreg Roach     * The URL to a page where the user can modify the configuration of this module.
6876692c8bSGreg Roach     *
69a5f7ed67SGreg Roach     * @return string
7076692c8bSGreg Roach     */
718f53f488SRico Sonntag    public function getConfigLink(): string
72c1010edaSGreg Roach    {
73c1010edaSGreg Roach        return route('module', [
74c1010edaSGreg Roach            'module' => $this->getName(),
75c1010edaSGreg Roach            'action' => 'Admin',
76c1010edaSGreg Roach        ]);
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
798c2e8227SGreg Roach    /**
80a5f7ed67SGreg Roach     * @return Response
818c2e8227SGreg Roach     */
8236e59714SGreg Roach    public function getAdminAction(): Response
83c1010edaSGreg Roach    {
84a5f7ed67SGreg Roach        $this->layout = 'layouts/administration';
85a5f7ed67SGreg Roach
86c1010edaSGreg Roach        $sitemap_url = route('module', [
87c1010edaSGreg Roach            'module' => 'sitemap',
88c1010edaSGreg Roach            'action' => 'Index',
89c1010edaSGreg Roach        ]);
90a5f7ed67SGreg Roach
91a5f7ed67SGreg Roach        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
92a5f7ed67SGreg Roach        $submit_urls = [
93a5f7ed67SGreg Roach            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
94a5f7ed67SGreg Roach            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
95a5f7ed67SGreg Roach        ];
96a5f7ed67SGreg Roach
97291c1b19SGreg Roach        return $this->viewResponse('modules/sitemap/config', [
98a5f7ed67SGreg Roach            'all_trees'   => Tree::getAll(),
99a5f7ed67SGreg Roach            'sitemap_url' => $sitemap_url,
100a5f7ed67SGreg Roach            'submit_urls' => $submit_urls,
101a5f7ed67SGreg Roach            'title'       => $this->getTitle(),
102a5f7ed67SGreg Roach        ]);
1038c2e8227SGreg Roach    }
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach    /**
106a5f7ed67SGreg Roach     * @param Request $request
107a5f7ed67SGreg Roach     *
108a5f7ed67SGreg Roach     * @return RedirectResponse
1098c2e8227SGreg Roach     */
110c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
111c1010edaSGreg Roach    {
1128c2e8227SGreg Roach        foreach (Tree::getAll() as $tree) {
11372cf66d4SGreg Roach            $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id());
114a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1158c2e8227SGreg Roach        }
116a5f7ed67SGreg Roach
117291c1b19SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
118a5f7ed67SGreg Roach
119a5f7ed67SGreg Roach        return new RedirectResponse($this->getConfigLink());
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
1228c2e8227SGreg Roach    /**
123a5f7ed67SGreg Roach     * @return Response
1248c2e8227SGreg Roach     */
12536e59714SGreg Roach    public function getIndexAction(): Response
126c1010edaSGreg Roach    {
127a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
128a5f7ed67SGreg Roach
129a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
130a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1318c2e8227SGreg Roach        } else {
132a5f7ed67SGreg Roach            $count_individuals = Database::prepare(
133a5f7ed67SGreg Roach                "SELECT i_file, COUNT(*) FROM `##individuals` GROUP BY i_file"
134a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
135a5f7ed67SGreg Roach
136a5f7ed67SGreg Roach            $count_media = Database::prepare(
137a5f7ed67SGreg Roach                "SELECT m_file, COUNT(*) FROM `##media` GROUP BY m_file"
138a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
139a5f7ed67SGreg Roach
140a5f7ed67SGreg Roach            $count_notes = Database::prepare(
141a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='NOTE' GROUP BY o_file"
142a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
143a5f7ed67SGreg Roach
144a5f7ed67SGreg Roach            $count_repositories = Database::prepare(
145a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='REPO' GROUP BY o_file"
146a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
147a5f7ed67SGreg Roach
148a5f7ed67SGreg Roach            $count_sources = Database::prepare(
149a5f7ed67SGreg Roach                "SELECT s_file, COUNT(*) FROM `##sources` GROUP BY s_file"
150a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
151a5f7ed67SGreg Roach
152a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
153a5f7ed67SGreg Roach                'all_trees'          => Tree::getAll(),
154a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
155a5f7ed67SGreg Roach                'count_media'        => $count_media,
156a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
157a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
158a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
159a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
160a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
161a5f7ed67SGreg Roach            ]);
162a5f7ed67SGreg Roach
163a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
164a5f7ed67SGreg Roach        }
165a5f7ed67SGreg Roach
166a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
167a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
168a5f7ed67SGreg Roach        ]);
169a5f7ed67SGreg Roach    }
170a5f7ed67SGreg Roach
171a5f7ed67SGreg Roach    /**
172a5f7ed67SGreg Roach     * @param Request $request
173a5f7ed67SGreg Roach     *
174a5f7ed67SGreg Roach     * @return Response
175a5f7ed67SGreg Roach     */
176c1010edaSGreg Roach    public function getFileAction(Request $request): Response
177c1010edaSGreg Roach    {
178a5f7ed67SGreg Roach        $file = $request->get('file', '');
179a5f7ed67SGreg Roach
180a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
181a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
182a5f7ed67SGreg Roach        }
183a5f7ed67SGreg Roach
184a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
185a5f7ed67SGreg Roach
186a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
187a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
188a5f7ed67SGreg Roach        } else {
189a5f7ed67SGreg Roach            $tree = Tree::findById((int) $match[1]);
190a5f7ed67SGreg Roach
191a5f7ed67SGreg Roach            if ($tree === null) {
192a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
193a5f7ed67SGreg Roach            }
194a5f7ed67SGreg Roach
195bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
196a5f7ed67SGreg Roach
197a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
198a5f7ed67SGreg Roach
199a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
200a5f7ed67SGreg Roach        }
201a5f7ed67SGreg Roach
202a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
203a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
204a5f7ed67SGreg Roach        ]);
205a5f7ed67SGreg Roach    }
206a5f7ed67SGreg Roach
207a5f7ed67SGreg Roach    /**
208a5f7ed67SGreg Roach     * @param Tree   $tree
209a5f7ed67SGreg Roach     * @param string $type
210a5f7ed67SGreg Roach     * @param int    $limit
211a5f7ed67SGreg Roach     * @param int    $offset
212a5f7ed67SGreg Roach     *
213a5f7ed67SGreg Roach     * @return array
214a5f7ed67SGreg Roach     */
215c1010edaSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array
216c1010edaSGreg Roach    {
217a5f7ed67SGreg Roach        switch ($type) {
2188c2e8227SGreg Roach            case 'i':
219a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
220a5f7ed67SGreg Roach                break;
221a5f7ed67SGreg Roach
222a5f7ed67SGreg Roach            case 'm':
223a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
224a5f7ed67SGreg Roach                break;
225a5f7ed67SGreg Roach
226a5f7ed67SGreg Roach            case 'n':
227a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
228a5f7ed67SGreg Roach                break;
229a5f7ed67SGreg Roach
230a5f7ed67SGreg Roach            case 'r':
231a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
232a5f7ed67SGreg Roach                break;
233a5f7ed67SGreg Roach
234a5f7ed67SGreg Roach            case 's':
235a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
236a5f7ed67SGreg Roach                break;
237a5f7ed67SGreg Roach
238a5f7ed67SGreg Roach            default:
239a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
240a5f7ed67SGreg Roach        }
241a5f7ed67SGreg Roach
242a5f7ed67SGreg Roach        // Skip records that no longer exist.
243a5f7ed67SGreg Roach        $records = array_filter($records);
244a5f7ed67SGreg Roach
245a5f7ed67SGreg Roach        // Skip private records.
246492c7072SGreg Roach        $records = array_filter($records, function (GedcomRecord $record): bool {
247a5f7ed67SGreg Roach            return $record->canShow();
248a5f7ed67SGreg Roach        });
249a5f7ed67SGreg Roach
250a5f7ed67SGreg Roach        return $records;
251a5f7ed67SGreg Roach    }
252a5f7ed67SGreg Roach
253a5f7ed67SGreg Roach    /**
254a5f7ed67SGreg Roach     * @param Tree $tree
255a5f7ed67SGreg Roach     * @param int  $limit
256a5f7ed67SGreg Roach     * @param int  $offset
257a5f7ed67SGreg Roach     *
258a5f7ed67SGreg Roach     * @return array
259a5f7ed67SGreg Roach     */
260c1010edaSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array
261c1010edaSGreg Roach    {
2628c2e8227SGreg Roach        $rows = Database::prepare(
26324ec66ceSGreg Roach            "SELECT i_id AS xref, i_gedcom AS gedcom" .
2648c2e8227SGreg Roach            " FROM `##individuals`" .
2658c2e8227SGreg Roach            " WHERE i_file = :tree_id" .
2668c2e8227SGreg Roach            " ORDER BY i_id" .
2678c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
26813abd6f3SGreg Roach        )->execute([
26972cf66d4SGreg Roach            'tree_id' => $tree->id(),
270a5f7ed67SGreg Roach            'limit'   => $limit,
271a5f7ed67SGreg Roach            'offset'  => $offset,
27213abd6f3SGreg Roach        ])->fetchAll();
273a5f7ed67SGreg Roach
274a5f7ed67SGreg Roach        $records = [];
275a5f7ed67SGreg Roach
2768c2e8227SGreg Roach        foreach ($rows as $row) {
27724ec66ceSGreg Roach            $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom);
2788c2e8227SGreg Roach        }
279a5f7ed67SGreg Roach
280a5f7ed67SGreg Roach        return $records;
2818c2e8227SGreg Roach    }
282a5f7ed67SGreg Roach
283a5f7ed67SGreg Roach    /**
284a5f7ed67SGreg Roach     * @param Tree $tree
285a5f7ed67SGreg Roach     * @param int  $limit
286a5f7ed67SGreg Roach     * @param int  $offset
287a5f7ed67SGreg Roach     *
288a5f7ed67SGreg Roach     * @return array
289a5f7ed67SGreg Roach     */
290c1010edaSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): array
291c1010edaSGreg Roach    {
2928c2e8227SGreg Roach        $rows = Database::prepare(
29324ec66ceSGreg Roach            "SELECT m_id AS xref, m_gedcom AS gedcom" .
2948c2e8227SGreg Roach            " FROM `##media`" .
2958c2e8227SGreg Roach            " WHERE m_file = :tree_id" .
2968c2e8227SGreg Roach            " ORDER BY m_id" .
2978c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
29813abd6f3SGreg Roach        )->execute([
29972cf66d4SGreg Roach            'tree_id' => $tree->id(),
300a5f7ed67SGreg Roach            'limit'   => $limit,
301a5f7ed67SGreg Roach            'offset'  => $offset,
30213abd6f3SGreg Roach        ])->fetchAll();
303a5f7ed67SGreg Roach
304a5f7ed67SGreg Roach        $records = [];
305a5f7ed67SGreg Roach
3068c2e8227SGreg Roach        foreach ($rows as $row) {
30724ec66ceSGreg Roach            $records[] = Media::getInstance($row->xref, $tree, $row->gedcom);
3088c2e8227SGreg Roach        }
309a5f7ed67SGreg Roach
310a5f7ed67SGreg Roach        return $records;
3118c2e8227SGreg Roach    }
3128c2e8227SGreg Roach
3138c2e8227SGreg Roach    /**
314a5f7ed67SGreg Roach     * @param Tree $tree
315a5f7ed67SGreg Roach     * @param int  $limit
316a5f7ed67SGreg Roach     * @param int  $offset
317a5f7ed67SGreg Roach     *
318a5f7ed67SGreg Roach     * @return array
3198c2e8227SGreg Roach     */
320c1010edaSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): array
321c1010edaSGreg Roach    {
322a5f7ed67SGreg Roach        $rows = Database::prepare(
323a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
324a5f7ed67SGreg Roach            " FROM `##other`" .
325a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'NOTE'" .
326a5f7ed67SGreg Roach            " ORDER BY o_id" .
327a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
328a5f7ed67SGreg Roach        )->execute([
32972cf66d4SGreg Roach            'tree_id' => $tree->id(),
330a5f7ed67SGreg Roach            'limit'   => $limit,
331a5f7ed67SGreg Roach            'offset'  => $offset,
332a5f7ed67SGreg Roach        ])->fetchAll();
3338c2e8227SGreg Roach
334a5f7ed67SGreg Roach        $records = [];
335a5f7ed67SGreg Roach
336a5f7ed67SGreg Roach        foreach ($rows as $row) {
337a5f7ed67SGreg Roach            $records[] = Note::getInstance($row->xref, $tree, $row->gedcom);
3388c2e8227SGreg Roach        }
3398c2e8227SGreg Roach
340a5f7ed67SGreg Roach        return $records;
3418c2e8227SGreg Roach    }
3428c2e8227SGreg Roach
343a5f7ed67SGreg Roach    /**
344a5f7ed67SGreg Roach     * @param Tree $tree
345a5f7ed67SGreg Roach     * @param int  $limit
346a5f7ed67SGreg Roach     * @param int  $offset
347a5f7ed67SGreg Roach     *
348a5f7ed67SGreg Roach     * @return array
349a5f7ed67SGreg Roach     */
350c1010edaSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): array
351c1010edaSGreg Roach    {
352a5f7ed67SGreg Roach        $rows = Database::prepare(
353a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
354a5f7ed67SGreg Roach            " FROM `##other`" .
355a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'REPO'" .
356a5f7ed67SGreg Roach            " ORDER BY o_id" .
357a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
358a5f7ed67SGreg Roach        )->execute([
35972cf66d4SGreg Roach            'tree_id' => $tree->id(),
360a5f7ed67SGreg Roach            'limit'   => $limit,
361a5f7ed67SGreg Roach            'offset'  => $offset,
362a5f7ed67SGreg Roach        ])->fetchAll();
363a5f7ed67SGreg Roach
364a5f7ed67SGreg Roach        $records = [];
365a5f7ed67SGreg Roach
366a5f7ed67SGreg Roach        foreach ($rows as $row) {
367a5f7ed67SGreg Roach            $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom);
368a5f7ed67SGreg Roach        }
369a5f7ed67SGreg Roach
370a5f7ed67SGreg Roach        return $records;
371a5f7ed67SGreg Roach    }
372a5f7ed67SGreg Roach
373a5f7ed67SGreg Roach    /**
374a5f7ed67SGreg Roach     * @param Tree $tree
375a5f7ed67SGreg Roach     * @param int  $limit
376a5f7ed67SGreg Roach     * @param int  $offset
377a5f7ed67SGreg Roach     *
378a5f7ed67SGreg Roach     * @return array
379a5f7ed67SGreg Roach     */
380c1010edaSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): array
381c1010edaSGreg Roach    {
382a5f7ed67SGreg Roach        $rows = Database::prepare(
383a5f7ed67SGreg Roach            "SELECT s_id AS xref, s_gedcom AS gedcom" .
384a5f7ed67SGreg Roach            " FROM `##sources`" .
385a5f7ed67SGreg Roach            " WHERE s_file = :tree_id" .
386a5f7ed67SGreg Roach            " ORDER BY s_id" .
387a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
388a5f7ed67SGreg Roach        )->execute([
38972cf66d4SGreg Roach            'tree_id' => $tree->id(),
390a5f7ed67SGreg Roach            'limit'   => $limit,
391a5f7ed67SGreg Roach            'offset'  => $offset,
392a5f7ed67SGreg Roach        ])->fetchAll();
393a5f7ed67SGreg Roach
394a5f7ed67SGreg Roach        $records = [];
395a5f7ed67SGreg Roach
396a5f7ed67SGreg Roach        foreach ($rows as $row) {
397a5f7ed67SGreg Roach            $records[] = Source::getInstance($row->xref, $tree, $row->gedcom);
398a5f7ed67SGreg Roach        }
399a5f7ed67SGreg Roach
400a5f7ed67SGreg Roach        return $records;
4018c2e8227SGreg Roach    }
4028c2e8227SGreg Roach}
403