1 <?php
2 
3 use dokuwiki\Extension\ActionPlugin;
4 use dokuwiki\Extension\EventHandler;
5 use dokuwiki\Extension\Event;
6 
7 /**
8  * Popularity Feedback Plugin
9  *
10  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11  */
12 class action_plugin_popularity extends ActionPlugin
13 {
14     /**
15      * @var helper_plugin_popularity
16      */
17     protected $helper;
18 
19     public function __construct()
20     {
21         $this->helper = $this->loadHelper('popularity', false);
22     }
23 
24     /** @inheritdoc */
25     public function register(EventHandler $controller)
26     {
27         $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', []);
28     }
29 
30     /**
31      * Event handler
32      *
33      * @param Event $event
34      * @param $param
35      */
36     public function autosubmit(Event $event, $param)
37     {
38         //Do we have to send the data now
39         if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) {
40             return;
41         }
42 
43         //Actually send it
44         $status = $this->helper->sendData($this->helper->gatherAsString());
45 
46         if ($status !== '') {
47             //If an error occured, log it
48             io_saveFile($this->helper->autosubmitErrorFile, $status);
49         } else {
50             //If the data has been sent successfully, previous log of errors are useless
51             @unlink($this->helper->autosubmitErrorFile);
52             //Update the last time we sent data
53             touch($this->helper->autosubmitFile);
54         }
55 
56         $event->stopPropagation();
57         $event->preventDefault();
58     }
59 
60     /**
61      * Check if it's time to send autosubmit data
62      * (we should have check if autosubmit is enabled first)
63      */
64     protected function isTooEarlyToSubmit()
65     {
66         $lastSubmit = $this->helper->lastSentTime();
67         return $lastSubmit + 24 * 60 * 60 * 30 > time();
68     }
69 }
70