xref: /webtrees/app/Module/SiteMapModule.php (revision bbb76c12bd7338ebbb054916678efe20cb71ce1f)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
19a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
20a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
21b1b85189SGreg Roachuse Fisharebest\Webtrees\Html;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
29a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
30a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request;
31a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response;
32a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
338c2e8227SGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class SiteMapModule
368c2e8227SGreg Roach */
37c1010edaSGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface
38c1010edaSGreg Roach{
398c2e8227SGreg Roach    const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
408c2e8227SGreg Roach    const CACHE_LIFE         = 1209600; // Two weeks
418c2e8227SGreg Roach
42a5f7ed67SGreg Roach    /**
43a5f7ed67SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
44a5f7ed67SGreg Roach     *
45a5f7ed67SGreg Roach     * @return string
46a5f7ed67SGreg Roach     */
47c1010edaSGreg Roach    public function getTitle()
48c1010edaSGreg Roach    {
49*bbb76c12SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
50*bbb76c12SGreg Roach        return I18N::translate('Sitemaps');
518c2e8227SGreg Roach    }
528c2e8227SGreg Roach
53a5f7ed67SGreg Roach    /**
54a5f7ed67SGreg Roach     * A sentence describing what this module does.
55a5f7ed67SGreg Roach     *
56a5f7ed67SGreg Roach     * @return string
57a5f7ed67SGreg Roach     */
58c1010edaSGreg Roach    public function getDescription()
59c1010edaSGreg Roach    {
60*bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
61*bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
6476692c8bSGreg Roach    /**
65a5f7ed67SGreg Roach     * The URL to a page where the user can modify the configuration of this module.
6676692c8bSGreg Roach     *
67a5f7ed67SGreg Roach     * @return string
6876692c8bSGreg Roach     */
69c1010edaSGreg Roach    public function getConfigLink()
70c1010edaSGreg Roach    {
71c1010edaSGreg Roach        return route('module', [
72c1010edaSGreg Roach            'module' => $this->getName(),
73c1010edaSGreg Roach            'action' => 'Admin',
74c1010edaSGreg Roach        ]);
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
778c2e8227SGreg Roach    /**
78a5f7ed67SGreg Roach     * @param Request $request
7976692c8bSGreg Roach     *
80a5f7ed67SGreg Roach     * @return Response
818c2e8227SGreg Roach     */
82c1010edaSGreg Roach    public function getAdminAction(Request $request): 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) {
113a5f7ed67SGreg Roach            $include_in_sitemap = (bool)$request->get('sitemap' . $tree->getTreeId());
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     * @param Request $request
1248c2e8227SGreg Roach     *
125a5f7ed67SGreg Roach     * @return Response
1268c2e8227SGreg Roach     */
127c1010edaSGreg Roach    public function getIndexAction(Request $request): Response
128c1010edaSGreg Roach    {
129a5f7ed67SGreg Roach        $timestamp = (int)$this->getPreference('sitemap.timestamp');
130a5f7ed67SGreg Roach
131a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
132a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1338c2e8227SGreg Roach        } else {
134a5f7ed67SGreg Roach            $count_individuals = Database::prepare(
135a5f7ed67SGreg Roach                "SELECT i_file, COUNT(*) FROM `##individuals` GROUP BY i_file"
136a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
137a5f7ed67SGreg Roach
138a5f7ed67SGreg Roach            $count_media = Database::prepare(
139a5f7ed67SGreg Roach                "SELECT m_file, COUNT(*) FROM `##media` GROUP BY m_file"
140a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
141a5f7ed67SGreg Roach
142a5f7ed67SGreg Roach            $count_notes = Database::prepare(
143a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='NOTE' GROUP BY o_file"
144a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
145a5f7ed67SGreg Roach
146a5f7ed67SGreg Roach            $count_repositories = Database::prepare(
147a5f7ed67SGreg Roach                "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='REPO' GROUP BY o_file"
148a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
149a5f7ed67SGreg Roach
150a5f7ed67SGreg Roach            $count_sources = Database::prepare(
151a5f7ed67SGreg Roach                "SELECT s_file, COUNT(*) FROM `##sources` GROUP BY s_file"
152a5f7ed67SGreg Roach            )->execute()->fetchAssoc();
153a5f7ed67SGreg Roach
154a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
155a5f7ed67SGreg Roach                'all_trees'          => Tree::getAll(),
156a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
157a5f7ed67SGreg Roach                'count_media'        => $count_media,
158a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
159a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
160a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
161a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
162a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
163a5f7ed67SGreg Roach            ]);
164a5f7ed67SGreg Roach
165a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
166a5f7ed67SGreg Roach        }
167a5f7ed67SGreg Roach
168a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
169a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
170a5f7ed67SGreg Roach        ]);
171a5f7ed67SGreg Roach    }
172a5f7ed67SGreg Roach
173a5f7ed67SGreg Roach    /**
174a5f7ed67SGreg Roach     * @param Request $request
175a5f7ed67SGreg Roach     *
176a5f7ed67SGreg Roach     * @return Response
177a5f7ed67SGreg Roach     */
178c1010edaSGreg Roach    public function getFileAction(Request $request): Response
179c1010edaSGreg Roach    {
180a5f7ed67SGreg Roach        $file = $request->get('file', '');
181a5f7ed67SGreg Roach
182a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
183a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
184a5f7ed67SGreg Roach        }
185a5f7ed67SGreg Roach
186a5f7ed67SGreg Roach        $timestamp = (int)$this->getPreference('sitemap-' . $file . '.timestamp');
187a5f7ed67SGreg Roach
188a5f7ed67SGreg Roach        if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) {
189a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
190a5f7ed67SGreg Roach        } else {
191a5f7ed67SGreg Roach            $tree = Tree::findById((int)$match[1]);
192a5f7ed67SGreg Roach
193a5f7ed67SGreg Roach            if ($tree === null) {
194a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
195a5f7ed67SGreg Roach            }
196a5f7ed67SGreg Roach
197a5f7ed67SGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME,
198a5f7ed67SGreg Roach                self::RECORDS_PER_VOLUME * $match[3]);
199a5f7ed67SGreg Roach
200a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
201a5f7ed67SGreg Roach
202a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
203a5f7ed67SGreg Roach        }
204a5f7ed67SGreg Roach
205a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
206a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
207a5f7ed67SGreg Roach        ]);
208a5f7ed67SGreg Roach    }
209a5f7ed67SGreg Roach
210a5f7ed67SGreg Roach    /**
211a5f7ed67SGreg Roach     * @param Tree   $tree
212a5f7ed67SGreg Roach     * @param string $type
213a5f7ed67SGreg Roach     * @param int    $limit
214a5f7ed67SGreg Roach     * @param int    $offset
215a5f7ed67SGreg Roach     *
216a5f7ed67SGreg Roach     * @return array
217a5f7ed67SGreg Roach     */
218c1010edaSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array
219c1010edaSGreg Roach    {
220a5f7ed67SGreg Roach        switch ($type) {
2218c2e8227SGreg Roach            case 'i':
222a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
223a5f7ed67SGreg Roach                break;
224a5f7ed67SGreg Roach
225a5f7ed67SGreg Roach            case 'm':
226a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
227a5f7ed67SGreg Roach                break;
228a5f7ed67SGreg Roach
229a5f7ed67SGreg Roach            case 'n':
230a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
231a5f7ed67SGreg Roach                break;
232a5f7ed67SGreg Roach
233a5f7ed67SGreg Roach            case 'r':
234a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
235a5f7ed67SGreg Roach                break;
236a5f7ed67SGreg Roach
237a5f7ed67SGreg Roach            case 's':
238a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
239a5f7ed67SGreg Roach                break;
240a5f7ed67SGreg Roach
241a5f7ed67SGreg Roach            default:
242a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
243a5f7ed67SGreg Roach        }
244a5f7ed67SGreg Roach
245a5f7ed67SGreg Roach        // Skip records that no longer exist.
246a5f7ed67SGreg Roach        $records = array_filter($records);
247a5f7ed67SGreg Roach
248a5f7ed67SGreg Roach        // Skip private records.
249492c7072SGreg Roach        $records = array_filter($records, function (GedcomRecord $record): bool {
250a5f7ed67SGreg Roach            return $record->canShow();
251a5f7ed67SGreg Roach        });
252a5f7ed67SGreg Roach
253a5f7ed67SGreg Roach        return $records;
254a5f7ed67SGreg Roach    }
255a5f7ed67SGreg Roach
256a5f7ed67SGreg Roach    /**
257a5f7ed67SGreg Roach     * @param Tree $tree
258a5f7ed67SGreg Roach     * @param int  $limit
259a5f7ed67SGreg Roach     * @param int  $offset
260a5f7ed67SGreg Roach     *
261a5f7ed67SGreg Roach     * @return array
262a5f7ed67SGreg Roach     */
263c1010edaSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array
264c1010edaSGreg Roach    {
2658c2e8227SGreg Roach        $rows = Database::prepare(
26624ec66ceSGreg Roach            "SELECT i_id AS xref, i_gedcom AS gedcom" .
2678c2e8227SGreg Roach            " FROM `##individuals`" .
2688c2e8227SGreg Roach            " WHERE i_file = :tree_id" .
2698c2e8227SGreg Roach            " ORDER BY i_id" .
2708c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
27113abd6f3SGreg Roach        )->execute([
272a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
273a5f7ed67SGreg Roach            'limit'   => $limit,
274a5f7ed67SGreg Roach            'offset'  => $offset,
27513abd6f3SGreg Roach        ])->fetchAll();
276a5f7ed67SGreg Roach
277a5f7ed67SGreg Roach        $records = [];
278a5f7ed67SGreg Roach
2798c2e8227SGreg Roach        foreach ($rows as $row) {
28024ec66ceSGreg Roach            $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom);
2818c2e8227SGreg Roach        }
282a5f7ed67SGreg Roach
283a5f7ed67SGreg Roach        return $records;
2848c2e8227SGreg Roach    }
285a5f7ed67SGreg Roach
286a5f7ed67SGreg Roach    /**
287a5f7ed67SGreg Roach     * @param Tree $tree
288a5f7ed67SGreg Roach     * @param int  $limit
289a5f7ed67SGreg Roach     * @param int  $offset
290a5f7ed67SGreg Roach     *
291a5f7ed67SGreg Roach     * @return array
292a5f7ed67SGreg Roach     */
293c1010edaSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): array
294c1010edaSGreg Roach    {
2958c2e8227SGreg Roach        $rows = Database::prepare(
29624ec66ceSGreg Roach            "SELECT m_id AS xref, m_gedcom AS gedcom" .
2978c2e8227SGreg Roach            " FROM `##media`" .
2988c2e8227SGreg Roach            " WHERE m_file = :tree_id" .
2998c2e8227SGreg Roach            " ORDER BY m_id" .
3008c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
30113abd6f3SGreg Roach        )->execute([
302a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
303a5f7ed67SGreg Roach            'limit'   => $limit,
304a5f7ed67SGreg Roach            'offset'  => $offset,
30513abd6f3SGreg Roach        ])->fetchAll();
306a5f7ed67SGreg Roach
307a5f7ed67SGreg Roach        $records = [];
308a5f7ed67SGreg Roach
3098c2e8227SGreg Roach        foreach ($rows as $row) {
31024ec66ceSGreg Roach            $records[] = Media::getInstance($row->xref, $tree, $row->gedcom);
3118c2e8227SGreg Roach        }
312a5f7ed67SGreg Roach
313a5f7ed67SGreg Roach        return $records;
3148c2e8227SGreg Roach    }
3158c2e8227SGreg Roach
3168c2e8227SGreg Roach    /**
317a5f7ed67SGreg Roach     * @param Tree $tree
318a5f7ed67SGreg Roach     * @param int  $limit
319a5f7ed67SGreg Roach     * @param int  $offset
320a5f7ed67SGreg Roach     *
321a5f7ed67SGreg Roach     * @return array
3228c2e8227SGreg Roach     */
323c1010edaSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): array
324c1010edaSGreg Roach    {
325a5f7ed67SGreg Roach        $rows = Database::prepare(
326a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
327a5f7ed67SGreg Roach            " FROM `##other`" .
328a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'NOTE'" .
329a5f7ed67SGreg Roach            " ORDER BY o_id" .
330a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
331a5f7ed67SGreg Roach        )->execute([
332a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
333a5f7ed67SGreg Roach            'limit'   => $limit,
334a5f7ed67SGreg Roach            'offset'  => $offset,
335a5f7ed67SGreg Roach        ])->fetchAll();
3368c2e8227SGreg Roach
337a5f7ed67SGreg Roach        $records = [];
338a5f7ed67SGreg Roach
339a5f7ed67SGreg Roach        foreach ($rows as $row) {
340a5f7ed67SGreg Roach            $records[] = Note::getInstance($row->xref, $tree, $row->gedcom);
3418c2e8227SGreg Roach        }
3428c2e8227SGreg Roach
343a5f7ed67SGreg Roach        return $records;
3448c2e8227SGreg Roach    }
3458c2e8227SGreg Roach
346a5f7ed67SGreg Roach    /**
347a5f7ed67SGreg Roach     * @param Tree $tree
348a5f7ed67SGreg Roach     * @param int  $limit
349a5f7ed67SGreg Roach     * @param int  $offset
350a5f7ed67SGreg Roach     *
351a5f7ed67SGreg Roach     * @return array
352a5f7ed67SGreg Roach     */
353c1010edaSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): array
354c1010edaSGreg Roach    {
355a5f7ed67SGreg Roach        $rows = Database::prepare(
356a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
357a5f7ed67SGreg Roach            " FROM `##other`" .
358a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'REPO'" .
359a5f7ed67SGreg Roach            " ORDER BY o_id" .
360a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
361a5f7ed67SGreg Roach        )->execute([
362a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
363a5f7ed67SGreg Roach            'limit'   => $limit,
364a5f7ed67SGreg Roach            'offset'  => $offset,
365a5f7ed67SGreg Roach        ])->fetchAll();
366a5f7ed67SGreg Roach
367a5f7ed67SGreg Roach        $records = [];
368a5f7ed67SGreg Roach
369a5f7ed67SGreg Roach        foreach ($rows as $row) {
370a5f7ed67SGreg Roach            $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom);
371a5f7ed67SGreg Roach        }
372a5f7ed67SGreg Roach
373a5f7ed67SGreg Roach        return $records;
374a5f7ed67SGreg Roach    }
375a5f7ed67SGreg Roach
376a5f7ed67SGreg Roach    /**
377a5f7ed67SGreg Roach     * @param Tree $tree
378a5f7ed67SGreg Roach     * @param int  $limit
379a5f7ed67SGreg Roach     * @param int  $offset
380a5f7ed67SGreg Roach     *
381a5f7ed67SGreg Roach     * @return array
382a5f7ed67SGreg Roach     */
383c1010edaSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): array
384c1010edaSGreg Roach    {
385a5f7ed67SGreg Roach        $rows = Database::prepare(
386a5f7ed67SGreg Roach            "SELECT s_id AS xref, s_gedcom AS gedcom" .
387a5f7ed67SGreg Roach            " FROM `##sources`" .
388a5f7ed67SGreg Roach            " WHERE s_file = :tree_id" .
389a5f7ed67SGreg Roach            " ORDER BY s_id" .
390a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
391a5f7ed67SGreg Roach        )->execute([
392a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
393a5f7ed67SGreg Roach            'limit'   => $limit,
394a5f7ed67SGreg Roach            'offset'  => $offset,
395a5f7ed67SGreg Roach        ])->fetchAll();
396a5f7ed67SGreg Roach
397a5f7ed67SGreg Roach        $records = [];
398a5f7ed67SGreg Roach
399a5f7ed67SGreg Roach        foreach ($rows as $row) {
400a5f7ed67SGreg Roach            $records[] = Source::getInstance($row->xref, $tree, $row->gedcom);
401a5f7ed67SGreg Roach        }
402a5f7ed67SGreg Roach
403a5f7ed67SGreg Roach        return $records;
4048c2e8227SGreg Roach    }
4058c2e8227SGreg Roach}
406