|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file concerns itself with scheduled tasks management. |
|
5
|
|
|
* |
|
6
|
|
|
* Simple Machines Forum (SMF) |
|
7
|
|
|
* |
|
8
|
|
|
* @package SMF |
|
9
|
|
|
* @author Simple Machines http://www.simplemachines.org |
|
10
|
|
|
* @copyright 2016 Simple Machines and individual contributors |
|
11
|
|
|
* @license http://www.simplemachines.org/about/smf/license.php BSD |
|
12
|
|
|
* |
|
13
|
|
|
* @version 2.1 Beta 3 |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
if (!defined('SMF')) |
|
17
|
|
|
die('No direct access...'); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Scheduled tasks management dispatcher. This function checks permissions and delegates |
|
21
|
|
|
* to the appropriate function based on the sub-action. |
|
22
|
|
|
* Everything here requires admin_forum permission. |
|
23
|
|
|
* |
|
24
|
|
|
* @uses ManageScheduledTasks template file |
|
25
|
|
|
* @uses ManageScheduledTasks language file |
|
26
|
|
|
*/ |
|
27
|
|
|
function ManageScheduledTasks() |
|
28
|
|
|
{ |
|
29
|
|
|
global $context, $txt; |
|
30
|
|
|
|
|
31
|
|
|
isAllowedTo('admin_forum'); |
|
32
|
|
|
|
|
33
|
|
|
loadLanguage('ManageScheduledTasks'); |
|
34
|
|
|
loadTemplate('ManageScheduledTasks'); |
|
35
|
|
|
|
|
36
|
|
|
$subActions = array( |
|
37
|
|
|
'taskedit' => 'EditTask', |
|
38
|
|
|
'tasklog' => 'TaskLog', |
|
39
|
|
|
'tasks' => 'ScheduledTasks', |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
// We need to find what's the action. |
|
43
|
|
|
if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']])) |
|
44
|
|
|
$context['sub_action'] = $_REQUEST['sa']; |
|
45
|
|
|
else |
|
46
|
|
|
$context['sub_action'] = 'tasks'; |
|
47
|
|
|
|
|
48
|
|
|
// Now for the lovely tabs. That we all love. |
|
49
|
|
|
$context[$context['admin_menu_name']]['tab_data'] = array( |
|
50
|
|
|
'title' => $txt['scheduled_tasks_title'], |
|
51
|
|
|
'help' => '', |
|
52
|
|
|
'description' => $txt['maintain_info'], |
|
53
|
|
|
'tabs' => array( |
|
54
|
|
|
'tasks' => array( |
|
55
|
|
|
'description' => $txt['maintain_tasks_desc'], |
|
56
|
|
|
), |
|
57
|
|
|
'tasklog' => array( |
|
58
|
|
|
'description' => $txt['scheduled_log_desc'], |
|
59
|
|
|
), |
|
60
|
|
|
), |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
call_integration_hook('integrate_manage_scheduled_tasks', array(&$subActions)); |
|
64
|
|
|
|
|
65
|
|
|
// Call it. |
|
66
|
|
|
call_helper($subActions[$context['sub_action']]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* List all the scheduled task in place on the forum. |
|
71
|
|
|
* |
|
72
|
|
|
* @uses ManageScheduledTasks template, view_scheduled_tasks sub-template |
|
73
|
|
|
*/ |
|
74
|
|
|
function ScheduledTasks() |
|
75
|
|
|
{ |
|
76
|
|
|
global $context, $txt, $sourcedir, $smcFunc, $scripturl; |
|
77
|
|
|
|
|
78
|
|
|
// Mama, setup the template first - cause it's like the most important bit, like pickle in a sandwich. |
|
79
|
|
|
// ... ironically I don't like pickle. </grudge> |
|
80
|
|
|
$context['sub_template'] = 'view_scheduled_tasks'; |
|
81
|
|
|
$context['page_title'] = $txt['maintain_tasks']; |
|
82
|
|
|
|
|
83
|
|
|
// Saving changes? |
|
84
|
|
|
if (isset($_REQUEST['save']) && isset($_POST['enable_task'])) |
|
85
|
|
|
{ |
|
86
|
|
|
checkSession(); |
|
87
|
|
|
|
|
88
|
|
|
// We'll recalculate the dates at the end! |
|
89
|
|
|
require_once($sourcedir . '/ScheduledTasks.php'); |
|
90
|
|
|
|
|
91
|
|
|
// Enable and disable as required. |
|
92
|
|
|
$enablers = array(0); |
|
93
|
|
|
foreach ($_POST['enable_task'] as $id => $enabled) |
|
94
|
|
|
if ($enabled) |
|
95
|
|
|
$enablers[] = (int) $id; |
|
96
|
|
|
|
|
97
|
|
|
// Do the update! |
|
98
|
|
|
$smcFunc['db_query']('', ' |
|
99
|
|
|
UPDATE {db_prefix}scheduled_tasks |
|
100
|
|
|
SET disabled = CASE WHEN id_task IN ({array_int:id_task_enable}) THEN 0 ELSE 1 END', |
|
101
|
|
|
array( |
|
102
|
|
|
'id_task_enable' => $enablers, |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
// Update the "allow_expire_redirect" setting... |
|
107
|
|
|
$get_info = $smcFunc['db_query']('', ' |
|
108
|
|
|
SELECT disabled |
|
109
|
|
|
FROM {db_prefix}scheduled_tasks |
|
110
|
|
|
WHERE task = {string:remove_redirect}', |
|
111
|
|
|
array( |
|
112
|
|
|
'remove_redirect' => 'remove_topic_redirect' |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$temp = $smcFunc['db_fetch_assoc']($get_info); |
|
117
|
|
|
$task_disabled = !empty($temp['disabled']) ? 0 : 1; |
|
118
|
|
|
$smcFunc['db_free_result']($get_info); |
|
119
|
|
|
|
|
120
|
|
|
updateSettings(array('allow_expire_redirect' => $task_disabled)); |
|
121
|
|
|
|
|
122
|
|
|
// Pop along... |
|
123
|
|
|
CalculateNextTrigger(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Want to run any of the tasks? |
|
127
|
|
|
if (isset($_REQUEST['run']) && isset($_POST['run_task'])) |
|
128
|
|
|
{ |
|
129
|
|
|
$task_string = ''; |
|
130
|
|
|
|
|
131
|
|
|
// Lets figure out which ones they want to run. |
|
132
|
|
|
$tasks = array(); |
|
133
|
|
|
foreach ($_POST['run_task'] as $task => $dummy) |
|
134
|
|
|
$tasks[] = (int) $task; |
|
135
|
|
|
|
|
136
|
|
|
// Load up the tasks. |
|
137
|
|
|
$request = $smcFunc['db_query']('', ' |
|
138
|
|
|
SELECT id_task, task, callable |
|
139
|
|
|
FROM {db_prefix}scheduled_tasks |
|
140
|
|
|
WHERE id_task IN ({array_int:tasks}) |
|
141
|
|
|
LIMIT {int:limit}', |
|
142
|
|
|
array( |
|
143
|
|
|
'tasks' => $tasks, |
|
144
|
|
|
'limit' => count($tasks), |
|
145
|
|
|
) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
// Lets get it on! |
|
149
|
|
|
require_once($sourcedir . '/ScheduledTasks.php'); |
|
150
|
|
|
ignore_user_abort(true); |
|
151
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
152
|
|
|
{ |
|
153
|
|
|
// What kind of task are we handling? |
|
154
|
|
View Code Duplication |
if (!empty($row['callable'])) |
|
|
|
|
|
|
155
|
|
|
$task_string = $row['callable']; |
|
156
|
|
|
|
|
157
|
|
|
// Default SMF task or old mods? |
|
158
|
|
|
elseif (function_exists('scheduled_' . $row['task'])) |
|
159
|
|
|
$task_string = 'scheduled_' . $row['task']; |
|
160
|
|
|
|
|
161
|
|
|
// One last resource, the task name. |
|
162
|
|
|
elseif (!empty($row['task'])) |
|
163
|
|
|
$task_string = $row['task']; |
|
164
|
|
|
|
|
165
|
|
|
$start_time = microtime(); |
|
166
|
|
|
// The functions got to exist for us to use it. |
|
167
|
|
|
if (empty($task_string)) |
|
168
|
|
|
continue; |
|
169
|
|
|
|
|
170
|
|
|
// Try to stop a timeout, this would be bad... |
|
171
|
|
|
@set_time_limit(300); |
|
|
|
|
|
|
172
|
|
|
if (function_exists('apache_reset_timeout')) |
|
173
|
|
|
@apache_reset_timeout(); |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
// Get the callable. |
|
176
|
|
|
$callable_task = call_helper($task_string, true); |
|
177
|
|
|
|
|
178
|
|
|
// Perform the task. |
|
179
|
|
|
if (!empty($callable_task)) |
|
180
|
|
|
$completed = call_user_func($callable_task); |
|
181
|
|
|
|
|
182
|
|
|
else |
|
183
|
|
|
$completed = false; |
|
184
|
|
|
|
|
185
|
|
|
// Log that we did it ;) |
|
186
|
|
View Code Duplication |
if ($completed) |
|
|
|
|
|
|
187
|
|
|
{ |
|
188
|
|
|
$total_time = round(array_sum(explode(' ', microtime())) - array_sum(explode(' ', $start_time)), 3); |
|
189
|
|
|
$smcFunc['db_insert']('', |
|
190
|
|
|
'{db_prefix}log_scheduled_tasks', |
|
191
|
|
|
array('id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float'), |
|
192
|
|
|
array($row['id_task'], time(), $total_time), |
|
193
|
|
|
array('id_task') |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
$smcFunc['db_free_result']($request); |
|
198
|
|
|
|
|
199
|
|
|
// If we had any errors, push them to session so we can pick them up next time to tell the user. |
|
200
|
|
|
if (!empty($context['scheduled_errors'])) |
|
201
|
|
|
$_SESSION['st_error'] = $context['scheduled_errors']; |
|
202
|
|
|
|
|
203
|
|
|
redirectexit('action=admin;area=scheduledtasks;done'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
if (isset($_SESSION['st_error'])) |
|
207
|
|
|
{ |
|
208
|
|
|
$context['scheduled_errors'] = $_SESSION['st_error']; |
|
209
|
|
|
unset ($_SESSION['st_error']); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$listOptions = array( |
|
213
|
|
|
'id' => 'scheduled_tasks', |
|
214
|
|
|
'title' => $txt['maintain_tasks'], |
|
215
|
|
|
'base_href' => $scripturl . '?action=admin;area=scheduledtasks', |
|
216
|
|
|
'get_items' => array( |
|
217
|
|
|
'function' => 'list_getScheduledTasks', |
|
218
|
|
|
), |
|
219
|
|
|
'columns' => array( |
|
220
|
|
|
'name' => array( |
|
221
|
|
|
'header' => array( |
|
222
|
|
|
'value' => $txt['scheduled_tasks_name'], |
|
223
|
|
|
'style' => 'width: 40%;', |
|
224
|
|
|
), |
|
225
|
|
|
'data' => array( |
|
226
|
|
|
'sprintf' => array( |
|
227
|
|
|
'format' => ' |
|
228
|
|
|
<a href="' . $scripturl . '?action=admin;area=scheduledtasks;sa=taskedit;tid=%1$d">%2$s</a><br><span class="smalltext">%3$s</span>', |
|
229
|
|
|
'params' => array( |
|
230
|
|
|
'id' => false, |
|
231
|
|
|
'name' => false, |
|
232
|
|
|
'desc' => false, |
|
233
|
|
|
), |
|
234
|
|
|
), |
|
235
|
|
|
), |
|
236
|
|
|
), |
|
237
|
|
|
'next_due' => array( |
|
238
|
|
|
'header' => array( |
|
239
|
|
|
'value' => $txt['scheduled_tasks_next_time'], |
|
240
|
|
|
), |
|
241
|
|
|
'data' => array( |
|
242
|
|
|
'db' => 'next_time', |
|
243
|
|
|
'class' => 'smalltext', |
|
244
|
|
|
), |
|
245
|
|
|
), |
|
246
|
|
|
'regularity' => array( |
|
247
|
|
|
'header' => array( |
|
248
|
|
|
'value' => $txt['scheduled_tasks_regularity'], |
|
249
|
|
|
), |
|
250
|
|
|
'data' => array( |
|
251
|
|
|
'db' => 'regularity', |
|
252
|
|
|
'class' => 'smalltext', |
|
253
|
|
|
), |
|
254
|
|
|
), |
|
255
|
|
|
'run_now' => array( |
|
256
|
|
|
'header' => array( |
|
257
|
|
|
'value' => $txt['scheduled_tasks_run_now'], |
|
258
|
|
|
'style' => 'width: 12%;', |
|
259
|
|
|
'class' => 'centercol', |
|
260
|
|
|
), |
|
261
|
|
|
'data' => array( |
|
262
|
|
|
'sprintf' => array( |
|
263
|
|
|
'format' => |
|
264
|
|
|
'<input type="checkbox" name="run_task[%1$d]" id="run_task_%1$d" class="input_check">', |
|
265
|
|
|
'params' => array( |
|
266
|
|
|
'id' => false, |
|
267
|
|
|
), |
|
268
|
|
|
), |
|
269
|
|
|
'class' => 'centercol', |
|
270
|
|
|
), |
|
271
|
|
|
), |
|
272
|
|
|
'enabled' => array( |
|
273
|
|
|
'header' => array( |
|
274
|
|
|
'value' => $txt['scheduled_tasks_enabled'], |
|
275
|
|
|
'style' => 'width: 6%;', |
|
276
|
|
|
'class' => 'centercol', |
|
277
|
|
|
), |
|
278
|
|
|
'data' => array( |
|
279
|
|
|
'sprintf' => array( |
|
280
|
|
|
'format' => |
|
281
|
|
|
'<input type="hidden" name="enable_task[%1$d]" id="task_%1$d" value="0"><input type="checkbox" name="enable_task[%1$d]" id="task_check_%1$d" %2$s class="input_check">', |
|
282
|
|
|
'params' => array( |
|
283
|
|
|
'id' => false, |
|
284
|
|
|
'checked_state' => false, |
|
285
|
|
|
), |
|
286
|
|
|
), |
|
287
|
|
|
'class' => 'centercol', |
|
288
|
|
|
), |
|
289
|
|
|
), |
|
290
|
|
|
), |
|
291
|
|
|
'form' => array( |
|
292
|
|
|
'href' => $scripturl . '?action=admin;area=scheduledtasks', |
|
293
|
|
|
), |
|
294
|
|
|
'additional_rows' => array( |
|
295
|
|
|
array( |
|
296
|
|
|
'position' => 'below_table_data', |
|
297
|
|
|
'value' => ' |
|
298
|
|
|
<input type="submit" name="save" value="' . $txt['scheduled_tasks_save_changes'] . '" class="button_submit"> |
|
299
|
|
|
<input type="submit" name="run" value="' . $txt['scheduled_tasks_run_now'] . '" class="button_submit">', |
|
300
|
|
|
), |
|
301
|
|
|
array( |
|
302
|
|
|
'position' => 'after_title', |
|
303
|
|
|
'value' => $txt['scheduled_tasks_time_offset'], |
|
304
|
|
|
'class' => 'windowbg2', |
|
305
|
|
|
), |
|
306
|
|
|
), |
|
307
|
|
|
); |
|
308
|
|
|
|
|
309
|
|
|
require_once($sourcedir . '/Subs-List.php'); |
|
310
|
|
|
createList($listOptions); |
|
311
|
|
|
|
|
312
|
|
|
$context['sub_template'] = 'view_scheduled_tasks'; |
|
313
|
|
|
|
|
314
|
|
|
$context['tasks_were_run'] = isset($_GET['done']); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Callback function for createList() in ScheduledTasks(). |
|
319
|
|
|
* |
|
320
|
|
|
* @param int $start The item to start with (not used here) |
|
321
|
|
|
* @param int $items_per_page The number of items to display per page (not used here) |
|
322
|
|
|
* @param string $sort A string indicating how to sort things (not used here) |
|
323
|
|
|
* @return array An array of information about available scheduled tasks |
|
324
|
|
|
*/ |
|
325
|
|
|
function list_getScheduledTasks($start, $items_per_page, $sort) |
|
|
|
|
|
|
326
|
|
|
{ |
|
327
|
|
|
global $smcFunc, $txt; |
|
328
|
|
|
|
|
329
|
|
|
$request = $smcFunc['db_query']('', ' |
|
330
|
|
|
SELECT id_task, next_time, time_offset, time_regularity, time_unit, disabled, task |
|
331
|
|
|
FROM {db_prefix}scheduled_tasks', |
|
332
|
|
|
array( |
|
333
|
|
|
) |
|
334
|
|
|
); |
|
335
|
|
|
$known_tasks = array(); |
|
336
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
337
|
|
|
{ |
|
338
|
|
|
// Find the next for regularity - don't offset as it's always server time! |
|
339
|
|
|
$offset = sprintf($txt['scheduled_task_reg_starting'], date('H:i', $row['time_offset'])); |
|
340
|
|
|
$repeating = sprintf($txt['scheduled_task_reg_repeating'], $row['time_regularity'], $txt['scheduled_task_reg_unit_' . $row['time_unit']]); |
|
341
|
|
|
|
|
342
|
|
|
$known_tasks[] = array( |
|
343
|
|
|
'id' => $row['id_task'], |
|
344
|
|
|
'function' => $row['task'], |
|
345
|
|
|
'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'], |
|
346
|
|
|
'desc' => isset($txt['scheduled_task_desc_' . $row['task']]) ? $txt['scheduled_task_desc_' . $row['task']] : '', |
|
347
|
|
|
'next_time' => $row['disabled'] ? $txt['scheduled_tasks_na'] : timeformat(($row['next_time'] == 0 ? time() : $row['next_time']), true, 'server'), |
|
348
|
|
|
'disabled' => $row['disabled'], |
|
349
|
|
|
'checked_state' => $row['disabled'] ? '' : 'checked', |
|
350
|
|
|
'regularity' => $offset . ', ' . $repeating, |
|
351
|
|
|
); |
|
352
|
|
|
} |
|
353
|
|
|
$smcFunc['db_free_result']($request); |
|
354
|
|
|
|
|
355
|
|
|
return $known_tasks; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Function for editing a task. |
|
360
|
|
|
* |
|
361
|
|
|
* @uses ManageScheduledTasks template, edit_scheduled_tasks sub-template |
|
362
|
|
|
*/ |
|
363
|
|
|
function EditTask() |
|
364
|
|
|
{ |
|
365
|
|
|
global $context, $txt, $sourcedir, $smcFunc; |
|
366
|
|
|
|
|
367
|
|
|
// Just set up some lovely context stuff. |
|
368
|
|
|
$context[$context['admin_menu_name']]['current_subsection'] = 'tasks'; |
|
369
|
|
|
$context['sub_template'] = 'edit_scheduled_tasks'; |
|
370
|
|
|
$context['page_title'] = $txt['scheduled_task_edit']; |
|
371
|
|
|
$context['server_time'] = timeformat(time(), false, 'server'); |
|
372
|
|
|
|
|
373
|
|
|
// Cleaning... |
|
374
|
|
|
if (!isset($_GET['tid'])) |
|
375
|
|
|
fatal_lang_error('no_access', false); |
|
|
|
|
|
|
376
|
|
|
$_GET['tid'] = (int) $_GET['tid']; |
|
377
|
|
|
|
|
378
|
|
|
// Saving? |
|
379
|
|
|
if (isset($_GET['save'])) |
|
380
|
|
|
{ |
|
381
|
|
|
checkSession(); |
|
382
|
|
|
validateToken('admin-st'); |
|
383
|
|
|
|
|
384
|
|
|
// We'll need this for calculating the next event. |
|
385
|
|
|
require_once($sourcedir . '/ScheduledTasks.php'); |
|
386
|
|
|
|
|
387
|
|
|
// Do we have a valid offset? |
|
388
|
|
|
preg_match('~(\d{1,2}):(\d{1,2})~', $_POST['offset'], $matches); |
|
389
|
|
|
|
|
390
|
|
|
// If a half is empty then assume zero offset! |
|
391
|
|
View Code Duplication |
if (!isset($matches[2]) || $matches[2] > 59) |
|
|
|
|
|
|
392
|
|
|
$matches[2] = 0; |
|
393
|
|
View Code Duplication |
if (!isset($matches[1]) || $matches[1] > 23) |
|
|
|
|
|
|
394
|
|
|
$matches[1] = 0; |
|
395
|
|
|
|
|
396
|
|
|
// Now the offset is easy; easy peasy - except we need to offset by a few hours... |
|
397
|
|
|
$offset = $matches[1] * 3600 + $matches[2] * 60 - date('Z'); |
|
398
|
|
|
|
|
399
|
|
|
// The other time bits are simple! |
|
400
|
|
|
$interval = max((int) $_POST['regularity'], 1); |
|
401
|
|
|
$unit = in_array(substr($_POST['unit'], 0, 1), array('m', 'h', 'd', 'w')) ? substr($_POST['unit'], 0, 1) : 'd'; |
|
402
|
|
|
|
|
403
|
|
|
// Don't allow one minute intervals. |
|
404
|
|
|
if ($interval == 1 && $unit == 'm') |
|
405
|
|
|
$interval = 2; |
|
406
|
|
|
|
|
407
|
|
|
// Is it disabled? |
|
408
|
|
|
$disabled = !isset($_POST['enabled']) ? 1 : 0; |
|
409
|
|
|
|
|
410
|
|
|
// Do the update! |
|
411
|
|
|
$smcFunc['db_query']('', ' |
|
412
|
|
|
UPDATE {db_prefix}scheduled_tasks |
|
413
|
|
|
SET disabled = {int:disabled}, time_offset = {int:time_offset}, time_unit = {string:time_unit}, |
|
414
|
|
|
time_regularity = {int:time_regularity} |
|
415
|
|
|
WHERE id_task = {int:id_task}', |
|
416
|
|
|
array( |
|
417
|
|
|
'disabled' => $disabled, |
|
418
|
|
|
'time_offset' => $offset, |
|
419
|
|
|
'time_regularity' => $interval, |
|
420
|
|
|
'id_task' => $_GET['tid'], |
|
421
|
|
|
'time_unit' => $unit, |
|
422
|
|
|
) |
|
423
|
|
|
); |
|
424
|
|
|
|
|
425
|
|
|
// Check the next event. |
|
426
|
|
|
CalculateNextTrigger($_GET['tid'], true); |
|
427
|
|
|
|
|
428
|
|
|
// Return to the main list. |
|
429
|
|
|
redirectexit('action=admin;area=scheduledtasks'); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
// Load the task, understand? Que? Que? |
|
433
|
|
|
$request = $smcFunc['db_query']('', ' |
|
434
|
|
|
SELECT id_task, next_time, time_offset, time_regularity, time_unit, disabled, task |
|
435
|
|
|
FROM {db_prefix}scheduled_tasks |
|
436
|
|
|
WHERE id_task = {int:id_task}', |
|
437
|
|
|
array( |
|
438
|
|
|
'id_task' => $_GET['tid'], |
|
439
|
|
|
) |
|
440
|
|
|
); |
|
441
|
|
|
|
|
442
|
|
|
// Should never, ever, happen! |
|
443
|
|
|
if ($smcFunc['db_num_rows']($request) == 0) |
|
444
|
|
|
fatal_lang_error('no_access', false); |
|
|
|
|
|
|
445
|
|
|
|
|
446
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
447
|
|
|
{ |
|
448
|
|
|
$context['task'] = array( |
|
449
|
|
|
'id' => $row['id_task'], |
|
450
|
|
|
'function' => $row['task'], |
|
451
|
|
|
'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'], |
|
452
|
|
|
'desc' => isset($txt['scheduled_task_desc_' . $row['task']]) ? $txt['scheduled_task_desc_' . $row['task']] : '', |
|
453
|
|
|
'next_time' => $row['disabled'] ? $txt['scheduled_tasks_na'] : timeformat($row['next_time'] == 0 ? time() : $row['next_time'], true, 'server'), |
|
454
|
|
|
'disabled' => $row['disabled'], |
|
455
|
|
|
'offset' => $row['time_offset'], |
|
456
|
|
|
'regularity' => $row['time_regularity'], |
|
457
|
|
|
'offset_formatted' => date('H:i', $row['time_offset']), |
|
458
|
|
|
'unit' => $row['time_unit'], |
|
459
|
|
|
); |
|
460
|
|
|
} |
|
461
|
|
|
$smcFunc['db_free_result']($request); |
|
462
|
|
|
|
|
463
|
|
|
createToken('admin-st'); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* Show the log of all tasks that have taken place. |
|
468
|
|
|
* |
|
469
|
|
|
* @uses ManageScheduledTasks language file |
|
470
|
|
|
*/ |
|
471
|
|
|
function TaskLog() |
|
472
|
|
|
{ |
|
473
|
|
|
global $scripturl, $context, $txt, $smcFunc, $sourcedir; |
|
474
|
|
|
|
|
475
|
|
|
// Lets load the language just incase we are outside the Scheduled area. |
|
476
|
|
|
loadLanguage('ManageScheduledTasks'); |
|
477
|
|
|
|
|
478
|
|
|
// Empty the log? |
|
479
|
|
|
if (!empty($_POST['removeAll'])) |
|
480
|
|
|
{ |
|
481
|
|
|
checkSession(); |
|
482
|
|
|
validateToken('admin-tl'); |
|
483
|
|
|
|
|
484
|
|
|
$smcFunc['db_query']('truncate_table', ' |
|
485
|
|
|
TRUNCATE {db_prefix}log_scheduled_tasks', |
|
486
|
|
|
array( |
|
487
|
|
|
) |
|
488
|
|
|
); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
// Setup the list. |
|
492
|
|
|
$listOptions = array( |
|
493
|
|
|
'id' => 'task_log', |
|
494
|
|
|
'items_per_page' => 30, |
|
495
|
|
|
'title' => $txt['scheduled_log'], |
|
496
|
|
|
'no_items_label' => $txt['scheduled_log_empty'], |
|
497
|
|
|
'base_href' => $context['admin_area'] == 'scheduledtasks' ? $scripturl . '?action=admin;area=scheduledtasks;sa=tasklog' : $scripturl . '?action=admin;area=logs;sa=tasklog', |
|
498
|
|
|
'default_sort_col' => 'date', |
|
499
|
|
|
'get_items' => array( |
|
500
|
|
|
'function' => 'list_getTaskLogEntries', |
|
501
|
|
|
), |
|
502
|
|
|
'get_count' => array( |
|
503
|
|
|
'function' => 'list_getNumTaskLogEntries', |
|
504
|
|
|
), |
|
505
|
|
|
'columns' => array( |
|
506
|
|
|
'name' => array( |
|
507
|
|
|
'header' => array( |
|
508
|
|
|
'value' => $txt['scheduled_tasks_name'], |
|
509
|
|
|
), |
|
510
|
|
|
'data' => array( |
|
511
|
|
|
'db' => 'name' |
|
512
|
|
|
), |
|
513
|
|
|
), |
|
514
|
|
|
'date' => array( |
|
515
|
|
|
'header' => array( |
|
516
|
|
|
'value' => $txt['scheduled_log_time_run'], |
|
517
|
|
|
), |
|
518
|
|
|
'data' => array( |
|
519
|
|
|
'function' => function ($rowData) |
|
520
|
|
|
{ |
|
521
|
|
|
return timeformat($rowData['time_run'], true); |
|
522
|
|
|
}, |
|
523
|
|
|
), |
|
524
|
|
|
'sort' => array( |
|
525
|
|
|
'default' => 'lst.id_log DESC', |
|
526
|
|
|
'reverse' => 'lst.id_log', |
|
527
|
|
|
), |
|
528
|
|
|
), |
|
529
|
|
|
'time_taken' => array( |
|
530
|
|
|
'header' => array( |
|
531
|
|
|
'value' => $txt['scheduled_log_time_taken'], |
|
532
|
|
|
), |
|
533
|
|
|
'data' => array( |
|
534
|
|
|
'sprintf' => array( |
|
535
|
|
|
'format' => $txt['scheduled_log_time_taken_seconds'], |
|
536
|
|
|
'params' => array( |
|
537
|
|
|
'time_taken' => false, |
|
538
|
|
|
), |
|
539
|
|
|
), |
|
540
|
|
|
), |
|
541
|
|
|
'sort' => array( |
|
542
|
|
|
'default' => 'lst.time_taken', |
|
543
|
|
|
'reverse' => 'lst.time_taken DESC', |
|
544
|
|
|
), |
|
545
|
|
|
), |
|
546
|
|
|
), |
|
547
|
|
|
'form' => array( |
|
548
|
|
|
'href' => $context['admin_area'] == 'scheduledtasks' ? $scripturl . '?action=admin;area=scheduledtasks;sa=tasklog' : $scripturl . '?action=admin;area=logs;sa=tasklog', |
|
549
|
|
|
'token' => 'admin-tl', |
|
550
|
|
|
), |
|
551
|
|
|
'additional_rows' => array( |
|
552
|
|
|
array( |
|
553
|
|
|
'position' => 'below_table_data', |
|
554
|
|
|
'value' => ' |
|
555
|
|
|
<input type="submit" name="removeAll" value="' . $txt['scheduled_log_empty_log'] . '" data-confirm="' . $txt['scheduled_log_empty_log_confirm'] . '" class="button_submit you_sure">', |
|
556
|
|
|
), |
|
557
|
|
|
array( |
|
558
|
|
|
'position' => 'after_title', |
|
559
|
|
|
'value' => $txt['scheduled_tasks_time_offset'], |
|
560
|
|
|
'class' => 'windowbg2', |
|
561
|
|
|
), |
|
562
|
|
|
), |
|
563
|
|
|
); |
|
564
|
|
|
|
|
565
|
|
|
createToken('admin-tl'); |
|
566
|
|
|
|
|
567
|
|
|
require_once($sourcedir . '/Subs-List.php'); |
|
568
|
|
|
createList($listOptions); |
|
569
|
|
|
|
|
570
|
|
|
$context['sub_template'] = 'show_list'; |
|
571
|
|
|
$context['default_list'] = 'task_log'; |
|
572
|
|
|
|
|
573
|
|
|
// Make it all look tify. |
|
574
|
|
|
$context[$context['admin_menu_name']]['current_subsection'] = 'tasklog'; |
|
575
|
|
|
$context['page_title'] = $txt['scheduled_log']; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Callback function for createList() in TaskLog(). |
|
580
|
|
|
* |
|
581
|
|
|
* @param int $start The item to start with (for pagination purposes) |
|
582
|
|
|
* @param int $items_per_page How many items to display per page |
|
583
|
|
|
* @param string $sort A string indicating how to sort the results |
|
584
|
|
|
* @return array An array of info about task log entries |
|
585
|
|
|
*/ |
|
586
|
|
|
function list_getTaskLogEntries($start, $items_per_page, $sort) |
|
587
|
|
|
{ |
|
588
|
|
|
global $smcFunc, $txt; |
|
589
|
|
|
|
|
590
|
|
|
$request = $smcFunc['db_query']('', ' |
|
591
|
|
|
SELECT lst.id_log, lst.id_task, lst.time_run, lst.time_taken, st.task |
|
592
|
|
|
FROM {db_prefix}log_scheduled_tasks AS lst |
|
593
|
|
|
INNER JOIN {db_prefix}scheduled_tasks AS st ON (st.id_task = lst.id_task) |
|
594
|
|
|
ORDER BY {raw:sort} |
|
595
|
|
|
LIMIT {int:start}, {int:items}', |
|
596
|
|
|
array( |
|
597
|
|
|
'sort' => $sort, |
|
598
|
|
|
'start' => $start, |
|
599
|
|
|
'items' => $items_per_page, |
|
600
|
|
|
) |
|
601
|
|
|
); |
|
602
|
|
|
$log_entries = array(); |
|
603
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
604
|
|
|
$log_entries[] = array( |
|
605
|
|
|
'id' => $row['id_log'], |
|
606
|
|
|
'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'], |
|
607
|
|
|
'time_run' => $row['time_run'], |
|
608
|
|
|
'time_taken' => $row['time_taken'], |
|
609
|
|
|
); |
|
610
|
|
|
$smcFunc['db_free_result']($request); |
|
611
|
|
|
|
|
612
|
|
|
return $log_entries; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
/** |
|
616
|
|
|
* Callback function for createList() in TaskLog(). |
|
617
|
|
|
* @return int The number of log entries |
|
618
|
|
|
*/ |
|
619
|
|
|
function list_getNumTaskLogEntries() |
|
620
|
|
|
{ |
|
621
|
|
|
global $smcFunc; |
|
622
|
|
|
|
|
623
|
|
|
$request = $smcFunc['db_query']('', ' |
|
624
|
|
|
SELECT COUNT(*) |
|
625
|
|
|
FROM {db_prefix}log_scheduled_tasks', |
|
626
|
|
|
array( |
|
627
|
|
|
) |
|
628
|
|
|
); |
|
629
|
|
|
list ($num_entries) = $smcFunc['db_fetch_row']($request); |
|
630
|
|
|
$smcFunc['db_free_result']($request); |
|
631
|
|
|
|
|
632
|
|
|
return $num_entries; |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
?> |
|
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.