Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InjectHighlightingListener
95.83% covered (success)
95.83%
23 / 24
66.67% covered (warning)
66.67%
2 / 3
8
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 attach
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 onSearchPre
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
6.01
1<?php
2
3/**
4 * Solr highlighting listener.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2013.
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   David Maus <maus@hab.de>
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\Solr;
31
32use Laminas\EventManager\EventInterface;
33use Laminas\EventManager\SharedEventManagerInterface;
34use VuFindSearch\Backend\BackendInterface;
35use VuFindSearch\Service;
36
37/**
38 * Solr highlighting listener.
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   David Maus <maus@hab.de>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46class InjectHighlightingListener
47{
48    /**
49     * Backend.
50     *
51     * @var BackendInterface
52     */
53    protected $backend;
54
55    /**
56     * Is highlighting active?
57     *
58     * @var bool
59     */
60    protected $active = false;
61
62    /**
63     * Fields to highlight when active.
64     *
65     * @var string
66     */
67    protected $fieldList;
68
69    /**
70     * Extra Solr highlighting parameters.
71     *
72     * @var array
73     */
74    protected $extraHighlightingParameters;
75
76    /**
77     * Constructor.
78     *
79     * @param BackendInterface $backend   Backend
80     * @param string           $fieldList Field(s) to highlight (hl.fl param)
81     * @param array            $extras    Extra Solr highlighting parameters
82     *
83     * @return void
84     */
85    public function __construct(BackendInterface $backend, $fieldList = '*', $extras = [])
86    {
87        $this->backend = $backend;
88        $this->fieldList = $fieldList;
89        $this->extraHighlightingParameters = $extras;
90    }
91
92    /**
93     * Attach listener to shared event manager.
94     *
95     * @param SharedEventManagerInterface $manager Shared event manager
96     *
97     * @return void
98     */
99    public function attach(SharedEventManagerInterface $manager)
100    {
101        $manager->attach(
102            Service::class,
103            Service::EVENT_PRE,
104            [$this, 'onSearchPre']
105        );
106    }
107
108    /**
109     * Set up highlighting parameters.
110     *
111     * @param EventInterface $event Event
112     *
113     * @return EventInterface
114     */
115    public function onSearchPre(EventInterface $event)
116    {
117        $command = $event->getParam('command');
118        if ($command->getContext() != 'search') {
119            return $event;
120        }
121        if ($command->getTargetIdentifier() === $this->backend->getIdentifier()) {
122            if ($params = $command->getSearchParameters()) {
123                // Set highlighting parameters unless explicitly disabled:
124                $hl = $params->get('hl');
125                if (($hl[0] ?? 'true') != 'false') {
126                    $this->active = true;
127                    // Set extra parameters first so they don't override necessary
128                    // core parameters:
129                    foreach ($this->extraHighlightingParameters as $key => $val) {
130                        $params->set($key, $val);
131                    }
132                    $params->set('hl', 'true');
133                    $params->set('hl.simple.pre', '{{{{START_HILITE}}}}');
134                    $params->set('hl.simple.post', '{{{{END_HILITE}}}}');
135
136                    // Turn on hl.q generation in query builder:
137                    $this->backend->getQueryBuilder()
138                        ->setFieldsToHighlight($this->fieldList);
139                }
140            }
141        }
142        return $event;
143    }
144}