xref: /dokuwiki/lib/plugins/popularity/action.php (revision 8596046db551f4eda739892c98b35d4461ef0019)
1*8596046dSAndreas Gohr<?php
2*8596046dSAndreas Gohr/**
3*8596046dSAndreas Gohr * Popularity Feedback Plugin
4*8596046dSAndreas Gohr *
5*8596046dSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6*8596046dSAndreas Gohr */
7*8596046dSAndreas Gohr
8*8596046dSAndreas Gohrrequire_once(DOKU_PLUGIN.'action.php');
9*8596046dSAndreas Gohrrequire_once(DOKU_PLUGIN.'popularity/admin.php');
10*8596046dSAndreas Gohr
11*8596046dSAndreas Gohrclass action_plugin_popularity extends Dokuwiki_Action_Plugin {
12*8596046dSAndreas Gohr    var $helper;
13*8596046dSAndreas Gohr
14*8596046dSAndreas Gohr    function action_plugin_popularity(){
15*8596046dSAndreas Gohr        $this->helper = $this->loadHelper('popularity', false);
16*8596046dSAndreas Gohr    }
17*8596046dSAndreas Gohr
18*8596046dSAndreas Gohr    /**
19*8596046dSAndreas Gohr     * Register its handlers with the dokuwiki's event controller
20*8596046dSAndreas Gohr     */
21*8596046dSAndreas Gohr    function register(&$controller) {
22*8596046dSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER',  $this, '_autosubmit', array());
23*8596046dSAndreas Gohr    }
24*8596046dSAndreas Gohr
25*8596046dSAndreas Gohr    function _autosubmit(&$event, $param){
26*8596046dSAndreas Gohr        //Do we have to send the data now
27*8596046dSAndreas Gohr        if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){
28*8596046dSAndreas Gohr            return;
29*8596046dSAndreas Gohr        }
30*8596046dSAndreas Gohr
31*8596046dSAndreas Gohr        //Actually send it
32*8596046dSAndreas Gohr        $status = $this->helper->sendData( $this->helper->gatherAsString() );
33*8596046dSAndreas Gohr
34*8596046dSAndreas Gohr
35*8596046dSAndreas Gohr        if ( $status !== '' ){
36*8596046dSAndreas Gohr            //If an error occured, log it
37*8596046dSAndreas Gohr            io_saveFile( $this->helper->autosubmitErrorFile, $status );
38*8596046dSAndreas Gohr        } else {
39*8596046dSAndreas Gohr            //If the data has been sent successfully, previous log of errors are useless
40*8596046dSAndreas Gohr            @unlink($this->helper->autosubmitErrorFile);
41*8596046dSAndreas Gohr            //Update the last time we sent data
42*8596046dSAndreas Gohr            touch ( $this->helper->autosubmitFile );
43*8596046dSAndreas Gohr        }
44*8596046dSAndreas Gohr
45*8596046dSAndreas Gohr        $event->stopPropagation();
46*8596046dSAndreas Gohr        $event->preventDefault();
47*8596046dSAndreas Gohr    }
48*8596046dSAndreas Gohr
49*8596046dSAndreas Gohr    /**
50*8596046dSAndreas Gohr     * Check if it's time to send autosubmit data
51*8596046dSAndreas Gohr     * (we should have check the autosubmit is enabled first)
52*8596046dSAndreas Gohr     */
53*8596046dSAndreas Gohr    function _isTooEarlyToSubmit(){
54*8596046dSAndreas Gohr        $lastSubmit = @filemtime($this->helper->autosubmitFile);
55*8596046dSAndreas Gohr        return $lastSubmit + 24*60*60*30 > time();
56*8596046dSAndreas Gohr    }
57*8596046dSAndreas Gohr}
58