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