cron.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Elgg cron library.
  4. *
  5. * @package Elgg.Core
  6. * @subpackage Cron
  7. */
  8. /**
  9. * Cron initialization
  10. *
  11. * @return void
  12. * @access private
  13. */
  14. function _elgg_cron_init() {
  15. elgg_register_page_handler('cron', '_elgg_cron_page_handler');
  16. elgg_register_plugin_hook_handler('cron', 'all', '_elgg_cron_monitor', 1000);
  17. elgg_set_config('elgg_cron_periods', array(
  18. 'minute',
  19. 'fiveminute',
  20. 'fifteenmin',
  21. 'halfhour',
  22. 'hourly',
  23. 'daily',
  24. 'weekly',
  25. 'monthly',
  26. 'yearly',
  27. // reboot is deprecated and probably does not work
  28. 'reboot',
  29. ));
  30. elgg_register_admin_menu_item('administer', 'cron', 'statistics');
  31. }
  32. /**
  33. * Cron handler
  34. *
  35. * @param array $page Pages
  36. *
  37. * @return bool
  38. * @throws CronException
  39. * @access private
  40. */
  41. function _elgg_cron_page_handler($page) {
  42. if (!isset($page[0])) {
  43. forward();
  44. }
  45. $period = strtolower($page[0]);
  46. $allowed_periods = elgg_get_config('elgg_cron_periods');
  47. if (!in_array($period, $allowed_periods)) {
  48. throw new \CronException("$period is not a recognized cron period.");
  49. }
  50. // Get a list of parameters
  51. $params = array();
  52. $params['time'] = time();
  53. // Data to return to
  54. $old_stdout = "";
  55. ob_start();
  56. $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
  57. $std_out = ob_get_clean();
  58. echo $std_out . $old_stdout;
  59. return true;
  60. }
  61. /**
  62. * Record cron running
  63. *
  64. * @param string $hook Hook name
  65. * @param string $period Cron period
  66. * @param string $output Output content
  67. * @param array $params Hook parameters
  68. * @return void
  69. * @access private
  70. */
  71. function _elgg_cron_monitor($hook, $period, $output, $params) {
  72. $time = $params['time'];
  73. $periods = elgg_get_config('elgg_cron_periods');
  74. if (in_array($period, $periods)) {
  75. $key = "cron_latest:$period:ts";
  76. elgg_get_site_entity()->setPrivateSetting($key, $time);
  77. }
  78. }
  79. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  80. $events->registerHandler('init', 'system', '_elgg_cron_init');
  81. };