Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.11% covered (danger)
8.11%
3 / 37
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchNormalizer
8.11% covered (danger)
8.11%
3 / 37
60.00% covered (warning)
60.00%
3 / 5
123.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalizeSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalizeMinifiedSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchesMatchingNormalizedSearch
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 saveNormalizedSearch
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * Search normalizer.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2022.
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/wiki/development Wiki
28 */
29
30namespace VuFind\Search;
31
32use DateTime;
33use minSO;
34use VuFind\Db\Entity\SearchEntityInterface;
35use VuFind\Db\Service\SearchServiceInterface;
36use VuFind\Search\Base\Results;
37use VuFind\Search\Results\PluginManager as ResultsManager;
38
39use function count;
40
41/**
42 * Search normalizer.
43 *
44 * @category VuFind
45 * @package  Search
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50class SearchNormalizer
51{
52    /**
53     * Constructor
54     *
55     * @param ResultsManager         $resultsManager Search results manager
56     * @param SearchServiceInterface $searchService  Search database service
57     */
58    public function __construct(
59        protected ResultsManager $resultsManager,
60        protected SearchServiceInterface $searchService
61    ) {
62    }
63
64    /**
65     * Normalize a search
66     *
67     * @param Results $results Search results object
68     *
69     * @return NormalizedSearch
70     */
71    public function normalizeSearch(Results $results): NormalizedSearch
72    {
73        return new NormalizedSearch($this->resultsManager, $results);
74    }
75
76    /**
77     * Normalize a minified search
78     *
79     * @param Minified $minified Minified search results object
80     *
81     * @return NormalizedSearch
82     */
83    public function normalizeMinifiedSearch(Minified $minified): NormalizedSearch
84    {
85        return $this->normalizeSearch($minified->deminify($this->resultsManager));
86    }
87
88    /**
89     * Return existing search table rows matching the provided normalized search.
90     *
91     * @param NormalizedSearch $normalized Normalized search to match against
92     * @param string           $sessionId  Current session ID
93     * @param int|null         $userId     Current user ID
94     * @param int              $limit      Max rows to retrieve
95     * (default = no limit)
96     *
97     * @return SearchEntityInterface[]
98     */
99    public function getSearchesMatchingNormalizedSearch(
100        NormalizedSearch $normalized,
101        string $sessionId,
102        ?int $userId,
103        int $limit = PHP_INT_MAX
104    ): array {
105        // Fetch all rows with the same CRC32 and try to match with the URL
106        $checksum = $normalized->getChecksum();
107        $results = [];
108        foreach ($this->searchService->getSearchesByChecksumAndOwner($checksum, $sessionId, $userId) as $match) {
109            if (!($minified = $match->getSearchObject())) {
110                throw new \Exception('Problem decoding saved search');
111            }
112            if ($normalized->isEquivalentToMinifiedSearch($minified)) {
113                $results[] = $match;
114                if (count($results) >= $limit) {
115                    break;
116                }
117            }
118        }
119        return $results;
120    }
121
122    /**
123     * Add a search into the search table (history)
124     *
125     * @param \VuFind\Search\Base\Results $results   Search to save
126     * @param string                      $sessionId Current session ID
127     * @param ?int                        $userId    Current user ID
128     *
129     * @return SearchEntityInterface
130     * @throws Exception
131     */
132    public function saveNormalizedSearch(
133        \VuFind\Search\Base\Results $results,
134        string $sessionId,
135        ?int $userId
136    ): SearchEntityInterface {
137        $normalized = $this->normalizeSearch($results);
138        $duplicates = $this->getSearchesMatchingNormalizedSearch(
139            $normalized,
140            $sessionId,
141            $userId,
142            1 // we only need to identify at most one duplicate match
143        );
144        if ($existingRow = array_shift($duplicates)) {
145            // Update the existing search only if it wasn't already saved
146            // (to make it the most recent history entry and make sure it's
147            // using the most up-to-date serialization):
148            if (!$existingRow->getSaved()) {
149                $existingRow->setCreated(new DateTime());
150                // Keep the ID of the old search:
151                $minified = $normalized->getMinified();
152                if (!$searchObject = $existingRow->getSearchObject()) {
153                    throw new \Exception('Problem decoding saved search');
154                }
155                $minified->id = $searchObject->id;
156                $existingRow->setSearchObject($minified);
157                $existingRow->setSessionId($sessionId);
158                $this->searchService->persistEntity($existingRow);
159            }
160            // Register the appropriate search history database row with the current
161            // search results object.
162            $results->updateSaveStatus($existingRow);
163            return $existingRow;
164        }
165
166        // If we got this far, we didn't find a saved duplicate, so we should
167        // save the new search:
168        $row = $this->searchService->createAndPersistEntityWithChecksum($normalized->getChecksum());
169
170        // Chicken and egg... We didn't know the id before insert
171        $results->updateSaveStatus($row);
172
173        // Don't set session ID until this stage, because we don't want to risk
174        // ever having a row that's associated with a session but which has no
175        // search object data attached to it; this could cause problems!
176        $row->setSessionId($sessionId);
177        $row->setSearchObject(new minSO($results));
178        $this->searchService->persistEntity($row);
179        return $row;
180    }
181}