xref: /webtrees/app/Module/SiteMapModule.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg 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     * @param Request $request
8176692c8bSGreg Roach     *
82a5f7ed67SGreg Roach     * @return Response
838c2e8227SGreg Roach     */
84c1010edaSGreg Roach    public function getAdminAction(Request $request): Response
85c1010edaSGreg Roach    {
86a5f7ed67SGreg Roach        $this->layout = 'layouts/administration';
87a5f7ed67SGreg Roach
88c1010edaSGreg Roach        $sitemap_url = route('module', [
89c1010edaSGreg Roach            'module' => 'sitemap',
90c1010edaSGreg Roach            'action' => 'Index',
91c1010edaSGreg Roach        ]);
92a5f7ed67SGreg Roach
93a5f7ed67SGreg Roach        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
94a5f7ed67SGreg Roach        $submit_urls = [
95a5f7ed67SGreg Roach            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
96a5f7ed67SGreg Roach            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
97a5f7ed67SGreg Roach        ];
98a5f7ed67SGreg Roach
99291c1b19SGreg Roach        return $this->viewResponse('modules/sitemap/config', [
100a5f7ed67SGreg Roach            'all_trees'   => Tree::getAll(),
101a5f7ed67SGreg Roach            'sitemap_url' => $sitemap_url,
102a5f7ed67SGreg Roach            'submit_urls' => $submit_urls,
103a5f7ed67SGreg Roach            'title'       => $this->getTitle(),
104a5f7ed67SGreg Roach        ]);
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach    /**
108a5f7ed67SGreg Roach     * @param Request $request
109a5f7ed67SGreg Roach     *
110a5f7ed67SGreg Roach     * @return RedirectResponse
1118c2e8227SGreg Roach     */
112c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
113c1010edaSGreg Roach    {
1148c2e8227SGreg Roach        foreach (Tree::getAll() as $tree) {
115a5f7ed67SGreg Roach            $include_in_sitemap = (bool) $request->get('sitemap' . $tree->getTreeId());
116a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1178c2e8227SGreg Roach        }
118a5f7ed67SGreg Roach
119291c1b19SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success');
120a5f7ed67SGreg Roach
121a5f7ed67SGreg Roach        return new RedirectResponse($this->getConfigLink());
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach    /**
125a5f7ed67SGreg Roach     * @param Request $request
1268c2e8227SGreg Roach     *
127a5f7ed67SGreg Roach     * @return Response
1288c2e8227SGreg Roach     */
129c1010edaSGreg Roach    public function getIndexAction(Request $request): Response
130c1010edaSGreg Roach    {
131a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
132a5f7ed67SGreg Roach
133a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
134a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1358c2e8227SGreg Roach        } else {
136a5f7ed67SGreg Roach            $count_individuals = Database::prepare(
137a5f7ed67SGreg Roach                "SELECT i_file, COUNT(*) FROM `##individuals` GROUP BY i_file"
138a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
139a5f7ed67SGreg Roach
140a5f7ed67SGreg Roach            $count_media = Database::prepare(
141a5f7ed67SGreg Roach                "SELECT m_file, COUNT(*) FROM `##media` GROUP BY m_file"
142a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
143a5f7ed67SGreg Roach
144a5f7ed67SGreg Roach            $count_notes = Database::prepare(
145a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='NOTE' GROUP BY o_file"
146a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
147a5f7ed67SGreg Roach
148a5f7ed67SGreg Roach            $count_repositories = Database::prepare(
149a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='REPO' GROUP BY o_file"
150a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
151a5f7ed67SGreg Roach
152a5f7ed67SGreg Roach            $count_sources = Database::prepare(
153a5f7ed67SGreg Roach                "SELECT s_file, COUNT(*) FROM `##sources` GROUP BY s_file"
154a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
155a5f7ed67SGreg Roach
156a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
157a5f7ed67SGreg Roach                'all_trees'          => Tree::getAll(),
158a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
159a5f7ed67SGreg Roach                'count_media'        => $count_media,
160a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
161a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
162a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
163a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
164a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
165a5f7ed67SGreg Roach            ]);
166a5f7ed67SGreg Roach
167a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
168a5f7ed67SGreg Roach        }
169a5f7ed67SGreg Roach
170a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
171a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
172a5f7ed67SGreg Roach        ]);
173a5f7ed67SGreg Roach    }
174a5f7ed67SGreg Roach
175a5f7ed67SGreg Roach    /**
176a5f7ed67SGreg Roach     * @param Request $request
177a5f7ed67SGreg Roach     *
178a5f7ed67SGreg Roach     * @return Response
179a5f7ed67SGreg Roach     */
180c1010edaSGreg Roach    public function getFileAction(Request $request): Response
181c1010edaSGreg Roach    {
182a5f7ed67SGreg Roach        $file = $request->get('file', '');
183a5f7ed67SGreg Roach
184a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
185a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
186a5f7ed67SGreg Roach        }
187a5f7ed67SGreg Roach
188a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
189a5f7ed67SGreg Roach
190a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
191a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
192a5f7ed67SGreg Roach        } else {
193a5f7ed67SGreg Roach            $tree = Tree::findById((int) $match[1]);
194a5f7ed67SGreg Roach
195a5f7ed67SGreg Roach            if ($tree === null) {
196a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
197a5f7ed67SGreg Roach            }
198a5f7ed67SGreg Roach
199bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
200a5f7ed67SGreg Roach
201a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
202a5f7ed67SGreg Roach
203a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
204a5f7ed67SGreg Roach        }
205a5f7ed67SGreg Roach
206a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
207a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
208a5f7ed67SGreg Roach        ]);
209a5f7ed67SGreg Roach    }
210a5f7ed67SGreg Roach
211a5f7ed67SGreg Roach    /**
212a5f7ed67SGreg Roach     * @param Tree   $tree
213a5f7ed67SGreg Roach     * @param string $type
214a5f7ed67SGreg Roach     * @param int    $limit
215a5f7ed67SGreg Roach     * @param int    $offset
216a5f7ed67SGreg Roach     *
217a5f7ed67SGreg Roach     * @return array
218a5f7ed67SGreg Roach     */
219c1010edaSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array
220c1010edaSGreg Roach    {
221a5f7ed67SGreg Roach        switch ($type) {
2228c2e8227SGreg Roach            case 'i':
223a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
224a5f7ed67SGreg Roach                break;
225a5f7ed67SGreg Roach
226a5f7ed67SGreg Roach            case 'm':
227a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
228a5f7ed67SGreg Roach                break;
229a5f7ed67SGreg Roach
230a5f7ed67SGreg Roach            case 'n':
231a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
232a5f7ed67SGreg Roach                break;
233a5f7ed67SGreg Roach
234a5f7ed67SGreg Roach            case 'r':
235a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
236a5f7ed67SGreg Roach                break;
237a5f7ed67SGreg Roach
238a5f7ed67SGreg Roach            case 's':
239a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
240a5f7ed67SGreg Roach                break;
241a5f7ed67SGreg Roach
242a5f7ed67SGreg Roach            default:
243a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
244a5f7ed67SGreg Roach        }
245a5f7ed67SGreg Roach
246a5f7ed67SGreg Roach        // Skip records that no longer exist.
247a5f7ed67SGreg Roach        $records = array_filter($records);
248a5f7ed67SGreg Roach
249a5f7ed67SGreg Roach        // Skip private records.
250492c7072SGreg Roach        $records = array_filter($records, function (GedcomRecord $record): bool {
251a5f7ed67SGreg Roach            return $record->canShow();
252a5f7ed67SGreg Roach        });
253a5f7ed67SGreg Roach
254a5f7ed67SGreg Roach        return $records;
255a5f7ed67SGreg Roach    }
256a5f7ed67SGreg Roach
257a5f7ed67SGreg Roach    /**
258a5f7ed67SGreg Roach     * @param Tree $tree
259a5f7ed67SGreg Roach     * @param int  $limit
260a5f7ed67SGreg Roach     * @param int  $offset
261a5f7ed67SGreg Roach     *
262a5f7ed67SGreg Roach     * @return array
263a5f7ed67SGreg Roach     */
264c1010edaSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array
265c1010edaSGreg Roach    {
2668c2e8227SGreg Roach        $rows = Database::prepare(
26724ec66ceSGreg Roach            "SELECT i_id AS xref, i_gedcom AS gedcom" .
2688c2e8227SGreg Roach            " FROM `##individuals`" .
2698c2e8227SGreg Roach            " WHERE i_file = :tree_id" .
2708c2e8227SGreg Roach            " ORDER BY i_id" .
2718c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
27213abd6f3SGreg Roach        )->execute([
273a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
274a5f7ed67SGreg Roach            'limit'   => $limit,
275a5f7ed67SGreg Roach            'offset'  => $offset,
27613abd6f3SGreg Roach        ])->fetchAll();
277a5f7ed67SGreg Roach
278a5f7ed67SGreg Roach        $records = [];
279a5f7ed67SGreg Roach
2808c2e8227SGreg Roach        foreach ($rows as $row) {
28124ec66ceSGreg Roach            $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom);
2828c2e8227SGreg Roach        }
283a5f7ed67SGreg Roach
284a5f7ed67SGreg Roach        return $records;
2858c2e8227SGreg Roach    }
286a5f7ed67SGreg Roach
287a5f7ed67SGreg Roach    /**
288a5f7ed67SGreg Roach     * @param Tree $tree
289a5f7ed67SGreg Roach     * @param int  $limit
290a5f7ed67SGreg Roach     * @param int  $offset
291a5f7ed67SGreg Roach     *
292a5f7ed67SGreg Roach     * @return array
293a5f7ed67SGreg Roach     */
294c1010edaSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): array
295c1010edaSGreg Roach    {
2968c2e8227SGreg Roach        $rows = Database::prepare(
29724ec66ceSGreg Roach            "SELECT m_id AS xref, m_gedcom AS gedcom" .
2988c2e8227SGreg Roach            " FROM `##media`" .
2998c2e8227SGreg Roach            " WHERE m_file = :tree_id" .
3008c2e8227SGreg Roach            " ORDER BY m_id" .
3018c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
30213abd6f3SGreg Roach        )->execute([
303a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
304a5f7ed67SGreg Roach            'limit'   => $limit,
305a5f7ed67SGreg Roach            'offset'  => $offset,
30613abd6f3SGreg Roach        ])->fetchAll();
307a5f7ed67SGreg Roach
308a5f7ed67SGreg Roach        $records = [];
309a5f7ed67SGreg Roach
3108c2e8227SGreg Roach        foreach ($rows as $row) {
31124ec66ceSGreg Roach            $records[] = Media::getInstance($row->xref, $tree, $row->gedcom);
3128c2e8227SGreg Roach        }
313a5f7ed67SGreg Roach
314a5f7ed67SGreg Roach        return $records;
3158c2e8227SGreg Roach    }
3168c2e8227SGreg Roach
3178c2e8227SGreg Roach    /**
318a5f7ed67SGreg Roach     * @param Tree $tree
319a5f7ed67SGreg Roach     * @param int  $limit
320a5f7ed67SGreg Roach     * @param int  $offset
321a5f7ed67SGreg Roach     *
322a5f7ed67SGreg Roach     * @return array
3238c2e8227SGreg Roach     */
324c1010edaSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): array
325c1010edaSGreg Roach    {
326a5f7ed67SGreg Roach        $rows = Database::prepare(
327a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
328a5f7ed67SGreg Roach            " FROM `##other`" .
329a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'NOTE'" .
330a5f7ed67SGreg Roach            " ORDER BY o_id" .
331a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
332a5f7ed67SGreg Roach        )->execute([
333a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
334a5f7ed67SGreg Roach            'limit'   => $limit,
335a5f7ed67SGreg Roach            'offset'  => $offset,
336a5f7ed67SGreg Roach        ])->fetchAll();
3378c2e8227SGreg Roach
338a5f7ed67SGreg Roach        $records = [];
339a5f7ed67SGreg Roach
340a5f7ed67SGreg Roach        foreach ($rows as $row) {
341a5f7ed67SGreg Roach            $records[] = Note::getInstance($row->xref, $tree, $row->gedcom);
3428c2e8227SGreg Roach        }
3438c2e8227SGreg Roach
344a5f7ed67SGreg Roach        return $records;
3458c2e8227SGreg Roach    }
3468c2e8227SGreg Roach
347a5f7ed67SGreg Roach    /**
348a5f7ed67SGreg Roach     * @param Tree $tree
349a5f7ed67SGreg Roach     * @param int  $limit
350a5f7ed67SGreg Roach     * @param int  $offset
351a5f7ed67SGreg Roach     *
352a5f7ed67SGreg Roach     * @return array
353a5f7ed67SGreg Roach     */
354c1010edaSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): array
355c1010edaSGreg Roach    {
356a5f7ed67SGreg Roach        $rows = Database::prepare(
357a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
358a5f7ed67SGreg Roach            " FROM `##other`" .
359a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'REPO'" .
360a5f7ed67SGreg Roach            " ORDER BY o_id" .
361a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
362a5f7ed67SGreg Roach        )->execute([
363a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
364a5f7ed67SGreg Roach            'limit'   => $limit,
365a5f7ed67SGreg Roach            'offset'  => $offset,
366a5f7ed67SGreg Roach        ])->fetchAll();
367a5f7ed67SGreg Roach
368a5f7ed67SGreg Roach        $records = [];
369a5f7ed67SGreg Roach
370a5f7ed67SGreg Roach        foreach ($rows as $row) {
371a5f7ed67SGreg Roach            $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom);
372a5f7ed67SGreg Roach        }
373a5f7ed67SGreg Roach
374a5f7ed67SGreg Roach        return $records;
375a5f7ed67SGreg Roach    }
376a5f7ed67SGreg Roach
377a5f7ed67SGreg Roach    /**
378a5f7ed67SGreg Roach     * @param Tree $tree
379a5f7ed67SGreg Roach     * @param int  $limit
380a5f7ed67SGreg Roach     * @param int  $offset
381a5f7ed67SGreg Roach     *
382a5f7ed67SGreg Roach     * @return array
383a5f7ed67SGreg Roach     */
384c1010edaSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): array
385c1010edaSGreg Roach    {
386a5f7ed67SGreg Roach        $rows = Database::prepare(
387a5f7ed67SGreg Roach            "SELECT s_id AS xref, s_gedcom AS gedcom" .
388a5f7ed67SGreg Roach            " FROM `##sources`" .
389a5f7ed67SGreg Roach            " WHERE s_file = :tree_id" .
390a5f7ed67SGreg Roach            " ORDER BY s_id" .
391a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
392a5f7ed67SGreg Roach        )->execute([
393a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
394a5f7ed67SGreg Roach            'limit'   => $limit,
395a5f7ed67SGreg Roach            'offset'  => $offset,
396a5f7ed67SGreg Roach        ])->fetchAll();
397a5f7ed67SGreg Roach
398a5f7ed67SGreg Roach        $records = [];
399a5f7ed67SGreg Roach
400a5f7ed67SGreg Roach        foreach ($rows as $row) {
401a5f7ed67SGreg Roach            $records[] = Source::getInstance($row->xref, $tree, $row->gedcom);
402a5f7ed67SGreg Roach        }
403a5f7ed67SGreg Roach
404a5f7ed67SGreg Roach        return $records;
4058c2e8227SGreg Roach    }
4068c2e8227SGreg Roach}
407