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