xref: /webtrees/app/Module/SiteMapModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
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     */
47*8f53f488SRico Sonntag    public function getTitle(): string
48c1010edaSGreg Roach    {
49bbb76c12SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
50bbb76c12SGreg 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     */
58*8f53f488SRico Sonntag    public function getDescription(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
61bbb76c12SGreg 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     */
69*8f53f488SRico Sonntag    public function getConfigLink(): string
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
197bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
198a5f7ed67SGreg Roach
199a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
200a5f7ed67SGreg Roach
201a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
202a5f7ed67SGreg Roach        }
203a5f7ed67SGreg Roach
204a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
205a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
206a5f7ed67SGreg Roach        ]);
207a5f7ed67SGreg Roach    }
208a5f7ed67SGreg Roach
209a5f7ed67SGreg Roach    /**
210a5f7ed67SGreg Roach     * @param Tree   $tree
211a5f7ed67SGreg Roach     * @param string $type
212a5f7ed67SGreg Roach     * @param int    $limit
213a5f7ed67SGreg Roach     * @param int    $offset
214a5f7ed67SGreg Roach     *
215a5f7ed67SGreg Roach     * @return array
216a5f7ed67SGreg Roach     */
217c1010edaSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array
218c1010edaSGreg Roach    {
219a5f7ed67SGreg Roach        switch ($type) {
2208c2e8227SGreg Roach            case 'i':
221a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
222a5f7ed67SGreg Roach                break;
223a5f7ed67SGreg Roach
224a5f7ed67SGreg Roach            case 'm':
225a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
226a5f7ed67SGreg Roach                break;
227a5f7ed67SGreg Roach
228a5f7ed67SGreg Roach            case 'n':
229a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
230a5f7ed67SGreg Roach                break;
231a5f7ed67SGreg Roach
232a5f7ed67SGreg Roach            case 'r':
233a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
234a5f7ed67SGreg Roach                break;
235a5f7ed67SGreg Roach
236a5f7ed67SGreg Roach            case 's':
237a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
238a5f7ed67SGreg Roach                break;
239a5f7ed67SGreg Roach
240a5f7ed67SGreg Roach            default:
241a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
242a5f7ed67SGreg Roach        }
243a5f7ed67SGreg Roach
244a5f7ed67SGreg Roach        // Skip records that no longer exist.
245a5f7ed67SGreg Roach        $records = array_filter($records);
246a5f7ed67SGreg Roach
247a5f7ed67SGreg Roach        // Skip private records.
248492c7072SGreg Roach        $records = array_filter($records, function (GedcomRecord $record): bool {
249a5f7ed67SGreg Roach            return $record->canShow();
250a5f7ed67SGreg Roach        });
251a5f7ed67SGreg Roach
252a5f7ed67SGreg Roach        return $records;
253a5f7ed67SGreg Roach    }
254a5f7ed67SGreg Roach
255a5f7ed67SGreg Roach    /**
256a5f7ed67SGreg Roach     * @param Tree $tree
257a5f7ed67SGreg Roach     * @param int  $limit
258a5f7ed67SGreg Roach     * @param int  $offset
259a5f7ed67SGreg Roach     *
260a5f7ed67SGreg Roach     * @return array
261a5f7ed67SGreg Roach     */
262c1010edaSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array
263c1010edaSGreg Roach    {
2648c2e8227SGreg Roach        $rows = Database::prepare(
26524ec66ceSGreg Roach            "SELECT i_id AS xref, i_gedcom AS gedcom" .
2668c2e8227SGreg Roach            " FROM `##individuals`" .
2678c2e8227SGreg Roach            " WHERE i_file = :tree_id" .
2688c2e8227SGreg Roach            " ORDER BY i_id" .
2698c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
27013abd6f3SGreg Roach        )->execute([
271a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
272a5f7ed67SGreg Roach            'limit'   => $limit,
273a5f7ed67SGreg Roach            'offset'  => $offset,
27413abd6f3SGreg Roach        ])->fetchAll();
275a5f7ed67SGreg Roach
276a5f7ed67SGreg Roach        $records = [];
277a5f7ed67SGreg Roach
2788c2e8227SGreg Roach        foreach ($rows as $row) {
27924ec66ceSGreg Roach            $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom);
2808c2e8227SGreg Roach        }
281a5f7ed67SGreg Roach
282a5f7ed67SGreg Roach        return $records;
2838c2e8227SGreg Roach    }
284a5f7ed67SGreg Roach
285a5f7ed67SGreg Roach    /**
286a5f7ed67SGreg Roach     * @param Tree $tree
287a5f7ed67SGreg Roach     * @param int  $limit
288a5f7ed67SGreg Roach     * @param int  $offset
289a5f7ed67SGreg Roach     *
290a5f7ed67SGreg Roach     * @return array
291a5f7ed67SGreg Roach     */
292c1010edaSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): array
293c1010edaSGreg Roach    {
2948c2e8227SGreg Roach        $rows = Database::prepare(
29524ec66ceSGreg Roach            "SELECT m_id AS xref, m_gedcom AS gedcom" .
2968c2e8227SGreg Roach            " FROM `##media`" .
2978c2e8227SGreg Roach            " WHERE m_file = :tree_id" .
2988c2e8227SGreg Roach            " ORDER BY m_id" .
2998c2e8227SGreg Roach            " LIMIT :limit OFFSET :offset"
30013abd6f3SGreg Roach        )->execute([
301a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
302a5f7ed67SGreg Roach            'limit'   => $limit,
303a5f7ed67SGreg Roach            'offset'  => $offset,
30413abd6f3SGreg Roach        ])->fetchAll();
305a5f7ed67SGreg Roach
306a5f7ed67SGreg Roach        $records = [];
307a5f7ed67SGreg Roach
3088c2e8227SGreg Roach        foreach ($rows as $row) {
30924ec66ceSGreg Roach            $records[] = Media::getInstance($row->xref, $tree, $row->gedcom);
3108c2e8227SGreg Roach        }
311a5f7ed67SGreg Roach
312a5f7ed67SGreg Roach        return $records;
3138c2e8227SGreg Roach    }
3148c2e8227SGreg Roach
3158c2e8227SGreg Roach    /**
316a5f7ed67SGreg Roach     * @param Tree $tree
317a5f7ed67SGreg Roach     * @param int  $limit
318a5f7ed67SGreg Roach     * @param int  $offset
319a5f7ed67SGreg Roach     *
320a5f7ed67SGreg Roach     * @return array
3218c2e8227SGreg Roach     */
322c1010edaSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): array
323c1010edaSGreg Roach    {
324a5f7ed67SGreg Roach        $rows = Database::prepare(
325a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
326a5f7ed67SGreg Roach            " FROM `##other`" .
327a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'NOTE'" .
328a5f7ed67SGreg Roach            " ORDER BY o_id" .
329a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
330a5f7ed67SGreg Roach        )->execute([
331a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
332a5f7ed67SGreg Roach            'limit'   => $limit,
333a5f7ed67SGreg Roach            'offset'  => $offset,
334a5f7ed67SGreg Roach        ])->fetchAll();
3358c2e8227SGreg Roach
336a5f7ed67SGreg Roach        $records = [];
337a5f7ed67SGreg Roach
338a5f7ed67SGreg Roach        foreach ($rows as $row) {
339a5f7ed67SGreg Roach            $records[] = Note::getInstance($row->xref, $tree, $row->gedcom);
3408c2e8227SGreg Roach        }
3418c2e8227SGreg Roach
342a5f7ed67SGreg Roach        return $records;
3438c2e8227SGreg Roach    }
3448c2e8227SGreg Roach
345a5f7ed67SGreg Roach    /**
346a5f7ed67SGreg Roach     * @param Tree $tree
347a5f7ed67SGreg Roach     * @param int  $limit
348a5f7ed67SGreg Roach     * @param int  $offset
349a5f7ed67SGreg Roach     *
350a5f7ed67SGreg Roach     * @return array
351a5f7ed67SGreg Roach     */
352c1010edaSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): array
353c1010edaSGreg Roach    {
354a5f7ed67SGreg Roach        $rows = Database::prepare(
355a5f7ed67SGreg Roach            "SELECT o_id AS xref, o_gedcom AS gedcom" .
356a5f7ed67SGreg Roach            " FROM `##other`" .
357a5f7ed67SGreg Roach            " WHERE o_file = :tree_id AND o_type = 'REPO'" .
358a5f7ed67SGreg Roach            " ORDER BY o_id" .
359a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
360a5f7ed67SGreg Roach        )->execute([
361a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
362a5f7ed67SGreg Roach            'limit'   => $limit,
363a5f7ed67SGreg Roach            'offset'  => $offset,
364a5f7ed67SGreg Roach        ])->fetchAll();
365a5f7ed67SGreg Roach
366a5f7ed67SGreg Roach        $records = [];
367a5f7ed67SGreg Roach
368a5f7ed67SGreg Roach        foreach ($rows as $row) {
369a5f7ed67SGreg Roach            $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom);
370a5f7ed67SGreg Roach        }
371a5f7ed67SGreg Roach
372a5f7ed67SGreg Roach        return $records;
373a5f7ed67SGreg Roach    }
374a5f7ed67SGreg Roach
375a5f7ed67SGreg Roach    /**
376a5f7ed67SGreg Roach     * @param Tree $tree
377a5f7ed67SGreg Roach     * @param int  $limit
378a5f7ed67SGreg Roach     * @param int  $offset
379a5f7ed67SGreg Roach     *
380a5f7ed67SGreg Roach     * @return array
381a5f7ed67SGreg Roach     */
382c1010edaSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): array
383c1010edaSGreg Roach    {
384a5f7ed67SGreg Roach        $rows = Database::prepare(
385a5f7ed67SGreg Roach            "SELECT s_id AS xref, s_gedcom AS gedcom" .
386a5f7ed67SGreg Roach            " FROM `##sources`" .
387a5f7ed67SGreg Roach            " WHERE s_file = :tree_id" .
388a5f7ed67SGreg Roach            " ORDER BY s_id" .
389a5f7ed67SGreg Roach            " LIMIT :limit OFFSET :offset"
390a5f7ed67SGreg Roach        )->execute([
391a5f7ed67SGreg Roach            'tree_id' => $tree->getTreeId(),
392a5f7ed67SGreg Roach            'limit'   => $limit,
393a5f7ed67SGreg Roach            'offset'  => $offset,
394a5f7ed67SGreg Roach        ])->fetchAll();
395a5f7ed67SGreg Roach
396a5f7ed67SGreg Roach        $records = [];
397a5f7ed67SGreg Roach
398a5f7ed67SGreg Roach        foreach ($rows as $row) {
399a5f7ed67SGreg Roach            $records[] = Source::getInstance($row->xref, $tree, $row->gedcom);
400a5f7ed67SGreg Roach        }
401a5f7ed67SGreg Roach
402a5f7ed67SGreg Roach        return $records;
4038c2e8227SGreg Roach    }
4048c2e8227SGreg Roach}
405