1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/*
6 * Copyright (c) 2012-2020 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 WMS 1.3.0 layer to your map.
24 */
25class syntax_plugin_openlayersmap_wmslayer extends SyntaxPlugin
26{
27    private $dflt = ['id'          => 'olmap', 'name'        => '', 'url'         => '', 'opacity'     => 0.8, 'attribution' => '', 'visible'     => false, 'layers'      => '', 'version'     => '1.3.0', 'format'      => 'image/png', 'transparent' => 'true', 'baselayer'   => 'false'];
28
29    /**
30     * (non-PHPdoc)
31     *
32     * @see DokuWiki_Syntax_Plugin::getPType()
33     */
34    public function getPType(): string
35    {
36        return 'block';
37    }
38
39    /**
40     * (non-PHPdoc)
41     *
42     * @see DokuWiki_Syntax_Plugin::getType()
43     */
44    public function getType(): string
45    {
46        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
47        return 'baseonly';
48    }
49
50    /**
51     * (non-PHPdoc)
52     *
53     * @see Doku_Parser_Mode::getSort()
54     */
55    public function getSort(): int
56    {
57        return 902;
58    }
59
60    /**
61     * Connect to our special pattern.
62     *
63     * @see Doku_Parser_Mode::connectTo()
64     */
65    public function connectTo($mode): void
66    {
67        // look for: <olmap_wmslayer id="olmap" name="cloud" url="http://openweathermap.org/t/tile.cgi?SERVICE=WMS"
68        // attribution="OpenWeatherMap" visible="true" layers="GLBETA_PR"></olmap_wmslayer>
69        $this->Lexer->addSpecialPattern(
70            '<olmap_wmslayer ?[^>\n]*>.*?</olmap_wmslayer>',
71            $mode,
72            'plugin_openlayersmap_wmslayer'
73        );
74    }
75
76    /**
77     * (non-PHPdoc)
78     *
79     * @see DokuWiki_Syntax_Plugin::handle()
80     */
81    public function handle($match, $state, $pos, Doku_Handler $handler): array
82    {
83        $param = [];
84        $data  = $this->dflt;
85
86        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);
87
88        foreach ($param as $kvpair) {
89            [$matched, $key, $val] = $kvpair;
90            if (isset($data [$key])) {
91                $key         = strtolower($key);
92                $data [$key] = $val;
93            }
94        }
95        return $data;
96    }
97
98    /**
99     * (non-PHPdoc)
100     *
101     * @see DokuWiki_Syntax_Plugin::render()
102     */
103    public function render($format, Doku_Renderer $renderer, $data): bool
104    {
105        if ($format !== 'xhtml') {
106            return false;
107        }
108
109        // incremented for each olmap_wmslayer tag in the page source
110        static $overlaynumber = 0;
111
112        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
113        $str           = '{';
114        foreach ($data as $key => $val) {
115            $str .= "'" . $key . "' : '" . $val . "',";
116        }
117        $str           .= "'type':'wms'}";
118        $renderer->doc .= base64_encode("olMapOverlays['wms" . $overlaynumber . "'] = " . $str . ";")
119            . '"></script>';
120        $overlaynumber++;
121        return true;
122    }
123}
124