diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2018-11-19 12:10:49 +0100 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2018-11-19 12:22:12 +0100 |
commit | b4702736fc52780cb848d699d06e73ec3165290b (patch) | |
tree | 4a8217405149eee38217a54d5a05b08d3ae57093 /lib | |
parent | ec4c627649a0b31f804f9cbdbfc84d375b774cd2 (diff) | |
download | dokuwiki-b4702736fc52780cb848d699d06e73ec3165290b.tar.gz dokuwiki-b4702736fc52780cb848d699d06e73ec3165290b.tar.bz2 dokuwiki-b4702736fc52780cb848d699d06e73ec3165290b.zip |
Update to new dokuwiki version, replace smarty with twig
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hostinfo.php | 60 | ||||
-rw-r--r-- | lib/twig.php | 129 |
2 files changed, 189 insertions, 0 deletions
diff --git a/lib/hostinfo.php b/lib/hostinfo.php new file mode 100644 index 0000000..33b87d5 --- /dev/null +++ b/lib/hostinfo.php @@ -0,0 +1,60 @@ +<?php + +require_once('Symfony/Component/Yaml/autoload.php'); +use Symfony\Component\Yaml\Yaml; + +class Hostinfo +{ + public function __construct($path) + { + $this->path = $path; + if (!file_exists("$this->path/metadata/hosts")) { + throw new Exception("Invalid hostinfo path: $this->path"); + } + + $this->hosts = null; + $this->_data = array(); + } + + protected function load_hosts() + { + $hosts = Yaml::parse( + file_get_contents("$this->path/metadata/hosts")); + $this->hosts = $hosts['hosts']; + } + + protected function load_host($host) + { + $this->_data[$host] = Yaml::parse( + file_get_contents("$this->path/$host")); + } + + public function get_hosts() + { + if ($this->hosts === null) { + $this->load_hosts(); + } + + return $this->hosts; + } + + public function get_host($host) + { + if (!array_key_exists($host, $this->_data)) { + $this->load_host($host); + } + + return $this->_data[$host]; + } + + public function get_data() + { + $data = array(); + foreach ($this->get_hosts() as $host) { + $data[$host] = $this->get_host($host); + } + return $data; + } +} + +// vim: set ts=4 sw=4 tw=0 et : diff --git a/lib/twig.php b/lib/twig.php new file mode 100644 index 0000000..db1354d --- /dev/null +++ b/lib/twig.php @@ -0,0 +1,129 @@ +<?php + +require_once('Twig/Autoloader.php'); +Twig_Autoloader::register(); + +class Twig_Loader_Dokuwiki implements Twig_LoaderInterface, Twig_ExistsLoaderInterface +{ + private function build_pagename($name) { + return 'hostinfo:templates:' . cleanID($name); + } + + public function getSource($name) + { + $page = $this->build_pagename($name); + return rawWiki($page); + } + + public function getCacheKey($name) + { + return $this->build_pagename($name); + } + + public function isFresh($name, $time) + { + $page = $this->build_pagename($name); + $last_change = p_get_metadata($page, 'last_change date'); + return $last_change < $time; + } + + public function exists($name) + { + $page = $this->build_pagename($name); + return page_exists($page); + } +} + +class Hostinfo_Twig_Extension extends Twig_Extension +{ + public function getFilters() + { + return array( + new Twig_SimpleFilter('contact_info', function($value) { + if (is_array($value)) { + return '[[' . current($value) . '|' . key($value) . ']]'; + } + + return '[[' . $value . '@spline.inf.fu-berlin.de' . '|' . $value . ']]'; + }), + + new Twig_SimpleFilter('parse_service', function($value) { + if (is_array($value)) { + return array( + 'port' => current($value), + 'name' => key($value), + ); + } + + return array( + 'name' => $value, + 'port' => $null, + ); + }), + + new Twig_SimpleFilter('group_by', function($value, $key) { + $array = array(); + + foreach ($value as $k => $v) { + if (array_key_exists($key, $v)) { + if (is_array($v[$key])) { + foreach ($v[$key] as $key_part) { + $array[$key_part][$k] = $v; + } + } + else { + $array[$v[$key]][$k] = $v; + } + } + } + + return $array; + }), + + new Twig_SimpleFilter('key_not_exists', function($value, $key) { + if (!is_array($value)) { + return array(); + } + + return array_filter($value, + function($v) use ($key) { + return !array_key_exists($key, $v); + }); + }), + + new Twig_SimpleFilter('count', function($value) { + return count($value); + }), + + new Twig_SimpleFilter('length', function($value) { + return strlen($value); + }), + ); + } + + public function getName() + { + return 'hostinfo'; + } +} + +class Twig +{ + function __construct() + { + $loader = new Twig_Loader_Dokuwiki(); + $this->twig = new Twig_Environment($loader, array( + 'cache' => __DIR__ . '/../templates_c/', + 'auto_reload' => true, + )); + $this->twig->addExtension(new Hostinfo_Twig_Extension()); + } + + public function render($template, $vars=null) + { + $tmpl = $this->twig->loadTemplate($template . '.tpl'); + return $tmpl->render($vars); + } +} + +// vim: set ts=4 sw=4 tw=0 et : |