Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Results
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatFuzzyQuery
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 performTagSearch
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 performSearch
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getFacetList
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Tags aspect of the Search Multi-class (Results)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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_Tags
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 Main Site
28 */
29
30namespace VuFind\Search\Tags;
31
32use VuFind\Record\Loader;
33use VuFind\Search\Base\Results as BaseResults;
34use VuFind\Tags\TagsService;
35use VuFindSearch\Service as SearchService;
36
37use function count;
38
39/**
40 * Search Tags Results
41 *
42 * @category VuFind
43 * @package  Search_Tags
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org Main Site
47 */
48class Results extends BaseResults
49{
50    /**
51     * Constructor
52     *
53     * @param \VuFind\Search\Base\Params $params        Object representing user
54     * search parameters.
55     * @param SearchService              $searchService Search service
56     * @param Loader                     $recordLoader  Record loader
57     * @param TagsService                $tagsService   Tags service
58     */
59    public function __construct(
60        \VuFind\Search\Base\Params $params,
61        SearchService $searchService,
62        Loader $recordLoader,
63        protected TagsService $tagsService
64    ) {
65        parent::__construct($params, $searchService, $recordLoader);
66    }
67
68    /**
69     * Process a fuzzy tag query.
70     *
71     * @param string $q Raw query
72     *
73     * @return string
74     */
75    protected function formatFuzzyQuery($q)
76    {
77        // Change unescaped asterisks to percent signs to translate more common
78        // wildcard character into format used by database.
79        return preg_replace('/(?<!\\\\)\\*/', '%', $q);
80    }
81
82    /**
83     * Return resources associated with the user tag query.
84     *
85     * @param bool $fuzzy Is this a fuzzy query or an exact match?
86     *
87     * @return array
88     */
89    protected function performTagSearch($fuzzy)
90    {
91        $query = $fuzzy
92            ? $this->formatFuzzyQuery($this->getParams()->getDisplayQuery())
93            : $this->getParams()->getDisplayQuery();
94        $rawResults = $this->tagsService->getResourcesMatchingTagQuery(
95            $query,
96            null,
97            $this->getParams()->getSort(),
98            0,
99            null,
100            $fuzzy
101        );
102
103        // How many results were there?
104        $this->resultTotal = count($rawResults);
105
106        // Apply offset and limit if necessary!
107        $limit = $this->getParams()->getLimit();
108        if ($this->resultTotal > $limit) {
109            $rawResults = $this->tagsService->getResourcesMatchingTagQuery(
110                $query,
111                null,
112                $this->getParams()->getSort(),
113                $this->getStartRecord() - 1,
114                $limit,
115                $fuzzy
116            );
117        }
118
119        return $rawResults;
120    }
121
122    /**
123     * Support method for performAndProcessSearch -- perform a search based on the
124     * parameters passed to the object.
125     *
126     * @return void
127     */
128    protected function performSearch()
129    {
130        // There are two possibilities here: either we are in "fuzzy" mode because
131        // we are coming in from a search, in which case we want to do a fuzzy
132        // search that supports wildcards, or else we are coming in from a tag
133        // link, in which case we want to do an exact match.
134        $results = $this->performTagSearch($this->getParams()->isFuzzyTagSearch());
135
136        // Retrieve record drivers for the selected items.
137        $callback = function ($row) {
138            return ['id' => $row['record_id'], 'source' => $row['source']];
139        };
140        $this->results = $this->recordLoader
141            ->loadBatch(array_map($callback, $results), true);
142    }
143
144    /**
145     * Returns the stored list of facets for the last search
146     *
147     * @param array $filter Array of field => on-screen description listing
148     * all of the desired facet fields; set to null to get all configured values.
149     *
150     * @return array        Facets data arrays
151     */
152    public function getFacetList($filter = null)
153    {
154        // Facets not supported:
155        return [];
156    }
157}