Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.11% covered (warning)
86.11%
31 / 36
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Index
86.11% covered (warning)
86.11%
31 / 36
25.00% covered (danger)
25.00%
1 / 4
10.27
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getSitemapName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrls
93.10% covered (success)
93.10%
27 / 29
0.00% covered (danger)
0.00%
0 / 1
7.02
 setOptions
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Index-based generator plugin
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  Sitemap
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/wiki/development:plugins:ils_drivers Wiki
28 */
29
30namespace VuFind\Sitemap\Plugin;
31
32/**
33 * Index-based generator plugin
34 *
35 * @category VuFind
36 * @package  Sitemap
37 * @author   Demian Katz <demian.katz@villanova.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
40 */
41class Index extends AbstractGeneratorPlugin
42{
43    /**
44     * Base URL for site
45     *
46     * @var string
47     */
48    protected $baseUrl  = '';
49
50    /**
51     * Settings specifying which backends to index.
52     *
53     * @var array
54     */
55    protected $backendSettings;
56
57    /**
58     * Helper for fetching IDs from the search service.
59     *
60     * @var Index\AbstractIdFetcher
61     */
62    protected $idFetcher;
63
64    /**
65     * Page size for data retrieval
66     *
67     * @var int
68     */
69    protected $countPerPage;
70
71    /**
72     * Search filters
73     *
74     * @var string[]
75     */
76    protected $filters;
77
78    /**
79     * Constructor
80     *
81     * @param array                   $backendSettings Settings specifying which
82     * backends to index
83     * @param Index\AbstractIdFetcher $idFetcher       The helper object for
84     * retrieving IDs
85     * @param int                     $countPerPage    Page size for data retrieval
86     * @param string[]                $filters         Search filters
87     */
88    public function __construct(
89        array $backendSettings,
90        Index\AbstractIdFetcher $idFetcher,
91        int $countPerPage,
92        array $filters = []
93    ) {
94        $this->backendSettings = $backendSettings;
95        $this->idFetcher = $idFetcher;
96        $this->countPerPage = $countPerPage;
97        $this->filters = $filters;
98    }
99
100    /**
101     * Get the name of the sitemap used to create the sitemap file. This will be
102     * appended to the configured base name, and may be blank to use the base
103     * name without a suffix.
104     *
105     * @return string
106     */
107    public function getSitemapName(): string
108    {
109        return '';
110    }
111
112    /**
113     * Generate urls for the sitemap.
114     *
115     * May yield a string per URL or an array that defines lastmod in addition to url.
116     *
117     * @return \Generator
118     */
119    public function getUrls(): \Generator
120    {
121        // Initialize variables for message displays within the loop below:
122        $currentPage = $recordCount = 0;
123
124        // Loop through all backends
125        foreach ($this->backendSettings as $current) {
126            $recordUrl = $this->baseUrl . $current['url'];
127            $this->verboseMsg(
128                'Adding records from ' . $current['id']
129                . " with record base url $recordUrl"
130            );
131            $offset = $this->idFetcher->getInitialOffset();
132            $this->idFetcher->setupBackend($current['id']);
133            while (true) {
134                $result = $this->idFetcher->getIdsFromBackend(
135                    $current['id'],
136                    $offset,
137                    $this->countPerPage,
138                    $this->filters
139                );
140                foreach ($result['ids'] as $index => $item) {
141                    $loc = htmlspecialchars($recordUrl . urlencode($item));
142                    if (!str_contains($loc, 'http')) {
143                        $loc = 'http://' . $loc;
144                    }
145                    $recordCount++;
146                    if (isset($result['lastmods'][$index])) {
147                        yield ['url' => $loc, 'lastmod' => $result['lastmods'][$index]];
148                    } else {
149                        yield $loc;
150                    }
151                }
152                $currentPage++;
153                $this->verboseMsg("Page $currentPage$recordCount processed");
154                if (!isset($result['nextOffset'])) {
155                    break;
156                }
157                $offset = $result['nextOffset'];
158            }
159        }
160    }
161
162    /**
163     * Set plugin options.
164     *
165     * @param array $options Options
166     *
167     * @return void
168     */
169    public function setOptions(array $options): void
170    {
171        parent::setOptions($options);
172        $this->baseUrl = $options['baseUrl'] ?? '';
173    }
174}