@@ -26,423 +26,423 @@ |
||
26 | 26 | class action_plugin_spatialhelper extends DokuWiki_Action_Plugin |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * Register for events. |
|
31 | - * |
|
32 | - * @param Doku_Event_Handler $controller |
|
33 | - * DokuWiki's event controller object. Also available as global $EVENT_HANDLER |
|
34 | - */ |
|
35 | - public function register(Doku_Event_Handler $controller): void |
|
36 | - { |
|
37 | - // listen for page add / delete events |
|
38 | - // http://www.dokuwiki.org/devel:event:indexer_page_add |
|
39 | - $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handleIndexerPageAdd'); |
|
40 | - $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'removeFromIndex'); |
|
41 | - |
|
42 | - // http://www.dokuwiki.org/devel:event:sitemap_generate |
|
43 | - $controller->register_hook('SITEMAP_GENERATE', 'BEFORE', $this, 'handleSitemapGenerateBefore'); |
|
44 | - // using after will only trigger us if a sitemap was actually created |
|
45 | - $controller->register_hook('SITEMAP_GENERATE', 'AFTER', $this, 'handleSitemapGenerateAfter'); |
|
46 | - |
|
47 | - // handle actions we know of |
|
48 | - $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess', array()); |
|
49 | - // handle HTML eg. /dokuwiki/doku.php?id=start&do=findnearby&geohash=u15vk4 |
|
50 | - $controller->register_hook( |
|
51 | - 'TPL_ACT_UNKNOWN', 'BEFORE', $this, 'findnearby', array( |
|
52 | - 'format' => 'HTML' |
|
53 | - ) |
|
54 | - ); |
|
55 | - // handles AJAX/json eg: jQuery.post("/dokuwiki/lib/exe/ajax.php?id=start&call=findnearby&geohash=u15vk4"); |
|
56 | - $controller->register_hook( |
|
57 | - 'AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'findnearby', array( |
|
58 | - 'format' => 'JSON' |
|
59 | - ) |
|
60 | - ); |
|
61 | - |
|
62 | - // listen for media uploads and deletes |
|
63 | - $controller->register_hook('MEDIA_UPLOAD_FINISH', 'BEFORE', $this, 'handleMediaUploaded', array()); |
|
64 | - $controller->register_hook('MEDIA_DELETE_FILE', 'BEFORE', $this, 'handleMediaDeleted', array()); |
|
65 | - |
|
66 | - $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput'); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Update the spatial index for the page. |
|
71 | - * |
|
72 | - * @param Doku_Event $event |
|
73 | - * event object |
|
74 | - * @param mixed $param |
|
75 | - * the parameters passed to register_hook when this handler was registered |
|
76 | - */ |
|
77 | - public function handleIndexerPageAdd(Doku_Event $event, $param): void |
|
78 | - { |
|
79 | - // $event→data['page'] – the page id |
|
80 | - // $event→data['body'] – empty, can be filled by additional content to index by your plugin |
|
81 | - // $event→data['metadata'] – the metadata that shall be indexed. This is an array where the keys are the |
|
82 | - // metadata indexes and the value a string or an array of strings with the values. |
|
83 | - // title and relation_references will already be set. |
|
84 | - $id = $event->data ['page']; |
|
85 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
86 | - $entries = $indexer->updateSpatialIndex($id); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Update the spatial index, removing the page. |
|
91 | - * |
|
92 | - * @param Doku_Event $event |
|
93 | - * event object |
|
94 | - * @param mixed $param |
|
95 | - * the parameters passed to register_hook when this handler was registered |
|
96 | - */ |
|
97 | - public function removeFromIndex(Doku_Event $event, $param): void |
|
98 | - { |
|
99 | - // event data: |
|
100 | - // $data[0] – The raw arguments for io_saveFile as an array. Do not change file path. |
|
101 | - // $data[0][0] – the file path. |
|
102 | - // $data[0][1] – the content to be saved, and may be modified. |
|
103 | - // $data[1] – ns: The colon separated namespace path minus the trailing page name. (false if root ns) |
|
104 | - // $data[2] – page_name: The wiki page name. |
|
105 | - // $data[3] – rev: The page revision, false for current wiki pages. |
|
106 | - |
|
107 | - Logger::getInstance(Logger::LOG_DEBUG)->log("Event data in removeFromIndex.", $event->data); |
|
108 | - if (@file_exists($event->data [0] [0])) { |
|
109 | - // file not new |
|
110 | - if (!$event->data [0] [1]) { |
|
111 | - // file is empty, page is being deleted |
|
112 | - if (empty ($event->data [1])) { |
|
113 | - // root namespace |
|
114 | - $id = $event->data [2]; |
|
115 | - } else { |
|
116 | - $id = $event->data [1] . ":" . $event->data [2]; |
|
117 | - } |
|
118 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
119 | - if ($indexer) { |
|
120 | - $indexer->deleteFromIndex($id); |
|
121 | - } |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Add a new SitemapItem object that points to the KML of public geocoded pages. |
|
128 | - * |
|
129 | - * @param Doku_Event $event |
|
130 | - * @param mixed $param |
|
131 | - */ |
|
132 | - public function handleSitemapGenerateBefore(Doku_Event $event, $param): void |
|
133 | - { |
|
134 | - $path = mediaFN($this->getConf('media_kml')); |
|
135 | - $lastmod = @filemtime($path); |
|
136 | - $event->data ['items'] [] = new Item( |
|
137 | - ml($this->getConf('media_kml'), |
|
138 | - '', |
|
139 | - true, |
|
140 | - '&', |
|
141 | - true), |
|
142 | - $lastmod |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Create a spatial sitemap or attach the geo/kml map to the sitemap. |
|
148 | - * |
|
149 | - * @param Doku_Event $event |
|
150 | - * event object, not used |
|
151 | - * @param mixed $param |
|
152 | - * parameter array, not used |
|
153 | - */ |
|
154 | - public function handleSitemapGenerateAfter(Doku_Event $event, $param): bool |
|
155 | - { |
|
156 | - // $event→data['items']: Array of SitemapItem instances, the array of sitemap items that already |
|
157 | - // contains all public pages of the wiki |
|
158 | - // $event→data['sitemap']: The path of the file the sitemap will be saved to. |
|
159 | - if ($helper = plugin_load('helper', 'spatialhelper_sitemap')) { |
|
160 | - $kml = $helper->createKMLSitemap($this->getConf('media_kml')); |
|
161 | - $rss = $helper->createGeoRSSSitemap($this->getConf('media_georss')); |
|
162 | - |
|
163 | - if (!empty ($this->getConf('sitemap_namespaces'))) { |
|
164 | - $namespaces = array_map( |
|
165 | - 'trim', |
|
166 | - explode("\n", |
|
167 | - $this->getConf('sitemap_namespaces')) |
|
168 | - ); |
|
169 | - foreach ($namespaces as $namespace) { |
|
170 | - $kmlN = $helper->createKMLSitemap($namespace . $this->getConf('media_kml')); |
|
171 | - $rssN = $helper->createGeoRSSSitemap($namespace . $this->getConf('media_georss')); |
|
172 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
173 | - "handleSitemapGenerateAfter, created KML / GeoRSS sitemap in $namespace, succes: ", |
|
174 | - $kmlN && $rssN |
|
175 | - ); |
|
176 | - } |
|
177 | - } |
|
178 | - return $kml && $rss; |
|
179 | - } else { |
|
180 | - Logger::getInstance(Logger::LOG_DEBUG)->log("createSpatialSitemap NOT loaded helper.", $helper); |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * trap findnearby action. |
|
186 | - * This addional handler is required as described at: https://www.dokuwiki.org/devel:event:tpl_act_unknown |
|
187 | - * |
|
188 | - * @param Doku_Event $event |
|
189 | - * event object |
|
190 | - * @param mixed $param |
|
191 | - * not used |
|
192 | - */ |
|
193 | - public function handleActionActPreprocess(Doku_Event $event, $param): void |
|
194 | - { |
|
195 | - if ($event->data !== 'findnearby') { |
|
196 | - return; |
|
197 | - } |
|
198 | - $event->preventDefault(); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * handle findnearby action. |
|
203 | - * |
|
204 | - * @param Doku_Event $event |
|
205 | - * event object |
|
206 | - * @param mixed $param |
|
207 | - * associative array with keys |
|
208 | - * 'format'=> HTML | JSON |
|
209 | - */ |
|
210 | - public function findnearby(Doku_Event $event, $param): void |
|
211 | - { |
|
212 | - if ($event->data !== 'findnearby') { |
|
213 | - return; |
|
214 | - } |
|
215 | - $event->preventDefault(); |
|
216 | - $results = array(); |
|
217 | - global $INPUT; |
|
218 | - if ($helper = plugin_load('helper', 'spatialhelper_search')) { |
|
219 | - if ($INPUT->has('lat') && $INPUT->has('lon')) { |
|
220 | - $results = $helper->findNearbyLatLon($INPUT->param('lat'), $INPUT->param('lon')); |
|
221 | - } elseif ($INPUT->has('geohash')) { |
|
222 | - $results = $helper->findNearby($INPUT->str('geohash')); |
|
223 | - } else { |
|
224 | - $results = array( |
|
225 | - 'error' => hsc($this->getLang('invalidinput')) |
|
226 | - ); |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - $showMedia = $INPUT->bool('showMedia', true); |
|
231 | - |
|
232 | - switch ($param['format']) { |
|
233 | - case 'JSON' : |
|
234 | - $this->printJSON($results); |
|
235 | - break; |
|
236 | - case 'HTML' : |
|
237 | - // fall through to default |
|
238 | - default : |
|
239 | - $this->printHTML($results, $showMedia); |
|
240 | - break; |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Print seachresults as HTML lists. |
|
246 | - * |
|
247 | - * @param array $searchresults |
|
248 | - */ |
|
249 | - private function printJSON(array $searchresults): void |
|
250 | - { |
|
251 | - require_once DOKU_INC . 'inc/JSON.php'; |
|
252 | - $json = new JSON(); |
|
253 | - header('Content-Type: application/json'); |
|
254 | - print $json->encode($searchresults); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Print seachresults as HTML lists. |
|
259 | - * |
|
260 | - * @param array $searchresults |
|
261 | - * @param bool $showMedia |
|
262 | - */ |
|
263 | - private function printHTML(array $searchresults, bool $showMedia = true): void |
|
264 | - { |
|
265 | - $pages = (array)($searchresults ['pages']); |
|
266 | - $media = (array)$searchresults ['media']; |
|
267 | - $lat = (float)$searchresults ['lat']; |
|
268 | - $lon = (float)$searchresults ['lon']; |
|
269 | - $geohash = (string)$searchresults ['geohash']; |
|
270 | - |
|
271 | - if (isset ($searchresults ['error'])) { |
|
272 | - print '<div class="level1"><p>' . hsc($searchresults ['error']) . '</p></div>'; |
|
273 | - return; |
|
274 | - } |
|
275 | - |
|
276 | - // print a HTML list |
|
277 | - print '<h1>' . $this->getLang('results_header') . '</h1>' . DOKU_LF; |
|
278 | - print '<div class="level1">' . DOKU_LF; |
|
279 | - if (!empty ($pages)) { |
|
280 | - $pagelist = '<ol>' . DOKU_LF; |
|
281 | - foreach ($pages as $page) { |
|
282 | - $pagelist .= '<li>' . html_wikilink( |
|
283 | - ':' . $page ['id'], useHeading('navigation') ? null : |
|
284 | - noNS($page ['id']) |
|
285 | - ) . ' (' . $this->getLang('results_distance_prefix') |
|
286 | - . $page ['distance'] . ' m) ' . $page ['description'] . '</li>' . DOKU_LF; |
|
287 | - } |
|
288 | - $pagelist .= '</ol>' . DOKU_LF; |
|
289 | - |
|
290 | - print '<h2>' . $this->getLang('results_pages') . hsc( |
|
291 | - ' lat;lon: ' . $lat . ';' . $lon |
|
292 | - . ' (geohash: ' . $geohash . ')' |
|
293 | - ) . '</h2>'; |
|
294 | - print '<div class="level2">' . DOKU_LF; |
|
295 | - print $pagelist; |
|
296 | - print '</div>' . DOKU_LF; |
|
297 | - } else { |
|
298 | - print '<p>' . hsc($this->getLang('nothingfound')) . '</p>'; |
|
299 | - } |
|
300 | - |
|
301 | - if (!empty ($media) && $showMedia) { |
|
302 | - $pagelist = '<ol>' . DOKU_LF; |
|
303 | - foreach ($media as $m) { |
|
304 | - $opts = array(); |
|
305 | - $link = ml($m ['id'], $opts, false, '&', false); |
|
306 | - $opts ['w'] = '100'; |
|
307 | - $src = ml($m ['id'], $opts); |
|
308 | - $pagelist .= '<li><a href="' . $link . '"><img src="' . $src . '"></a> (' |
|
309 | - . $this->getLang('results_distance_prefix') . $page ['distance'] . ' m) ' . hsc($desc) |
|
310 | - . '</li>' . DOKU_LF; |
|
311 | - } |
|
312 | - $pagelist .= '</ol>' . DOKU_LF; |
|
313 | - |
|
314 | - print '<h2>' . $this->getLang('results_media') . hsc( |
|
315 | - ' lat;lon: ' . $lat . ';' . $lon |
|
316 | - . ' (geohash: ' . $geohash . ')' |
|
317 | - ) . '</h2>' . DOKU_LF; |
|
318 | - print '<div class="level2">' . DOKU_LF; |
|
319 | - print $pagelist; |
|
320 | - print '</div>' . DOKU_LF; |
|
321 | - } |
|
322 | - print '<p>' . $this->getLang('results_precision') . $searchresults ['precision'] . ' m. '; |
|
323 | - if (strlen($geohash) > 1) { |
|
324 | - $url = wl( |
|
325 | - getID(), array( |
|
326 | - 'do' => 'findnearby', |
|
327 | - 'geohash' => substr($geohash, 0, -1) |
|
328 | - ) |
|
329 | - ); |
|
330 | - print '<a href="' . $url . '" class="findnearby">' . $this->getLang('search_largerarea') . '</a>.</p>' |
|
331 | - . DOKU_LF; |
|
332 | - } |
|
333 | - print '</div>' . DOKU_LF; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * add media to spatial index. |
|
338 | - * |
|
339 | - * @param Doku_Event $event |
|
340 | - * @param mixed $param |
|
341 | - */ |
|
342 | - public function handleMediaUploaded(Doku_Event $event, $param): void |
|
343 | - { |
|
344 | - // data[0] temporary file name (read from $_FILES) |
|
345 | - // data[1] file name of the file being uploaded |
|
346 | - // data[2] future directory id of the file being uploaded |
|
347 | - // data[3] the mime type of the file being uploaded |
|
348 | - // data[4] true if the uploaded file exists already |
|
349 | - // data[5] (since 2011-02-06) the PHP function used to move the file to the correct location |
|
350 | - |
|
351 | - Logger::getInstance(Logger::LOG_DEBUG)->log("handleMediaUploaded::event data", $event->data); |
|
352 | - |
|
353 | - // check the list of mimetypes |
|
354 | - // if it's a supported type call appropriate index function |
|
355 | - if (substr_compare($event->data [3], 'image/jpeg', 0)) { |
|
356 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
357 | - if ($indexer) { |
|
358 | - $indexer->indexImage($event->data [2], $event->data [1]); |
|
359 | - } |
|
360 | - } |
|
361 | - // TODO add image/tiff |
|
362 | - // TODO kml, gpx, geojson... |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * removes the media from the index. |
|
367 | - */ |
|
368 | - public function handleMediaDeleted(Doku_Event $event, $param): void |
|
369 | - { |
|
370 | - // data['id'] ID data['unl'] unlink return code |
|
371 | - // data['del'] Namespace directory unlink return code |
|
372 | - // data['name'] file name data['path'] full path to the file |
|
373 | - // data['size'] file size |
|
374 | - |
|
375 | - Logger::getInstance(Logger::LOG_DEBUG)->log("handleMediaDeleted::event data", $event->data); |
|
376 | - |
|
377 | - // remove the media id from the index |
|
378 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
379 | - if ($indexer) { |
|
380 | - $indexer->deleteFromIndex('media__' . $event->data ['id']); |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * add a link to the spatial sitemap files in the header. |
|
386 | - * |
|
387 | - * @param Doku_Event $event |
|
388 | - * the DokuWiki event. $event->data is a two-dimensional |
|
389 | - * array of all meta headers. The keys are meta, link and script. |
|
390 | - * @param mixed $param |
|
391 | - * |
|
392 | - * @see http://www.dokuwiki.org/devel:event:tpl_metaheader_output |
|
393 | - */ |
|
394 | - public function handleMetaheaderOutput(Doku_Event $event, $param): void |
|
395 | - { |
|
396 | - // TODO maybe test for exist |
|
397 | - $event->data ["link"] [] = array( |
|
398 | - "type" => "application/atom+xml", |
|
399 | - "rel" => "alternate", |
|
400 | - "href" => ml($this->getConf('media_georss')), |
|
401 | - "title" => "Spatial ATOM Feed" |
|
402 | - ); |
|
403 | - $event->data ["link"] [] = array( |
|
404 | - "type" => "application/vnd.google-earth.kml+xml", |
|
405 | - "rel" => "alternate", |
|
406 | - "href" => ml($this->getConf('media_kml')), |
|
407 | - "title" => "KML Sitemap" |
|
408 | - ); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Calculate a new coordinate based on start, distance and bearing |
|
413 | - * |
|
414 | - * @param $start array |
|
415 | - * - start coordinate as decimal lat/lon pair |
|
416 | - * @param $dist float |
|
417 | - * - distance in kilometers |
|
418 | - * @param $brng float |
|
419 | - * - bearing in degrees (compass direction) |
|
420 | - */ |
|
421 | - private function geoDestination(array $start, float $dist, float $brng): array |
|
422 | - { |
|
423 | - $lat1 = $this->toRad($start [0]); |
|
424 | - $lon1 = $this->toRad($start [1]); |
|
425 | - // http://en.wikipedia.org/wiki/Earth_radius |
|
426 | - // average earth radius in km |
|
427 | - $dist = $dist / 6371.01; |
|
428 | - $brng = $this->toRad($brng); |
|
429 | - |
|
430 | - $lon2 = $lon1 + atan2(sin($brng) * sin($dist) * cos($lat1), cos($dist) - sin($lat1) * sin($lat2)); |
|
431 | - $lon2 = fmod(($lon2 + 3 * M_PI), (2 * M_PI)) - M_PI; |
|
432 | - |
|
433 | - return array( |
|
434 | - $this->toDeg($lat2), |
|
435 | - $this->toDeg($lon2) |
|
436 | - ); |
|
437 | - } |
|
438 | - |
|
439 | - private function toRad(float $deg): float |
|
440 | - { |
|
441 | - return $deg * M_PI / 180; |
|
442 | - } |
|
443 | - |
|
444 | - private function toDeg(float $rad): float |
|
445 | - { |
|
446 | - return $rad * 180 / M_PI; |
|
447 | - } |
|
29 | + /** |
|
30 | + * Register for events. |
|
31 | + * |
|
32 | + * @param Doku_Event_Handler $controller |
|
33 | + * DokuWiki's event controller object. Also available as global $EVENT_HANDLER |
|
34 | + */ |
|
35 | + public function register(Doku_Event_Handler $controller): void |
|
36 | + { |
|
37 | + // listen for page add / delete events |
|
38 | + // http://www.dokuwiki.org/devel:event:indexer_page_add |
|
39 | + $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handleIndexerPageAdd'); |
|
40 | + $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'removeFromIndex'); |
|
41 | + |
|
42 | + // http://www.dokuwiki.org/devel:event:sitemap_generate |
|
43 | + $controller->register_hook('SITEMAP_GENERATE', 'BEFORE', $this, 'handleSitemapGenerateBefore'); |
|
44 | + // using after will only trigger us if a sitemap was actually created |
|
45 | + $controller->register_hook('SITEMAP_GENERATE', 'AFTER', $this, 'handleSitemapGenerateAfter'); |
|
46 | + |
|
47 | + // handle actions we know of |
|
48 | + $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess', array()); |
|
49 | + // handle HTML eg. /dokuwiki/doku.php?id=start&do=findnearby&geohash=u15vk4 |
|
50 | + $controller->register_hook( |
|
51 | + 'TPL_ACT_UNKNOWN', 'BEFORE', $this, 'findnearby', array( |
|
52 | + 'format' => 'HTML' |
|
53 | + ) |
|
54 | + ); |
|
55 | + // handles AJAX/json eg: jQuery.post("/dokuwiki/lib/exe/ajax.php?id=start&call=findnearby&geohash=u15vk4"); |
|
56 | + $controller->register_hook( |
|
57 | + 'AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'findnearby', array( |
|
58 | + 'format' => 'JSON' |
|
59 | + ) |
|
60 | + ); |
|
61 | + |
|
62 | + // listen for media uploads and deletes |
|
63 | + $controller->register_hook('MEDIA_UPLOAD_FINISH', 'BEFORE', $this, 'handleMediaUploaded', array()); |
|
64 | + $controller->register_hook('MEDIA_DELETE_FILE', 'BEFORE', $this, 'handleMediaDeleted', array()); |
|
65 | + |
|
66 | + $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput'); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Update the spatial index for the page. |
|
71 | + * |
|
72 | + * @param Doku_Event $event |
|
73 | + * event object |
|
74 | + * @param mixed $param |
|
75 | + * the parameters passed to register_hook when this handler was registered |
|
76 | + */ |
|
77 | + public function handleIndexerPageAdd(Doku_Event $event, $param): void |
|
78 | + { |
|
79 | + // $event→data['page'] – the page id |
|
80 | + // $event→data['body'] – empty, can be filled by additional content to index by your plugin |
|
81 | + // $event→data['metadata'] – the metadata that shall be indexed. This is an array where the keys are the |
|
82 | + // metadata indexes and the value a string or an array of strings with the values. |
|
83 | + // title and relation_references will already be set. |
|
84 | + $id = $event->data ['page']; |
|
85 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
86 | + $entries = $indexer->updateSpatialIndex($id); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Update the spatial index, removing the page. |
|
91 | + * |
|
92 | + * @param Doku_Event $event |
|
93 | + * event object |
|
94 | + * @param mixed $param |
|
95 | + * the parameters passed to register_hook when this handler was registered |
|
96 | + */ |
|
97 | + public function removeFromIndex(Doku_Event $event, $param): void |
|
98 | + { |
|
99 | + // event data: |
|
100 | + // $data[0] – The raw arguments for io_saveFile as an array. Do not change file path. |
|
101 | + // $data[0][0] – the file path. |
|
102 | + // $data[0][1] – the content to be saved, and may be modified. |
|
103 | + // $data[1] – ns: The colon separated namespace path minus the trailing page name. (false if root ns) |
|
104 | + // $data[2] – page_name: The wiki page name. |
|
105 | + // $data[3] – rev: The page revision, false for current wiki pages. |
|
106 | + |
|
107 | + Logger::getInstance(Logger::LOG_DEBUG)->log("Event data in removeFromIndex.", $event->data); |
|
108 | + if (@file_exists($event->data [0] [0])) { |
|
109 | + // file not new |
|
110 | + if (!$event->data [0] [1]) { |
|
111 | + // file is empty, page is being deleted |
|
112 | + if (empty ($event->data [1])) { |
|
113 | + // root namespace |
|
114 | + $id = $event->data [2]; |
|
115 | + } else { |
|
116 | + $id = $event->data [1] . ":" . $event->data [2]; |
|
117 | + } |
|
118 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
119 | + if ($indexer) { |
|
120 | + $indexer->deleteFromIndex($id); |
|
121 | + } |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Add a new SitemapItem object that points to the KML of public geocoded pages. |
|
128 | + * |
|
129 | + * @param Doku_Event $event |
|
130 | + * @param mixed $param |
|
131 | + */ |
|
132 | + public function handleSitemapGenerateBefore(Doku_Event $event, $param): void |
|
133 | + { |
|
134 | + $path = mediaFN($this->getConf('media_kml')); |
|
135 | + $lastmod = @filemtime($path); |
|
136 | + $event->data ['items'] [] = new Item( |
|
137 | + ml($this->getConf('media_kml'), |
|
138 | + '', |
|
139 | + true, |
|
140 | + '&', |
|
141 | + true), |
|
142 | + $lastmod |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Create a spatial sitemap or attach the geo/kml map to the sitemap. |
|
148 | + * |
|
149 | + * @param Doku_Event $event |
|
150 | + * event object, not used |
|
151 | + * @param mixed $param |
|
152 | + * parameter array, not used |
|
153 | + */ |
|
154 | + public function handleSitemapGenerateAfter(Doku_Event $event, $param): bool |
|
155 | + { |
|
156 | + // $event→data['items']: Array of SitemapItem instances, the array of sitemap items that already |
|
157 | + // contains all public pages of the wiki |
|
158 | + // $event→data['sitemap']: The path of the file the sitemap will be saved to. |
|
159 | + if ($helper = plugin_load('helper', 'spatialhelper_sitemap')) { |
|
160 | + $kml = $helper->createKMLSitemap($this->getConf('media_kml')); |
|
161 | + $rss = $helper->createGeoRSSSitemap($this->getConf('media_georss')); |
|
162 | + |
|
163 | + if (!empty ($this->getConf('sitemap_namespaces'))) { |
|
164 | + $namespaces = array_map( |
|
165 | + 'trim', |
|
166 | + explode("\n", |
|
167 | + $this->getConf('sitemap_namespaces')) |
|
168 | + ); |
|
169 | + foreach ($namespaces as $namespace) { |
|
170 | + $kmlN = $helper->createKMLSitemap($namespace . $this->getConf('media_kml')); |
|
171 | + $rssN = $helper->createGeoRSSSitemap($namespace . $this->getConf('media_georss')); |
|
172 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
173 | + "handleSitemapGenerateAfter, created KML / GeoRSS sitemap in $namespace, succes: ", |
|
174 | + $kmlN && $rssN |
|
175 | + ); |
|
176 | + } |
|
177 | + } |
|
178 | + return $kml && $rss; |
|
179 | + } else { |
|
180 | + Logger::getInstance(Logger::LOG_DEBUG)->log("createSpatialSitemap NOT loaded helper.", $helper); |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * trap findnearby action. |
|
186 | + * This addional handler is required as described at: https://www.dokuwiki.org/devel:event:tpl_act_unknown |
|
187 | + * |
|
188 | + * @param Doku_Event $event |
|
189 | + * event object |
|
190 | + * @param mixed $param |
|
191 | + * not used |
|
192 | + */ |
|
193 | + public function handleActionActPreprocess(Doku_Event $event, $param): void |
|
194 | + { |
|
195 | + if ($event->data !== 'findnearby') { |
|
196 | + return; |
|
197 | + } |
|
198 | + $event->preventDefault(); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * handle findnearby action. |
|
203 | + * |
|
204 | + * @param Doku_Event $event |
|
205 | + * event object |
|
206 | + * @param mixed $param |
|
207 | + * associative array with keys |
|
208 | + * 'format'=> HTML | JSON |
|
209 | + */ |
|
210 | + public function findnearby(Doku_Event $event, $param): void |
|
211 | + { |
|
212 | + if ($event->data !== 'findnearby') { |
|
213 | + return; |
|
214 | + } |
|
215 | + $event->preventDefault(); |
|
216 | + $results = array(); |
|
217 | + global $INPUT; |
|
218 | + if ($helper = plugin_load('helper', 'spatialhelper_search')) { |
|
219 | + if ($INPUT->has('lat') && $INPUT->has('lon')) { |
|
220 | + $results = $helper->findNearbyLatLon($INPUT->param('lat'), $INPUT->param('lon')); |
|
221 | + } elseif ($INPUT->has('geohash')) { |
|
222 | + $results = $helper->findNearby($INPUT->str('geohash')); |
|
223 | + } else { |
|
224 | + $results = array( |
|
225 | + 'error' => hsc($this->getLang('invalidinput')) |
|
226 | + ); |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + $showMedia = $INPUT->bool('showMedia', true); |
|
231 | + |
|
232 | + switch ($param['format']) { |
|
233 | + case 'JSON' : |
|
234 | + $this->printJSON($results); |
|
235 | + break; |
|
236 | + case 'HTML' : |
|
237 | + // fall through to default |
|
238 | + default : |
|
239 | + $this->printHTML($results, $showMedia); |
|
240 | + break; |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Print seachresults as HTML lists. |
|
246 | + * |
|
247 | + * @param array $searchresults |
|
248 | + */ |
|
249 | + private function printJSON(array $searchresults): void |
|
250 | + { |
|
251 | + require_once DOKU_INC . 'inc/JSON.php'; |
|
252 | + $json = new JSON(); |
|
253 | + header('Content-Type: application/json'); |
|
254 | + print $json->encode($searchresults); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Print seachresults as HTML lists. |
|
259 | + * |
|
260 | + * @param array $searchresults |
|
261 | + * @param bool $showMedia |
|
262 | + */ |
|
263 | + private function printHTML(array $searchresults, bool $showMedia = true): void |
|
264 | + { |
|
265 | + $pages = (array)($searchresults ['pages']); |
|
266 | + $media = (array)$searchresults ['media']; |
|
267 | + $lat = (float)$searchresults ['lat']; |
|
268 | + $lon = (float)$searchresults ['lon']; |
|
269 | + $geohash = (string)$searchresults ['geohash']; |
|
270 | + |
|
271 | + if (isset ($searchresults ['error'])) { |
|
272 | + print '<div class="level1"><p>' . hsc($searchresults ['error']) . '</p></div>'; |
|
273 | + return; |
|
274 | + } |
|
275 | + |
|
276 | + // print a HTML list |
|
277 | + print '<h1>' . $this->getLang('results_header') . '</h1>' . DOKU_LF; |
|
278 | + print '<div class="level1">' . DOKU_LF; |
|
279 | + if (!empty ($pages)) { |
|
280 | + $pagelist = '<ol>' . DOKU_LF; |
|
281 | + foreach ($pages as $page) { |
|
282 | + $pagelist .= '<li>' . html_wikilink( |
|
283 | + ':' . $page ['id'], useHeading('navigation') ? null : |
|
284 | + noNS($page ['id']) |
|
285 | + ) . ' (' . $this->getLang('results_distance_prefix') |
|
286 | + . $page ['distance'] . ' m) ' . $page ['description'] . '</li>' . DOKU_LF; |
|
287 | + } |
|
288 | + $pagelist .= '</ol>' . DOKU_LF; |
|
289 | + |
|
290 | + print '<h2>' . $this->getLang('results_pages') . hsc( |
|
291 | + ' lat;lon: ' . $lat . ';' . $lon |
|
292 | + . ' (geohash: ' . $geohash . ')' |
|
293 | + ) . '</h2>'; |
|
294 | + print '<div class="level2">' . DOKU_LF; |
|
295 | + print $pagelist; |
|
296 | + print '</div>' . DOKU_LF; |
|
297 | + } else { |
|
298 | + print '<p>' . hsc($this->getLang('nothingfound')) . '</p>'; |
|
299 | + } |
|
300 | + |
|
301 | + if (!empty ($media) && $showMedia) { |
|
302 | + $pagelist = '<ol>' . DOKU_LF; |
|
303 | + foreach ($media as $m) { |
|
304 | + $opts = array(); |
|
305 | + $link = ml($m ['id'], $opts, false, '&', false); |
|
306 | + $opts ['w'] = '100'; |
|
307 | + $src = ml($m ['id'], $opts); |
|
308 | + $pagelist .= '<li><a href="' . $link . '"><img src="' . $src . '"></a> (' |
|
309 | + . $this->getLang('results_distance_prefix') . $page ['distance'] . ' m) ' . hsc($desc) |
|
310 | + . '</li>' . DOKU_LF; |
|
311 | + } |
|
312 | + $pagelist .= '</ol>' . DOKU_LF; |
|
313 | + |
|
314 | + print '<h2>' . $this->getLang('results_media') . hsc( |
|
315 | + ' lat;lon: ' . $lat . ';' . $lon |
|
316 | + . ' (geohash: ' . $geohash . ')' |
|
317 | + ) . '</h2>' . DOKU_LF; |
|
318 | + print '<div class="level2">' . DOKU_LF; |
|
319 | + print $pagelist; |
|
320 | + print '</div>' . DOKU_LF; |
|
321 | + } |
|
322 | + print '<p>' . $this->getLang('results_precision') . $searchresults ['precision'] . ' m. '; |
|
323 | + if (strlen($geohash) > 1) { |
|
324 | + $url = wl( |
|
325 | + getID(), array( |
|
326 | + 'do' => 'findnearby', |
|
327 | + 'geohash' => substr($geohash, 0, -1) |
|
328 | + ) |
|
329 | + ); |
|
330 | + print '<a href="' . $url . '" class="findnearby">' . $this->getLang('search_largerarea') . '</a>.</p>' |
|
331 | + . DOKU_LF; |
|
332 | + } |
|
333 | + print '</div>' . DOKU_LF; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * add media to spatial index. |
|
338 | + * |
|
339 | + * @param Doku_Event $event |
|
340 | + * @param mixed $param |
|
341 | + */ |
|
342 | + public function handleMediaUploaded(Doku_Event $event, $param): void |
|
343 | + { |
|
344 | + // data[0] temporary file name (read from $_FILES) |
|
345 | + // data[1] file name of the file being uploaded |
|
346 | + // data[2] future directory id of the file being uploaded |
|
347 | + // data[3] the mime type of the file being uploaded |
|
348 | + // data[4] true if the uploaded file exists already |
|
349 | + // data[5] (since 2011-02-06) the PHP function used to move the file to the correct location |
|
350 | + |
|
351 | + Logger::getInstance(Logger::LOG_DEBUG)->log("handleMediaUploaded::event data", $event->data); |
|
352 | + |
|
353 | + // check the list of mimetypes |
|
354 | + // if it's a supported type call appropriate index function |
|
355 | + if (substr_compare($event->data [3], 'image/jpeg', 0)) { |
|
356 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
357 | + if ($indexer) { |
|
358 | + $indexer->indexImage($event->data [2], $event->data [1]); |
|
359 | + } |
|
360 | + } |
|
361 | + // TODO add image/tiff |
|
362 | + // TODO kml, gpx, geojson... |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * removes the media from the index. |
|
367 | + */ |
|
368 | + public function handleMediaDeleted(Doku_Event $event, $param): void |
|
369 | + { |
|
370 | + // data['id'] ID data['unl'] unlink return code |
|
371 | + // data['del'] Namespace directory unlink return code |
|
372 | + // data['name'] file name data['path'] full path to the file |
|
373 | + // data['size'] file size |
|
374 | + |
|
375 | + Logger::getInstance(Logger::LOG_DEBUG)->log("handleMediaDeleted::event data", $event->data); |
|
376 | + |
|
377 | + // remove the media id from the index |
|
378 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
379 | + if ($indexer) { |
|
380 | + $indexer->deleteFromIndex('media__' . $event->data ['id']); |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * add a link to the spatial sitemap files in the header. |
|
386 | + * |
|
387 | + * @param Doku_Event $event |
|
388 | + * the DokuWiki event. $event->data is a two-dimensional |
|
389 | + * array of all meta headers. The keys are meta, link and script. |
|
390 | + * @param mixed $param |
|
391 | + * |
|
392 | + * @see http://www.dokuwiki.org/devel:event:tpl_metaheader_output |
|
393 | + */ |
|
394 | + public function handleMetaheaderOutput(Doku_Event $event, $param): void |
|
395 | + { |
|
396 | + // TODO maybe test for exist |
|
397 | + $event->data ["link"] [] = array( |
|
398 | + "type" => "application/atom+xml", |
|
399 | + "rel" => "alternate", |
|
400 | + "href" => ml($this->getConf('media_georss')), |
|
401 | + "title" => "Spatial ATOM Feed" |
|
402 | + ); |
|
403 | + $event->data ["link"] [] = array( |
|
404 | + "type" => "application/vnd.google-earth.kml+xml", |
|
405 | + "rel" => "alternate", |
|
406 | + "href" => ml($this->getConf('media_kml')), |
|
407 | + "title" => "KML Sitemap" |
|
408 | + ); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Calculate a new coordinate based on start, distance and bearing |
|
413 | + * |
|
414 | + * @param $start array |
|
415 | + * - start coordinate as decimal lat/lon pair |
|
416 | + * @param $dist float |
|
417 | + * - distance in kilometers |
|
418 | + * @param $brng float |
|
419 | + * - bearing in degrees (compass direction) |
|
420 | + */ |
|
421 | + private function geoDestination(array $start, float $dist, float $brng): array |
|
422 | + { |
|
423 | + $lat1 = $this->toRad($start [0]); |
|
424 | + $lon1 = $this->toRad($start [1]); |
|
425 | + // http://en.wikipedia.org/wiki/Earth_radius |
|
426 | + // average earth radius in km |
|
427 | + $dist = $dist / 6371.01; |
|
428 | + $brng = $this->toRad($brng); |
|
429 | + |
|
430 | + $lon2 = $lon1 + atan2(sin($brng) * sin($dist) * cos($lat1), cos($dist) - sin($lat1) * sin($lat2)); |
|
431 | + $lon2 = fmod(($lon2 + 3 * M_PI), (2 * M_PI)) - M_PI; |
|
432 | + |
|
433 | + return array( |
|
434 | + $this->toDeg($lat2), |
|
435 | + $this->toDeg($lon2) |
|
436 | + ); |
|
437 | + } |
|
438 | + |
|
439 | + private function toRad(float $deg): float |
|
440 | + { |
|
441 | + return $deg * M_PI / 180; |
|
442 | + } |
|
443 | + |
|
444 | + private function toDeg(float $rad): float |
|
445 | + { |
|
446 | + return $rad * 180 / M_PI; |
|
447 | + } |
|
448 | 448 | } |
@@ -262,11 +262,11 @@ |
||
262 | 262 | */ |
263 | 263 | private function printHTML(array $searchresults, bool $showMedia = true): void |
264 | 264 | { |
265 | - $pages = (array)($searchresults ['pages']); |
|
266 | - $media = (array)$searchresults ['media']; |
|
267 | - $lat = (float)$searchresults ['lat']; |
|
268 | - $lon = (float)$searchresults ['lon']; |
|
269 | - $geohash = (string)$searchresults ['geohash']; |
|
265 | + $pages = ( array ) ($searchresults ['pages']); |
|
266 | + $media = ( array ) $searchresults ['media']; |
|
267 | + $lat = ( float ) $searchresults ['lat']; |
|
268 | + $lon = ( float ) $searchresults ['lon']; |
|
269 | + $geohash = ( string ) $searchresults ['geohash']; |
|
270 | 270 | |
271 | 271 | if (isset ($searchresults ['error'])) { |
272 | 272 | print '<div class="level1"><p>' . hsc($searchresults ['error']) . '</p></div>'; |
@@ -23,105 +23,105 @@ |
||
23 | 23 | */ |
24 | 24 | class syntax_plugin_spatialhelper_findnearby extends DokuWiki_Syntax_Plugin |
25 | 25 | { |
26 | - /** |
|
27 | - * |
|
28 | - * @see DokuWiki_Syntax_Plugin::getType() |
|
29 | - */ |
|
30 | - public function getType(): string |
|
31 | - { |
|
32 | - return 'substition'; |
|
33 | - } |
|
26 | + /** |
|
27 | + * |
|
28 | + * @see DokuWiki_Syntax_Plugin::getType() |
|
29 | + */ |
|
30 | + public function getType(): string |
|
31 | + { |
|
32 | + return 'substition'; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Return 'normal' so this syntax can be rendered inline. |
|
37 | - * |
|
38 | - * @see DokuWiki_Syntax_Plugin::getPType() |
|
39 | - */ |
|
40 | - public function getPType(): string |
|
41 | - { |
|
42 | - return 'normal'; |
|
43 | - } |
|
35 | + /** |
|
36 | + * Return 'normal' so this syntax can be rendered inline. |
|
37 | + * |
|
38 | + * @see DokuWiki_Syntax_Plugin::getPType() |
|
39 | + */ |
|
40 | + public function getPType(): string |
|
41 | + { |
|
42 | + return 'normal'; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * |
|
47 | - * @see Doku_Parser_Mode::getSort() |
|
48 | - */ |
|
49 | - public function getSort(): int |
|
50 | - { |
|
51 | - return 307; |
|
52 | - } |
|
45 | + /** |
|
46 | + * |
|
47 | + * @see Doku_Parser_Mode::getSort() |
|
48 | + */ |
|
49 | + public function getSort(): int |
|
50 | + { |
|
51 | + return 307; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * define our special pattern: {{findnearby>Some linkt text or nothing}}. |
|
56 | - * |
|
57 | - * @see Doku_Parser_Mode::connectTo() |
|
58 | - */ |
|
59 | - public function connectTo($mode): void |
|
60 | - { |
|
61 | - $this->Lexer->addSpecialPattern( |
|
62 | - '\{\{findnearby>.*?\}\}', |
|
63 | - $mode, |
|
64 | - 'plugin_spatialhelper_findnearby' |
|
65 | - ); |
|
66 | - } |
|
54 | + /** |
|
55 | + * define our special pattern: {{findnearby>Some linkt text or nothing}}. |
|
56 | + * |
|
57 | + * @see Doku_Parser_Mode::connectTo() |
|
58 | + */ |
|
59 | + public function connectTo($mode): void |
|
60 | + { |
|
61 | + $this->Lexer->addSpecialPattern( |
|
62 | + '\{\{findnearby>.*?\}\}', |
|
63 | + $mode, |
|
64 | + 'plugin_spatialhelper_findnearby' |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * look up the page's geo metadata and pass that on to render. |
|
70 | - * |
|
71 | - * @param string $match The text matched by the patterns |
|
72 | - * @param int $state The lexer state for the match |
|
73 | - * @param int $pos The character position of the matched text |
|
74 | - * @param Doku_Handler $handler The Doku_Handler object |
|
75 | - * @return bool|array Return an array with all data you want to use in render, false don't add an instruction |
|
76 | - * |
|
77 | - * @see DokuWiki_Syntax_Plugin::handle() |
|
78 | - */ |
|
79 | - public function handle($match, $state, $pos, Doku_Handler $handler) |
|
80 | - { |
|
81 | - $data = array(); |
|
82 | - $data [0] = trim(substr($match, strlen('{{findnearby>'), -2)); |
|
83 | - if (strlen($data [0]) < 1) { |
|
84 | - $data [0] = $this->getLang('search_findnearby'); |
|
85 | - } |
|
86 | - $meta = p_get_metadata(getID(), 'geo'); |
|
87 | - if ($meta) { |
|
88 | - if ($meta ['lat'] && $meta ['lon']) { |
|
89 | - $data [1] = array( |
|
90 | - 'do' => 'findnearby', |
|
91 | - 'lat' => $meta ['lat'], |
|
92 | - 'lon' => $meta ['lon'] |
|
93 | - ); |
|
94 | - } elseif ($meta ['geohash']) { |
|
95 | - $data [1] = array( |
|
96 | - 'do' => 'findnearby', |
|
97 | - 'geohash' => $meta ['geohash'] |
|
98 | - ); |
|
99 | - } |
|
100 | - return $data; |
|
101 | - } |
|
102 | - return false; |
|
103 | - } |
|
68 | + /** |
|
69 | + * look up the page's geo metadata and pass that on to render. |
|
70 | + * |
|
71 | + * @param string $match The text matched by the patterns |
|
72 | + * @param int $state The lexer state for the match |
|
73 | + * @param int $pos The character position of the matched text |
|
74 | + * @param Doku_Handler $handler The Doku_Handler object |
|
75 | + * @return bool|array Return an array with all data you want to use in render, false don't add an instruction |
|
76 | + * |
|
77 | + * @see DokuWiki_Syntax_Plugin::handle() |
|
78 | + */ |
|
79 | + public function handle($match, $state, $pos, Doku_Handler $handler) |
|
80 | + { |
|
81 | + $data = array(); |
|
82 | + $data [0] = trim(substr($match, strlen('{{findnearby>'), -2)); |
|
83 | + if (strlen($data [0]) < 1) { |
|
84 | + $data [0] = $this->getLang('search_findnearby'); |
|
85 | + } |
|
86 | + $meta = p_get_metadata(getID(), 'geo'); |
|
87 | + if ($meta) { |
|
88 | + if ($meta ['lat'] && $meta ['lon']) { |
|
89 | + $data [1] = array( |
|
90 | + 'do' => 'findnearby', |
|
91 | + 'lat' => $meta ['lat'], |
|
92 | + 'lon' => $meta ['lon'] |
|
93 | + ); |
|
94 | + } elseif ($meta ['geohash']) { |
|
95 | + $data [1] = array( |
|
96 | + 'do' => 'findnearby', |
|
97 | + 'geohash' => $meta ['geohash'] |
|
98 | + ); |
|
99 | + } |
|
100 | + return $data; |
|
101 | + } |
|
102 | + return false; |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Render a link to a search page. |
|
107 | - * |
|
108 | - * @see DokuWiki_Syntax_Plugin::render() |
|
109 | - */ |
|
110 | - public function render($format, Doku_Renderer $renderer, $data): bool |
|
111 | - { |
|
112 | - if ($data === false) { |
|
113 | - return false; |
|
114 | - } |
|
105 | + /** |
|
106 | + * Render a link to a search page. |
|
107 | + * |
|
108 | + * @see DokuWiki_Syntax_Plugin::render() |
|
109 | + */ |
|
110 | + public function render($format, Doku_Renderer $renderer, $data): bool |
|
111 | + { |
|
112 | + if ($data === false) { |
|
113 | + return false; |
|
114 | + } |
|
115 | 115 | |
116 | - if ($format === 'xhtml') { |
|
117 | - $renderer->doc .= '<a href="' . wl(getID(), $data [1]) . '" class="findnearby">' . hsc($data [0]) . '</a>'; |
|
118 | - return true; |
|
119 | - } elseif ($format === 'metadata') { |
|
120 | - return false; |
|
121 | - } elseif ($format === 'odt') { |
|
122 | - // don't render anything in ODT |
|
123 | - return false; |
|
124 | - } |
|
125 | - return false; |
|
126 | - } |
|
116 | + if ($format === 'xhtml') { |
|
117 | + $renderer->doc .= '<a href="' . wl(getID(), $data [1]) . '" class="findnearby">' . hsc($data [0]) . '</a>'; |
|
118 | + return true; |
|
119 | + } elseif ($format === 'metadata') { |
|
120 | + return false; |
|
121 | + } elseif ($format === 'odt') { |
|
122 | + // don't render anything in ODT |
|
123 | + return false; |
|
124 | + } |
|
125 | + return false; |
|
126 | + } |
|
127 | 127 | } |
@@ -23,184 +23,184 @@ |
||
23 | 23 | */ |
24 | 24 | class helper_plugin_spatialhelper_search extends DokuWiki_Plugin |
25 | 25 | { |
26 | - /** |
|
27 | - * spatial index. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $spatial_idx = array(); |
|
32 | - /** |
|
33 | - * handle to the geoPHP plugin. |
|
34 | - */ |
|
35 | - protected $geophp; |
|
36 | - /** |
|
37 | - * Precision, Distance of Adjacent Cell in Meters. |
|
38 | - * |
|
39 | - * @see https://stackoverflow.com/questions/13836416/geohash-and-max-distance |
|
40 | - * |
|
41 | - * @var float |
|
42 | - */ |
|
43 | - private $precision = array( |
|
44 | - 5003530, |
|
45 | - 625441, |
|
46 | - 123264, |
|
47 | - 19545, |
|
48 | - 3803, |
|
49 | - 610, |
|
50 | - 118, |
|
51 | - 19, |
|
52 | - 3.7, |
|
53 | - 0.6 |
|
54 | - ); |
|
26 | + /** |
|
27 | + * spatial index. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $spatial_idx = array(); |
|
32 | + /** |
|
33 | + * handle to the geoPHP plugin. |
|
34 | + */ |
|
35 | + protected $geophp; |
|
36 | + /** |
|
37 | + * Precision, Distance of Adjacent Cell in Meters. |
|
38 | + * |
|
39 | + * @see https://stackoverflow.com/questions/13836416/geohash-and-max-distance |
|
40 | + * |
|
41 | + * @var float |
|
42 | + */ |
|
43 | + private $precision = array( |
|
44 | + 5003530, |
|
45 | + 625441, |
|
46 | + 123264, |
|
47 | + 19545, |
|
48 | + 3803, |
|
49 | + 610, |
|
50 | + 118, |
|
51 | + 19, |
|
52 | + 3.7, |
|
53 | + 0.6 |
|
54 | + ); |
|
55 | 55 | |
56 | - /** |
|
57 | - * constructor; initialize/load spatial index. |
|
58 | - */ |
|
59 | - public function __construct() |
|
60 | - { |
|
61 | - // parent::__construct (); |
|
62 | - global $conf; |
|
56 | + /** |
|
57 | + * constructor; initialize/load spatial index. |
|
58 | + */ |
|
59 | + public function __construct() |
|
60 | + { |
|
61 | + // parent::__construct (); |
|
62 | + global $conf; |
|
63 | 63 | |
64 | - if (!$geophp = plugin_load('helper', 'geophp')) { |
|
65 | - $message = 'helper_plugin_spatialhelper_search::spatialhelper_search: geophp plugin is not available.'; |
|
66 | - msg($message, -1); |
|
67 | - } |
|
64 | + if (!$geophp = plugin_load('helper', 'geophp')) { |
|
65 | + $message = 'helper_plugin_spatialhelper_search::spatialhelper_search: geophp plugin is not available.'; |
|
66 | + msg($message, -1); |
|
67 | + } |
|
68 | 68 | |
69 | - $idx_dir = $conf ['indexdir']; |
|
70 | - if (!@file_exists($idx_dir . '/spatial.idx')) { |
|
71 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
72 | - } |
|
69 | + $idx_dir = $conf ['indexdir']; |
|
70 | + if (!@file_exists($idx_dir . '/spatial.idx')) { |
|
71 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
72 | + } |
|
73 | 73 | |
74 | - $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false)); |
|
75 | - } |
|
74 | + $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false)); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Find locations based on the coordinate pair. |
|
79 | - * |
|
80 | - * @param float $lat |
|
81 | - * The y coordinate (or latitude) |
|
82 | - * @param float $lon |
|
83 | - * The x coordinate (or longitude) |
|
84 | - */ |
|
85 | - public function findNearbyLatLon(float $lat, float $lon): array |
|
86 | - { |
|
87 | - $geometry = new Point($lon, $lat); |
|
88 | - return $this->findNearby($geometry->out('geohash'), $geometry); |
|
89 | - } |
|
77 | + /** |
|
78 | + * Find locations based on the coordinate pair. |
|
79 | + * |
|
80 | + * @param float $lat |
|
81 | + * The y coordinate (or latitude) |
|
82 | + * @param float $lon |
|
83 | + * The x coordinate (or longitude) |
|
84 | + */ |
|
85 | + public function findNearbyLatLon(float $lat, float $lon): array |
|
86 | + { |
|
87 | + $geometry = new Point($lon, $lat); |
|
88 | + return $this->findNearby($geometry->out('geohash'), $geometry); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * finds nearby elements in the index based on the geohash. |
|
93 | - * returns a list of documents and the bunding box. |
|
94 | - * |
|
95 | - * @param string $geohash |
|
96 | - * @param Point $p |
|
97 | - * optional point |
|
98 | - * @return array of ... |
|
99 | - */ |
|
100 | - public function findNearby(string $geohash, Point $p = null): array |
|
101 | - { |
|
102 | - $_geohashClass = new Geohash(); |
|
103 | - if (!$p) { |
|
104 | - $decodedPoint = $_geohashClass->read($geohash); |
|
105 | - } else { |
|
106 | - $decodedPoint = $p; |
|
107 | - } |
|
91 | + /** |
|
92 | + * finds nearby elements in the index based on the geohash. |
|
93 | + * returns a list of documents and the bunding box. |
|
94 | + * |
|
95 | + * @param string $geohash |
|
96 | + * @param Point $p |
|
97 | + * optional point |
|
98 | + * @return array of ... |
|
99 | + */ |
|
100 | + public function findNearby(string $geohash, Point $p = null): array |
|
101 | + { |
|
102 | + $_geohashClass = new Geohash(); |
|
103 | + if (!$p) { |
|
104 | + $decodedPoint = $_geohashClass->read($geohash); |
|
105 | + } else { |
|
106 | + $decodedPoint = $p; |
|
107 | + } |
|
108 | 108 | |
109 | - // find adjacent blocks |
|
110 | - $adjacent = array(); |
|
111 | - $adjacent ['center'] = $geohash; |
|
112 | - $adjacent ['top'] = $_geohashClass->adjacent($adjacent ['center'], 'top'); |
|
113 | - $adjacent ['bottom'] = $_geohashClass->adjacent($adjacent ['center'], 'bottom'); |
|
114 | - $adjacent ['right'] = $_geohashClass->adjacent($adjacent ['center'], 'right'); |
|
115 | - $adjacent ['left'] = $_geohashClass->adjacent($adjacent ['center'], 'left'); |
|
116 | - $adjacent ['topleft'] = $_geohashClass->adjacent($adjacent ['left'], 'top'); |
|
117 | - $adjacent ['topright'] = $_geohashClass->adjacent($adjacent ['right'], 'top'); |
|
118 | - $adjacent ['bottomright'] = $_geohashClass->adjacent($adjacent ['right'], 'bottom'); |
|
119 | - $adjacent ['bottomleft'] = $_geohashClass->adjacent($adjacent ['left'], 'bottom'); |
|
120 | - Logger::getInstance(Logger::LOG_DEBUG)->log("adjacent geo hashes", $adjacent); |
|
109 | + // find adjacent blocks |
|
110 | + $adjacent = array(); |
|
111 | + $adjacent ['center'] = $geohash; |
|
112 | + $adjacent ['top'] = $_geohashClass->adjacent($adjacent ['center'], 'top'); |
|
113 | + $adjacent ['bottom'] = $_geohashClass->adjacent($adjacent ['center'], 'bottom'); |
|
114 | + $adjacent ['right'] = $_geohashClass->adjacent($adjacent ['center'], 'right'); |
|
115 | + $adjacent ['left'] = $_geohashClass->adjacent($adjacent ['center'], 'left'); |
|
116 | + $adjacent ['topleft'] = $_geohashClass->adjacent($adjacent ['left'], 'top'); |
|
117 | + $adjacent ['topright'] = $_geohashClass->adjacent($adjacent ['right'], 'top'); |
|
118 | + $adjacent ['bottomright'] = $_geohashClass->adjacent($adjacent ['right'], 'bottom'); |
|
119 | + $adjacent ['bottomleft'] = $_geohashClass->adjacent($adjacent ['left'], 'bottom'); |
|
120 | + Logger::getInstance(Logger::LOG_DEBUG)->log("adjacent geo hashes", $adjacent); |
|
121 | 121 | |
122 | - // find all the pages in the index that overlap with the adjacent hashes |
|
123 | - $docIds = array(); |
|
124 | - foreach ($adjacent as $adjHash) { |
|
125 | - if (is_array($this->spatial_idx)) { |
|
126 | - foreach ($this->spatial_idx as $_geohash => $_docIds) { |
|
127 | - if (strpos($_geohash, $adjHash) !== false) { |
|
128 | - // Logger::getInstance(Logger::LOG_DEBUG) |
|
129 | - // ->log("Found adjacent geo hash: $adjHash in $_geohash" ); |
|
130 | - // if $adjHash similar to geohash |
|
131 | - $docIds = array_merge($docIds, $_docIds); |
|
132 | - } |
|
133 | - } |
|
134 | - } |
|
135 | - } |
|
136 | - $docIds = array_unique($docIds); |
|
137 | - Logger::getInstance(Logger::LOG_DEBUG)->log("found docIDs", $docIds); |
|
122 | + // find all the pages in the index that overlap with the adjacent hashes |
|
123 | + $docIds = array(); |
|
124 | + foreach ($adjacent as $adjHash) { |
|
125 | + if (is_array($this->spatial_idx)) { |
|
126 | + foreach ($this->spatial_idx as $_geohash => $_docIds) { |
|
127 | + if (strpos($_geohash, $adjHash) !== false) { |
|
128 | + // Logger::getInstance(Logger::LOG_DEBUG) |
|
129 | + // ->log("Found adjacent geo hash: $adjHash in $_geohash" ); |
|
130 | + // if $adjHash similar to geohash |
|
131 | + $docIds = array_merge($docIds, $_docIds); |
|
132 | + } |
|
133 | + } |
|
134 | + } |
|
135 | + } |
|
136 | + $docIds = array_unique($docIds); |
|
137 | + Logger::getInstance(Logger::LOG_DEBUG)->log("found docIDs", $docIds); |
|
138 | 138 | |
139 | - // create associative array of pages + calculate distance |
|
140 | - $pages = array(); |
|
141 | - $media = array(); |
|
142 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
139 | + // create associative array of pages + calculate distance |
|
140 | + $pages = array(); |
|
141 | + $media = array(); |
|
142 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
143 | 143 | |
144 | - foreach ($docIds as $id) { |
|
145 | - if (strpos($id, 'media__', 0) === 0) { |
|
146 | - $id = substr($id, strlen('media__')); |
|
147 | - if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) { |
|
148 | - $point = $indexer->getCoordsFromExif($id); |
|
149 | - $line = new LineString( |
|
150 | - [ |
|
151 | - $decodedPoint, |
|
152 | - $point |
|
153 | - ] |
|
154 | - ); |
|
155 | - $media [] = array( |
|
156 | - 'id' => $id, |
|
157 | - 'distance' => (int)($line->greatCircleLength()), |
|
158 | - 'lat' => $point->y(), |
|
159 | - 'lon' => $point->x() |
|
160 | - // optionally add other meta such as tag, description... |
|
161 | - ); |
|
162 | - } |
|
163 | - } else { |
|
164 | - if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) { |
|
165 | - $geotags = p_get_metadata($id, 'geo'); |
|
166 | - $point = new Point($geotags ['lon'], $geotags ['lat']); |
|
167 | - $line = new LineString( |
|
168 | - [ |
|
169 | - $decodedPoint, |
|
170 | - $point |
|
171 | - ] |
|
172 | - ); |
|
173 | - $pages [] = array( |
|
174 | - 'id' => $id, |
|
175 | - 'distance' => (int)($line->greatCircleLength()), |
|
176 | - 'description' => p_get_metadata($id, 'description')['abstract'], |
|
177 | - 'lat' => $geotags ['lat'], |
|
178 | - 'lon' => $geotags ['lon'] |
|
179 | - // optionally add other meta such as tag... |
|
180 | - ); |
|
181 | - } |
|
182 | - } |
|
183 | - } |
|
144 | + foreach ($docIds as $id) { |
|
145 | + if (strpos($id, 'media__', 0) === 0) { |
|
146 | + $id = substr($id, strlen('media__')); |
|
147 | + if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) { |
|
148 | + $point = $indexer->getCoordsFromExif($id); |
|
149 | + $line = new LineString( |
|
150 | + [ |
|
151 | + $decodedPoint, |
|
152 | + $point |
|
153 | + ] |
|
154 | + ); |
|
155 | + $media [] = array( |
|
156 | + 'id' => $id, |
|
157 | + 'distance' => (int)($line->greatCircleLength()), |
|
158 | + 'lat' => $point->y(), |
|
159 | + 'lon' => $point->x() |
|
160 | + // optionally add other meta such as tag, description... |
|
161 | + ); |
|
162 | + } |
|
163 | + } else { |
|
164 | + if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) { |
|
165 | + $geotags = p_get_metadata($id, 'geo'); |
|
166 | + $point = new Point($geotags ['lon'], $geotags ['lat']); |
|
167 | + $line = new LineString( |
|
168 | + [ |
|
169 | + $decodedPoint, |
|
170 | + $point |
|
171 | + ] |
|
172 | + ); |
|
173 | + $pages [] = array( |
|
174 | + 'id' => $id, |
|
175 | + 'distance' => (int)($line->greatCircleLength()), |
|
176 | + 'description' => p_get_metadata($id, 'description')['abstract'], |
|
177 | + 'lat' => $geotags ['lat'], |
|
178 | + 'lon' => $geotags ['lon'] |
|
179 | + // optionally add other meta such as tag... |
|
180 | + ); |
|
181 | + } |
|
182 | + } |
|
183 | + } |
|
184 | 184 | |
185 | - // sort all the pages/media using distance |
|
186 | - usort( |
|
187 | - $pages, static function ($a, $b) { |
|
188 | - return strnatcmp($a ['distance'], $b ['distance']); |
|
189 | - } |
|
190 | - ); |
|
191 | - usort( |
|
192 | - $media, static function ($a, $b) { |
|
193 | - return strnatcmp($a ['distance'], $b ['distance']); |
|
194 | - } |
|
195 | - ); |
|
185 | + // sort all the pages/media using distance |
|
186 | + usort( |
|
187 | + $pages, static function ($a, $b) { |
|
188 | + return strnatcmp($a ['distance'], $b ['distance']); |
|
189 | + } |
|
190 | + ); |
|
191 | + usort( |
|
192 | + $media, static function ($a, $b) { |
|
193 | + return strnatcmp($a ['distance'], $b ['distance']); |
|
194 | + } |
|
195 | + ); |
|
196 | 196 | |
197 | - return array( |
|
198 | - 'pages' => $pages, |
|
199 | - 'media' => $media, |
|
200 | - 'lat' => $decodedPoint->y(), |
|
201 | - 'lon' => $decodedPoint->x(), |
|
202 | - 'geohash' => $geohash, |
|
203 | - 'precision' => $this->precision [strlen($geohash)] |
|
204 | - ); |
|
205 | - } |
|
197 | + return array( |
|
198 | + 'pages' => $pages, |
|
199 | + 'media' => $media, |
|
200 | + 'lat' => $decodedPoint->y(), |
|
201 | + 'lon' => $decodedPoint->x(), |
|
202 | + 'geohash' => $geohash, |
|
203 | + 'precision' => $this->precision [strlen($geohash)] |
|
204 | + ); |
|
205 | + } |
|
206 | 206 | } |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | ); |
155 | 155 | $media [] = array( |
156 | 156 | 'id' => $id, |
157 | - 'distance' => (int)($line->greatCircleLength()), |
|
157 | + 'distance' => ( int ) ($line->greatCircleLength()), |
|
158 | 158 | 'lat' => $point->y(), |
159 | 159 | 'lon' => $point->x() |
160 | 160 | // optionally add other meta such as tag, description... |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | ); |
173 | 173 | $pages [] = array( |
174 | 174 | 'id' => $id, |
175 | - 'distance' => (int)($line->greatCircleLength()), |
|
175 | + 'distance' => ( int ) ($line->greatCircleLength()), |
|
176 | 176 | 'description' => p_get_metadata($id, 'description')['abstract'], |
177 | 177 | 'lat' => $geotags ['lat'], |
178 | 178 | 'lon' => $geotags ['lon'] |
@@ -184,12 +184,12 @@ discard block |
||
184 | 184 | |
185 | 185 | // sort all the pages/media using distance |
186 | 186 | usort( |
187 | - $pages, static function ($a, $b) { |
|
187 | + $pages, static function($a, $b) { |
|
188 | 188 | return strnatcmp($a ['distance'], $b ['distance']); |
189 | 189 | } |
190 | 190 | ); |
191 | 191 | usort( |
192 | - $media, static function ($a, $b) { |
|
192 | + $media, static function($a, $b) { |
|
193 | 193 | return strnatcmp($a ['distance'], $b ['distance']); |
194 | 194 | } |
195 | 195 | ); |
@@ -25,304 +25,304 @@ |
||
25 | 25 | */ |
26 | 26 | class helper_plugin_spatialhelper_index extends DokuWiki_Plugin |
27 | 27 | { |
28 | - /** |
|
29 | - * directory for index files. |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $idx_dir = ''; |
|
28 | + /** |
|
29 | + * directory for index files. |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $idx_dir = ''; |
|
34 | 34 | |
35 | - /** |
|
36 | - * spatial index, well lookup list/array so we can do spatial queries. |
|
37 | - * entries should be: array("geohash" => {"id1","id3",}) |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $spatial_idx = array(); |
|
35 | + /** |
|
36 | + * spatial index, well lookup list/array so we can do spatial queries. |
|
37 | + * entries should be: array("geohash" => {"id1","id3",}) |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $spatial_idx = array(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * handle to the geoPHP plugin. |
|
45 | - */ |
|
46 | - protected $geophp; |
|
43 | + /** |
|
44 | + * handle to the geoPHP plugin. |
|
45 | + */ |
|
46 | + protected $geophp; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor, initialises the spatial index. |
|
50 | - */ |
|
51 | - public function __construct() |
|
52 | - { |
|
53 | - if (!$geophp = plugin_load('helper', 'geophp')) { |
|
54 | - $message = 'helper_plugin_spatialhelper_index::spatialhelper_index: geophp plugin is not available.'; |
|
55 | - msg($message, -1); |
|
56 | - } |
|
48 | + /** |
|
49 | + * Constructor, initialises the spatial index. |
|
50 | + */ |
|
51 | + public function __construct() |
|
52 | + { |
|
53 | + if (!$geophp = plugin_load('helper', 'geophp')) { |
|
54 | + $message = 'helper_plugin_spatialhelper_index::spatialhelper_index: geophp plugin is not available.'; |
|
55 | + msg($message, -1); |
|
56 | + } |
|
57 | 57 | |
58 | - global $conf; |
|
59 | - $this->idx_dir = $conf ['indexdir']; |
|
60 | - // test if there is a spatialindex, if not build one for the wiki |
|
61 | - if (!@file_exists($this->idx_dir . '/spatial.idx')) { |
|
62 | - // creates and stores the index |
|
63 | - $this->generateSpatialIndex(); |
|
64 | - } else { |
|
65 | - $this->spatial_idx = unserialize(io_readFile($this->idx_dir . '/spatial.idx', false)); |
|
66 | - Logger::getInstance(Logger::LOG_DEBUG)->log('done loading spatial index', $this->spatial_idx); |
|
67 | - } |
|
68 | - } |
|
58 | + global $conf; |
|
59 | + $this->idx_dir = $conf ['indexdir']; |
|
60 | + // test if there is a spatialindex, if not build one for the wiki |
|
61 | + if (!@file_exists($this->idx_dir . '/spatial.idx')) { |
|
62 | + // creates and stores the index |
|
63 | + $this->generateSpatialIndex(); |
|
64 | + } else { |
|
65 | + $this->spatial_idx = unserialize(io_readFile($this->idx_dir . '/spatial.idx', false)); |
|
66 | + Logger::getInstance(Logger::LOG_DEBUG)->log('done loading spatial index', $this->spatial_idx); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * (re-)Generates the spatial index by running through all the pages in the wiki. |
|
72 | - * |
|
73 | - * @todo add an option to erase the old index |
|
74 | - */ |
|
75 | - public function generateSpatialIndex(): bool |
|
76 | - { |
|
77 | - global $conf; |
|
78 | - require_once(DOKU_INC . 'inc/search.php'); |
|
79 | - $pages = array(); |
|
80 | - search($pages, $conf ['datadir'], 'search_allpages', array()); |
|
81 | - foreach ($pages as $page) { |
|
82 | - $this->updateSpatialIndex($page ['id']); |
|
83 | - } |
|
84 | - // media |
|
85 | - $media = array(); |
|
86 | - search($media, $conf ['mediadir'], 'search_media', array()); |
|
87 | - foreach ($media as $medium) { |
|
88 | - if ($medium ['isimg']) { |
|
89 | - $this->indexImage($medium); |
|
90 | - } |
|
91 | - } |
|
92 | - return true; |
|
93 | - } |
|
70 | + /** |
|
71 | + * (re-)Generates the spatial index by running through all the pages in the wiki. |
|
72 | + * |
|
73 | + * @todo add an option to erase the old index |
|
74 | + */ |
|
75 | + public function generateSpatialIndex(): bool |
|
76 | + { |
|
77 | + global $conf; |
|
78 | + require_once(DOKU_INC . 'inc/search.php'); |
|
79 | + $pages = array(); |
|
80 | + search($pages, $conf ['datadir'], 'search_allpages', array()); |
|
81 | + foreach ($pages as $page) { |
|
82 | + $this->updateSpatialIndex($page ['id']); |
|
83 | + } |
|
84 | + // media |
|
85 | + $media = array(); |
|
86 | + search($media, $conf ['mediadir'], 'search_media', array()); |
|
87 | + foreach ($media as $medium) { |
|
88 | + if ($medium ['isimg']) { |
|
89 | + $this->indexImage($medium); |
|
90 | + } |
|
91 | + } |
|
92 | + return true; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Update the spatial index for the page. |
|
97 | - * |
|
98 | - * @param string $id |
|
99 | - * the document ID |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function updateSpatialIndex(string $id): bool |
|
103 | - { |
|
104 | - $geotags = p_get_metadata($id, 'geo'); |
|
105 | - if (empty ($geotags)) { |
|
106 | - return false; |
|
107 | - } |
|
108 | - if (empty ($geotags ['lon']) || empty ($geotags ['lat'])) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - Logger::getInstance(Logger::LOG_DEBUG)->log("Geo metadata found for page $id", $geotags); |
|
112 | - $geometry = new Point($geotags ['lon'], $geotags ['lat']); |
|
113 | - $geohash = $geometry->out('geohash'); |
|
114 | - Logger::getInstance(Logger::LOG_DEBUG)->log("Update index for geohash: $geohash"); |
|
115 | - return $this->addToIndex($geohash, $id); |
|
116 | - } |
|
95 | + /** |
|
96 | + * Update the spatial index for the page. |
|
97 | + * |
|
98 | + * @param string $id |
|
99 | + * the document ID |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function updateSpatialIndex(string $id): bool |
|
103 | + { |
|
104 | + $geotags = p_get_metadata($id, 'geo'); |
|
105 | + if (empty ($geotags)) { |
|
106 | + return false; |
|
107 | + } |
|
108 | + if (empty ($geotags ['lon']) || empty ($geotags ['lat'])) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + Logger::getInstance(Logger::LOG_DEBUG)->log("Geo metadata found for page $id", $geotags); |
|
112 | + $geometry = new Point($geotags ['lon'], $geotags ['lat']); |
|
113 | + $geohash = $geometry->out('geohash'); |
|
114 | + Logger::getInstance(Logger::LOG_DEBUG)->log("Update index for geohash: $geohash"); |
|
115 | + return $this->addToIndex($geohash, $id); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Store the hash/id entry in the index. |
|
120 | - * |
|
121 | - * @param string $geohash |
|
122 | - * @param string $id |
|
123 | - * page or media id |
|
124 | - * @return bool true when succesful |
|
125 | - */ |
|
126 | - private function addToIndex(string $geohash, string $id): bool |
|
127 | - { |
|
128 | - $pageIds = array(); |
|
129 | - // check index for key/geohash |
|
130 | - if (!array_key_exists($geohash, $this->spatial_idx)) { |
|
131 | - Logger::getInstance(Logger::LOG_DEBUG)->log("Geohash $geohash not in index, just add $id."); |
|
132 | - $pageIds [] = $id; |
|
133 | - } else { |
|
134 | - Logger::getInstance(Logger::LOG_DEBUG)->log('Geohash for document is in index, find it.'); |
|
135 | - // check the index for document |
|
136 | - $knownHashes = $this->findHashesForId($id, $this->spatial_idx); |
|
137 | - if (empty ($knownHashes)) { |
|
138 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
139 | - "No index record found for document $id, just add" |
|
140 | - ); |
|
141 | - $pageIds = $this->spatial_idx [$geohash]; |
|
142 | - $pageIds [] = $id; |
|
143 | - } |
|
144 | - // TODO shortcut, need to make sure there is only one element, if not the index is corrupt |
|
145 | - $knownHash = $knownHashes [0]; |
|
118 | + /** |
|
119 | + * Store the hash/id entry in the index. |
|
120 | + * |
|
121 | + * @param string $geohash |
|
122 | + * @param string $id |
|
123 | + * page or media id |
|
124 | + * @return bool true when succesful |
|
125 | + */ |
|
126 | + private function addToIndex(string $geohash, string $id): bool |
|
127 | + { |
|
128 | + $pageIds = array(); |
|
129 | + // check index for key/geohash |
|
130 | + if (!array_key_exists($geohash, $this->spatial_idx)) { |
|
131 | + Logger::getInstance(Logger::LOG_DEBUG)->log("Geohash $geohash not in index, just add $id."); |
|
132 | + $pageIds [] = $id; |
|
133 | + } else { |
|
134 | + Logger::getInstance(Logger::LOG_DEBUG)->log('Geohash for document is in index, find it.'); |
|
135 | + // check the index for document |
|
136 | + $knownHashes = $this->findHashesForId($id, $this->spatial_idx); |
|
137 | + if (empty ($knownHashes)) { |
|
138 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
139 | + "No index record found for document $id, just add" |
|
140 | + ); |
|
141 | + $pageIds = $this->spatial_idx [$geohash]; |
|
142 | + $pageIds [] = $id; |
|
143 | + } |
|
144 | + // TODO shortcut, need to make sure there is only one element, if not the index is corrupt |
|
145 | + $knownHash = $knownHashes [0]; |
|
146 | 146 | |
147 | - if ($knownHash === $geohash) { |
|
148 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
149 | - "Document $id was found in index and has the same geohash, nothing to do." |
|
150 | - ); |
|
151 | - return true; |
|
152 | - } |
|
147 | + if ($knownHash === $geohash) { |
|
148 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
149 | + "Document $id was found in index and has the same geohash, nothing to do." |
|
150 | + ); |
|
151 | + return true; |
|
152 | + } |
|
153 | 153 | |
154 | - if (!empty ($knownHash)) { |
|
155 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
156 | - "Document/media $id was found in index but has different geohash (it moved)." |
|
157 | - ); |
|
158 | - $knownIds = $this->spatial_idx [$knownHash]; |
|
159 | - Logger::getInstance(Logger::LOG_DEBUG)->log("Known id's for this hash", $knownIds); |
|
160 | - // remove it from the old geohash element |
|
161 | - $i = array_search($id, $knownIds); |
|
162 | - Logger::getInstance(Logger::LOG_DEBUG)->log('Unsetting:' . $knownIds [$i]); |
|
163 | - unset ($knownIds [$i]); |
|
164 | - $this->spatial_idx [$knownHash] = $knownIds; |
|
165 | - // set on new geohash element |
|
166 | - $pageIds = $this->spatial_idx [$geohash]; |
|
167 | - $pageIds [] = $id; |
|
168 | - } |
|
169 | - } |
|
170 | - // store and save |
|
171 | - $this->spatial_idx [$geohash] = $pageIds; |
|
172 | - return $this->saveIndex(); |
|
173 | - } |
|
154 | + if (!empty ($knownHash)) { |
|
155 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
156 | + "Document/media $id was found in index but has different geohash (it moved)." |
|
157 | + ); |
|
158 | + $knownIds = $this->spatial_idx [$knownHash]; |
|
159 | + Logger::getInstance(Logger::LOG_DEBUG)->log("Known id's for this hash", $knownIds); |
|
160 | + // remove it from the old geohash element |
|
161 | + $i = array_search($id, $knownIds); |
|
162 | + Logger::getInstance(Logger::LOG_DEBUG)->log('Unsetting:' . $knownIds [$i]); |
|
163 | + unset ($knownIds [$i]); |
|
164 | + $this->spatial_idx [$knownHash] = $knownIds; |
|
165 | + // set on new geohash element |
|
166 | + $pageIds = $this->spatial_idx [$geohash]; |
|
167 | + $pageIds [] = $id; |
|
168 | + } |
|
169 | + } |
|
170 | + // store and save |
|
171 | + $this->spatial_idx [$geohash] = $pageIds; |
|
172 | + return $this->saveIndex(); |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Looks up the geohash(es) for the document in the index. |
|
177 | - * |
|
178 | - * @param String $id |
|
179 | - * document ID |
|
180 | - * @param array $index |
|
181 | - * spatial index |
|
182 | - */ |
|
183 | - public function findHashesForId(string $id, array $index): array |
|
184 | - { |
|
185 | - $hashes = array(); |
|
186 | - foreach ($index as $hash => $docIds) { |
|
187 | - if (in_array($id, $docIds, false)) { |
|
188 | - $hashes [] = $hash; |
|
189 | - } |
|
190 | - } |
|
191 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
192 | - "Found the following hashes for $id (should only be 1)", |
|
193 | - $hashes |
|
194 | - ); |
|
195 | - return $hashes; |
|
196 | - } |
|
175 | + /** |
|
176 | + * Looks up the geohash(es) for the document in the index. |
|
177 | + * |
|
178 | + * @param String $id |
|
179 | + * document ID |
|
180 | + * @param array $index |
|
181 | + * spatial index |
|
182 | + */ |
|
183 | + public function findHashesForId(string $id, array $index): array |
|
184 | + { |
|
185 | + $hashes = array(); |
|
186 | + foreach ($index as $hash => $docIds) { |
|
187 | + if (in_array($id, $docIds, false)) { |
|
188 | + $hashes [] = $hash; |
|
189 | + } |
|
190 | + } |
|
191 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
192 | + "Found the following hashes for $id (should only be 1)", |
|
193 | + $hashes |
|
194 | + ); |
|
195 | + return $hashes; |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Save spatial index. |
|
200 | - */ |
|
201 | - private function saveIndex(): bool |
|
202 | - { |
|
203 | - return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx)); |
|
204 | - } |
|
198 | + /** |
|
199 | + * Save spatial index. |
|
200 | + */ |
|
201 | + private function saveIndex(): bool |
|
202 | + { |
|
203 | + return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx)); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Add an index entry for this file having EXIF / IPTC data. |
|
208 | - * |
|
209 | - * @param $img |
|
210 | - * a Dokuwiki image |
|
211 | - * @return bool true when image was succesfully added to the index. |
|
212 | - * @throws Exception |
|
213 | - * @see http://www.php.net/manual/en/function.iptcparse.php |
|
214 | - * @see http://php.net/manual/en/function.exif-read-data.php |
|
215 | - * |
|
216 | - */ |
|
217 | - public function indexImage($img): bool |
|
218 | - { |
|
219 | - // test for supported files (jpeg only) |
|
220 | - if ( |
|
221 | - (substr($img ['file'], -strlen('.jpg')) !== '.jpg') && |
|
222 | - (substr($img ['file'], -strlen('.jpeg')) !== '.jpeg')) { |
|
223 | - return false; |
|
224 | - } |
|
206 | + /** |
|
207 | + * Add an index entry for this file having EXIF / IPTC data. |
|
208 | + * |
|
209 | + * @param $img |
|
210 | + * a Dokuwiki image |
|
211 | + * @return bool true when image was succesfully added to the index. |
|
212 | + * @throws Exception |
|
213 | + * @see http://www.php.net/manual/en/function.iptcparse.php |
|
214 | + * @see http://php.net/manual/en/function.exif-read-data.php |
|
215 | + * |
|
216 | + */ |
|
217 | + public function indexImage($img): bool |
|
218 | + { |
|
219 | + // test for supported files (jpeg only) |
|
220 | + if ( |
|
221 | + (substr($img ['file'], -strlen('.jpg')) !== '.jpg') && |
|
222 | + (substr($img ['file'], -strlen('.jpeg')) !== '.jpeg')) { |
|
223 | + return false; |
|
224 | + } |
|
225 | 225 | |
226 | - $geometry = $this->getCoordsFromExif($img ['id']); |
|
227 | - if (!$geometry) { |
|
228 | - return false; |
|
229 | - } |
|
230 | - $geohash = $geometry->out('geohash'); |
|
231 | - // TODO truncate the geohash to something reasonable, otherwise they are |
|
232 | - // useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too |
|
233 | - // precise, limit at ~9 as most GPS are not submeter accurate |
|
234 | - return $this->addToIndex($geohash, 'media__' . $img ['id']); |
|
235 | - } |
|
226 | + $geometry = $this->getCoordsFromExif($img ['id']); |
|
227 | + if (!$geometry) { |
|
228 | + return false; |
|
229 | + } |
|
230 | + $geohash = $geometry->out('geohash'); |
|
231 | + // TODO truncate the geohash to something reasonable, otherwise they are |
|
232 | + // useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too |
|
233 | + // precise, limit at ~9 as most GPS are not submeter accurate |
|
234 | + return $this->addToIndex($geohash, 'media__' . $img ['id']); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * retrieve GPS decimal coordinates from exif. |
|
239 | - * |
|
240 | - * @param string $id |
|
241 | - * @return Point|false |
|
242 | - * @throws Exception |
|
243 | - */ |
|
244 | - public function getCoordsFromExif(string $id) |
|
245 | - { |
|
246 | - $exif = exif_read_data(mediaFN($id), 0, true); |
|
247 | - if (empty ($exif ['GPS'])) { |
|
248 | - return false; |
|
249 | - } |
|
237 | + /** |
|
238 | + * retrieve GPS decimal coordinates from exif. |
|
239 | + * |
|
240 | + * @param string $id |
|
241 | + * @return Point|false |
|
242 | + * @throws Exception |
|
243 | + */ |
|
244 | + public function getCoordsFromExif(string $id) |
|
245 | + { |
|
246 | + $exif = exif_read_data(mediaFN($id), 0, true); |
|
247 | + if (empty ($exif ['GPS'])) { |
|
248 | + return false; |
|
249 | + } |
|
250 | 250 | |
251 | - $lat = $this->convertDMStoD( |
|
252 | - array( |
|
253 | - $exif ['GPS'] ['GPSLatitude'] [0], |
|
254 | - $exif ['GPS'] ['GPSLatitude'] [1], |
|
255 | - $exif ['GPS'] ['GPSLatitude'] [2], |
|
256 | - $exif ['GPS'] ['GPSLatitudeRef'] |
|
257 | - ) |
|
258 | - ); |
|
251 | + $lat = $this->convertDMStoD( |
|
252 | + array( |
|
253 | + $exif ['GPS'] ['GPSLatitude'] [0], |
|
254 | + $exif ['GPS'] ['GPSLatitude'] [1], |
|
255 | + $exif ['GPS'] ['GPSLatitude'] [2], |
|
256 | + $exif ['GPS'] ['GPSLatitudeRef'] |
|
257 | + ) |
|
258 | + ); |
|
259 | 259 | |
260 | - $lon = $this->convertDMStoD( |
|
261 | - array( |
|
262 | - $exif ['GPS'] ['GPSLongitude'] [0], |
|
263 | - $exif ['GPS'] ['GPSLongitude'] [1], |
|
264 | - $exif ['GPS'] ['GPSLongitude'] [2], |
|
265 | - $exif ['GPS'] ['GPSLongitudeRef'] |
|
266 | - ) |
|
267 | - ); |
|
260 | + $lon = $this->convertDMStoD( |
|
261 | + array( |
|
262 | + $exif ['GPS'] ['GPSLongitude'] [0], |
|
263 | + $exif ['GPS'] ['GPSLongitude'] [1], |
|
264 | + $exif ['GPS'] ['GPSLongitude'] [2], |
|
265 | + $exif ['GPS'] ['GPSLongitudeRef'] |
|
266 | + ) |
|
267 | + ); |
|
268 | 268 | |
269 | - return new Point($lon, $lat); |
|
270 | - } |
|
269 | + return new Point($lon, $lat); |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * convert DegreesMinutesSeconds to Decimal degrees. |
|
274 | - * |
|
275 | - * @param array $param array of rational DMS |
|
276 | - * @return float |
|
277 | - */ |
|
278 | - public function convertDMStoD(array $param): float |
|
279 | - { |
|
280 | - if (!is_array($param)) { |
|
281 | - $param = array($param); |
|
282 | - } |
|
283 | - $deg = $this->convertRationaltoFloat($param [0]); |
|
284 | - $min = $this->convertRationaltoFloat($param [1]) / 60; |
|
285 | - $sec = $this->convertRationaltoFloat($param [2]) / 60 / 60; |
|
286 | - // Hemisphere (N, S, W or E) |
|
287 | - $hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1; |
|
288 | - return $hem * ($deg + $min + $sec); |
|
289 | - } |
|
272 | + /** |
|
273 | + * convert DegreesMinutesSeconds to Decimal degrees. |
|
274 | + * |
|
275 | + * @param array $param array of rational DMS |
|
276 | + * @return float |
|
277 | + */ |
|
278 | + public function convertDMStoD(array $param): float |
|
279 | + { |
|
280 | + if (!is_array($param)) { |
|
281 | + $param = array($param); |
|
282 | + } |
|
283 | + $deg = $this->convertRationaltoFloat($param [0]); |
|
284 | + $min = $this->convertRationaltoFloat($param [1]) / 60; |
|
285 | + $sec = $this->convertRationaltoFloat($param [2]) / 60 / 60; |
|
286 | + // Hemisphere (N, S, W or E) |
|
287 | + $hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1; |
|
288 | + return $hem * ($deg + $min + $sec); |
|
289 | + } |
|
290 | 290 | |
291 | - public function convertRationaltoFloat($param): float |
|
292 | - { |
|
293 | - // rational64u |
|
294 | - $nums = explode('/', $param); |
|
295 | - if ((int)$nums[1] > 0) { |
|
296 | - return (float)$nums[0] / (int)$nums[1]; |
|
297 | - } else { |
|
298 | - return (float)$nums[0]; |
|
299 | - } |
|
300 | - } |
|
291 | + public function convertRationaltoFloat($param): float |
|
292 | + { |
|
293 | + // rational64u |
|
294 | + $nums = explode('/', $param); |
|
295 | + if ((int)$nums[1] > 0) { |
|
296 | + return (float)$nums[0] / (int)$nums[1]; |
|
297 | + } else { |
|
298 | + return (float)$nums[0]; |
|
299 | + } |
|
300 | + } |
|
301 | 301 | |
302 | - /** |
|
303 | - * Deletes the page from the index. |
|
304 | - * |
|
305 | - * @param string $id document ID |
|
306 | - */ |
|
307 | - public function deleteFromIndex(string $id): void |
|
308 | - { |
|
309 | - // check the index for document |
|
310 | - $knownHashes = $this->findHashesForId($id, $this->spatial_idx); |
|
311 | - if (empty ($knownHashes)) { |
|
312 | - return; |
|
313 | - } |
|
302 | + /** |
|
303 | + * Deletes the page from the index. |
|
304 | + * |
|
305 | + * @param string $id document ID |
|
306 | + */ |
|
307 | + public function deleteFromIndex(string $id): void |
|
308 | + { |
|
309 | + // check the index for document |
|
310 | + $knownHashes = $this->findHashesForId($id, $this->spatial_idx); |
|
311 | + if (empty ($knownHashes)) { |
|
312 | + return; |
|
313 | + } |
|
314 | 314 | |
315 | - // TODO shortcut, need to make sure there is only one element, if not the index is corrupt |
|
316 | - $knownHash = $knownHashes [0]; |
|
317 | - $knownIds = $this->spatial_idx [$knownHash]; |
|
318 | - $i = array_search($id, $knownIds); |
|
319 | - Logger::getInstance(Logger::LOG_DEBUG)->log("removing: $knownIds[$i] from the index."); |
|
320 | - unset ($knownIds [$i]); |
|
321 | - $this->spatial_idx [$knownHash] = $knownIds; |
|
322 | - if (empty ($this->spatial_idx [$knownHash])) { |
|
323 | - // Logger::getInstance(Logger::LOG_DEBUG)->log ( "removing key: $knownHash from the index." ); |
|
324 | - unset ($this->spatial_idx [$knownHash]); |
|
325 | - } |
|
326 | - $this->saveIndex(); |
|
327 | - } |
|
315 | + // TODO shortcut, need to make sure there is only one element, if not the index is corrupt |
|
316 | + $knownHash = $knownHashes [0]; |
|
317 | + $knownIds = $this->spatial_idx [$knownHash]; |
|
318 | + $i = array_search($id, $knownIds); |
|
319 | + Logger::getInstance(Logger::LOG_DEBUG)->log("removing: $knownIds[$i] from the index."); |
|
320 | + unset ($knownIds [$i]); |
|
321 | + $this->spatial_idx [$knownHash] = $knownIds; |
|
322 | + if (empty ($this->spatial_idx [$knownHash])) { |
|
323 | + // Logger::getInstance(Logger::LOG_DEBUG)->log ( "removing key: $knownHash from the index." ); |
|
324 | + unset ($this->spatial_idx [$knownHash]); |
|
325 | + } |
|
326 | + $this->saveIndex(); |
|
327 | + } |
|
328 | 328 | } |
@@ -292,10 +292,10 @@ |
||
292 | 292 | { |
293 | 293 | // rational64u |
294 | 294 | $nums = explode('/', $param); |
295 | - if ((int)$nums[1] > 0) { |
|
296 | - return (float)$nums[0] / (int)$nums[1]; |
|
295 | + if (( int ) $nums[1] > 0) { |
|
296 | + return ( float ) $nums[0] / ( int ) $nums[1]; |
|
297 | 297 | } else { |
298 | - return (float)$nums[0]; |
|
298 | + return ( float ) $nums[0]; |
|
299 | 299 | } |
300 | 300 | } |
301 | 301 |
@@ -23,228 +23,228 @@ |
||
23 | 23 | */ |
24 | 24 | class helper_plugin_spatialhelper_sitemap extends DokuWiki_Plugin |
25 | 25 | { |
26 | - /** |
|
27 | - * spatial index. |
|
28 | - */ |
|
29 | - private $spatial_idx; |
|
30 | - |
|
31 | - /** |
|
32 | - * constructor, load spatial index. |
|
33 | - */ |
|
34 | - public function __construct() |
|
35 | - { |
|
36 | - global $conf; |
|
37 | - $idx_dir = $conf['indexdir']; |
|
38 | - if (!@file_exists($idx_dir . '/spatial.idx')) { |
|
39 | - $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
40 | - $indexer->generateSpatialIndex(); |
|
41 | - } |
|
42 | - $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false)); |
|
43 | - } |
|
44 | - |
|
45 | - public function getMethods(): array |
|
46 | - { |
|
47 | - $result[] = array( |
|
48 | - 'name' => 'createGeoRSSSitemap', |
|
49 | - 'desc' => 'create a spatial sitemap in GeoRSS format.', |
|
50 | - 'params' => array( |
|
51 | - 'path' => 'string' |
|
52 | - ), |
|
53 | - 'return' => array( |
|
54 | - 'success' => 'boolean' |
|
55 | - ) |
|
56 | - ); |
|
57 | - $result[] = array( |
|
58 | - 'name' => 'createKMLSitemap', |
|
59 | - 'desc' => 'create a spatial sitemap in KML format.', |
|
60 | - 'params' => array( |
|
61 | - 'path' => 'string' |
|
62 | - ), |
|
63 | - 'return' => array( |
|
64 | - 'success' => 'boolean' |
|
65 | - ) |
|
66 | - ); |
|
67 | - return $result; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Create a GeoRSS Simple sitemap (Atom). |
|
72 | - * |
|
73 | - * @param string $mediaID id |
|
74 | - * for the GeoRSS file |
|
75 | - */ |
|
76 | - public function createGeoRSSSitemap(string $mediaID): bool |
|
77 | - { |
|
78 | - global $conf; |
|
79 | - $namespace = getNS($mediaID); |
|
80 | - |
|
81 | - $idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ','; |
|
82 | - |
|
83 | - $RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
|
84 | - $RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" '; |
|
85 | - $RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF; |
|
86 | - $RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF; |
|
87 | - if (!empty($conf['tagline'])) { |
|
88 | - $RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF; |
|
89 | - } |
|
90 | - $RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF; |
|
91 | - $RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF; |
|
92 | - $RSSstart .= '<link href="' . ml($mediaID, '', true, '&', true) . '" rel="self" />'; |
|
93 | - $RSSstart .= DOKU_LF . '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF; |
|
94 | - $RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH) |
|
95 | - . '</id>' . DOKU_LF; |
|
96 | - $RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF; |
|
97 | - |
|
98 | - $RSSend = '</feed>' . DOKU_LF; |
|
99 | - |
|
100 | - io_createNamespace($mediaID, 'media'); |
|
101 | - @touch(mediaFN($mediaID)); |
|
102 | - @chmod(mediaFN($mediaID), $conf['fmode']); |
|
103 | - $fh = fopen(mediaFN($mediaID), 'w'); |
|
104 | - fwrite($fh, $RSSstart); |
|
105 | - |
|
106 | - foreach ($this->spatial_idx as $idxEntry) { |
|
107 | - // get list of id's |
|
108 | - foreach ($idxEntry as $id) { |
|
109 | - // for document item in the index |
|
110 | - if (strpos($id, 'media__', 0) !== 0) { |
|
111 | - if ($this->skipPage($id, $namespace)) { |
|
112 | - continue; |
|
113 | - } |
|
114 | - |
|
115 | - $meta = p_get_metadata($id); |
|
116 | - |
|
117 | - // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
|
118 | - $desc = strip_tags($meta['description']['abstract']); |
|
119 | - |
|
120 | - $entry = '<entry>' . DOKU_LF; |
|
121 | - $entry .= ' <title>' . $meta['title'] . '</title>' . DOKU_LF; |
|
122 | - $entry .= ' <summary>' . $desc . '</summary>' . DOKU_LF; |
|
123 | - $entry .= ' <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon'] |
|
124 | - . '</georss:point>' . DOKU_LF; |
|
125 | - if ($meta['geo']['alt']) { |
|
126 | - $entry .= ' <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF; |
|
127 | - } |
|
128 | - $entry .= ' <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF; |
|
129 | - if (empty($meta['creator'])) { |
|
130 | - $meta['creator'] = $conf['title']; |
|
131 | - } |
|
132 | - $entry .= ' <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF; |
|
133 | - $entry .= ' <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF; |
|
134 | - $entry .= ' <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF; |
|
135 | - $entry .= ' <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':' |
|
136 | - . parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF; |
|
137 | - $entry .= '</entry>' . DOKU_LF; |
|
138 | - fwrite($fh, $entry); |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - fwrite($fh, $RSSend); |
|
144 | - return fclose($fh); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * will return true for non-public or hidden pages or pages that are not below or in the namespace. |
|
149 | - */ |
|
150 | - private function skipPage(string $id, string $namespace): bool |
|
151 | - { |
|
152 | - Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
153 | - "helper_plugin_spatialhelper_sitemap::skipPage, check for $id in $namespace" |
|
154 | - ); |
|
155 | - if (isHiddenPage($id)) { |
|
156 | - return true; |
|
157 | - } |
|
158 | - if (auth_aclcheck($id, '', null) < AUTH_READ) { |
|
159 | - return true; |
|
160 | - } |
|
161 | - |
|
162 | - if (!empty($namespace)) { |
|
163 | - // only if id is in or below namespace |
|
164 | - if (0 !== strpos(getNS($id), $namespace)) { |
|
165 | - // Logger::getInstance(Logger::LOG_DEBUG)->log("helper_plugin_spatialhelper_sitemap::skipPage, |
|
166 | - // skipping $id, not in $namespace"); |
|
167 | - return true; |
|
168 | - } |
|
169 | - } |
|
170 | - return false; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Create a KML sitemap. |
|
175 | - * |
|
176 | - * @param string $mediaID id for the KML file |
|
177 | - */ |
|
178 | - public function createKMLSitemap(string $mediaID): bool |
|
179 | - { |
|
180 | - global $conf; |
|
181 | - $namespace = getNS($mediaID); |
|
182 | - |
|
183 | - $KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
|
184 | - $KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" '; |
|
185 | - $KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
186 | - $KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"'; |
|
187 | - $KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 '; |
|
188 | - $KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF; |
|
189 | - $KMLstart .= '<Document id="root_doc">' . DOKU_LF; |
|
190 | - $KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF; |
|
191 | - $KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF; |
|
192 | - $KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF; |
|
193 | - $KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>'; |
|
194 | - $KMLstart .= '<Icon><href>' |
|
195 | - . DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF; |
|
196 | - |
|
197 | - $KMLend = '</Document>' . DOKU_LF . '</kml>'; |
|
198 | - |
|
199 | - io_createNamespace($mediaID, 'media'); |
|
200 | - @touch(mediaFN($mediaID)); |
|
201 | - @chmod(mediaFN($mediaID), $conf['fmode']); |
|
202 | - |
|
203 | - $fh = fopen(mediaFN($mediaID), 'w'); |
|
204 | - fwrite($fh, $KMLstart); |
|
205 | - |
|
206 | - foreach ($this->spatial_idx as $idxEntry) { |
|
207 | - // get list of id's |
|
208 | - foreach ($idxEntry as $id) { |
|
209 | - // for document item in the index |
|
210 | - if (strpos($id, 'media__', 0) !== 0) { |
|
211 | - if ($this->skipPage($id, $namespace)) { |
|
212 | - continue; |
|
213 | - } |
|
214 | - |
|
215 | - $meta = p_get_metadata($id); |
|
216 | - |
|
217 | - // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
|
218 | - $desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>'; |
|
219 | - $desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] |
|
220 | - . '</a></p>'; |
|
221 | - |
|
222 | - // create an entry and store it |
|
223 | - $plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF; |
|
224 | - $plcm .= ' <name>' . $meta['title'] . '</name>' . DOKU_LF; |
|
225 | - // TODO escape quotes in: title="' . $meta['title'] . '" |
|
226 | - $plcm .= ' <atom:link href="' . wl($id, '' . true); |
|
227 | - $plcm .= '" rel="alternate" type="text/html" />' . DOKU_LF; |
|
228 | - if (!empty($meta['creator'])) { |
|
229 | - $plcm .= ' <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>' |
|
230 | - . DOKU_LF; |
|
231 | - } |
|
232 | - |
|
233 | - $plcm .= ' <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF; |
|
234 | - $plcm .= ' <styleUrl>#icon</styleUrl>' . DOKU_LF; |
|
235 | - |
|
236 | - $plcm .= ' <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat']; |
|
237 | - if ($meta['geo']['alt']) { |
|
238 | - $plcm .= ',' . $meta['geo']['alt']; |
|
239 | - } |
|
240 | - $plcm .= '</coordinates></Point>' . DOKU_LF; |
|
241 | - $plcm .= '</Placemark>' . DOKU_LF; |
|
242 | - |
|
243 | - fwrite($fh, $plcm); |
|
244 | - } |
|
245 | - } |
|
246 | - } |
|
247 | - fwrite($fh, $KMLend); |
|
248 | - return fclose($fh); |
|
249 | - } |
|
26 | + /** |
|
27 | + * spatial index. |
|
28 | + */ |
|
29 | + private $spatial_idx; |
|
30 | + |
|
31 | + /** |
|
32 | + * constructor, load spatial index. |
|
33 | + */ |
|
34 | + public function __construct() |
|
35 | + { |
|
36 | + global $conf; |
|
37 | + $idx_dir = $conf['indexdir']; |
|
38 | + if (!@file_exists($idx_dir . '/spatial.idx')) { |
|
39 | + $indexer = plugin_load('helper', 'spatialhelper_index'); |
|
40 | + $indexer->generateSpatialIndex(); |
|
41 | + } |
|
42 | + $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false)); |
|
43 | + } |
|
44 | + |
|
45 | + public function getMethods(): array |
|
46 | + { |
|
47 | + $result[] = array( |
|
48 | + 'name' => 'createGeoRSSSitemap', |
|
49 | + 'desc' => 'create a spatial sitemap in GeoRSS format.', |
|
50 | + 'params' => array( |
|
51 | + 'path' => 'string' |
|
52 | + ), |
|
53 | + 'return' => array( |
|
54 | + 'success' => 'boolean' |
|
55 | + ) |
|
56 | + ); |
|
57 | + $result[] = array( |
|
58 | + 'name' => 'createKMLSitemap', |
|
59 | + 'desc' => 'create a spatial sitemap in KML format.', |
|
60 | + 'params' => array( |
|
61 | + 'path' => 'string' |
|
62 | + ), |
|
63 | + 'return' => array( |
|
64 | + 'success' => 'boolean' |
|
65 | + ) |
|
66 | + ); |
|
67 | + return $result; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Create a GeoRSS Simple sitemap (Atom). |
|
72 | + * |
|
73 | + * @param string $mediaID id |
|
74 | + * for the GeoRSS file |
|
75 | + */ |
|
76 | + public function createGeoRSSSitemap(string $mediaID): bool |
|
77 | + { |
|
78 | + global $conf; |
|
79 | + $namespace = getNS($mediaID); |
|
80 | + |
|
81 | + $idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ','; |
|
82 | + |
|
83 | + $RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
|
84 | + $RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" '; |
|
85 | + $RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF; |
|
86 | + $RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF; |
|
87 | + if (!empty($conf['tagline'])) { |
|
88 | + $RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF; |
|
89 | + } |
|
90 | + $RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF; |
|
91 | + $RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF; |
|
92 | + $RSSstart .= '<link href="' . ml($mediaID, '', true, '&', true) . '" rel="self" />'; |
|
93 | + $RSSstart .= DOKU_LF . '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF; |
|
94 | + $RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH) |
|
95 | + . '</id>' . DOKU_LF; |
|
96 | + $RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF; |
|
97 | + |
|
98 | + $RSSend = '</feed>' . DOKU_LF; |
|
99 | + |
|
100 | + io_createNamespace($mediaID, 'media'); |
|
101 | + @touch(mediaFN($mediaID)); |
|
102 | + @chmod(mediaFN($mediaID), $conf['fmode']); |
|
103 | + $fh = fopen(mediaFN($mediaID), 'w'); |
|
104 | + fwrite($fh, $RSSstart); |
|
105 | + |
|
106 | + foreach ($this->spatial_idx as $idxEntry) { |
|
107 | + // get list of id's |
|
108 | + foreach ($idxEntry as $id) { |
|
109 | + // for document item in the index |
|
110 | + if (strpos($id, 'media__', 0) !== 0) { |
|
111 | + if ($this->skipPage($id, $namespace)) { |
|
112 | + continue; |
|
113 | + } |
|
114 | + |
|
115 | + $meta = p_get_metadata($id); |
|
116 | + |
|
117 | + // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
|
118 | + $desc = strip_tags($meta['description']['abstract']); |
|
119 | + |
|
120 | + $entry = '<entry>' . DOKU_LF; |
|
121 | + $entry .= ' <title>' . $meta['title'] . '</title>' . DOKU_LF; |
|
122 | + $entry .= ' <summary>' . $desc . '</summary>' . DOKU_LF; |
|
123 | + $entry .= ' <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon'] |
|
124 | + . '</georss:point>' . DOKU_LF; |
|
125 | + if ($meta['geo']['alt']) { |
|
126 | + $entry .= ' <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF; |
|
127 | + } |
|
128 | + $entry .= ' <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF; |
|
129 | + if (empty($meta['creator'])) { |
|
130 | + $meta['creator'] = $conf['title']; |
|
131 | + } |
|
132 | + $entry .= ' <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF; |
|
133 | + $entry .= ' <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF; |
|
134 | + $entry .= ' <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF; |
|
135 | + $entry .= ' <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':' |
|
136 | + . parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF; |
|
137 | + $entry .= '</entry>' . DOKU_LF; |
|
138 | + fwrite($fh, $entry); |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + fwrite($fh, $RSSend); |
|
144 | + return fclose($fh); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * will return true for non-public or hidden pages or pages that are not below or in the namespace. |
|
149 | + */ |
|
150 | + private function skipPage(string $id, string $namespace): bool |
|
151 | + { |
|
152 | + Logger::getInstance(Logger::LOG_DEBUG)->log( |
|
153 | + "helper_plugin_spatialhelper_sitemap::skipPage, check for $id in $namespace" |
|
154 | + ); |
|
155 | + if (isHiddenPage($id)) { |
|
156 | + return true; |
|
157 | + } |
|
158 | + if (auth_aclcheck($id, '', null) < AUTH_READ) { |
|
159 | + return true; |
|
160 | + } |
|
161 | + |
|
162 | + if (!empty($namespace)) { |
|
163 | + // only if id is in or below namespace |
|
164 | + if (0 !== strpos(getNS($id), $namespace)) { |
|
165 | + // Logger::getInstance(Logger::LOG_DEBUG)->log("helper_plugin_spatialhelper_sitemap::skipPage, |
|
166 | + // skipping $id, not in $namespace"); |
|
167 | + return true; |
|
168 | + } |
|
169 | + } |
|
170 | + return false; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Create a KML sitemap. |
|
175 | + * |
|
176 | + * @param string $mediaID id for the KML file |
|
177 | + */ |
|
178 | + public function createKMLSitemap(string $mediaID): bool |
|
179 | + { |
|
180 | + global $conf; |
|
181 | + $namespace = getNS($mediaID); |
|
182 | + |
|
183 | + $KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
|
184 | + $KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" '; |
|
185 | + $KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
186 | + $KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"'; |
|
187 | + $KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 '; |
|
188 | + $KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF; |
|
189 | + $KMLstart .= '<Document id="root_doc">' . DOKU_LF; |
|
190 | + $KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF; |
|
191 | + $KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF; |
|
192 | + $KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF; |
|
193 | + $KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>'; |
|
194 | + $KMLstart .= '<Icon><href>' |
|
195 | + . DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF; |
|
196 | + |
|
197 | + $KMLend = '</Document>' . DOKU_LF . '</kml>'; |
|
198 | + |
|
199 | + io_createNamespace($mediaID, 'media'); |
|
200 | + @touch(mediaFN($mediaID)); |
|
201 | + @chmod(mediaFN($mediaID), $conf['fmode']); |
|
202 | + |
|
203 | + $fh = fopen(mediaFN($mediaID), 'w'); |
|
204 | + fwrite($fh, $KMLstart); |
|
205 | + |
|
206 | + foreach ($this->spatial_idx as $idxEntry) { |
|
207 | + // get list of id's |
|
208 | + foreach ($idxEntry as $id) { |
|
209 | + // for document item in the index |
|
210 | + if (strpos($id, 'media__', 0) !== 0) { |
|
211 | + if ($this->skipPage($id, $namespace)) { |
|
212 | + continue; |
|
213 | + } |
|
214 | + |
|
215 | + $meta = p_get_metadata($id); |
|
216 | + |
|
217 | + // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
|
218 | + $desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>'; |
|
219 | + $desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] |
|
220 | + . '</a></p>'; |
|
221 | + |
|
222 | + // create an entry and store it |
|
223 | + $plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF; |
|
224 | + $plcm .= ' <name>' . $meta['title'] . '</name>' . DOKU_LF; |
|
225 | + // TODO escape quotes in: title="' . $meta['title'] . '" |
|
226 | + $plcm .= ' <atom:link href="' . wl($id, '' . true); |
|
227 | + $plcm .= '" rel="alternate" type="text/html" />' . DOKU_LF; |
|
228 | + if (!empty($meta['creator'])) { |
|
229 | + $plcm .= ' <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>' |
|
230 | + . DOKU_LF; |
|
231 | + } |
|
232 | + |
|
233 | + $plcm .= ' <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF; |
|
234 | + $plcm .= ' <styleUrl>#icon</styleUrl>' . DOKU_LF; |
|
235 | + |
|
236 | + $plcm .= ' <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat']; |
|
237 | + if ($meta['geo']['alt']) { |
|
238 | + $plcm .= ',' . $meta['geo']['alt']; |
|
239 | + } |
|
240 | + $plcm .= '</coordinates></Point>' . DOKU_LF; |
|
241 | + $plcm .= '</Placemark>' . DOKU_LF; |
|
242 | + |
|
243 | + fwrite($fh, $plcm); |
|
244 | + } |
|
245 | + } |
|
246 | + } |
|
247 | + fwrite($fh, $KMLend); |
|
248 | + return fclose($fh); |
|
249 | + } |
|
250 | 250 | } |