Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
WorldCatSimilar
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3/**
4 * Related Records: WorldCat-based similarity
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2009, 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  Related_Records
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:related_records_modules Wiki
28 */
29
30namespace VuFind\Related;
31
32use VuFindSearch\Command\SearchCommand;
33
34/**
35 * Related Records: WorldCat-based similarity
36 *
37 * @category VuFind
38 * @package  Related_Records
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development:plugins:related_records_modules Wiki
42 */
43class WorldCatSimilar extends Similar
44{
45    /**
46     * Establishes base settings for making recommendations.
47     *
48     * @param string                            $settings Settings from config.ini
49     * @param \VuFind\RecordDriver\AbstractBase $driver   Record driver object
50     *
51     * @return void
52     */
53    public function init($settings, $driver)
54    {
55        // Create array of query parts:
56        $parts = [];
57
58        // Add Dewey class to query
59        $deweyClass = $driver->tryMethod('getDeweyCallNumber');
60        if (!empty($deweyClass)) {
61            // Skip "English Fiction" Dewey class -- this won't give us useful
62            // matches because there's too much of it and it's too broad.
63            if (!str_starts_with($deweyClass, '823')) {
64                $parts[] = 'srw.dd any "' . $deweyClass . '"';
65            }
66        }
67
68        // Add author to query
69        $author = $driver->getPrimaryAuthor();
70        if (!empty($author)) {
71            $parts[] = 'srw.au all "' . $author . '"';
72        }
73
74        // Add subjects to query
75        $subjects = $driver->getAllSubjectHeadings();
76        foreach ($subjects as $current) {
77            $parts[] = 'srw.su all "' . implode(' ', $current) . '"';
78        }
79
80        // Add title to query
81        $title = $driver->getTitle();
82        if (!empty($title)) {
83            $parts[] = 'srw.ti any "' . str_replace('"', '', $title) . '"';
84        }
85
86        // Build basic query:
87        $query = '(' . implode(' or ', $parts) . ')';
88
89        // Not current record ID if this is already a WorldCat record:
90        if ($driver->getSourceIdentifier() == 'WorldCat') {
91            $id = $driver->getUniqueId();
92            $query .= " not srw.no all \"$id\"";
93        }
94
95        // Perform the search and save results:
96        $queryObj = new \VuFindSearch\Query\Query($query);
97        $command = new SearchCommand('WorldCat', $queryObj, 0, 5);
98        $result = $this->searchService->invoke($command)->getResult();
99        $this->results = $result->getRecords();
100    }
101}