Passed
Pull Request — master (#3706)
by
unknown
06:53
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 assert;
35
use function explode;
36
use function json_decode;
37
use function json_last_error;
38
use function json_last_error_msg;
39
use function rawurlencode;
40
use function trim;
41
42
use const JSON_ERROR_NONE;
43
44
/**
45
 * Autocomplete handler for places
46
 */
47
class AutoCompletePlace extends AbstractAutocompleteHandler
48
{
49
    // Gazetteer urls
50
    private const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search';
51
    private const OPENROUTE_URL = 'https://api.openrouteservice.org/geocode/autocomplete';
52
53
    // Options for fetching files using GuzzleHTTP
54
    private const GUZZLE_OPTIONS = [
55
        'connect_timeout' => 3,
56
        'read_timeout'    => 3,
57
        'timeout'         => 3,
58
    ];
59
60
    protected function search(ServerRequestInterface $request): Collection
61
    {
62
        $tree = $request->getAttribute('tree');
63
        assert($tree instanceof Tree);
64
65
        $query = $request->getAttribute('query');
66
67
        $data = $this->search_service
68
            ->searchPlaces($tree, $query, 0, static::LIMIT)
69
            ->map(static function (Place $place): string {
70
                return $place->gedcomName();
71
            });
72
73
        if ((bool) Site::getPreference('use_gazetteer')) {
74
            $data = $data->concat($this->searchGazetteer($query))->unique();
75
            $data = $data->slice(0, static::LIMIT);
76
        }
77
78
        return new Collection($data);
79
    }
80
81
    /**
82
     *
83
     * @param string $query
84
     *
85
     * @return Collection<string>
86
     */
87
    private function searchGazetteer($query): collection
88
    {
89
        $key          = Site::getPreference('openroute_key');
90
        $data         = new Collection();
91
        $user_service = new UserService();
92
93
        if ($key === '') {
94
            $url   = self::NOMINATIM_URL;
95
            $qry = [
96
                'q'               => rawurlencode($query),
97
                'format'          => 'jsonv2',
98
                'limit'           => static::LIMIT,
99
                'accept-language' => I18N::languageTag(),
100
                'featuretype'     => 'settlement',
101
                'email'           => rawurlencode($user_service->administrators()->first()->email()),
102
            ];
103
        } else {
104
            $url   = self::OPENROUTE_URL;
105
            $qry = [
106
                'api_key' => $key,
107
                'text'    => rawurlencode($query),
108
                'layers'  => 'coarse',
109
                'size'    => static::LIMIT,
110
            ];
111
        }
112
113
        // Read from the URL
114
        $client = new Client();
115
        try {
116
            $json     = $client->get($url, array_merge(self::GUZZLE_OPTIONS, ['query' => $qry]))->getBody()->__toString();
117
            $results  = json_decode($json, false);
118
            if (json_last_error() !== JSON_ERROR_NONE) {
119
                throw new Exception(I18N::translate('Geocoder: %s', json_last_error_msg()));
120
            }
121
            if ($key === '') {
122
                foreach ($results as $result) {
123
                    $data->add($result->display_name);
124
                }
125
            } else {
126
                $parts = explode(',', Site::getPreference('openroute_parts'));
127
                foreach ($results->features as $result) {
128
                    if ($parts[0] === '') {
129
                        $place = $result->properties->label;
130
                    } else {
131
                        $place = '';
132
                        foreach ($parts as $part) {
133
                            if (isset($result->properties->{$part})) {
134
                                $place .= $result->properties->{$part} . Gedcom::PLACE_SEPARATOR;
135
                            }
136
                        }
137
                        $place = trim($place, Gedcom::PLACE_SEPARATOR);
138
                    }
139
                    $data->add($place);
140
                }
141
            }
142
        } catch (Exception | RequestException $ex) {
143
            // Json error? Service down?  Quota exceeded?
144
            $data->add($ex->getMessage());
145
        }
146
147
        return $data;
148
    }
149
}
150