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