1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/*
6 * Copyright (c) 2023 Mark C. Prins <mprins@users.sf.net>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
21 */
22/**
23 * adds a WMTS 1.0.0 layer to your map.
24 */
25class syntax_plugin_openlayersmap_wmtslayer extends SyntaxPlugin
26{
27    private $dflt = ['id' => 'olmap', 'name' => '', 'url' => '', 'opacity' => 0.8, 'attribution' => '', 'visible' => false, 'layer' => '', 'matrixSet' => '', 'transparent' => 'true', 'baselayer' => 'false'];
28
29    /**
30     * (non-PHPdoc)
31     *
32     * @see DokuWiki_Syntax_Plugin::getPType()
33     */
34    final public function getPType(): string
35    {
36        return 'block';
37    }
38
39    /**
40     * (non-PHPdoc)
41     *
42     * @see DokuWiki_Syntax_Plugin::getType()
43     */
44    final public function getType(): string
45    {
46        return 'baseonly';
47    }
48
49    /**
50     * (non-PHPdoc)
51     *
52     * @see Doku_Parser_Mode::getSort()
53     */
54    final public function getSort(): int
55    {
56        return 902;
57    }
58
59    /**
60     * Connect to our special pattern.
61     *
62     * @see Doku_Parser_Mode::connectTo()
63     */
64    final public function connectTo($mode): void
65    {
66        // look for: <olmap_wmstlayer id="olmap" name="geolandbasemap" url="https://mapsneu.wien.gv.at/basemapneu/1.0.0/WMTSCapabilities.xml"
67        // attribution="basemap.at" visible="true" layer="geolandbasemap" matrixSet=google3857></olmap_wmtslayer>
68        $this->Lexer->addSpecialPattern(
69            '<olmap_wmtslayer ?[^>\n]*>.*?</olmap_wmtslayer>',
70            $mode,
71            'plugin_openlayersmap_wmtslayer'
72        );
73    }
74
75    /**
76     * (non-PHPdoc)
77     *
78     * @see DokuWiki_Syntax_Plugin::handle()
79     */
80    final public function handle($match, $state, $pos, Doku_Handler $handler): array
81    {
82        $param = [];
83        $data = $this->dflt;
84
85        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
86
87        foreach ($param as $kvpair) {
88            [$matched, $key, $val] = $kvpair;
89            if (isset($data [$key])) {
90                $key = strtolower($key);
91                $data [$key] = $val;
92            }
93        }
94        return $data;
95    }
96
97    /**
98     * (non-PHPdoc)
99     *
100     * @see DokuWiki_Syntax_Plugin::render()
101     */
102    final public function render($format, Doku_Renderer $renderer, $data): bool
103    {
104        if ($format !== 'xhtml') {
105            return false;
106        }
107
108        // incremented for each olmap_wmtslayer tag in the page source
109        static $overlaynumber = 0;
110
111        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
112        $str = '{';
113        foreach ($data as $key => $val) {
114            $str .= "'" . $key . "':'" . $val . "', ";
115        }
116        $str .= "'type':'wmts'}";
117        $renderer->doc .= base64_encode("olMapOverlays['wmts" . $overlaynumber . "'] = " . $str . ";")
118            . '"></script>';
119        $overlaynumber++;
120        return true;
121    }
122}
123