summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alex@spline.inf.fu-berlin.de>2013-08-30 15:20:30 +0000
committerAlexander Sulfrian <alex@spline.inf.fu-berlin.de>2013-08-30 15:20:30 +0000
commita1e990177b94299d00ff72125bb1d4ea6a05fd73 (patch)
treec80f394163dd5a5efb9cdf144d577938ec85c8bc
parent1c70b0447570619f537147db8ca491c054467a12 (diff)
downloaddokuwiki-a1e990177b94299d00ff72125bb1d4ea6a05fd73.tar.gz
dokuwiki-a1e990177b94299d00ff72125bb1d4ea6a05fd73.tar.bz2
dokuwiki-a1e990177b94299d00ff72125bb1d4ea6a05fd73.zip
use smarty to render templates from wikipages
-rw-r--r--.gitignore1
-rw-r--r--plugins/function.parse_service.php25
-rw-r--r--plugins/modifier.contact_info.php12
-rw-r--r--plugins/modifier.count.php8
-rw-r--r--plugins/modifier.group_by.php23
-rw-r--r--plugins/modifier.key_not_exists.php11
-rw-r--r--plugins/prefilter.whitespace_control.php63
-rw-r--r--plugins/resource.dokuwiki.php33
-rw-r--r--templates/host.tpl.php85
-rw-r--r--templates/start.tpl.php67
-rw-r--r--templates_c/.keep0
-rwxr-xr-xupdate.php112
12 files changed, 210 insertions, 230 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..689da03
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+templates_c/*.php
diff --git a/plugins/function.parse_service.php b/plugins/function.parse_service.php
new file mode 100644
index 0000000..1b02886
--- /dev/null
+++ b/plugins/function.parse_service.php
@@ -0,0 +1,25 @@
+<?php
+
+function smarty_function_parse_service($params, Smarty_Internal_Template $template)
+{
+ if (empty($params['var'])) {
+ trigger_error("assign: missing 'var' parameter");
+ return;
+ }
+
+ if (!in_array('value', array_keys($params))) {
+ trigger_error("assign: missing 'value' parameter");
+ return;
+ }
+
+ $name = $params['value'];
+ $port = null;
+ if (preg_match('/(.*): (.*)/', $params['value'], $match)) {
+ $name = $match[1];
+ $port = $match[2];
+ }
+
+ $template->assign($params['var'], array('name' => $name, 'port' => $port));
+}
+
+?>
diff --git a/plugins/modifier.contact_info.php b/plugins/modifier.contact_info.php
new file mode 100644
index 0000000..89b16a4
--- /dev/null
+++ b/plugins/modifier.contact_info.php
@@ -0,0 +1,12 @@
+<?php
+
+function smarty_modifier_contact_info($value)
+{
+ if (preg_match('/^([^:]*): (.*@.*)$/', $value, $match)) {
+ return '[[' . $match[2] . '|' . $match[1] . ']]';
+ }
+
+ return '[[' . $value . '@spline.inf.fu-berlin.de' . '|' . $value . ']]';
+}
+
+?>
diff --git a/plugins/modifier.count.php b/plugins/modifier.count.php
new file mode 100644
index 0000000..c077c11
--- /dev/null
+++ b/plugins/modifier.count.php
@@ -0,0 +1,8 @@
+<?php
+
+function smarty_modifier_count($value)
+{
+ return count($value);
+}
+
+?>
diff --git a/plugins/modifier.group_by.php b/plugins/modifier.group_by.php
new file mode 100644
index 0000000..74bf045
--- /dev/null
+++ b/plugins/modifier.group_by.php
@@ -0,0 +1,23 @@
+<?php
+
+function smarty_modifier_group_by($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;
+}
+
+?>
diff --git a/plugins/modifier.key_not_exists.php b/plugins/modifier.key_not_exists.php
new file mode 100644
index 0000000..f6906cd
--- /dev/null
+++ b/plugins/modifier.key_not_exists.php
@@ -0,0 +1,11 @@
+<?php
+
+function smarty_modifier_key_not_exists($value, $key)
+{
+ return array_filter($value,
+ function($v) use ($key) {
+ return !array_key_exists($key, $v);
+ });
+}
+
+?>
diff --git a/plugins/prefilter.whitespace_control.php b/plugins/prefilter.whitespace_control.php
new file mode 100644
index 0000000..fa6b854
--- /dev/null
+++ b/plugins/prefilter.whitespace_control.php
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * Smarty Whitespace Control
+ *
+ * {-tag} remove white space infront of tag up to the previous non-whitespace character or beginning of the line
+ * "text \n\n\t {-tag}" -> "text \n\n{tag}"
+ * "text \n\n\t text\t {-tag}" -> "text \n\n\t text{tag}"
+ * {--tag} remove white space infront of tag up to the previous non-whitespace character
+ * "text \n\n\t {--tag}" -> "text{tag}"
+ * "text \n\n\t text\t {--tag}" -> "text \n\n\t text{tag}"
+ * {+-tag}
+ * {-+tag} replace white space infront of tag up to the previous non-whitespace character by a single line-break
+ * "text \n\n\t {-+tag}" -> "text\n{tag}"
+ * "text \n\n\t text\t {-+tag}" -> "text \n\n\t text\n{tag}"
+ *
+ * {tag-} remove white space after tag up to the next non-whitespace character or end of the line
+ * "{tag-} \n\n\t text" -> "{tag}\n\n\t text"
+ * "{tag-} text \n\n\t text" -> "{tag}text \n\n\t text"
+ * {tag--} remove white space after tag up to the next non-whitespace character
+ * "{tag--} \n\n\t text" -> "{tag}text"
+ * "{tag--} text \n\n\t text" -> "{tag}text \n\n\t text"
+ * {tag+-}
+ * {tag-+} replace white space after tag up to the next non-whitespace character by a single line-break
+ * "{tag-+} \n\n\t text" -> "{tag}\n\ntext"
+ * "{tag-+} text \n\n\t text" -> "{tag}\n\ntext \n\n\t text"
+ *
+ * {tag+} replace white space after tag up to the end of the line with an additional line-break
+ * "{tag+} \n\t text" -> "{tag}\n\n\t text"
+ * "{tag+} text \n\n\t text" -> "{tag}\n\ntext \n\n\t text"
+ *
+ * Any combination of the above, say {--tag+} is possible. Any + modifiers are executed before - modifiers, so
+ * "{tag+-}{--tag}" will lead to "{tag}{tag}"
+ *
+ * NOTE: {tag+} and {tag-+} cause two trailing \n. This is done because PHP itself throws away the first \n.
+ * So \n\n in the template will lead to \n in the output
+ *
+ * @param string $string raw template source
+ * @param Smarty_Internal_Template $template Template instance
+ * @return string raw template source after whitespace control was applied
+ * @author Rodney Rehm
+ */
+function smarty_prefilter_whitespace_control($string, Smarty_Internal_Template $template) {
+ $ldelim = $template->smarty->left_delimiter;
+ $rdelim = $template->smarty->right_delimiter;
+ $_ldelim = preg_quote($ldelim);
+ $_rdelim = preg_quote($rdelim);
+
+ // remove preceeding whitepsace preserving a single line-break
+ $string = preg_replace('#\s*'. $_ldelim .'(?:-\+|\+-)#', "\n" . $ldelim, $string);
+ // remove trailing whitespace preserving s single line-break
+ $string = preg_replace('#(?:\+-|-\+)'. $_rdelim .'\s*#', $rdelim . "\n\n", $string);
+
+ // remove preceeding whitepsace
+ $string = preg_replace('#\s*'. $_ldelim .'--|[^\S\r\n]*'. $_ldelim .'-#', $ldelim, $string);
+ // remove trailing whitespace
+ $string = preg_replace('#--'. $_rdelim .'\s*|-'. $_rdelim .'[^\S\r\n]*#', $rdelim, $string);
+
+ // force trailing line-break
+ $string = preg_replace('#\+'. $_rdelim .'(?:\s*[\r\n]|[^\S\r\n]*)#', $rdelim . "\n\n", $string);
+
+ return $string;
+}
diff --git a/plugins/resource.dokuwiki.php b/plugins/resource.dokuwiki.php
new file mode 100644
index 0000000..4842524
--- /dev/null
+++ b/plugins/resource.dokuwiki.php
@@ -0,0 +1,33 @@
+<?php
+
+class Smarty_Resource_Dokuwiki extends Smarty_Resource_Custom {
+
+ private function build_pagename($name) {
+ return 'hostinfo:templates:' . cleanID($name);
+ }
+
+ protected function fetch($name, &$source, &$mtime)
+ {
+ $page = $this->build_pagename($name);
+ if (page_exists($page)) {
+ $source = rawWiki($page);
+ $mtime = p_get_metadata($page, 'last_change date');
+ }
+ else {
+ $source = null;
+ $mtime = null;
+ }
+ }
+
+ protected function fetchTimestamp($name) {
+ $page = $this->build_pagename($name);
+ if (page_exists($page)) {
+ return p_get_metadata($page, 'last_change date');
+ }
+ else {
+ return null;
+ }
+ }
+}
+
+?>
diff --git a/templates/host.tpl.php b/templates/host.tpl.php
deleted file mode 100644
index 68af670..0000000
--- a/templates/host.tpl.php
+++ /dev/null
@@ -1,85 +0,0 @@
-====== <?php echo str_replace('.spline.inf.fu-berlin.de', '', $hostname) ?> ======
-
-<?php echo $data['description'] ?>
-
-===== Summary =====
-
- * **hostname:** <?php echo $hostname . "\n" ?>
- * **os:** <?php echo_with_default($os, 'unknown'); echo "\n" ?>
- * **arch:** <?php echo_with_default($arch, 'unknown'); echo "\n" ?>
-<?php if (isset($vserver)) { ?>
- * **vserver:** <?php echo $vserver . "\n" ?>
-<?php } ?>
-<?php if (isset($vserver_host)) { ?>
- * **vserver host:** <?php echo $vserver_host . "\n"?>
-<?php } ?>
-<?php if (is_array($maintainers)) { ?>
- * **maintainers:** <?php
-
- $first = true;
- foreach ($maintainers as $maintainer) {
- list($name, $mail) = get_contact_info($maintainer);
- if (!$first) {
- echo ', ';
- }
- $first = false;
-
- echo "[[$mail|$name]]";
- }
- echo "\n";
-?>
-<?php } ?>
-<?php if (isset($groups)) { ?>
- * **bcfg2-groups:** [[https://bcfg2.spline.de/client/<?php echo $hostname ?>|view client in bcfg2]]
-<?php foreach ($groups as $group) { ?>
- * <?php echo $group . "\n" ?>
-<?php } ?>
-<?php } ?>
-
-===== network interfaces =====
-
-<?php $filtered_addresses = select_by($addresses, 'vserver', false); ?>
-<?php if (count($filtered_addresses) > 0) { ?>
-<?php foreach (group_by($filtered_addresses, 'interface') as $group => $value) { ?>
- * **<?php echo $group ?>**
-<?php foreach ($value as $address) { ?>
- * <?php echo $address['address'] . '/' . $address['netmask'] . "\n" ?>
-<?php } ?>
-<?php } ?>
-<?php } else { ?>
-No network interfaces configured.
-<?php } ?>
-
-===== open ports =====
-
-<?php if (is_array($ports)) { ?>
-<sortable 1>
-^ Port ^ IP ^ Process ^ Protocol ^
-<?php foreach ($ports as $port) { ?>
-| <?php echo $port['port'] ?> | <?php echo $port['ip'] ?> | <?php echo $port['process'] ?> | <?php echo $port['proto'] ?> |
-<?php } ?>
-</sortable>
-<?php } else { ?>
-No open ports
-<?php } ?>
-
-===== services =====
-
-<?php if (is_array($services) && count($services) > 0) { ?>
-<sortable>
-^ Name ^ Port ^ Visibility ^
-<?php foreach ($services as $category => $service_category) { ?>
-<?php if (is_array($service_category)) { ?>
-<?php foreach ($service_category as $service) { ?>
-<?php if (preg_match('/(.*): (.*)/', $service, $match)) { ?>
-| <?php echo $match[1] ?> | <?php echo $match[2] ?> | <?php echo $category ?> |
-<?php } else { ?>
-| <?php echo $service ?> | default | <?php echo $category ?> |
-<?php } ?>
-<?php } ?>
-<?php } ?>
-<?php } ?>
-</sortable>
-<?php } else { ?>
-no services provoided
-<?php } ?>
diff --git a/templates/start.tpl.php b/templates/start.tpl.php
deleted file mode 100644
index f3a8df4..0000000
--- a/templates/start.tpl.php
+++ /dev/null
@@ -1,67 +0,0 @@
-====== Hostinfo ======
-
-<WRAP centeralign>{confsearch> @hostinfo > Search in hostinfo}</WRAP>
-
-===== Übersicht =====
-
-<flattable key="Hostname" os="OS" arch="Arch" vserver="VServer"="--" maint="Maintainers"=0 nagios="Monitoring" bcfg2="Bcfg2" doc="Doku"="FIXME">
-<?php foreach ($HOSTINFO as $host => $data) { ?>
-[[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]]:
- @os: <?php echo $data['os']; echo "\n" ?>
- @arch: <?php echo $data['arch']; echo "\n" ?>
-<?php if (isset($data['vserver']) && !empty($data['vserver'])) { ?>
- @vserver: <?php echo $data['vserver']; echo "\n" ?>
-<?php } ?>
- @maint: <?php echo count($data['maintainers']); echo "\n" ?>
- @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>|Link]]
- @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/<?php echo $host ?>|Link]]
-<?php if (isset($data['doc']) && strlen(trim($data['doc'])) > 100) { ?>
- @doc: :-D
-<?php } ?>
-
-<?php } ?>
-</flattable>
-
-===== IPs =====
-
-<flattable key="IP" host="Hostname" sort="1">
-<?php foreach ($HOSTINFO as $host => $data) { ?>
-<?php if (isset($data['addresses']) && is_array($data['addresses'])) { ?>
-<?php foreach ($data['addresses'] as $address) { ?>
-<?php if (!isset($address['vserver'])) { ?>
-<?php echo $address['address'] ?>:
- @host: [[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]]
-
-<?php } ?>
-<?php } ?>
-<?php } ?>
-<?php } ?>
-</flattable>
-
-===== Bcfg2 Groups =====
-
-<?php foreach (get_bcfg2_groups($HOSTINFO) as $group => $hosts) { ?>
-==== <?php echo $group ?> ====
-
-<?php if ($hosts == 'all') { ?>
-[[start#uebersicht|alle Server]]
-<?php } else { ?>
-<flattable key="Hostname" os="OS" arch="Arch" vserver="VServer"="--" maint="Maintainers"=0 nagios="Monitoring" bcfg2="Bcfg2" doc="Doku"="FIXME">
-<?php foreach ($hosts as $host => $data) { ?>
-[[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]]:
- @os: <?php echo $data['os']; echo "\n" ?>
- @arch: <?php echo $data['arch']; echo "\n" ?>
-<?php if (isset($data['vserver']) && !empty($data['vserver'])) { ?>
- @vserver: <?php echo $data['vserver']; echo "\n" ?>
-<?php } ?>
- @maint: <?php echo count($data['maintainers']); echo "\n" ?>
- @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>|Link]]
- @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/<?php echo $host ?>|Link]]
-<?php if (isset($data['doc']) && strlen(trim($data['doc'])) > 100) { ?>
- @doc: :-D
-<?php } ?>
-
-<?php } ?>
-</flattable>
-<?php } ?>
-<?php } ?>
diff --git a/templates_c/.keep b/templates_c/.keep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/templates_c/.keep
diff --git a/update.php b/update.php
index 15fa8d8..0094aa7 100755
--- a/update.php
+++ b/update.php
@@ -11,6 +11,7 @@ if(!defined('DOKU_INC')) define('DOKU_INC', '/usr/share/dokuwiki/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/cliopts.php');
require_once('SymfonyComponents/YAML/sfYaml.php');
+require_once('smarty3/Smarty.class.php');
session_write_close();
#------------------------------------------------------------------------------
@@ -70,6 +71,28 @@ class Doku_Indexer_Mass_Remover extends Doku_Indexer {
}
#------------------------------------------------------------------------------
+# Smarty
+
+class Smarty_Hostinfo extends Smarty {
+
+ function __construct()
+ {
+ parent::__construct();
+
+ $this->setCompileDir(__DIR__ . '/templates_c/');
+ $this->setConfigDir(__DIR__ . '/configs/');
+
+ $this->default_resource_type = 'dokuwiki';
+
+ $this->addPluginsDir(__DIR__ . '/plugins/');
+ $this->loadFilter("pre", 'whitespace_control');
+
+ $this->caching = Smarty::CACHING_OFF;
+ }
+}
+$smarty = new Smarty_Hostinfo();
+
+#------------------------------------------------------------------------------
# main()
$path_to_hostinfo = $OPTS->arg(0);
@@ -91,7 +114,13 @@ function _find_current_pages() {
global $conf, $PAGES;
$data = array();
- search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true), 'hostinfo');
+ $opts = array(
+ 'skipacl' => true,
+
+ // ignore pages in subnamespaces (templates)
+ 'depth' => 2,
+ );
+ search($data, $conf['datadir'], 'search_allpages', $opts, 'hostinfo');
foreach ($data as $page) {
$PAGES[$page['id']] = 'delete';
@@ -162,29 +191,6 @@ function _render($target, $file, $vars=null) {
}
#------------------------------------------------------------------------------
-# default
-
-function echo_with_default($value, $default) {
- if (!isset($value) || empty($value)) {
- echo $default;
- }
- else {
- echo $value;
- }
-}
-
-#------------------------------------------------------------------------------
-# get_contact_info
-
-function get_contact_info($maintainer) {
- if (preg_match('/^([^:]*): (.*@.*)$/', $maintainer, $match)) {
- return array($match[1], $match[2]);
- }
-
- return array($maintainer, $maintainer . '@spline.inf.fu-berlin.de');
-}
-
-#------------------------------------------------------------------------------
# get_bcfg2_groups
function get_bcfg2_groups($hostinfo) {
@@ -218,63 +224,13 @@ function get_bcfg2_groups($hostinfo) {
}
#------------------------------------------------------------------------------
-# group_by
-
-function group_by($data, $key) {
- $result = array();
-
- foreach ($data as $value) {
- if (array_key_exists($key, $value)) {
- $result[$value[$key]][] = $value;
- }
- }
-
- return $result;
-}
-
-#------------------------------------------------------------------------------
-# select_by
-
-function select_by($data, $key, $exists = true) {
- $result = array();
-
- if (is_array($data)) {
- foreach ($data as $value) {
- if (array_key_exists($key, $value) === $exists) {
- $result[] = $value;
- }
- }
- }
-
- return $result;
-}
-
-#------------------------------------------------------------------------------
# _render_template
-function _render_template($file, $vars=null) {
- // validate $file
- $file = preg_replace('/^(?:.+\/)?([a-zA-Z_-]*)(?:\.[a-zA-Z])?$/', '$1', $file);
- $file = 'templates/' . $file . '.tpl.php';
-
- if (!file_exists($file)) {
- // template does not exists
- return false;
- }
-
- if (is_array($vars) && !empty($vars)) {
- if (array_key_exists('file', $vars)) {
- // if $vars would contain a 'file' key, this would overwrite the
- // first parameter of the template file
- unset($vars['file']);
- }
-
- extract($vars);
- }
+function _render_template($template, $vars=null) {
+ global $smarty;
- ob_start();
- include $file;
- return ob_get_clean();
+ $smarty->assign($vars);
+ return $smarty->fetch($template . '.tpl');
}
#------------------------------------------------------------------------------