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