xref: /dokuwiki/lib/plugins/popularity/action.php (revision 54cc7aa41e0f453bd6887b0e79242a139d84a47a)
18596046dSAndreas Gohr<?php
28596046dSAndreas Gohr/**
38596046dSAndreas Gohr * Popularity Feedback Plugin
48596046dSAndreas Gohr *
58596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
68596046dSAndreas Gohr */
78596046dSAndreas Gohr
8d0a7b199SAndreas Gohrclass action_plugin_popularity extends DokuWiki_Action_Plugin
929fc53cfSAndreas Gohr{
1038479cbbSDominik Eckelmann
1138479cbbSDominik Eckelmann    /**
1238479cbbSDominik Eckelmann     * @var helper_plugin_popularity
1338479cbbSDominik Eckelmann     */
143dc2d50cSAndreas Gohr    protected $helper;
158596046dSAndreas Gohr
1629fc53cfSAndreas Gohr    public function __construct()
1729fc53cfSAndreas Gohr    {
188596046dSAndreas Gohr        $this->helper = $this->loadHelper('popularity', false);
198596046dSAndreas Gohr    }
208596046dSAndreas Gohr
2129fc53cfSAndreas Gohr    /** @inheritdoc */
2229fc53cfSAndreas Gohr    public function register(Doku_Event_Handler $controller)
2329fc53cfSAndreas Gohr    {
24*54cc7aa4SAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', []);
258596046dSAndreas Gohr    }
268596046dSAndreas Gohr
273dc2d50cSAndreas Gohr    /**
283dc2d50cSAndreas Gohr     * Event handler
293dc2d50cSAndreas Gohr     *
303dc2d50cSAndreas Gohr     * @param Doku_Event $event
313dc2d50cSAndreas Gohr     * @param $param
323dc2d50cSAndreas Gohr     */
3329fc53cfSAndreas Gohr    public function autosubmit(Doku_Event &$event, $param)
3429fc53cfSAndreas Gohr    {
358596046dSAndreas Gohr        //Do we have to send the data now
3629fc53cfSAndreas Gohr        if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) {
378596046dSAndreas Gohr            return;
388596046dSAndreas Gohr        }
398596046dSAndreas Gohr
408596046dSAndreas Gohr        //Actually send it
418596046dSAndreas Gohr        $status = $this->helper->sendData($this->helper->gatherAsString());
428596046dSAndreas Gohr
438596046dSAndreas Gohr        if ($status !== '') {
448596046dSAndreas Gohr            //If an error occured, log it
458596046dSAndreas Gohr            io_saveFile($this->helper->autosubmitErrorFile, $status);
468596046dSAndreas Gohr        } else {
478596046dSAndreas Gohr            //If the data has been sent successfully, previous log of errors are useless
488596046dSAndreas Gohr            @unlink($this->helper->autosubmitErrorFile);
498596046dSAndreas Gohr            //Update the last time we sent data
508596046dSAndreas Gohr            touch($this->helper->autosubmitFile);
518596046dSAndreas Gohr        }
528596046dSAndreas Gohr
538596046dSAndreas Gohr        $event->stopPropagation();
548596046dSAndreas Gohr        $event->preventDefault();
558596046dSAndreas Gohr    }
568596046dSAndreas Gohr
578596046dSAndreas Gohr    /**
588596046dSAndreas Gohr     * Check if it's time to send autosubmit data
595827ba0bSGuillaume Turri     * (we should have check if autosubmit is enabled first)
608596046dSAndreas Gohr     */
6129fc53cfSAndreas Gohr    protected function isTooEarlyToSubmit()
6229fc53cfSAndreas Gohr    {
635827ba0bSGuillaume Turri        $lastSubmit = $this->helper->lastSentTime();
648596046dSAndreas Gohr        return $lastSubmit + 24*60*60*30 > time();
658596046dSAndreas Gohr    }
668596046dSAndreas Gohr}
67