#!/usr/bin/env php lock()) { $cli->fatal('Cannot lock index!'); } foreach($pages as $page) { $cli->debug("Removing page from index: $page"); $this->deletePageNoLock($page); $cli->info("Removed page from index: $page"); } $this->unlock(); } } #------------------------------------------------------------------------------ # CLI class UpdateHostinfo extends CLI { function __construct() { parent::__construct(); $this->twig = new Twig(); $this->pages = array(); } 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.' ); } protected function main(Options $options) { $this->pages = $this->find_current_pages(); $hostinfo_path = $options->getArgs()[0]; $hostinfo = new Hostinfo($hostinfo_path); if (!$options->getOpt('clear')) { $this->render_pages($hostinfo); $this->render('start', 'start', array('HOSTINFO' => $hostinfo)); $this->update_index(); } $this->remove_pages(); } protected function find_current_pages() { global $conf; $data = array(); $opts = array( 'skipacl' => true, // ignore pages in subnamespaces (templates) 'depth' => 2, ); search($data, $conf['datadir'], 'search_allpages', $opts, 'hostinfo'); $pages = array(); foreach ($data as $page) { $pages[$page['id']] = 'delete'; } $this->info('Found ' . count($pages) . ' current pages.'); return $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.'); $this->remove_index(); } protected function remove_index() { $data = array(); foreach ($this->pages as $page => $state) { if ($state == 'delete') { $data[] = $page; } } $idx = new Doku_Indexer_Mass_Remover(); $idx->deletePages($this, $data); $this->success('Removed outdated index.'); } 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"); } } } protected function render_pages($hostinfo) { // render sites foreach ($hostinfo->get_hosts() as $host) { $this->render($host, 'host', $hostinfo->get_host($host)); } } 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; } } } $cli = new UpdateHostinfo(); $cli->run(); // vim: set ts=4 sw=4 tw=0 et :