xref: /plugin/mikioplugin/action.php (revision fd0b763c6861a9e2b9df068e61f5ce938ab99469)
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[] = '/' . $filename;
69        }
70      }
71    }
72
73    $stylesheets = array_unique($stylesheets);
74
75    array_unshift($stylesheets, '/assets/variables.css');
76    if($conf['template'] === 'mikio' && file_exists(tpl_incdir() . 'template.info.txt')) {
77      $tpl_supported = false;
78      $tpl_info = [];
79      $tpl_data = file_get_contents(tpl_incdir() . 'template.info.txt');
80      foreach(preg_split("/(\r\n|\n|\r)/", $tpl_data) as $line){
81        if(preg_match("/([a-z]*)\s+(.*)/", $line, $matches)) {
82          $tpl_info[$matches[1]] = $matches[2];
83        }
84      }
85
86      if(array_key_exists('date', $tpl_info)) {
87        $date = array_map('intval', explode('-', $tpl_info['date']));
88        if(count($date) === 3) {
89          // Date of mikio template is > 2022-10-12
90          if($date[0] > 2022 || ($date[0] == 2022 && ($date[1] > 10 || ($date[1] == 10 && $date[2] > 12)))) {
91            $tpl_supported = true;
92          }
93        }
94      }
95
96      if($tpl_supported === true && ($key = array_search('/assets/variables.css', $stylesheets)) !== false) {
97        unset($stylesheets[$key]);
98      }
99    }
100
101    array_unshift($stylesheets, '/assets/plugin.css');
102
103    // css
104    foreach ($stylesheets as $style) {
105      if (strtolower(substr($style, -5)) == '.less') {
106        $less[] = $style;
107      } else {
108        array_unshift($event->data['link'], array(
109          'type' => 'text/css',
110          'rel'  => 'stylesheet',
111          'href' => $style
112        ));
113      }
114    }
115
116    $lessSorted = [];
117    foreach ($less as $key => $value) {
118      if (substr(strtolower($value), -14) == 'variables.less') {
119        $lessSorted[] = $value;
120        unset($less[$key]);
121      }
122    }
123
124    $lessSorted = array_merge($lessSorted, $less);
125    $lessPath = implode(',', $lessSorted);
126
127    if(strlen($lessPath) > 0) {
128      array_unshift($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    // js
136    foreach ($scripts as $script) {
137      $event->data['script'][] = array(
138        'type'  => 'text/javascript',
139        '_data' => '',
140        'src'   => $script
141      );
142    }
143  }
144}
145