Passed
Pull Request — master (#3706)
by
unknown
06:34
created

AutoCompletePlace::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 19
rs 9.9
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Exception;
23
use Fisharebest\Webtrees\Gedcom;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Place;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Fisharebest\Webtrees\Site;
28
use Fisharebest\Webtrees\Tree;
29
use GuzzleHttp\Client;
30
use GuzzleHttp\Exception\RequestException;
31
use Illuminate\Support\Collection;
32
use Psr\Http\Message\ServerRequestInterface;
33
34
use function array_filter;
35
use function array_merge;
36
use function assert;
37
use function explode;
38
use function implode;
39
use function json_decode;
40
use function json_last_error;
41
use function json_last_error_msg;
42
use function rawurlencode;
43
44
use const JSON_ERROR_NONE;
45
46
/**
47
 * Autocomplete handler for places
48
 */
49
class AutoCompletePlace extends AbstractAutocompleteHandler
50
{
51
    // Gazetteer urls
52
    private const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search';
53
    private const OPENROUTE_URL = 'https://api.openrouteservice.org/geocode/autocomplete';
54
55
    // Options for fetching files using GuzzleHTTP
56
    private const GUZZLE_OPTIONS = [
57
        'connect_timeout' => 3,
58
        'read_timeout'    => 3,
59
        'timeout'         => 3,
60
    ];
61
62
    protected function search(ServerRequestInterface $request): Collection
63
    {
64
        $tree = $request->getAttribute('tree');
65
        assert($tree instanceof Tree);
66
67
        $query = $request->getAttribute('query');
68
69
        $data = $this->search_service
70
            ->searchPlaces($tree, $query, 0, static::LIMIT)
71
            ->map(static function (Place $place): string {
72
                return $place->gedcomName();
73
            });
74
75
        if ((bool) Site::getPreference('use_gazetteer')) {
76
            $data = $data->concat($this->searchGazetteer($query))->unique();
77
            $data = $data->slice(0, static::LIMIT);
78
        }
79
80
        return new Collection($data);
81
    }
82
83
    /**
84
     *
85
     * @param string $query
86
     *
87
     * @return Collection<string>
88
     */
89
    private function searchGazetteer($query): collection
90
    {
91
        $key          = Site::getPreference('openroute_key');
92
        $data         = new Collection();
93
        $user_service = new UserService();
94
95
        if ($key === '') {
96
            $url = self::NOMINATIM_URL;
97
            $qry = [
98
                'q'               => rawurlencode($query),
99
                'format'          => 'jsonv2',
100
                'limit'           => static::LIMIT,
101
                'accept-language' => I18N::languageTag(),
102
                'featuretype'     => 'settlement',
103
                'email'           => rawurlencode($user_service->administrators()->first()->email()),
104
            ];
105
        } else {
106
            $url = self::OPENROUTE_URL;
107
            $qry = [
108
                'api_key' => $key,
109
                'text'    => rawurlencode($query),
110
                'layers'  => 'coarse',
111
                'size'    => static::LIMIT,
112
            ];
113
        }
114
115
        // Read from the URL
116
        $client = new Client();
117
        try {
118
            $json     = $client->get($url, array_merge(self::GUZZLE_OPTIONS, ['query' => $qry]))->getBody()->__toString();
119
            $results  = json_decode($json, false);
120
            if (json_last_error() !== JSON_ERROR_NONE) {
121
                throw new Exception(I18N::translate('Geocoder: %s', json_last_error_msg()));
122
            }
123
            if ($key === '') {
124
                // Use Nominatim
125
                foreach ($results as $result) {
126
                    $data->add($result->display_name);
127
                }
128
            } else {
129
                // Use Openroutesearch
130
                $place_elements = array_filter(explode(',', Site::getPreference('openroute_layers')));
131
                foreach ($results->features as $result) {
132
                    $place_parts = [];
133
                    foreach ($place_elements as $place_element) {
134
                        if (isset($result->properties->{$place_element})) {
135
                            $place_parts[] = $result->properties->{$place_element};
136
                        }
137
                    }
138
                    // If no elements are selected in the control panel
139
                    // or none of the selected elements are present in the place
140
                    // then default to the label which is always present (I think)
141
                    $data->add(implode(Gedcom::PLACE_SEPARATOR, $place_parts) ?: $result->properties->label);
142
                }
143
            }
144
        } catch (Exception | RequestException $ex) {
145
            // Json error? Service down?  Quota exceeded?
146
            $data->add($ex->getMessage());
147
        }
148
149
        return $data;
150
    }
151
}
152