1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * DokuWiki Plugin logviewer (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author Andreas Gohr <andi@splitbrain.org>
12 */
13class action_plugin_logviewer extends ActionPlugin
14{
15    /** @inheritDoc */
16    public function register(EventHandler $controller)
17    {
18        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'pruneLogs');
19    }
20
21
22    /**
23     * Event handler for INDEXER_TASKS_RUN
24     *
25     * @see https://www.dokuwiki.org/devel:events:INDEXER_TASKS_RUN
26     * @param Event $event Event object
27     * @param mixed $param optional parameter passed when event was registered
28     * @return void
29     */
30    public function pruneLogs(Event $event, $param)
31    {
32        global $conf;
33
34        $prune = $conf['logdir'] . '/pruned';
35        if (@filemtime($prune) > time() - 24 * 60 * 60) {
36            return; // already pruned today
37        }
38
39        $logdirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
40        foreach ($logdirs as $dir) {
41            $dates = glob($dir . '/*.log'); // glob returns sorted results
42            if (count($dates) > $conf['logretain']) {
43                $dates = array_slice($dates, 0, -1 * $conf['logretain']);
44                foreach ($dates as $date) {
45                    io_rmdir($date, true);
46                }
47            }
48        }
49
50        io_saveFile($prune, '');
51    }
52}
53