You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

70 lines
1.8 KiB

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