Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SimilarBuilder
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 build
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * SOLR SimilarBuilder.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2016.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Search
26 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
27 * @author   David Maus <maus@hab.de>
28 * @author   Demian Katz <demian.katz@villanova.edu>
29 * @author   Ere Maijala <ere.maijala@helsinki.fi>
30 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
31 * @link     https://vufind.org
32 */
33
34namespace VuFindSearch\Backend\Solr;
35
36use VuFindSearch\ParamBag;
37
38/**
39 * SOLR SimilarBuilder.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
44 * @author   David Maus <maus@hab.de>
45 * @author   Demian Katz <demian.katz@villanova.edu>
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org
49 */
50class SimilarBuilder implements SimilarBuilderInterface
51{
52    /**
53     * Solr field used to store unique identifier
54     *
55     * @var string
56     */
57    protected $uniqueKey;
58
59    /**
60     * Whether to use MoreLikeThis Handler instead of the traditional MoreLikeThis
61     * component.
62     *
63     * @var bool
64     */
65    protected $useHandler = false;
66
67    /**
68     * MoreLikeThis Handler parameters
69     *
70     * @var string
71     */
72    protected $handlerParams = '';
73
74    /**
75     * Number of similar records to retrieve
76     *
77     * @var int
78     */
79    protected $count = 5;
80
81    /**
82     * Constructor.
83     *
84     * @param \Laminas\Config\Config $searchConfig Search config
85     * @param string                 $uniqueKey    Solr field used to store unique
86     * identifier
87     *
88     * @return void
89     */
90    public function __construct(
91        \Laminas\Config\Config $searchConfig = null,
92        $uniqueKey = 'id'
93    ) {
94        $this->uniqueKey = $uniqueKey;
95        if (isset($searchConfig->MoreLikeThis)) {
96            $mlt = $searchConfig->MoreLikeThis;
97            if (
98                isset($mlt->useMoreLikeThisHandler)
99                && $mlt->useMoreLikeThisHandler
100            ) {
101                $this->useHandler = true;
102                $this->handlerParams = $mlt->params ?? '';
103            }
104            if (isset($mlt->count)) {
105                $this->count = $mlt->count;
106            }
107        }
108    }
109
110    /// Public API
111
112    /**
113     * Return SOLR search parameters based on a record Id and params.
114     *
115     * @param string $id Record Id
116     *
117     * @return ParamBag
118     */
119    public function build($id)
120    {
121        $params = new ParamBag();
122        if ($this->useHandler) {
123            $mltParams = $this->handlerParams
124                ? $this->handlerParams
125                : 'qf=title,title_short,callnumber-label,topic,language,author,'
126                    . 'publishDate mintf=1 mindf=1';
127            $params->set('q', sprintf('{!mlt %s}%s', $mltParams, $id));
128        } else {
129            $params->set(
130                'q',
131                sprintf('%s:"%s"', $this->uniqueKey, addcslashes($id, '"'))
132            );
133            $params->set('qt', 'morelikethis');
134        }
135        if (null === $params->get('rows')) {
136            $params->set('rows', $this->count);
137        }
138        return $params;
139    }
140}