xref: /plugin/geotag/action.php (revision d5b4dd3624c6feb4ad359a7f5f4b048015b9ee07)
1c3b1bf17SMark C. Prins<?php
2d0af2299Sgithub-actions[bot]
3d0af2299Sgithub-actions[bot]use dokuwiki\Extension\ActionPlugin;
4d0af2299Sgithub-actions[bot]use dokuwiki\Extension\Event;
53c71dabfSMark 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 */
223c71dabfSMark 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>
28*d5b4dd36SMark Prins *
29*d5b4dd36SMark Prins * @phpcs:disable Squiz.Classes.ValidClassName.NotPascalCase
30c3b1bf17SMark C. Prins */
31d0af2299Sgithub-actions[bot]class action_plugin_geotag extends ActionPlugin
32a9083e48SMark Prins{
3381dae509SMark C. Prins    /**
3481dae509SMark C. Prins     * Register for events.
3581dae509SMark C. Prins     *
36e362bed5Sgithub-actions[bot]     * @param EventHandler $controller
372b579309SMark C. Prins     *          DokuWiki's event controller object. Also available as global $EVENT_HANDLER
3881dae509SMark C. Prins     */
39d0af2299Sgithub-actions[bot]    final public function register(EventHandler $controller): void
40a9083e48SMark Prins    {
412be1c2ecSMark Prins        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput');
4294769ee6SMark C. Prins        if ($this->getConf('toolbar_icon')) {
43d0af2299Sgithub-actions[bot]            $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insertButton', []);
4494769ee6SMark C. Prins        }
453dfca210SMark Prins        $controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'popularity');
46c3b1bf17SMark C. Prins    }
47c3b1bf17SMark C. Prins
4881dae509SMark C. Prins    /**
492b579309SMark C. Prins     * Retrieve metadata and add to the head of the page using appropriate meta tags.
502b579309SMark C. Prins     *
51e362bed5Sgithub-actions[bot]     * @param Event $event
522b579309SMark C. Prins     *          the DokuWiki event. $event->data is a two-dimensional
53a9539fccSMark C. Prins     *          array of all meta headers. The keys are meta, link and script.
54a42972efSMark C. Prins     *
552ca95f77SMark Prins     * @see https://www.dokuwiki.org/devel:event:tpl_metaheader_output
5681dae509SMark C. Prins     */
57d0af2299Sgithub-actions[bot]    final public function handleMetaheaderOutput(Event $event): void
58a9083e48SMark Prins    {
59c3b1bf17SMark C. Prins        global $ID;
602ca95f77SMark Prins        $title = p_get_metadata($ID, 'title', METADATA_RENDER_USING_SIMPLE_CACHE);
61d0af2299Sgithub-actions[bot]        $geotags = p_get_metadata($ID, 'geo', METADATA_RENDER_USING_SIMPLE_CACHE) ?? [];
62a9083e48SMark Prins        $region = $geotags ['region'] ?? null;
63a9083e48SMark Prins        $lat = $geotags ['lat'] ?? null;
64a9083e48SMark Prins        $lon = $geotags ['lon'] ?? null;
65a9083e48SMark Prins        $alt = $geotags ['alt'] ?? null;
66a9083e48SMark Prins        $country = $geotags ['country'] ?? null;
67a9083e48SMark Prins        $placename = $geotags ['placename'] ?? null;
68a9083e48SMark Prins        $geohash = $geotags ['geohash'] ?? null;
69c3b1bf17SMark C. Prins
70a42972efSMark C. Prins        if (!empty($region)) {
71d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.region', 'content' => $region];
72a42972efSMark C. Prins        }
73a42972efSMark C. Prins        if (!empty($placename)) {
74d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.placename', 'content' => $placename];
75a42972efSMark C. Prins        }
76a42972efSMark C. Prins        if (!(empty($lat) && empty($lon))) {
772b579309SMark C. Prins            if (!empty($alt)) {
78d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon . ';' . $alt];
798eb104d9SMark C. Prins            } else {
80d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => 'geo.position', 'content' => $lat . ';' . $lon];
812b579309SMark C. Prins            }
82a42972efSMark C. Prins        }
83a42972efSMark C. Prins        if (!empty($country)) {
84d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.country', 'content' => $country];
85a42972efSMark C. Prins        }
86dfaf64a7SMark C. Prins        if (!(empty($lat) && empty($lon))) {
87d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => "ICBM", 'content' => $lat . ', ' . $lon];
8842b5f05aSMark C. Prins            // icbm is generally useless without a DC.title,
8942b5f05aSMark C. Prins            // so we copy that from title unless it's empty...
90a42972efSMark C. Prins            if (!(empty($title))) {
91b7214539SMark C. Prins                /*
92b7214539SMark C. Prins                 * don't specify the DC namespace as this is incomplete; it should be done at the
93b7214539SMark C. Prins                 * template level as it also needs a 'profile' attribute on the head/container,
94a9083e48SMark Prins                 * see: https://dublincore.org/documents/dc-html/#sect-3.1.1
951fdb6e09SMark Prins                 * $event->data ['link'] [] = array ('rel' => 'schema.DC',
961fdb6e09SMark Prins                 * 'href' => 'http://purl.org/dc/elements/1.1/');
97b7214539SMark C. Prins                 */
98d0af2299Sgithub-actions[bot]                $event->data ['meta'] [] = ['name' => "DC.title", 'content' => $title];
99a42972efSMark C. Prins            }
100a42972efSMark C. Prins        }
101a42972efSMark C. Prins        if (!empty($geohash)) {
102d0af2299Sgithub-actions[bot]            $event->data ['meta'] [] = ['name' => 'geo.geohash', 'content' => $geohash];
103c3b1bf17SMark C. Prins        }
104dfaf64a7SMark C. Prins    }
105dfaf64a7SMark C. Prins
10681dae509SMark C. Prins    /**
10794769ee6SMark C. Prins     * Inserts the toolbar button.
108a42972efSMark C. Prins     *
109e362bed5Sgithub-actions[bot]     * @param Event $event
1102b579309SMark C. Prins     *          the DokuWiki event
11194769ee6SMark C. Prins     */
112*d5b4dd36SMark Prins    final public function insertButton(Event $event): void
113a9083e48SMark Prins    {
1143c71dabfSMark Prins        $event->data [] = [
1153c71dabfSMark Prins            'type' => 'format',
1163c71dabfSMark Prins            'title' => $this->getLang('toolbar_desc'),
1173c71dabfSMark Prins            'icon' => '../../plugins/geotag/images/geotag.png',
1183c71dabfSMark Prins            'open' => '{{geotag>lat:', 'sample' => '52.2345',
1193c71dabfSMark Prins            'close' => ', lon:7.521, alt: , placename: , country: , region: }}'
1203c71dabfSMark Prins        ];
12194769ee6SMark C. Prins    }
1223dfca210SMark Prins
1233dfca210SMark Prins    /**
1243dfca210SMark Prins     * Add geotag popularity data.
1253dfca210SMark Prins     *
126e362bed5Sgithub-actions[bot]     * @param Event $event
1273dfca210SMark Prins     *          the DokuWiki event
1283dfca210SMark Prins     */
129d0af2299Sgithub-actions[bot]    final public function popularity(Event $event): void
130a9083e48SMark Prins    {
131aca15265SMark Prins        $versionInfo = getVersionData();
1323dfca210SMark Prins        $plugin_info = $this->getInfo();
1333dfca210SMark Prins        $event->data['geotag']['version'] = $plugin_info['date'];
134aca15265SMark Prins        $event->data['geotag']['dwversion'] = $versionInfo['date'];
135aca15265SMark Prins        $event->data['geotag']['combinedversion'] = $versionInfo['date'] . '_' . $plugin_info['date'];
1363dfca210SMark Prins    }
137c3b1bf17SMark C. Prins}
138