xref: /webtrees/app/Http/RequestHandlers/MapDataImportAction.php (revision a27e4c49028acfd0158cb2597665c57cf161d5bb)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Exception;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\PlaceLocation;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\MapDataService;
29use Illuminate\Database\Capsule\Manager as DB;
30use League\Flysystem\FilesystemException;
31use League\Flysystem\UnableToCheckFileExistence;
32use League\Flysystem\UnableToReadFile;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Message\UploadedFileInterface;
36use Psr\Http\Server\RequestHandlerInterface;
37
38use function array_filter;
39use function array_reverse;
40use function array_slice;
41use function count;
42use function fclose;
43use function fgetcsv;
44use function implode;
45use function is_numeric;
46use function json_decode;
47use function redirect;
48use function rewind;
49use function route;
50use function str_contains;
51use function stream_get_contents;
52
53use const JSON_THROW_ON_ERROR;
54use const UPLOAD_ERR_OK;
55
56/**
57 * Import geographic data.
58 */
59class MapDataImportAction implements RequestHandlerInterface
60{
61    private MapDataService $map_data_service;
62
63    /**
64     * MapDataImportAction constructor.
65     *
66     * @param MapDataService $map_data_service
67     */
68    public function __construct(MapDataService $map_data_service)
69    {
70        $this->map_data_service = $map_data_service;
71    }
72
73    /**
74     * This function assumes the input file layout is
75     * level followed by a variable number of placename fields
76     * followed by Longitude, Latitude, Zoom & Icon
77     *
78     * @param ServerRequestInterface $request
79     *
80     * @return ResponseInterface
81     * @throws Exception
82     */
83    public function handle(ServerRequestInterface $request): ResponseInterface
84    {
85        $data_filesystem = Registry::filesystem()->data();
86
87        $params = (array) $request->getParsedBody();
88
89        $serverfile     = $params['serverfile'] ?? '';
90        $options        = $params['import-options'] ?? '';
91        $clear_database = (bool) ($params['cleardatabase'] ?? false);
92        $local_file     = $request->getUploadedFiles()['localfile'] ?? null;
93
94        $places = [];
95
96        $url = route(MapDataList::class, ['parent_id' => 0]);
97
98        $fp = false;
99
100        try {
101            $file_exists = $data_filesystem->fileExists(MapDataService::PLACES_FOLDER . $serverfile);
102        } catch (FilesystemException | UnableToCheckFileExistence $ex) {
103            $file_exists = false;
104        }
105
106
107        if ($serverfile !== '' && $file_exists) {
108            // first choice is file on server
109            try {
110                $fp = $data_filesystem->readStream(MapDataService::PLACES_FOLDER . $serverfile);
111            } catch (FilesystemException | UnableToReadFile $ex) {
112                $fp = false;
113            }
114        } elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) {
115            // 2nd choice is local file
116            $fp = $local_file->getStream()->detach();
117        }
118
119        if ($fp === false || $fp === null) {
120            return redirect($url);
121        }
122
123        $string = stream_get_contents($fp);
124
125        // Check the file type
126        if (str_contains($string, 'FeatureCollection')) {
127            $input_array = json_decode($string, false, 512, JSON_THROW_ON_ERROR);
128
129            foreach ($input_array->features as $feature) {
130                $places[] = [
131                    'latitude'  => $feature->geometry->coordinates[1],
132                    'longitude' => $feature->geometry->coordinates[0],
133                    'name'      => $feature->properties->name,
134                ];
135            }
136        } else {
137            rewind($fp);
138            while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) {
139                // Skip the header
140                if (!is_numeric($row[0])) {
141                    continue;
142                }
143
144                $level = (int) $row[0];
145                $count = count($row);
146                $name  = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level)));
147
148                $places[] = [
149                    'latitude'  => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']),
150                    'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']),
151                    'name'      => $name
152                ];
153            }
154        }
155
156        fclose($fp);
157
158        if ($clear_database) {
159            // Child places are deleted via on-delete-cascade...
160            DB::table('place_location')
161                ->whereNull('parent_id')
162                ->delete();
163
164            // Automatically import any new/missing places.
165            $this->map_data_service->importMissingLocations();
166        }
167
168        $added   = 0;
169        $updated = 0;
170
171        // Remove places with 0,0 coordinates at lower levels.
172        $places = array_filter($places, static function ($place) {
173            return !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0;
174        });
175
176        foreach ($places as $place) {
177            $location = new PlaceLocation($place['name']);
178            $exists   = $location->exists();
179
180            // Only update existing records
181            if ($options === 'update' && !$exists) {
182                continue;
183            }
184
185            // Only add new records
186            if ($options === 'add' && $exists) {
187                continue;
188            }
189
190            if (!$exists) {
191                $added++;
192            }
193
194            $updated += DB::table('place_location')
195                ->where('id', '=', $location->id())
196                ->update([
197                    'latitude'  => $place['latitude'],
198                    'longitude' => $place['longitude'],
199                ]);
200        }
201
202        FlashMessages::addMessage(
203            I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)),
204            'info'
205        );
206
207        return redirect($url);
208    }
209}
210