ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLoggerCronCleanErrorFiles.php
Go to the documentation of this file.
1 <?php
2 
3 require_once("Services/Cron/classes/class.ilCronJob.php");
4 require_once("Services/Logging/classes/error/class.ilLoggingErrorSettings.php");
5 require_once("Services/Administration/classes/class.ilSetting.php");
6 require_once("Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
7 require_once("Services/Form/classes/class.ilTextInputGUI.php");
8 require_once("Services/Calendar/classes/class.ilDateTime.php");
9 require_once("Services/Cron/classes/class.ilCronJobResult.php");
10 require_once("Services/Form/classes/class.ilPropertyFormGUI.php");
11 
12 
14 {
16 
17  public function __construct()
18  {
19  global $DIC;
20 
21  $lng = $DIC['lng'];
22 
23  $this->lng = $lng;
24  $this->lng->loadLanguageModule("logging");
25  $this->settings = new ilSetting('log');
26  $this->error_settings = ilLoggingErrorSettings::getInstance();
27  }
28 
32  public function getId()
33  {
34  return "log_error_file_cleanup";
35  }
36 
40  public function getTitle()
41  {
42  return $this->lng->txt("log_error_file_cleanup_title");
43  }
44 
48  public function getDescription()
49  {
50  return $this->lng->txt("log_error_file_cleanup_info");
51  }
52 
56  public function getDefaultScheduleType()
57  {
58  return self::SCHEDULE_TYPE_IN_DAYS;
59  }
60 
64  public function getDefaultScheduleValue()
65  {
66  return 10;
67  }
68 
72  public function hasAutoActivation()
73  {
74  return false;
75  }
76 
80  public function hasFlexibleSchedule()
81  {
82  return true;
83  }
84 
88  public function hasCustomSettings()
89  {
90  return true;
91  }
92 
96  public function run()
97  {
98  $result = new ilCronJobResult();
99  $folder = $this->error_settings->folder();
100  if (!is_dir($folder)) {
102  $result->setMessage($this->lng->txt("log_error_path_not_configured_or_wrong"));
103  return $result;
104  }
105 
106  $offset = $this->settings->get('clear_older_then');
107  if (!$offset) {
108  $offset = self::DEFAULT_VALUE_OLDER_THAN;
109  }
110 
111  $files = $this->readLogDir($folder);
112  $delete_date = new ilDateTime(date("Y-m-d"), IL_CAL_DATE);
113  $delete_date->increment(ilDateTime::DAY, (-1 * $offset));
114 
115  foreach ($files as $file) {
116  $file_date = date("Y-m-d", filemtime($this->error_settings->folder() . "/" . $file));
117 
118  if ($file_date <= $delete_date->get(IL_CAL_DATE)) {
119  $this->deleteFile($this->error_settings->folder() . "/" . $file);
120  }
121  }
122 
124  return $result;
125  }
126 
127  protected function readLogDir($path)
128  {
129  $ret = array();
130 
131  $folder = dir($path);
132  while ($file_name = $folder->read()) {
133  if (filetype($path . "/" . $file_name) != "dir") {
134  $ret[] = $file_name;
135  }
136  }
137  $folder->close();
138 
139  return $ret;
140  }
141 
142  protected function deleteFile($path)
143  {
144  unlink($path);
145  }
146 
151  {
152  $offset = $this->settings->get('clear_older_then');
153  if (!$offset) {
154  $offset = self::DEFAULT_VALUE_OLDER_THAN;
155  }
156  $clear_older_then = new ilNumberInputGUI($this->lng->txt('frm_clear_older_then'), 'clear_older_then');
157  $clear_older_then->allowDecimals(false);
158  $clear_older_then->setMinValue(1, true);
159  $clear_older_then->setValue($offset);
160  $clear_older_then->setInfo($this->lng->txt('frm_clear_older_then_info'));
161 
162  $a_form->addItem($clear_older_then);
163  }
164 
169  public function saveCustomSettings(ilPropertyFormGUI $a_form)
170  {
171  $this->settings->set('clear_older_then', $a_form->getInput('clear_older_then'));
172  return true;
173  }
174 }
$path
Definition: aliased.php:25
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
settings()
Definition: settings.php:2
$files
Definition: metarefresh.php:49
$result
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
Cron job application base class.
addItem($a_item)
Add Item (Property, SectionHeader).
allowDecimals($a_value)
Toggle Decimals.
$lng
This class represents a number property in a property form.
Date and time handling
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
const IL_CAL_DATE
$ret
Definition: parser.php:6
Cron job result data container.