xref: /dokuwiki/lib/plugins/popularity/action.php (revision 5827ba0b8aa706e4201a3dc654b3c2cf141f6dd2)
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
88596046dSAndreas Gohrrequire_once(DOKU_PLUGIN.'action.php');
98596046dSAndreas Gohrrequire_once(DOKU_PLUGIN.'popularity/admin.php');
108596046dSAndreas Gohr
118596046dSAndreas Gohrclass action_plugin_popularity extends Dokuwiki_Action_Plugin {
128596046dSAndreas Gohr    var $helper;
138596046dSAndreas Gohr
148596046dSAndreas Gohr    function action_plugin_popularity(){
158596046dSAndreas Gohr        $this->helper = $this->loadHelper('popularity', false);
168596046dSAndreas Gohr    }
178596046dSAndreas Gohr
188596046dSAndreas Gohr    /**
198596046dSAndreas Gohr     * Register its handlers with the dokuwiki's event controller
208596046dSAndreas Gohr     */
218596046dSAndreas Gohr    function register(&$controller) {
228596046dSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER',  $this, '_autosubmit', array());
238596046dSAndreas Gohr    }
248596046dSAndreas Gohr
258596046dSAndreas Gohr    function _autosubmit(&$event, $param){
268596046dSAndreas Gohr        //Do we have to send the data now
278596046dSAndreas Gohr        if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){
288596046dSAndreas Gohr            return;
298596046dSAndreas Gohr        }
308596046dSAndreas Gohr
318596046dSAndreas Gohr        //Actually send it
328596046dSAndreas Gohr        $status = $this->helper->sendData( $this->helper->gatherAsString() );
338596046dSAndreas Gohr
348596046dSAndreas Gohr
358596046dSAndreas Gohr        if ( $status !== '' ){
368596046dSAndreas Gohr            //If an error occured, log it
378596046dSAndreas Gohr            io_saveFile( $this->helper->autosubmitErrorFile, $status );
388596046dSAndreas Gohr        } else {
398596046dSAndreas Gohr            //If the data has been sent successfully, previous log of errors are useless
408596046dSAndreas Gohr            @unlink($this->helper->autosubmitErrorFile);
418596046dSAndreas Gohr            //Update the last time we sent data
428596046dSAndreas Gohr            touch ( $this->helper->autosubmitFile );
438596046dSAndreas Gohr        }
448596046dSAndreas Gohr
458596046dSAndreas Gohr        $event->stopPropagation();
468596046dSAndreas Gohr        $event->preventDefault();
478596046dSAndreas Gohr    }
488596046dSAndreas Gohr
498596046dSAndreas Gohr    /**
508596046dSAndreas Gohr     * Check if it's time to send autosubmit data
51*5827ba0bSGuillaume Turri     * (we should have check if autosubmit is enabled first)
528596046dSAndreas Gohr     */
538596046dSAndreas Gohr    function _isTooEarlyToSubmit(){
54*5827ba0bSGuillaume Turri        $lastSubmit = $this->helper->lastSentTime();
558596046dSAndreas Gohr        return $lastSubmit + 24*60*60*30 > time();
568596046dSAndreas Gohr    }
578596046dSAndreas Gohr}
58