aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/librarysearch/searchmodules/BASELibrarySearch.php
blob: e0408e7295267f8e6aa9fd52cbcb4cd078b25f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
 * This file is part of Stud.IP.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2020
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.6
 */


/**
 * This is a LibrarySearch implementation for the BASE catalog.
 *
 * @see LibrarySearch
 */
class BASELibrarySearch extends LibrarySearch
{
    /**
     * This array is used to map the general search field names to
     * the real field names.
     */
    protected static $field_replacements = [
        LibrarySearch::TITLE       => 'dctitle',
        LibrarySearch::AUTHOR      => 'dccreator',
        LibrarySearch::YEAR        => 'dcyear',
        LibrarySearch::NUMBER      => 'dcrelation',
        LibrarySearch::ISSN        => 'dcrelation', //No special ISSN field available.
        LibrarySearch::ISBN        => 'dcrelation', //No special ISBN field available.
        LibrarySearch::PUBLICATION => 'dcpublisher',
        LibrarySearch::SIGNATURE   => 'dcdocid'
    ];


    /**
     * @see LibrarySearch::translateQueryFields
     */
    protected function translateQueryFields(array $query_fields = []) : array
    {
        if (!$query_fields) {
            return [];
        }

        $translated_fields = [];
        foreach ($query_fields as $key => $value) {
            if (in_array($key, array_keys(self::$field_replacements))) {
                $new_key = self::$field_replacements[$key];
                $translated_fields[$new_key] = $value;
            } else {
                $translated_fields[$key] = $value;
            }
        }
        return $translated_fields;
    }


    /**
     * @see LibrarySearch::query
     */
    public function query(
        array $search_parameters = [],
        string $order_by = self::ORDER_BY_RELEVANCE,
        int $limit = 125
    ) : array
    {
        if (!$search_parameters) {
            return [];
        }

        //The standardised parameter names must be converted to module-specific
        //search parameter names before being added to the query string.
        $search_parameters = $this->translateQueryFields($search_parameters);
        $query_string = '';
        foreach ($search_parameters as $key => $value) {
            if (!empty($query_string)) {
                $query_string .= ' AND ';
            }
            //TODO: escape colon in data!
            $query_string .= sprintf('%1$s:(%2$s)', $key, $value);
        }

        $query_parameters = [];
        $query_parameters['func'] = 'PerformSearch';
        $query_parameters['coll'] = $this->settings['collection']
                                  ? $this->settings['collection']
                                  : 'de';
        $query_parameters['query'] = $query_string;
        //Order by relevance is the default in BASE.
        //No URL parameter needed in that case.
        if ($order_by == self::ORDER_BY_YEAR) {
            $query_parameters['sortby'] = 'dcyear desc';
        }
        if ($limit > 0) {
            //BASE has a limit of 125 items per response.
            if ($limit > 125) {
                $limit = 125;
            }
            $query_parameters['hits'] = $limit;
        }
        $data = $this->requestData($this->request_base_url, $query_parameters);
        if ($data === null) {
            //There are no data we can retrieve.
            return [];
        }
        $parser = new BASELibraryResultParser();
        return $parser->readResultSet($data);
    }
}