ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilLoggerCronCleanErrorFiles.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
24 {
25  protected const DEFAULT_VALUE_OLDER_THAN = 31;
26 
27  protected ilLanguage $lng;
28  protected ilSetting $settings;
30 
31  public function __construct()
32  {
33  global $DIC;
34 
35  $this->lng = $DIC->language();
36  $this->lng->loadLanguageModule("logging");
37  $this->settings = new ilSetting('log');
38  $this->error_settings = ilLoggingErrorSettings::getInstance();
39  }
40 
41  public function getId(): string
42  {
43  return "log_error_file_cleanup";
44  }
45 
46  public function getTitle(): string
47  {
48  return $this->lng->txt("log_error_file_cleanup_title");
49  }
50 
51  public function getDescription(): string
52  {
53  return $this->lng->txt("log_error_file_cleanup_info");
54  }
55 
57  {
58  return CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS;
59  }
60 
61  public function getDefaultScheduleValue(): int
62  {
63  return 10;
64  }
65 
66  public function hasAutoActivation(): bool
67  {
68  return false;
69  }
70 
71  public function hasFlexibleSchedule(): bool
72  {
73  return true;
74  }
75 
76  public function hasCustomSettings(): bool
77  {
78  return true;
79  }
80 
81  public function run(): ilCronJobResult
82  {
83  $result = new ilCronJobResult();
84  $folder = $this->error_settings->folder();
85  if (!is_dir($folder)) {
86  $result->setStatus(ilCronJobResult::STATUS_OK);
87  $result->setMessage($this->lng->txt("log_error_path_not_configured_or_wrong"));
88  return $result;
89  }
90 
91  $offset = $this->settings->get('clear_older_then', '');
92  if ($offset) {
93  $offset = (int) $offset;
94  } else {
95  $offset = self::DEFAULT_VALUE_OLDER_THAN;
96  }
97 
98  $files = $this->readLogDir($folder);
99  $delete_date = new ilDateTime(date("Y-m-d"), IL_CAL_DATE);
100  $delete_date->increment(ilDateTime::DAY, (-1 * $offset));
101 
102  foreach ($files as $file) {
103  $file_date = date("Y-m-d", filemtime($this->error_settings->folder() . "/" . $file));
104 
105  if ($file_date <= $delete_date->get(IL_CAL_DATE)) {
106  $this->deleteFile($this->error_settings->folder() . "/" . $file);
107  }
108  }
109 
110  $result->setStatus(ilCronJobResult::STATUS_OK);
111  return $result;
112  }
113 
114  protected function readLogDir(string $path): array
115  {
116  $ret = [];
117 
118  $folder = dir($path);
119  while ($file_name = $folder->read()) {
120  if (filetype($path . "/" . $file_name) != "dir") {
121  $ret[] = $file_name;
122  }
123  }
124  $folder->close();
125 
126  return $ret;
127  }
128 
129  protected function deleteFile(string $path): void
130  {
131  unlink($path);
132  }
133 
134  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
135  {
136  $offset = $this->settings->get('clear_older_then', '');
137  if (!$offset) {
138  $offset = (string) self::DEFAULT_VALUE_OLDER_THAN;
139  }
140 
141  $clear_older_then = new ilNumberInputGUI($this->lng->txt('frm_clear_older_then'), 'clear_older_then');
142  $clear_older_then->allowDecimals(false);
143  $clear_older_then->setMinValue(1, true);
144  $clear_older_then->setValue($offset);
145  $clear_older_then->setInfo($this->lng->txt('frm_clear_older_then_info'));
146 
147  $a_form->addItem($clear_older_then);
148  }
149 
150  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
151  {
152  $threshold = $a_form->getInput('clear_older_then');
153  if ((string) $threshold === '') {
154  $this->settings->delete('clear_older_then');
155  } else {
156  $this->settings->set('clear_older_then', (string) ((int) $a_form->getInput('clear_older_then')));
157  }
158 
159  return true;
160  }
161 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
const IL_CAL_DATE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...