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