1<?php
2/**
3 * DokuWiki Plugin cleanup (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_cleanup extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'handle_indexer_tasks_run');
23
24    }
25
26    /**
27     * [Custom event handler which performs action]
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34
35    public function handle_indexer_tasks_run(Doku_Event &$event, $param) {
36        if(!$this->getConf('runautomatically')) return;
37        global $conf;
38
39        // only run once everyday
40        $lastrun = $conf['cachedir'].'/cleanup.run';
41        $ranat   = @filemtime($lastrun);
42        if($ranat && (time() - $ranat) < 60*60*24 ){
43            echo "cleanup: skipped\n";
44            return;
45        }
46        io_saveFile($lastrun,'');
47
48        // our turn!
49        $event->preventDefault();
50        $event->stopPropagation();
51
52        // and action
53        echo "cleanup: started\n";
54        /** @var helper_plugin_cleanup $helper */
55        $helper = $this->loadHelper('cleanup', false);
56        $helper->run(true);
57        echo 'cleanup: finished. found '.count($helper->list)." files\n";
58    }
59
60}
61
62// vim:ts=4:sw=4:et:
63