Elgg  Version 2.3
cron.php
Go to the documentation of this file.
1 <?php
15 function _elgg_cron_init() {
16  elgg_register_page_handler('cron', '_elgg_cron_page_handler');
17 
18  elgg_register_plugin_hook_handler('cron', 'all', '_elgg_cron_monitor', 1000);
19 
20  elgg_set_config('elgg_cron_periods', array(
21  'minute',
22  'fiveminute',
23  'fifteenmin',
24  'halfhour',
25  'hourly',
26  'daily',
27  'weekly',
28  'monthly',
29  'yearly',
30  // reboot is deprecated and probably does not work
31  'reboot',
32  ));
33 
34  elgg_register_admin_menu_item('administer', 'cron', 'statistics');
35 }
36 
51 function _elgg_cron_run() {
52  $now = time();
53  $params = array();
54  $params['time'] = $now;
55 
56  $all_std_out = "";
57 
58  $periods = array(
59  'minute' => 60,
60  'fiveminute' => 300,
61  'fifteenmin' => 900,
62  'halfhour' => 1800,
63  'hourly' => 3600,
64  'daily' => 86400,
65  'weekly' => 604800,
66  'monthly' => 2628000,
67  'yearly' => 31536000,
68  'reboot' => 31536000,
69  );
70 
71  foreach ($periods as $period => $interval) {
72  $key = "cron_latest:$period:ts";
73  $ts = elgg_get_site_entity()->getPrivateSetting($key);
74  $deadline = $ts + $interval;
75 
76  if ($now > $deadline) {
77  $msg_key = "cron_latest:$period:msg";
78  $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]);
79  elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
80 
81  ob_start();
82 
83  $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, '');
84  $std_out = ob_get_clean();
85 
86  $period_std_out = $std_out . $old_stdout;
87  $all_std_out .= $period_std_out;
88 
89  elgg_get_site_entity()->setPrivateSetting($msg_key, $period_std_out);
90  }
91  }
92 
93  echo $all_std_out;
94 }
95 
105 function _elgg_cron_page_handler($page) {
106  if (!isset($page[0])) {
107  forward();
108  }
109 
110  $period = strtolower($page[0]);
111 
112  $allowed_periods = elgg_get_config('elgg_cron_periods');
113 
114  if (($period != 'run') && !in_array($period, $allowed_periods)) {
115  throw new \CronException("$period is not a recognized cron period.");
116  }
117 
118  if ($period == 'run') {
119  _elgg_cron_run();
120  } else {
121  // Get a list of parameters
122  $params = array();
123  $params['time'] = time();
124 
125  // Data to return to
126  $old_stdout = "";
127  ob_start();
128 
129  $msg_key = "cron_latest:$period:msg";
130  $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]);
131  elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
132 
133  $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout);
134  $std_out = ob_get_clean();
135 
136  $msg = $std_out . $old_stdout;
137  echo $msg;
138 
139  elgg_get_site_entity()->setPrivateSetting($msg_key, $msg);
140  }
141  return true;
142 }
143 
154 function _elgg_cron_monitor($hook, $period, $output, $params) {
155  $time = $params['time'];
156  $periods = elgg_get_config('elgg_cron_periods');
157 
158  if (in_array($period, $periods)) {
159  $key = "cron_latest:$period:ts";
160  elgg_get_site_entity()->setPrivateSetting($key, $time);
161  echo elgg_echo('admin:cron:complete', [$period, date('r', $time)]);
162  }
163 }
164 
165 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
166  $events->registerHandler('init', 'system', '_elgg_cron_init');
167 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_get_site_entity($site_guid=0)
Get an entity (default is current site)
Definition: sites.php:18
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
_elgg_cron_page_handler($page)
Cron handler.
Definition: cron.php:105
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:425
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
_elgg_cron_run()
Cron run.
Definition: cron.php:51
$params
Definition: login.php:72
elgg_register_admin_menu_item($section, $menu_id, $parent_id=null, $priority=100)
Add an admin area section or child section.
Definition: admin.php:140
$key
Definition: summary.php:34
elgg_set_config($name, $value)
Set an Elgg configuration value.
elgg echo
Translates a string.
Definition: languages.js:48
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:826
_elgg_cron_monitor($hook, $period, $output, $params)
Record cron running.
Definition: cron.php:154
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
_elgg_cron_init()
Cron initialization.
Definition: cron.php:15
$periods
Cron statistics.
Definition: cron.php:7
elgg subtext time
$output
Definition: item.php:10