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