diff options
Diffstat (limited to 'update.php')
-rwxr-xr-x | update.php | 351 |
1 files changed, 133 insertions, 218 deletions
@@ -9,65 +9,30 @@ $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; ini_set('memory_limit','128M'); 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('smarty3/Smarty.class.php'); -require_once('Symfony/Component/Yaml/Yaml.php'); -require_once('Symfony/Component/Yaml/Parser.php'); -require_once('Symfony/Component/Yaml/Inline.php'); -require_once('Symfony/Component/Yaml/Unescaper.php'); - session_write_close(); -#------------------------------------------------------------------------------ -# handle options - -$short_opts = 'hcq'; -$long_opts = array('help', 'clear', 'quiet'); -$OPTS = Doku_Cli_Opts::getOptions(__FILE__, $short_opts, $long_opts); -if ( $OPTS->isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$CLEAR = false; -$QUIET = false; -$PAGES = array(); - -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'c': - case 'clear': - $CLEAR = true; - break; - case 'q': - case 'quiet': - $QUIET = true; - break; - } -} +require_once(DOKU_INC.'vendor/autoload.php'); +use splitbrain\phpcli\CLI; +use splitbrain\phpcli\Options; -if (!$OPTS->hasArgs()) { - _usage(); - exit; -} +require_once(__DIR__ . '/lib/twig.php'); +require_once(__DIR__ . '/lib/hostinfo.php'); #------------------------------------------------------------------------------ # Doku_Indexer_Mass_Remover -class Doku_Indexer_Mass_Remover extends Doku_Indexer { - function deletePages($pages) { +class Doku_Indexer_Mass_Remover extends Doku_Indexer +{ + function deletePages($cli, $pages) + { if (!$this->lock()) { - die('Locked'); + $cli->fatal('Cannot lock index!'); } foreach($pages as $page) { - _quietecho('Removing from index: ' . $page); + $cli->debug("Removing page from index: $page"); $this->deletePageNoLock($page); - _quietecho(" done.\n"); + $cli->info("Removed page from index: $page"); } $this->unlock(); @@ -75,207 +40,157 @@ class Doku_Indexer_Mass_Remover extends Doku_Indexer { } #------------------------------------------------------------------------------ -# Smarty - -class Smarty_Hostinfo extends Smarty { +# CLI - function __construct() - { +class UpdateHostinfo extends CLI +{ + function __construct() + { parent::__construct(); - $this->setCompileDir(__DIR__ . '/templates_c/'); - - $this->addPluginsDir(__DIR__ . '/plugins/'); - $this->default_resource_type = 'dokuwiki'; - $this->loadFilter("pre", 'whitespace_control'); - - $this->caching = Smarty::CACHING_OFF; - } -} -$smarty = new Smarty_Hostinfo(); - -#------------------------------------------------------------------------------ -# main() - -$path_to_hostinfo = $OPTS->arg(0); -_load_hostinfo_data($path_to_hostinfo); - -_find_current_pages(); -if (!$CLEAR) { - _update_content(); - _render('start', 'start', array('HOSTINFO' => $HOSTINFO)); - _update_index(); -} -_remove_outdated(); - - -#------------------------------------------------------------------------------ -# _find_current_pages - -function _find_current_pages() { - global $conf, $PAGES; - - $data = array(); - $opts = array( - 'skipacl' => true, - - // ignore pages in subnamespaces (templates) - 'depth' => 2, - ); - search($data, $conf['datadir'], 'search_allpages', $opts, 'hostinfo'); + $this->twig = new Twig(); + $this->pages = array(); + } - foreach ($data as $page) { - $PAGES[$page['id']] = 'delete'; + protected function setup(Options $options) + { + $options->setHelp('Updates the hostinfo information by '. + 'first generating all changed pages and add them '. + 'to the fulltext index. Pages that should not '. + 'exists anymore are removed (also from the '. + 'fulltext index) afterwards. When the -c option '. + 'is given the index is only cleared and nothing '. + 'is updated.'); + + $options->registerOption( + 'clear', + 'Only clear the index.', + 'c' + ); + + $options->registerArgument( + 'PATH', + 'Path to hostinfo source files.' + ); } -} -#------------------------------------------------------------------------------ -# _load_hostinfo_data + protected function main(Options $options) + { + $this->pages = $this->find_current_pages(); -function _load_hostinfo_data($path) { - global $HOSTINFO; + $hostinfo_path = $options->getArgs()[0]; + $hostinfo = new Hostinfo($hostinfo_path); - $hosts_file = $path . '/metadata/hosts'; - if (!file_exists($hosts_file)) { - die("Invalid hostinfo path: $path\n"); + if (!$options->getOpt('clear')) { + $this->render_pages($hostinfo); + $this->render('start', 'start', array('HOSTINFO' => $hostinfo)); + $this->update_index(); + } + $this->remove_pages(); } - $hosts = Symfony\Component\Yaml\Yaml::parse($hosts_file); - foreach ($hosts['hosts'] as $host) { - $HOSTINFO[$host] = Symfony\Component\Yaml\Yaml::parse($path . '/' . $host); - } -} + protected function find_current_pages() { + global $conf; -#------------------------------------------------------------------------------ -# _update_content + $data = array(); + $opts = array( + 'skipacl' => true, -function _update_content() { - global $conf, $HOSTINFO; + // ignore pages in subnamespaces (templates) + 'depth' => 2, + ); + search($data, $conf['datadir'], 'search_allpages', $opts, 'hostinfo'); - // render sites - foreach ($HOSTINFO as $host => $host_data) { - _render($host, 'host', $host_data); + $pages = array(); + foreach ($data as $page) { + $pages[$page['id']] = 'delete'; + } + $this->info('Found ' . count($pages) . ' current pages.'); + return $pages; } -} - -#------------------------------------------------------------------------------ -# _render -function _render($target, $file, $vars=null) { - global $conf, $PAGES; + protected function remove_pages() + { + foreach ($this->pages as $page => $state) { + if ($state == 'delete') { + $this->debug("Removing old page: $page"); + unlink(wikiFN($page)); + $this->info("Removed old page: $page"); + } + } + $this->success('Removed outdated pages.'); - _quietecho("Rendering $file into $target"); - $content = _render_template($file, $vars); - if ($content === false) { - _quietecho(" FAILED (missing template)\n"); - return false; + $this->remove_index(); } - else { - $pagename = cleanID('hostinfo:' . $target); - $page = wikiFN($pagename); - $old_content = ''; - if (file_exists($page)) { - $old_content = file_get_contents($page); + protected function remove_index() + { + $data = array(); + foreach ($this->pages as $page => $state) { + if ($state == 'delete') { + $data[] = $page; + } } - if ($content != $old_content) { - $PAGES[$pagename] = 'update'; - saveWikiText($pagename, $content, 'auto generated'); - _quietecho(" done\n"); - } - else { - unset($PAGES[$pagename]); - _quietecho(" no change\n"); - } - return true; + $idx = new Doku_Indexer_Mass_Remover(); + $idx->deletePages($this, $data); + $this->success('Removed outdated index.'); } -} - -#------------------------------------------------------------------------------ -# _render_template - -function _render_template($template, $vars=null) { - global $smarty; - - $smarty->clearAllAssign(); - $smarty->assign($vars); - return $smarty->fetch($template . '.tpl'); -} -#------------------------------------------------------------------------------ -# _update_index - -function _update_index() { - global $conf, $PAGES; - - - foreach ($PAGES as $page => $state) { - if ($state != 'delete') { - _quietecho('Add to index: ' . $page); - idx_addPage($page, false, true); - _quietecho(" done.\n"); + protected function update_index() + { + foreach ($this->pages as $page => $state) { + if ($state != 'delete') { + $this->debug("Adding page to index: $page"); + idx_addPage($page, false, true); + $this->info("Added page to index: $page"); + } } } -} - -#------------------------------------------------------------------------------ -# _remove_index - -function _remove_index() { - global $PAGES; - $data = array(); - foreach ($PAGES as $page => $state) { - if ($state == 'delete') { - $data[] = $page; + protected function render_pages($hostinfo) + { + // render sites + foreach ($hostinfo->get_hosts() as $host) { + $this->render($host, 'host', $hostinfo->get_host($host)); } } - $idx = new Doku_Indexer_Mass_Remover(); - $idx->deletePages($data); -} - -#------------------------------------------------------------------------------ -# _remove_outdated - -function _remove_outdated() { - global $PAGES; - - foreach ($PAGES as $page => $state) { - if ($state == 'delete') { - _quietecho('Removing old page: ' . $page); - unlink(wikiFN($page)); - _quietecho(" done.\n"); + protected function render($target, $file, $vars=null) + { + $this->debug("Render $file into $target"); + $content = $this->twig->render($file, $vars); + if ($content === false) { + $this->critical("Cannot render $file! Maybe the template is missing?"); + return false; + } + else { + $this->debug("Rendered $file, checking existing content"); + $pagename = cleanID('hostinfo:' . $target); + $page = wikiFN($pagename); + + $old_content = ''; + if (file_exists($page)) { + $old_content = file_get_contents($page); + } + + if ($content != $old_content) { + $this->debug("Content of $target changed, updating page"); + $this->pages[$pagename] = 'update'; + saveWikiText($pagename, $content, 'auto generated'); + $this->success("Rendered $file into $target"); + } + else { + unset($this->pages[$pagename]); + $this->debug("Content of $target unchanged"); + } + + return true; } } - _remove_index(); -} - -#------------------------------------------------------------------------------ -# _usage - -function _usage() { - print "Usage: update.php [OPTIONS] <path to hostinfo> - -Updates the hostinfo information by first generating all changed pages and -add them to the fulltext index. Pages that should not exists anymore are -removed (also from the fulltext index) afterwards. -When the -c option is given the index is only cleared and nothing is updated. - -OPTIONS - -h, --help show this help and exit - -c, --clear only clear the index - -q, --quiet don't produce any output -"; } -#------------------------------------------------------------------------------ -# _quietecho - -function _quietecho($msg) { - global $QUIET; - if(!$QUIET) echo $msg; -} +$cli = new UpdateHostinfo(); +$cli->run(); -?> +// vim: set ts=4 sw=4 tw=0 et : |