xref: /dokuwiki/lib/plugins/popularity/action.php (revision a087da71ea972d05ca659a1e9406c87aa7ae258b)
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    protected $helper;
17
18    public function __construct(){
19        $this->helper = $this->loadHelper('popularity', false);
20    }
21
22    /**
23     * Register its handlers with the dokuwiki's event controller
24     */
25    public function register(Doku_Event_Handler $controller) {
26        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER',  $this, '_autosubmit', array());
27    }
28
29    /**
30     * Event handler
31     *
32     * @param Doku_Event $event
33     * @param $param
34     */
35    public function _autosubmit(Doku_Event &$event, $param){
36        //Do we have to send the data now
37        if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){
38            return;
39        }
40
41        //Actually send it
42        $status = $this->helper->sendData( $this->helper->gatherAsString() );
43
44        if ( $status !== '' ){
45            //If an error occured, log it
46            io_saveFile( $this->helper->autosubmitErrorFile, $status );
47        } else {
48            //If the data has been sent successfully, previous log of errors are useless
49            @unlink($this->helper->autosubmitErrorFile);
50            //Update the last time we sent data
51            touch ( $this->helper->autosubmitFile );
52        }
53
54        $event->stopPropagation();
55        $event->preventDefault();
56    }
57
58    /**
59     * Check if it's time to send autosubmit data
60     * (we should have check if autosubmit is enabled first)
61     */
62    protected function _isTooEarlyToSubmit(){
63        $lastSubmit = $this->helper->lastSentTime();
64        return $lastSubmit + 24*60*60*30 > time();
65    }
66}
67