xref: /plugin/geotag/action.php (revision 3c71dabf75811a69b8583f9a4c1afff4bd4d2efb)
1c3b1bf17SMark C. Prins<?php
2d0af2299Sgithub-actions[bot]
3d0af2299Sgithub-actions[bot]use dokuwiki\Extension\ActionPlugin;
4d0af2299Sgithub-actions[bot]use dokuwiki\Extension\Event;
5*3c71dabfSMark Prinsuse dokuwiki\Extension\EventHandler;
6d0af2299Sgithub-actions[bot]
7c3b1bf17SMark C. Prins/*
8a9083e48SMark Prins * Copyright (c) 2011 Mark C. Prins <mprins@users.sf.net>
9c3b1bf17SMark C. Prins *
10c3b1bf17SMark C. Prins * Permission to use, copy, modify, and distribute this software for any
11c3b1bf17SMark C. Prins * purpose with or without fee is hereby granted, provided that the above
12c3b1bf17SMark C. Prins * copyright notice and this permission notice appear in all copies.
13c3b1bf17SMark C. Prins *
14c3b1bf17SMark C. Prins * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15c3b1bf17SMark C. Prins * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16c3b1bf17SMark C. Prins * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17c3b1bf17SMark C. Prins * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18c3b1bf17SMark C. Prins * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19c3b1bf17SMark C. Prins * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20c3b1bf17SMark C. Prins * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21c3b1bf17SMark C. Prins */
22*3c71dabfSMark Prins
23c3b1bf17SMark C. Prins/**
24a42972efSMark C. Prins * DokuWiki Plugin geotag (Action Component).
25c3b1bf17SMark C. Prins *
2662d582daSMark C. Prins * @license BSD license
2730c0df9cSMark C. Prins * @author  Mark C. Prins <mprins@users.sf.net>
28c3b1bf17SMark C. Prins */
29d0af2299Sgithub-actions[bot]class action_plugin_geotag extends ActionPlugin
30a9083e48SMark Prins{
3181dae509SMark C. Prins    /**
3281dae509SMark C. Prins     * Register for events.
3381dae509SMark C. Prins     *
34e362bed5Sgithub-actions[bot]     * @param EventHandler $controller
352b579309SMark C. Prins     *          DokuWiki's event controller object. Also available as global $EVENT_HANDLER
3681dae509SMark C. Prins     */
37d0af2299Sgithub-actions[bot]    final public function register(EventHandler $controller): void
38a9083e48SMark Prins    {
392be1c2ecSMark Prins        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput');
4094769ee6SMark C. Prins        if ($this->getConf('toolbar_icon')) {
41d0af2299Sgithub-actions[bot]            $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insertButton', []);
4294769ee6SMark C. Prins        }
433dfca210SMark Prins        $controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'popularity');
44c3b1bf17SMark C. Prins    }
45c3b1bf17SMark C. Prins
4681dae509SMark C. Prins    /**
472b579309SMark C. Prins     * Retrieve metadata and add to the head of the page using appropriate meta tags.
482b579309SMark C. Prins     *
49e362bed5Sgithub-actions[bot]     * @param Event $event
502b579309SMark C. Prins     *          the DokuWiki event. $event->data is a two-dimensional
51a9539fccSMark C. Prins     *          array of all meta headers. The keys are meta, link and script.
52a42972efSMark C. Prins     *
532ca95f77SMark Prins     * @see https://www.dokuwiki.org/devel:event:tpl_metaheader_output
5481dae509SMark C. Prins     */
55d0af2299Sgithub-actions[bot]    final public function handleMetaheaderOutput(Event $event): void
56a9083e48SMark Prins    {
57c3b1bf17SMark C. Prins        global $ID;
582ca95f77SMark Prins        $title = p_get_metadata($ID, 'title', METADATA_RENDER_USING_SIMPLE_CACHE);
59d0af2299Sgithub-actions[bot]        $geotags = p_get_metadata($ID, 'geo', METADATA_RENDER_USING_SIMPLE_CACHE) ?? [];
60a9083e48SMark Prins        $region = $geotags ['region'] ?? null;
61a9083e48SMark Prins        $lat = $geotags ['lat'] ?? null;
62a9083e48SMark Prins        $lon = $geotags ['lon'] ?? null;
63a9083e48SMark Prins        $alt = $geotags ['alt'] ?? null;
64a9083e48SMark Prins        $country = $geotags ['country'] ?? null;
65a9083e48SMark Prins        $placename = $geotags ['placename'] ?? null;
66a9083e48SMark Prins        $geohash = $geotags ['geohash'] ?? null;
67c3b1bf17SMark C. Prins
68a42972efSMark C. Prins        if (!empty($region)) {
69d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.region', 'content' => $region];
70a42972efSMark C. Prins        }
71a42972efSMark C. Prins        if (!empty($placename)) {
72d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.placename', 'content' => $placename];
73a42972efSMark C. Prins        }
74a42972efSMark C. Prins        if (!(empty($lat) && empty($lon))) {
752b579309SMark C. Prins            if (!empty($alt)) {
76d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon . ';' . $alt];
778eb104d9SMark C. Prins            } else {
78d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon];
792b579309SMark C. Prins            }
80a42972efSMark C. Prins        }
81a42972efSMark C. Prins        if (!empty($country)) {
82d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.country', 'content' => $country];
83a42972efSMark C. Prins        }
84dfaf64a7SMark C. Prins        if (!(empty($lat) && empty($lon))) {
85d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => "ICBM", 'content' => $lat . ', ' . $lon];
8642b5f05aSMark C. Prins            // icbm is generally useless without a DC.title,
8742b5f05aSMark C. Prins            // so we copy that from title unless it's empty...
88a42972efSMark C. Prins            if (!(empty($title))) {
89b7214539SMark C. Prins                /*
90b7214539SMark C. Prins                 * don't specify the DC namespace as this is incomplete; it should be done at the
91b7214539SMark C. Prins                 * template level as it also needs a 'profile' attribute on the head/container,
92a9083e48SMark Prins                 * see: https://dublincore.org/documents/dc-html/#sect-3.1.1
931fdb6e09SMark Prins                 * $event->data ['link'] [] = array ('rel' => 'schema.DC',
941fdb6e09SMark Prins                 * 'href' => 'http://purl.org/dc/elements/1.1/');
95b7214539SMark C. Prins                 */
96d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => "DC.title", 'content' => $title];
97a42972efSMark C. Prins            }
98a42972efSMark C. Prins        }
99a42972efSMark C. Prins        if (!empty($geohash)) {
100d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.geohash', 'content' => $geohash];
101c3b1bf17SMark C. Prins        }
102dfaf64a7SMark C. Prins    }
103dfaf64a7SMark C. Prins
10481dae509SMark C. Prins    /**
10594769ee6SMark C. Prins     * Inserts the toolbar button.
106a42972efSMark C. Prins     *
107e362bed5Sgithub-actions[bot]     * @param Event $event
1082b579309SMark C. Prins     *          the DokuWiki event
10994769ee6SMark C. Prins     */
110d0af2299Sgithub-actions[bot]    final public function insertButton(Event $event, array $param): void
111a9083e48SMark Prins    {
112*3c71dabfSMark Prins        $event->data [] = [
113*3c71dabfSMark Prins            'type' => 'format',
114*3c71dabfSMark Prins            'title' => $this->getLang('toolbar_desc'),
115*3c71dabfSMark Prins            'icon' => '../../plugins/geotag/images/geotag.png',
116*3c71dabfSMark Prins            'open' => '{{geotag>lat:', 'sample' => '52.2345',
117*3c71dabfSMark Prins            'close' => ', lon:7.521, alt: , placename: , country: , region: }}'
118*3c71dabfSMark Prins        ];
11994769ee6SMark C. Prins    }
1203dfca210SMark Prins
1213dfca210SMark Prins    /**
1223dfca210SMark Prins     * Add geotag popularity data.
1233dfca210SMark Prins     *
124e362bed5Sgithub-actions[bot]     * @param Event $event
1253dfca210SMark Prins     *          the DokuWiki event
1263dfca210SMark Prins     */
127d0af2299Sgithub-actions[bot]    final public function popularity(Event $event): void
128a9083e48SMark Prins    {
129aca15265SMark Prins        $versionInfo = getVersionData();
1303dfca210SMark Prins        $plugin_info = $this->getInfo();
1313dfca210SMark Prins        $event->data['geotag']['version'] = $plugin_info['date'];
132aca15265SMark Prins        $event->data['geotag']['dwversion'] = $versionInfo['date'];
133aca15265SMark Prins        $event->data['geotag']['combinedversion'] = $versionInfo['date'] . '_' . $plugin_info['date'];
1343dfca210SMark Prins    }
135c3b1bf17SMark C. Prins}
136