Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TermsIdFetcher
92.31% covered (success)
92.31%
12 / 13
66.67% covered (warning)
66.67%
2 / 3
6.02
0.00% covered (danger)
0.00%
0 / 1
 getInitialOffset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setupBackend
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIdsFromBackend
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Plugin to get IDs for a sitemap from a backend using terms (if supported).
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2021.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Search
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFind\Sitemap\Plugin\Index;
31
32use VuFindSearch\Command\GetUniqueKeyCommand;
33use VuFindSearch\Command\TermsCommand;
34
35use function count;
36
37/**
38 * Plugin to get IDs for a sitemap from a backend using terms (if supported).
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org
45 */
46class TermsIdFetcher extends AbstractIdFetcher
47{
48    /**
49     * Get the initial offset to seed the search process
50     *
51     * @return string
52     */
53    public function getInitialOffset(): string
54    {
55        return '';
56    }
57
58    /**
59     * Set up the backend.
60     *
61     * @param string $backend Search backend ID
62     *
63     * @return void
64     */
65    public function setupBackend(string $backend): void
66    {
67        // No special action needed.
68    }
69
70    /**
71     * Retrieve a batch of IDs. Returns an array with two possible keys: ids (the
72     * latest set of retrieved IDs) and nextOffset (an offset which can be passed
73     * to the next call to this function to retrieve the next page). When all IDs
74     * have been retrieved, the nextOffset value MUST NOT be included in the return
75     * array.
76     *
77     * @param string $backend      Search backend ID
78     * @param string $lastTerm     String representing progress through set
79     * @param int    $countPerPage Page size
80     * @param array  $filters      Filters to apply to the search
81     *
82     * @return array
83     */
84    public function getIdsFromBackend(
85        string $backend,
86        string $lastTerm,
87        int $countPerPage,
88        array $filters
89    ): array {
90        if (!empty($filters)) {
91            throw new \Exception('extraFilters[] option incompatible with terms');
92        }
93        $getKeyCommand = new GetUniqueKeyCommand($backend, []);
94        $key = $this->searchService->invoke($getKeyCommand)->getResult();
95        $termsCommand = new TermsCommand($backend, $key, $lastTerm, $countPerPage);
96        $info = $this->searchService->invoke($termsCommand)->getResult()
97            ->getFieldTerms($key);
98        $ids = null === $info ? [] : array_keys($info->toArray());
99        // Only include the next offset if we have a non-empty set:
100        return empty($ids)
101            ? compact('ids')
102            : ['ids' => $ids, 'nextOffset' => $ids[count($ids) - 1]];
103    }
104}