xref: /dokuwiki/lib/plugins/popularity/action.php (revision 8553d24d33ab5f260c6e19959de764dd8472d438)
18596046dSAndreas Gohr<?php
2*8553d24dSAndreas Gohr
3*8553d24dSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4*8553d24dSAndreas Gohruse dokuwiki\Extension\EventHandler;
5*8553d24dSAndreas Gohruse dokuwiki\Extension\Event;
68596046dSAndreas Gohr/**
78596046dSAndreas Gohr * Popularity Feedback Plugin
88596046dSAndreas Gohr *
98596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
108596046dSAndreas Gohr */
11*8553d24dSAndreas Gohrclass action_plugin_popularity extends ActionPlugin
1229fc53cfSAndreas Gohr{
1338479cbbSDominik Eckelmann
1438479cbbSDominik Eckelmann    /**
1538479cbbSDominik Eckelmann     * @var helper_plugin_popularity
1638479cbbSDominik Eckelmann     */
173dc2d50cSAndreas Gohr    protected $helper;
188596046dSAndreas Gohr
1929fc53cfSAndreas Gohr    public function __construct()
2029fc53cfSAndreas Gohr    {
218596046dSAndreas Gohr        $this->helper = $this->loadHelper('popularity', false);
228596046dSAndreas Gohr    }
238596046dSAndreas Gohr
2429fc53cfSAndreas Gohr    /** @inheritdoc */
25*8553d24dSAndreas Gohr    public function register(EventHandler $controller)
2629fc53cfSAndreas Gohr    {
2754cc7aa4SAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', []);
288596046dSAndreas Gohr    }
298596046dSAndreas Gohr
303dc2d50cSAndreas Gohr    /**
313dc2d50cSAndreas Gohr     * Event handler
323dc2d50cSAndreas Gohr     *
333dc2d50cSAndreas Gohr     * @param Doku_Event $event
343dc2d50cSAndreas Gohr     * @param $param
353dc2d50cSAndreas Gohr     */
36*8553d24dSAndreas Gohr    public function autosubmit(Event &$event, $param)
3729fc53cfSAndreas Gohr    {
388596046dSAndreas Gohr        //Do we have to send the data now
3929fc53cfSAndreas Gohr        if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) {
408596046dSAndreas Gohr            return;
418596046dSAndreas Gohr        }
428596046dSAndreas Gohr
438596046dSAndreas Gohr        //Actually send it
448596046dSAndreas Gohr        $status = $this->helper->sendData($this->helper->gatherAsString());
458596046dSAndreas Gohr
468596046dSAndreas Gohr        if ($status !== '') {
478596046dSAndreas Gohr            //If an error occured, log it
488596046dSAndreas Gohr            io_saveFile($this->helper->autosubmitErrorFile, $status);
498596046dSAndreas Gohr        } else {
508596046dSAndreas Gohr            //If the data has been sent successfully, previous log of errors are useless
518596046dSAndreas Gohr            @unlink($this->helper->autosubmitErrorFile);
528596046dSAndreas Gohr            //Update the last time we sent data
538596046dSAndreas Gohr            touch($this->helper->autosubmitFile);
548596046dSAndreas Gohr        }
558596046dSAndreas Gohr
568596046dSAndreas Gohr        $event->stopPropagation();
578596046dSAndreas Gohr        $event->preventDefault();
588596046dSAndreas Gohr    }
598596046dSAndreas Gohr
608596046dSAndreas Gohr    /**
618596046dSAndreas Gohr     * Check if it's time to send autosubmit data
625827ba0bSGuillaume Turri     * (we should have check if autosubmit is enabled first)
638596046dSAndreas Gohr     */
6429fc53cfSAndreas Gohr    protected function isTooEarlyToSubmit()
6529fc53cfSAndreas Gohr    {
665827ba0bSGuillaume Turri        $lastSubmit = $this->helper->lastSentTime();
678596046dSAndreas Gohr        return $lastSubmit + 24*60*60*30 > time();
688596046dSAndreas Gohr    }
698596046dSAndreas Gohr}
70