1<?php
2
3/*
4 * Copyright (c) 2011-2024 Mark C. Prins <mprins@users.sf.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19use dokuwiki\Extension\ActionPlugin;
20use dokuwiki\Extension\Event;
21use dokuwiki\Extension\EventHandler;
22use dokuwiki\Logger;
23use dokuwiki\Sitemap\Item;
24
25/**
26 * DokuWiki Plugin dokuwikispatial (Action Component).
27 *
28 * @license BSD license
29 * @author  Mark C. Prins <mprins@users.sf.net>
30 */
31class action_plugin_spatialhelper extends ActionPlugin
32{
33    /**
34     * Register for events.
35     *
36     * @param EventHandler $controller
37     *          DokuWiki's event controller object.
38     */
39    final public function register(EventHandler $controller): void
40    {
41        // listen for page add / delete events
42        // http://www.dokuwiki.org/devel:event:indexer_page_add
43        $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handleIndexerPageAdd');
44        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'removeFromIndex');
45
46        // http://www.dokuwiki.org/devel:event:sitemap_generate
47        $controller->register_hook('SITEMAP_GENERATE', 'BEFORE', $this, 'handleSitemapGenerateBefore');
48        // using after will only trigger us if a sitemap was actually created
49        $controller->register_hook('SITEMAP_GENERATE', 'AFTER', $this, 'handleSitemapGenerateAfter');
50
51        // handle actions we know of
52        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess', []);
53        // handle HTML eg. /dokuwiki/doku.php?id=start&do=findnearby&geohash=u15vk4
54        $controller->register_hook(
55            'TPL_ACT_UNKNOWN',
56            'BEFORE',
57            $this,
58            'findnearby',
59            ['format' => 'HTML']
60        );
61        // handles AJAX/json eg: jQuery.post("/dokuwiki/lib/exe/ajax.php?id=start&call=findnearby&geohash=u15vk4");
62        $controller->register_hook(
63            'AJAX_CALL_UNKNOWN',
64            'BEFORE',
65            $this,
66            'findnearby',
67            ['format' => 'JSON']
68        );
69
70        // listen for media uploads and deletes
71        $controller->register_hook('MEDIA_UPLOAD_FINISH', 'BEFORE', $this, 'handleMediaUploaded', []);
72        $controller->register_hook('MEDIA_DELETE_FILE', 'BEFORE', $this, 'handleMediaDeleted', []);
73
74        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput');
75        $controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'popularity');
76    }
77
78    /**
79     * Update the spatial index for the page.
80     *
81     * @param Event $event
82     *          event object
83     */
84    final  public function handleIndexerPageAdd(Event $event): void
85    {
86        // $event→data['page'] – the page id
87        // $event→data['body'] – empty, can be filled by additional content to index by your plugin
88        // $event→data['metadata'] – the metadata that shall be indexed. This is an array where the keys are the
89        //    metadata indexes and the value a string or an array of strings with the values.
90        //    title and relation_references will already be set.
91        $id = $event->data ['page'];
92        $indexer = plugin_load('helper', 'spatialhelper_index');
93        $indexer->updateSpatialIndex($id);
94    }
95
96    /**
97     * Update the spatial index, removing the page.
98     *
99     * @param Event $event
100     *          event object
101     */
102    final    public function removeFromIndex(Event $event): void
103    {
104        // event data:
105        // $data[0] – The raw arguments for io_saveFile as an array. Do not change file path.
106        // $data[0][0] – the file path.
107        // $data[0][1] – the content to be saved, and may be modified.
108        // $data[1] – ns: The colon separated namespace path minus the trailing page name. (false if root ns)
109        // $data[2] – page_name: The wiki page name.
110        // $data[3] – rev: The page revision, false for current wiki pages.
111
112        Logger::debug("Event data in removeFromIndex.", $event->data);
113        if (@file_exists($event->data [0] [0])) {
114            // file not new
115            if (!$event->data [0] [1]) {
116                // file is empty, page is being deleted
117                if (empty($event->data [1])) {
118                    // root namespace
119                    $id = $event->data [2];
120                } else {
121                    $id = $event->data [1] . ":" . $event->data [2];
122                }
123                $indexer = plugin_load('helper', 'spatialhelper_index');
124                if ($indexer !== null) {
125                    $indexer->deleteFromIndex($id);
126                }
127            }
128        }
129    }
130
131    /**
132     * Add a new SitemapItem object that points to the KML of public geocoded pages.
133     *
134     * @param Event $event
135     */
136    final   public function handleSitemapGenerateBefore(Event $event): void
137    {
138        $path = mediaFN($this->getConf('media_kml'));
139        $lastmod = @filemtime($path);
140        $event->data ['items'] [] = new Item(ml($this->getConf('media_kml'), '', true, '&amp;', true), $lastmod);
141    }
142
143    /**
144     * Create a spatial sitemap or attach the geo/kml map to the sitemap.
145     *
146     * @param Event $event
147     *          event object, not used
148     */
149    final   public function handleSitemapGenerateAfter(Event $event): bool
150    {
151        // $event→data['items']: Array of SitemapItem instances, the array of sitemap items that already
152        //      contains all public pages of the wiki
153        // $event→data['sitemap']: The path of the file the sitemap will be saved to.
154        if (($helper = plugin_load('helper', 'spatialhelper_sitemap')) !== null) {
155            $kml = $helper->createKMLSitemap($this->getConf('media_kml'));
156            $rss = $helper->createGeoRSSSitemap($this->getConf('media_georss'));
157
158            if (!empty($this->getConf('sitemap_namespaces'))) {
159                $namespaces = array_map('trim', explode("\n", $this->getConf('sitemap_namespaces')));
160                foreach ($namespaces as $namespace) {
161                    $kmlN = $helper->createKMLSitemap($namespace . $this->getConf('media_kml'));
162                    $rssN = $helper->createGeoRSSSitemap($namespace . $this->getConf('media_georss'));
163                    Logger::debug(
164                        "handleSitemapGenerateAfter, created KML / GeoRSS sitemap in $namespace, succes: ",
165                        $kmlN && $rssN
166                    );
167                }
168            }
169            return $kml && $rss;
170        }
171        return false;
172    }
173
174    /**
175     * trap findnearby action.
176     * This additional handler is required as described at: https://www.dokuwiki.org/devel:event:tpl_act_unknown
177     *
178     * @param Event $event
179     *          event object
180     */
181    final  public function handleActionActPreprocess(Event $event): void
182    {
183        if ($event->data !== 'findnearby') {
184            return;
185        }
186        $event->preventDefault();
187    }
188
189    /**
190     * handle findnearby action.
191     *
192     * @param Event $event
193     *          event object
194     * @param array $param
195     *          associative array with keys
196     *          'format'=> HTML | JSON
197     * @throws JsonException if anything goes wrong with JSON encoding
198     */
199    final  public function findnearby(Event $event, array $param): void
200    {
201        if ($event->data !== 'findnearby') {
202            return;
203        }
204        $event->preventDefault();
205        $results = [];
206        global $INPUT;
207        if (($helper = plugin_load('helper', 'spatialhelper_search')) !== null) {
208            if ($INPUT->has('lat') && $INPUT->has('lon')) {
209                $results = $helper->findNearbyLatLon($INPUT->param('lat'), $INPUT->param('lon'));
210            } elseif ($INPUT->has('geohash')) {
211                $results = $helper->findNearby($INPUT->str('geohash'));
212            } else {
213                $results = ['error' => hsc($this->getLang('invalidinput'))];
214            }
215        }
216
217        $showMedia = $INPUT->bool('showMedia', true);
218
219        switch ($param['format']) {
220            case 'JSON':
221                $this->printJSON($results);
222                break;
223            case 'HTML':
224                // fall through to default
225            default:
226                $this->printHTML($results, $showMedia);
227                break;
228        }
229    }
230
231    /**
232     * Print seachresults as HTML lists.
233     *
234     * @param array $searchresults
235     * @throws JsonException if anything goes wrong with JSON encoding
236     */
237    private function printJSON(array $searchresults): void
238    {
239        header('Content-Type: application/json');
240        echo json_encode($searchresults, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
241    }
242
243    /**
244     * Print seachresults as HTML lists.
245     *
246     * @param array $searchresults
247     * @param bool $showMedia
248     */
249    private function printHTML(array $searchresults, bool $showMedia = true): void
250    {
251        $pages = (array)($searchresults ['pages']);
252        $media = (array)$searchresults ['media'];
253        $lat = (float)$searchresults ['lat'];
254        $lon = (float)$searchresults ['lon'];
255        $geohash = (string)$searchresults ['geohash'];
256
257        if (isset($searchresults ['error'])) {
258            echo '<div class="level1"><p>' . hsc($searchresults ['error']) . '</p></div>';
259            return;
260        }
261
262        // print a HTML list
263        echo '<h1>' . $this->getLang('results_header') . '</h1>' . DOKU_LF;
264        echo '<div class="level1">' . DOKU_LF;
265        if ($pages !== []) {
266            $pagelist = '<ol>' . DOKU_LF;
267            foreach ($pages as $page) {
268                $pagelist .= '<li>' . html_wikilink(
269                    ':' . $page ['id'],
270                    useHeading('navigation') ? null :
271                            noNS($page ['id'])
272                ) . ' (' . $this->getLang('results_distance_prefix')
273                    . $page ['distance'] . '&nbsp;m) ' . $page ['description'] . '</li>' . DOKU_LF;
274            }
275            $pagelist .= '</ol>' . DOKU_LF;
276
277            echo '<h2>' . $this->getLang('results_pages') . hsc(
278                ' lat;lon: ' . $lat . ';' . $lon
279                    . ' (geohash: ' . $geohash . ')'
280            ) . '</h2>';
281            echo '<div class="level2">' . DOKU_LF;
282            echo $pagelist;
283            echo '</div>' . DOKU_LF;
284        } else {
285            echo '<p>' . hsc($this->getLang('nothingfound')) . '</p>';
286        }
287
288        if ($media !== [] && $showMedia) {
289            $pagelist = '<ol>' . DOKU_LF;
290            foreach ($media as $m) {
291                $opts = [];
292                $link = ml($m ['id'], $opts, false);
293                $opts ['w'] = '100';
294                $src = ml($m ['id'], $opts);
295                $pagelist .= '<li><a href="' . $link . '"><img src="' . $src . '" alt=""></a> ('
296                    . $this->getLang('results_distance_prefix') . $m ['distance'] . '&nbsp;m) '
297                    . '</li>' . DOKU_LF;
298            }
299            $pagelist .= '</ol>' . DOKU_LF;
300
301            echo '<h2>' . $this->getLang('results_media') . hsc(
302                ' lat;lon: ' . $lat . ';' . $lon
303                    . ' (geohash: ' . $geohash . ')'
304            ) . '</h2>' . DOKU_LF;
305            echo '<div class="level2">' . DOKU_LF;
306            echo $pagelist;
307            echo '</div>' . DOKU_LF;
308        }
309        echo '<p>' . $this->getLang('results_precision') . $searchresults ['precision'] . ' m. ';
310        if (strlen($geohash) > 1) {
311            $url = wl(
312                getID(),
313                ['do' => 'findnearby', 'geohash' => substr($geohash, 0, -1)]
314            );
315            echo '<a href="' . $url . '" class="findnearby">' . $this->getLang('search_largerarea') . '</a>.</p>'
316                . DOKU_LF;
317        }
318        echo '</div>' . DOKU_LF;
319    }
320
321    /**
322     * add media to spatial index.
323     *
324     * @param Event $event
325     */
326    final  public function handleMediaUploaded(Event $event): void
327    {
328        //data[0] path/to/new/media.file (normally read from $_FILES, potentially could come from elsewhere)
329        //data[1] file name of the file being uploaded
330        //data[2] future directory id of the file being uploaded
331        //data[3] the mime type of the file being uploaded
332        //data[4] true if the uploaded file exists already
333        //data[5] (since 2011-02-06) the PHP function used to move the file to the correct location
334
335        Logger::debug("handleMediaUploaded::event data", $event->data);
336
337        // check the list of mimetypes
338        // if it's a supported type call appropriate index function
339        if (str_contains($event->data [3], 'image/jpeg')) {
340            $indexer = plugin_load('helper', 'spatialhelper_index');
341            if ($indexer !== null) {
342                $indexer->indexImage($event->data [2]);
343            }
344        }
345        // TODO add image/tiff
346        // TODO kml, gpx, geojson...
347    }
348
349    /**
350     * removes the media from the index.
351     * @param Event $event event object with data
352     */
353    final   public function handleMediaDeleted(Event $event): void
354    {
355        // data['id'] ID data['unl'] unlink return code
356        // data['del'] Namespace directory unlink return code
357        // data['name'] file name data['path'] full path to the file
358        // data['size'] file size
359
360        // remove the media id from the index
361        $indexer = plugin_load('helper', 'spatialhelper_index');
362        if ($indexer !== null) {
363            $indexer->deleteFromIndex('media__' . $event->data ['id']);
364        }
365    }
366
367    /**
368     * add a link to the spatial sitemap files in the header.
369     *
370     * @param Event $event the DokuWiki event. $event->data is a two-dimensional array of all meta headers.
371     * The keys are meta, link and script.
372     *
373     * @see http://www.dokuwiki.org/devel:event:tpl_metaheader_output
374     */
375    final  public function handleMetaheaderOutput(Event $event): void
376    {
377        // TODO maybe test for exist
378        $event->data ["link"] [] = ["type" => "application/atom+xml", "rel" => "alternate",
379            "href" => ml($this->getConf('media_georss')), "title" => "Spatial ATOM Feed"];
380        $event->data ["link"] [] = ["type" => "application/vnd.google-earth.kml+xml", "rel" => "alternate",
381            "href" => ml($this->getConf('media_kml')), "title" => "KML Sitemap"];
382    }
383
384    /**
385     * Add spatialhelper popularity data.
386     *
387     * @param Event $event
388     *          the DokuWiki event
389     */
390    final public function popularity(Event $event): void
391    {
392        $versionInfo = getVersionData();
393        $plugin_info = $this->getInfo();
394        $event->data['spatialhelper']['version'] = $plugin_info['date'];
395        $event->data['spatialhelper']['dwversion'] = $versionInfo['date'];
396        $event->data['spatialhelper']['combinedversion'] = $versionInfo['date'] . '_' . $plugin_info['date'];
397    }
398}
399