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