xref: /plugin/mikioplugin/action.php (revision f51f4f12e7d8324d30279c7ca19d10c248b93c46)
1<?php
2
3/**
4 * Mikio Plugin
5 *
6 * @version 1.0
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  James Collins <james.collins@outlook.com.au>
9 */
10
11if (!defined('DOKU_INC')) { die();
12}
13
14require_once 'icons/icons.php';
15require_once dirname(__FILE__) . '/inc/polyfill-array-key-first.php';
16
17if (!function_exists('glob_recursive')) {
18    function glob_recursive($pattern, $flags = 0)
19    {
20        $files = glob($pattern, $flags);
21        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
22            $subFiles = glob_recursive($dir . '/' . basename($pattern), $flags);
23            foreach ($subFiles as $file) {
24                $files[] = $file;
25            }
26        }
27        return $files;
28    }
29}
30
31class action_plugin_mikioplugin extends DokuWiki_Action_Plugin
32{
33    public function register(Doku_Event_Handler $controller)
34    {
35        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_load');
36    }
37
38    public function _load(Doku_Event $event, $param)
39    {
40        global $conf;
41        global $MIKIO_ICONS;
42
43        $baseDir = str_replace('\\', '/', DOKU_BASE . 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/');
44        $stylesheets = [];
45        $less = [];
46        $scripts = [];
47
48        if (is_array($MIKIO_ICONS)) {
49            $icons = array();
50            foreach ($MIKIO_ICONS as $icon) {
51                if (isset($icon['name']) && isset($icon['css']) && isset($icon['insert'])) {
52                    $icons[] = $icon;
53
54                    if ($icon['css'] != '') {
55                        if (strpos($icon['css'], '//') === false) {
56                            $stylesheets[] = $baseDir . 'icons/' . $icon['css'];
57                        } else {
58                            $stylesheets[] = $icon['css'];
59                        }
60                    }
61                }
62            }
63            $MIKIO_ICONS = $icons;
64        } else {
65            $MIKIO_ICONS = [];
66        }
67
68        $stylesList = glob_recursive(str_replace('\\', '/', 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/styles/*'));
69        if ($stylesList !== false) {
70            foreach ($stylesList as $value) {
71                $filename = strtolower($value);
72                if (substr($filename, -5) == '.less' || substr($filename, -4) == '.css') {
73                    $stylesheets[] = DOKU_BASE . $filename;
74                }
75            }
76        }
77
78        $stylesheets[] = $baseDir . 'assets/variables.css';
79
80        $stylesheets = array_unique($stylesheets);
81
82        $tpl_supported = false;
83        if($conf['template'] === 'mikio' && file_exists(tpl_incdir() . 'template.info.txt')) {
84            $tpl_info = [];
85            $tpl_data = file_get_contents(tpl_incdir() . 'template.info.txt');
86            foreach(preg_split("/(\r\n|\n|\r)/", $tpl_data) as $line){
87                if(preg_match("/([a-z]*)\s+(.*)/", $line, $matches)) {
88                    $tpl_info[$matches[1]] = $matches[2];
89                }
90            }
91
92            if(array_key_exists('date', $tpl_info)) {
93                $date = array_map('intval', explode('-', $tpl_info['date']));
94                if(count($date) === 3) {
95                    // Date of mikio template is > 2022-10-12
96                    if($date[0] > 2022 || ($date[0] == 2022 && ($date[1] > 10 || ($date[1] == 10 && $date[2] > 12)))) {
97                        $tpl_supported = true;
98                    }
99                }
100            }
101        }
102
103        if($tpl_supported == false) {
104            array_unshift($stylesheets, $baseDir . 'assets/variables.css');
105        }
106
107        array_unshift($stylesheets, $baseDir . 'assets/styles.less');
108
109        // css
110        foreach ($stylesheets as $style) {
111            if (strtolower(substr($style, -5)) == '.less') {
112                $less[] = $style;
113            } else {
114                array_unshift(
115                    $event->data['link'], array(
116                    'type' => 'text/css',
117                    'rel'  => 'stylesheet',
118                    'href' => $style
119                    )
120                );
121            }
122        }
123
124        $lessPath = implode(',', $less);
125
126        if(strlen($lessPath) > 0) {
127            array_unshift(
128                $event->data['link'], array(
129                'type' => 'text/css',
130                'rel'  => 'stylesheet',
131                'href' => $baseDir . 'css.php?css=' . str_replace($baseDir, '', $lessPath)
132                )
133            );
134        }
135
136        // js
137        foreach ($scripts as $script) {
138            $event->data['script'][] = array(
139            'type'  => 'text/javascript',
140            '_data' => '',
141            'src'   => $script
142            );
143        }
144    }
145}
146