action.php 1.7 KB

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