summaryrefslogtreecommitdiffstats
path: root/update.php
blob: 15fa8d8931ef6d05cf11d482350b70c143827f6b (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/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');
require_once(DOKU_INC.'inc/cliopts.php');
require_once('SymfonyComponents/YAML/sfYaml.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;
    }
}

if (!$OPTS->hasArgs()) {
    _usage();
    exit;
}

#------------------------------------------------------------------------------
# Doku_Indexer_Mass_Remover

class Doku_Indexer_Mass_Remover extends Doku_Indexer {
    function deletePages($pages) {
        if (!$this->lock()) {
            die('Locked');
        }

        foreach($pages as $page) {
            _quietecho('Removing from index: ' . $page);
            $this->deletePageNoLock($page);
            _quietecho(" done.\n");
        }

        $this->unlock();
    }
}

#------------------------------------------------------------------------------
# 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();
    search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true), 'hostinfo');

    foreach ($data as $page) {
        $PAGES[$page['id']] = 'delete';
    }
}

#------------------------------------------------------------------------------
# _load_hostinfo_data

function _load_hostinfo_data($path) {
    global $HOSTINFO;

    $hosts_file = $path . '/metadata/hosts';
    if (!file_exists($hosts_file)) {
        die("Invalid hostinfo path: $path\n");
    }

    $hosts = sfYaml::load($hosts_file);
    foreach ($hosts['hosts'] as $host) {
        $HOSTINFO[$host] = sfYaml::load($path . '/' . $host);
    }
}

#------------------------------------------------------------------------------
# _update_content

function _update_content() {
    global $conf, $HOSTINFO;

    // render sites
    foreach ($HOSTINFO as $host => $host_data) {
        _render($host, 'host', $host_data);
    }
}

#------------------------------------------------------------------------------
# _render

function _render($target, $file, $vars=null) {
    global $conf, $PAGES;

    _quietecho("Rendering $file into $target");
    $content = _render_template($file, $vars);
    if ($content === false) {
        _quietecho(" FAILED (missing template)\n");
        return false;
    }
    else {
        $pagename = cleanID('hostinfo:' . $target);
        $page = wikiFN($pagename);

        $old_content = '';
        if (file_exists($page)) {
            $old_content = file_get_contents($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;
    }
}

#------------------------------------------------------------------------------
# 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) {
    $groups = array();

    foreach ($hostinfo as $host => $data) {
        foreach ($data['groups'] as $group) {
             $groups[$group] = array();
        }
    }

    foreach (array_keys($groups) as $group) {
        foreach ($hostinfo as $host => $data) {
            if (in_array($group, $data['groups'])) {
                $groups[$group][$host] = $data;
            }
        }
    }

    foreach ($groups as $group => $hosts) {
        if (count($hosts) < 2) {
            unset($groups[$group]);
        }

        if (count($hosts) == count($hostinfo)) {
            $groups[$group] = 'all';
        }
    }

    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);
    }

    ob_start();
    include $file;
    return ob_get_clean();
}

#------------------------------------------------------------------------------
# _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");
        }
    }
}

#------------------------------------------------------------------------------
# _remove_index

function _remove_index() {
    global $PAGES;

    $data = array();
    foreach ($PAGES as $page => $state) {
        if ($state == 'delete') {
            $data[] = $page;
        }
    }

    $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");
        }
    }
    _remove_index();
}

#------------------------------------------------------------------------------
# _usage

function _usage() {
    print "Usage: update.php <options> <path to hostinfo>

Updates the hostinfo information by first removing all pages in the hostinfo
namespace from the fulltext index, generating the new files and then
reindexing all pages.
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;
}

?>