xref: /plugin/qc/action/cron.php (revision 2fc45e0c1b2076ea358377649512960c5d3d694a)
11c845774SAndreas Gohr<?php
276bbc49cSAnna Dabrowska
3*2fc45e0cSsplitbrainuse dokuwiki\Extension\ActionPlugin;
4*2fc45e0cSsplitbrainuse dokuwiki\Extension\EventHandler;
5*2fc45e0cSsplitbrainuse dokuwiki\Extension\Event;
6*2fc45e0cSsplitbrain
71c845774SAndreas Gohr/**
81c845774SAndreas Gohr * QC Cronjob Action Plugin: Clean up the history once per day
91c845774SAndreas Gohr *
101c845774SAndreas Gohr * @author Dominik Eckelmann <dokuwiki@cosmocode.de>
111c845774SAndreas Gohr */
12*2fc45e0cSsplitbrainclass action_plugin_qc_cron extends ActionPlugin
1376bbc49cSAnna Dabrowska{
141c845774SAndreas Gohr    /**
151c845774SAndreas Gohr     * if true a cleanup process is already running
161c845774SAndreas Gohr     * or done in the last 24h
171c845774SAndreas Gohr     */
18293182bbSAnna Dabrowska    protected $run = false;
191c845774SAndreas Gohr
201c845774SAndreas Gohr    /**
211c845774SAndreas Gohr     * File with the queue informations
221c845774SAndreas Gohr     */
23293182bbSAnna Dabrowska    protected $file;
241c845774SAndreas Gohr
251c845774SAndreas Gohr    /**
261c845774SAndreas Gohr     * Constructor - set up some pathes
271c845774SAndreas Gohr     */
28293182bbSAnna Dabrowska    public function __construct()
2976bbc49cSAnna Dabrowska    {
301c845774SAndreas Gohr        global $conf;
311c845774SAndreas Gohr        $this->file = $conf['tmpdir'] . '/qcgather';
321c845774SAndreas Gohr    }
331c845774SAndreas Gohr
341c845774SAndreas Gohr    /**
351c845774SAndreas Gohr     * Register its handlers with the dokuwiki's event controller
361c845774SAndreas Gohr     *
371c845774SAndreas Gohr     * we need hook the indexer to trigger the cleanup
381c845774SAndreas Gohr     */
39*2fc45e0cSsplitbrain    public function register(EventHandler $controller)
4076bbc49cSAnna Dabrowska    {
41*2fc45e0cSsplitbrain        $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'qccron', []);
421c845774SAndreas Gohr    }
431c845774SAndreas Gohr
441c845774SAndreas Gohr    /**
451c845774SAndreas Gohr     * start the scan
461c845774SAndreas Gohr     *
471c845774SAndreas Gohr     * Scan for fixmes
481c845774SAndreas Gohr     */
49*2fc45e0cSsplitbrain    public function qccron(Event $event, $param)
5076bbc49cSAnna Dabrowska    {
511c845774SAndreas Gohr        if ($this->run) return;
521c845774SAndreas Gohr
531c845774SAndreas Gohr        global $ID;
541c845774SAndreas Gohr        if (!$ID) return;
551c845774SAndreas Gohr
561c845774SAndreas Gohr        $this->run = true;
571c845774SAndreas Gohr        echo 'qc data gatherer: started on ' . $ID . NL;
581c845774SAndreas Gohr        /** @var helper_plugin_qc $qc */
598ae469bfSAndreas Gohr        $qc = $this->loadHelper('qc');
601c845774SAndreas Gohr
618ae469bfSAndreas Gohr        $persist = [];
621c845774SAndreas Gohr        if (is_file($this->file)) {
631c845774SAndreas Gohr            $persist = file_get_contents($this->file);
641c845774SAndreas Gohr            $persist = unserialize($persist);
651c845774SAndreas Gohr        } else {
661c845774SAndreas Gohr            echo '2';
671c845774SAndreas Gohr        }
681c845774SAndreas Gohr
691c845774SAndreas Gohr        $fixme = $qc->getQCData($ID);
701c845774SAndreas Gohr
711c845774SAndreas Gohr        // when there are no quality problems we won't need the information
7263fbf510SAndreas Gohr        if (!is_array($fixme) || $this->isOk($fixme['err'])) {
7363fbf510SAndreas Gohr            if (isset($persist[$ID])) unset($persist[$ID]);
741c845774SAndreas Gohr        } else {
751c845774SAndreas Gohr            $persist[$ID] = $fixme;
761c845774SAndreas Gohr        }
771c845774SAndreas Gohr
781c845774SAndreas Gohr        $persist = serialize($persist);
791c845774SAndreas Gohr        file_put_contents($this->file, $persist);
801c845774SAndreas Gohr    }
811c845774SAndreas Gohr
821c845774SAndreas Gohr    /**
831c845774SAndreas Gohr     * checks an array to quality
841c845774SAndreas Gohr     *
851c845774SAndreas Gohr     * @return true when everything is alright
861c845774SAndreas Gohr     */
87293182bbSAnna Dabrowska    protected function isOk($arr)
8876bbc49cSAnna Dabrowska    {
891c845774SAndreas Gohr        return count(array_filter((array)$arr)) == 0;
901c845774SAndreas Gohr    }
911c845774SAndreas Gohr}
92