-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.php
135 lines (128 loc) · 5.31 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
/*
* Copyright (c) 2011 Mark C. Prins <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* DokuWiki Plugin geotag (Action Component).
*
* @license BSD license
* @author Mark C. Prins <[email protected]>
*/
class action_plugin_geotag extends ActionPlugin
{
/**
* Register for events.
*
* @param EventHandler $controller
* DokuWiki's event controller object. Also available as global $EVENT_HANDLER
*/
final public function register(EventHandler $controller): void
{
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput');
if ($this->getConf('toolbar_icon')) {
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insertButton', []);
}
$controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'popularity');
}
/**
* Retrieve metadata and add to the head of the page using appropriate meta tags.
*
* @param Event $event
* the DokuWiki event. $event->data is a two-dimensional
* array of all meta headers. The keys are meta, link and script.
*
* @see https://www.dokuwiki.org/devel:event:tpl_metaheader_output
*/
final public function handleMetaheaderOutput(Event $event): void
{
global $ID;
$title = p_get_metadata($ID, 'title', METADATA_RENDER_USING_SIMPLE_CACHE);
$geotags = p_get_metadata($ID, 'geo', METADATA_RENDER_USING_SIMPLE_CACHE) ?? [];
$region = $geotags ['region'] ?? null;
$lat = $geotags ['lat'] ?? null;
$lon = $geotags ['lon'] ?? null;
$alt = $geotags ['alt'] ?? null;
$country = $geotags ['country'] ?? null;
$placename = $geotags ['placename'] ?? null;
$geohash = $geotags ['geohash'] ?? null;
if (!empty($region)) {
$event->data ['meta'] [] = ['name' => 'geo.region', 'content' => $region];
}
if (!empty($placename)) {
$event->data ['meta'] [] = ['name' => 'geo.placename', 'content' => $placename];
}
if (!(empty($lat) && empty($lon))) {
if (!empty($alt)) {
$event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon . ';' . $alt];
} else {
$event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon];
}
}
if (!empty($country)) {
$event->data ['meta'] [] = ['name' => 'geo.country', 'content' => $country];
}
if (!(empty($lat) && empty($lon))) {
$event->data ['meta'] [] = ['name' => "ICBM", 'content' => $lat . ', ' . $lon];
// icbm is generally useless without a DC.title,
// so we copy that from title unless it's empty...
if (!(empty($title))) {
/*
* don't specify the DC namespace as this is incomplete; it should be done at the
* template level as it also needs a 'profile' attribute on the head/container,
* see: https://dublincore.org/documents/dc-html/#sect-3.1.1
* $event->data ['link'] [] = array ('rel' => 'schema.DC',
* 'href' => 'http://purl.org/dc/elements/1.1/');
*/
$event->data ['meta'] [] = ['name' => "DC.title", 'content' => $title];
}
}
if (!empty($geohash)) {
$event->data ['meta'] [] = ['name' => 'geo.geohash', 'content' => $geohash];
}
}
/**
* Inserts the toolbar button.
*
* @param Event $event
* the DokuWiki event
*/
final public function insertButton(Event $event, array $param): void
{
$event->data [] = [
'type' => 'format',
'title' => $this->getLang('toolbar_desc'),
'icon' => '../../plugins/geotag/images/geotag.png',
'open' => '{{geotag>lat:', 'sample' => '52.2345',
'close' => ', lon:7.521, alt: , placename: , country: , region: }}'
];
}
/**
* Add geotag popularity data.
*
* @param Event $event
* the DokuWiki event
*/
final public function popularity(Event $event): void
{
$versionInfo = getVersionData();
$plugin_info = $this->getInfo();
$event->data['geotag']['version'] = $plugin_info['date'];
$event->data['geotag']['dwversion'] = $versionInfo['date'];
$event->data['geotag']['combinedversion'] = $versionInfo['date'] . '_' . $plugin_info['date'];
}
}