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