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