xref: /plugin/qc/action/cron.php (revision b6b2145cc338cb25cf403c26dea71467a23fd215)
1<?php
2
3/**
4 * QC Cronjob Action Plugin: Clean up the history once per day
5 *
6 * @author Dominik Eckelmann <dokuwiki@cosmocode.de>
7 */
8class action_plugin_qc_cron extends DokuWiki_Action_Plugin
9{
10
11    /**
12     * if true a cleanup process is already running
13     * or done in the last 24h
14     */
15    protected $run = false;
16
17    /**
18     * File with the queue informations
19     */
20    protected $file;
21
22    /**
23     * Constructor - set up some pathes
24     */
25    public function __construct()
26    {
27        global $conf;
28        $this->file = $conf['tmpdir'] . '/qcgather';
29    }
30
31    /**
32     * Register its handlers with the dokuwiki's event controller
33     *
34     * we need hook the indexer to trigger the cleanup
35     */
36    public function register(Doku_Event_Handler $controller)
37    {
38        $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'qccron', array());
39    }
40
41    /**
42     * start the scan
43     *
44     * Scan for fixmes
45     */
46    public function qccron(Doku_Event $event, $param)
47    {
48        if ($this->run) return;
49
50        global $ID;
51        if (!$ID) return;
52
53        $this->run = true;
54        echo 'qc data gatherer: started on ' . $ID . NL;
55        /** @var helper_plugin_qc $qc */
56        $qc = $this->loadHelper('qc');
57
58        $persist = [];
59        if (is_file($this->file)) {
60            $persist = file_get_contents($this->file);
61            $persist = unserialize($persist);
62        } else {
63            echo '2';
64        }
65
66        $fixme = $qc->getQCData($ID);
67
68        // when there are no quality problems we won't need the information
69        if ($this->isOk($fixme['err'])) {
70            unset($persist[$ID]);
71        } else {
72            $persist[$ID] = $fixme;
73        }
74
75        $persist = serialize($persist);
76        file_put_contents($this->file, $persist);
77    }
78
79    /**
80     * checks an array to quality
81     *
82     * @return true when everything is alright
83     */
84    protected function isOk($arr)
85    {
86        return count(array_filter((array)$arr)) == 0;
87    }
88}
89