18596046dSAndreas Gohr<?php 28596046dSAndreas Gohr/** 38596046dSAndreas Gohr * Popularity Feedback Plugin 48596046dSAndreas Gohr * 58596046dSAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 68596046dSAndreas Gohr */ 78596046dSAndreas Gohr 8*29fc53cfSAndreas Gohrclass action_plugin_popularity extends Dokuwiki_Action_Plugin 9*29fc53cfSAndreas Gohr{ 1038479cbbSDominik Eckelmann 1138479cbbSDominik Eckelmann /** 1238479cbbSDominik Eckelmann * @var helper_plugin_popularity 1338479cbbSDominik Eckelmann */ 143dc2d50cSAndreas Gohr protected $helper; 158596046dSAndreas Gohr 16*29fc53cfSAndreas Gohr public function __construct() 17*29fc53cfSAndreas Gohr { 188596046dSAndreas Gohr $this->helper = $this->loadHelper('popularity', false); 198596046dSAndreas Gohr } 208596046dSAndreas Gohr 21*29fc53cfSAndreas Gohr /** @inheritdoc */ 22*29fc53cfSAndreas Gohr public function register(Doku_Event_Handler $controller) 23*29fc53cfSAndreas Gohr { 24*29fc53cfSAndreas Gohr $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', array()); 258596046dSAndreas Gohr } 268596046dSAndreas Gohr 273dc2d50cSAndreas Gohr /** 283dc2d50cSAndreas Gohr * Event handler 293dc2d50cSAndreas Gohr * 303dc2d50cSAndreas Gohr * @param Doku_Event $event 313dc2d50cSAndreas Gohr * @param $param 323dc2d50cSAndreas Gohr */ 33*29fc53cfSAndreas Gohr public function autosubmit(Doku_Event &$event, $param) 34*29fc53cfSAndreas Gohr { 358596046dSAndreas Gohr //Do we have to send the data now 36*29fc53cfSAndreas Gohr if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) { 378596046dSAndreas Gohr return; 388596046dSAndreas Gohr } 398596046dSAndreas Gohr 408596046dSAndreas Gohr //Actually send it 418596046dSAndreas Gohr $status = $this->helper->sendData($this->helper->gatherAsString()); 428596046dSAndreas Gohr 438596046dSAndreas Gohr if ($status !== '') { 448596046dSAndreas Gohr //If an error occured, log it 458596046dSAndreas Gohr io_saveFile($this->helper->autosubmitErrorFile, $status); 468596046dSAndreas Gohr } else { 478596046dSAndreas Gohr //If the data has been sent successfully, previous log of errors are useless 488596046dSAndreas Gohr @unlink($this->helper->autosubmitErrorFile); 498596046dSAndreas Gohr //Update the last time we sent data 508596046dSAndreas Gohr touch($this->helper->autosubmitFile); 518596046dSAndreas Gohr } 528596046dSAndreas Gohr 538596046dSAndreas Gohr $event->stopPropagation(); 548596046dSAndreas Gohr $event->preventDefault(); 558596046dSAndreas Gohr } 568596046dSAndreas Gohr 578596046dSAndreas Gohr /** 588596046dSAndreas Gohr * Check if it's time to send autosubmit data 595827ba0bSGuillaume Turri * (we should have check if autosubmit is enabled first) 608596046dSAndreas Gohr */ 61*29fc53cfSAndreas Gohr protected function isTooEarlyToSubmit() 62*29fc53cfSAndreas Gohr { 635827ba0bSGuillaume Turri $lastSubmit = $this->helper->lastSentTime(); 648596046dSAndreas Gohr return $lastSubmit + 24*60*60*30 > time(); 658596046dSAndreas Gohr } 668596046dSAndreas Gohr} 67