summaryrefslogtreecommitdiffstats
path: root/update.php
blob: b4065466ae16de8373e864fc4746cd5ab65696cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env php
<?php
if ('cli' != php_sapi_name()) die();

// fake enviroment
$_SERVER['HTTP_HOST'] = 'doku.spline.inf.fu-berlin.de';
$_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');
session_write_close();

require_once(DOKU_INC.'vendor/autoload.php');
use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

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($cli, $pages)
    {
        if (!$this->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 :