From a1e990177b94299d00ff72125bb1d4ea6a05fd73 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 30 Aug 2013 15:20:30 +0000 Subject: use smarty to render templates from wikipages --- .gitignore | 1 + plugins/function.parse_service.php | 25 +++++++ plugins/modifier.contact_info.php | 12 ++++ plugins/modifier.count.php | 8 +++ plugins/modifier.group_by.php | 23 +++++++ plugins/modifier.key_not_exists.php | 11 +++ plugins/prefilter.whitespace_control.php | 63 +++++++++++++++++ plugins/resource.dokuwiki.php | 33 +++++++++ templates/host.tpl.php | 85 ----------------------- templates/start.tpl.php | 67 ------------------ templates_c/.keep | 0 update.php | 112 ++++++++++--------------------- 12 files changed, 210 insertions(+), 230 deletions(-) create mode 100644 .gitignore create mode 100644 plugins/function.parse_service.php create mode 100644 plugins/modifier.contact_info.php create mode 100644 plugins/modifier.count.php create mode 100644 plugins/modifier.group_by.php create mode 100644 plugins/modifier.key_not_exists.php create mode 100644 plugins/prefilter.whitespace_control.php create mode 100644 plugins/resource.dokuwiki.php delete mode 100644 templates/host.tpl.php delete mode 100644 templates/start.tpl.php create mode 100644 templates_c/.keep 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 @@ +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 @@ + 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 @@ + 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 @@ + $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 @@ + 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 @@ + "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 @@ +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 @@ -====== ====== - - - -===== Summary ===== - - * **hostname:** - * **os:** - * **arch:** - - * **vserver:** - - - * **vserver host:** - - - * **maintainers:** - - - * **bcfg2-groups:** [[https://bcfg2.spline.de/client/|view client in bcfg2]] - - * - - - -===== network interfaces ===== - - - 0) { ?> - $value) { ?> - * **** - - * - - - -No network interfaces configured. - - -===== open ports ===== - - - -^ Port ^ IP ^ Process ^ Protocol ^ - -| | | | | - - - -No open ports - - -===== services ===== - - 0) { ?> - -^ Name ^ Port ^ Visibility ^ - $service_category) { ?> - - - -| | | | - -| | default | | - - - - - - -no services provoided - 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 ====== - -{confsearch> @hostinfo > Search in hostinfo} - -===== Übersicht ===== - - - $data) { ?> -[[|]]: - @os: - @arch: - - @vserver: - - @maint: - @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/|Link]] - @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/|Link]] - 100) { ?> - @doc: :-D - - - - - -===== IPs ===== - - - $data) { ?> - - - -: - @host: [[|]] - - - - - - - -===== Bcfg2 Groups ===== - - $hosts) { ?> -==== ==== - - -[[start#uebersicht|alle Server]] - - - $data) { ?> -[[|]]: - @os: - @arch: - - @vserver: - - @maint: - @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/|Link]] - @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/|Link]] - 100) { ?> - @doc: :-D - - - - - - diff --git a/templates_c/.keep b/templates_c/.keep new file mode 100644 index 0000000..e69de29 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(); #------------------------------------------------------------------------------ @@ -69,6 +70,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() @@ -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'; @@ -161,29 +190,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 @@ -217,64 +223,14 @@ function get_bcfg2_groups($hostinfo) { return $groups; } -#------------------------------------------------------------------------------ -# 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'); } #------------------------------------------------------------------------------ -- cgit v1.2.3-1-g7c22