Issues (1369)

includes/functions/sys_maintenance.php (1 issue)

Severity
1
<?php
2
3
function sys_maintenance()
4
{
5
  global $config;
6
7
  $bashing_time_limit = SN_TIME_NOW - $config->fleet_bashing_scope;
8
9
  // TODO: Move here some cleaning procedures from admin/maintenance.php
10
  // TODO: Add description of operation to log it
11
  $queries = array(
12
    // Cleaning outdated records from bashing table
13
    array('query' => "DELETE FROM `{{bashing}}` WHERE bashing_time < {$bashing_time_limit};", 'result' => false, 'error' => '', 'affected_rows' => 0),
14
    // Cleaning ACS table from empty records
15
    array('query' => 'DELETE FROM `{{aks}}` WHERE `id` NOT IN (SELECT DISTINCT `fleet_group` FROM `{{fleets}}`);', 'result' => false, 'error' => '', 'affected_rows' => 0),
16
    // Cleaning destroyed planets & moons which outlives it's time
17
    array('query' => "DELETE FROM `{{planets}}` WHERE `id_owner` = 0 AND `destruyed` < UNIX_TIMESTAMP();", 'result' => false, 'error' => '', 'affected_rows' => 0),
18
  );
19
20
  foreach($queries as &$query)
21
  {
22
    $query['result'] = doquery($query['query']);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

22
    $query['result'] = /** @scrutinizer ignore-deprecated */ doquery($query['query']);
Loading history...
23
    $query['error']  = SN::$db->db_error();
24
    $query['affected_rows']  = SN::$db->db_affected_rows();
25
  }
26
27
  return $queries;
28
}
29
30
// define('SCHEDULER_PREG2', '/^(?:(\w\@))?(?:(?:(?:(?:(?:(\d*)-)?(\d*)-)?(?:(\d*)\ ))?(?:(\d*):))?(?:(\d*):))?(\d*)?$/i');
31
32
// format: [<m|w|d|h|m|s>@]<time>
33
// first param: m - monthly, w - weekly, d - daily, h - hourly, i - minutly, s - secondly
34
// second param: [<months>-[<days|weeks> [<hours>:[<minutes>:]]]<seconds>
35
//        valid: '10' - runtime every 10 s
36
//        valid: '05:' or '05:00' - runtime every 5 m
37
//        valid: '02::' or '02:00:' or '02:00:00' - runtime every 2 h
38
//        etc
39
40
/*
41
 * Формат
42
 *
43
 * 1. Y-M-D H:I:S  - указывает раз в сколько времени должна запускаться задача и где Y, M, D, H, I, S - соответственно количество лет, месяцев, дней, часов, минут, секунд, определяющих интервал
44
 * TODO: 2. [<m|w|d|h|m|s>@]<time>
45
 */
46
47
function sys_schedule_get_prev_run($scheduleList, $recorded_run = SN_TIME_NOW, $return_next_run = false)
48
{
49
  static $date_part_names_reverse = array('seconds', 'minutes', 'hours', 'days', 'months', 'years',);
50
51
  $possible_schedules = array();
52
53
  $recorded_run = strtotime($recorded_run);
54
55
  $prev_run_array = getdate($recorded_run);
56
  $prev_run_array = array($prev_run_array['seconds'],$prev_run_array['minutes'],$prev_run_array['hours'],$prev_run_array['mday'],$prev_run_array['mon'],$prev_run_array['year']);
57
  $today_array = getdate(SN_TIME_NOW);
58
  $today_array = array($today_array['seconds'],$today_array['minutes'],$today_array['hours'],$today_array['mday'],$today_array['mon'],$today_array['year']);
59
  $scheduleList = explode(',', $scheduleList);
60
  array_walk($scheduleList, function(&$schedule) use ($prev_run_array, $today_array, $date_part_names_reverse, &$possible_schedules) {
61
    $schedule = array('schedule_array' => array_reverse(explode(':', trim($schedule))));
62
63
    $interval = $date_part_names_reverse[count($schedule['schedule_array'])];
64
65
    foreach($prev_run_array as $index => $date_part) {
66
      $schedule['array']['recorded'][$index] = isset($schedule['schedule_array'][$index]) ? intval($schedule['schedule_array'][$index]) : $date_part;
67
      $schedule['array']['now'][$index] = isset($schedule['schedule_array'][$index]) ? intval($schedule['schedule_array'][$index]) : $today_array[$index];
68
    }
69
    if($schedule['array']['recorded'] == $schedule['array']['now']) {
70
      unset($schedule['array']['now']);
71
    }
72
73
    foreach($schedule['array'] as $name => $array) {
74
      $schedule['string'][$name] = "{$array[5]}-{$array[4]}-{$array[3]} {$array[2]}:{$array[1]}:{$array[0]}";
75
      $schedule['string'][$name . '_next'] = $schedule['string'][$name] . ' +1 ' . $interval;
76
      $schedule['string'][$name . '_prev'] = $schedule['string'][$name] . ' -1 ' . $interval;
77
    }
78
79
    foreach($schedule['string'] as $string) {
80
      $timestamp = strtotime($string);
81
      $schedule['timestamp'][$timestamp] = $possible_schedules[$timestamp] = date(FMT_DATE_TIME_SQL, strtotime($string));
82
    }
83
  });
84
85
  ksort($possible_schedules);
86
87
  $prev_run = 0;
88
  $next_run = 0;
89
  foreach($possible_schedules as $timestamp => $string_date) {
90
    $prev_run = SN_TIME_NOW >= $timestamp ? $timestamp : $prev_run;
91
    $next_run = SN_TIME_NOW < $timestamp && !$next_run ? $timestamp : $next_run;
92
  }
93
94
  return $return_next_run ? $next_run : $prev_run;
95
}
96