aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/librarysearch/LibrarySearch.php
blob: 473eacfbdc534d73d2bfb49b8988fd8269217c80 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?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 class contains basic methods for querying a library catalog
 * using standardised search parameters.
 */
abstract class LibrarySearch
{
    //The following constants define the strings for the
    //standardised field names for the query method. These can be
    //converted to library-specific field names.
    const TITLE       = 'title';
    const AUTHOR      = 'author';
    const YEAR        = 'year';
    const NUMBER      = 'number';
    const ISSN        = 'issn';
    const ISBN        = 'isbn';
    const PUBLICATION = 'publication';
    const SIGNATURE   = 'signature';

    //Constants for the ordering of results.
    const ORDER_BY_RELEVANCE = 'relevance';
    const ORDER_BY_YEAR = 'year';


    /**
     * The base URL for the HTTP request to retrieve data.
     */
    protected $request_base_url = '';


    /**
     * Additional URL parameters for the HTTP request to retrieve data.
     */
    protected $request_url_parameters = [];


    /**
     * Implementation-specific configuration that can define the behavior
     * of the LibrarySearch implementation.
     */
    protected $settings = [];


    /**
     * A basic constructor.
     *
     * @param array $configuration The configuration for the LibrarySearch
     *     implementation. It should be an associative array with the following
     *     keys:
     *     - base_url: The base URL for retrieving data.
     *     - additional_url_parameters: Additional URL parameters for the base URL.
     *     - settings: Implementation-specific configuration. This should also
     *         be an associative array.
     */
    public function __construct(array $configuration = [])
    {
        if ($configuration['base_url']) {
            $this->request_base_url = $configuration['base_url'];
        }
        if (is_array($configuration['additional_url_parameters'])) {
            $this->request_url_parameters = $configuration['additional_url_parameters'];
        }
        if (is_array($configuration['settings'])) {
            $this->settings = $configuration['settings'];
        }
    }


    /**
     * This method shall replace the generalised search query fields with the
     * implementation specific query fields.
     *
     * @param array $query_fields An array with query parameters using the
     *     generalised query fields.
     *
     * @returns array The translated version of the $query_fields array.
     */
    abstract protected function translateQueryFields(array $query_fields = []) : array;


    /**
     * A common method for the libcurl code to request data from an URL so that
     * LibrarySearch implementations don't have to include their own libcurl
     * code to get data.
     *
     * @param string $base_url The base URL to request data from.
     *
     * @param array $url_parameters URL parameters for the request. The array
     *     should consist of an associative array with keys representing
     *     the parameter name and the values representing the parameter values.
     *
     * @returns string|bool The result of the request. If the base URL is empty
     *     or no data could be retrieved due to an error, false is returned.
     *     In case of success, a string with the retrieved data is returned.
     */
    protected function requestData(string $base_url = '', array $url_parameters = [])
    {
        if (!$base_url) {
            return false;
        }
        $full_url = $base_url;
        if ($url_parameters) {
            $full_url .= '?' . http_build_query($url_parameters);
        }

        $data = file_get_contents($full_url, false, get_default_http_stream_context($base_url));
        return $data;
    }


    /**
     * Starts a query to a library catalogue using the specified
     * parameters. If standardised parameters as defined in the FIELD_
     * constants of this class are used as keys in the $search_parameters array,
     * their keys may be converted to library-specific search keys.
     *
     * @param array $search_parameters The search parameters to be used.
     *     The array must be an associative array where the keys represent
     *     the fields.
     *
     * @param string $order_by
     *
     * @param int $limit The maximum amount of items that shall be retrieved
     *     from the catalog.
     *
     * @returns LibrarySearchResult[] An array of LibrarySearchResult items
     *     if entries matching the search could be found in the library.
     */
    abstract public function query(
        array $search_parameters = [],
        string $order_by = self::ORDER_BY_RELEVANCE,
        int $limit = 200
    ) : array;
}