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