ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilFileSystemCleanTempDirCron.php
Go to the documentation of this file.
1 <?php
2 
22 
29 {
30  protected \ILIAS\Filesystem\Filesystem $filesystem;
31 
32  protected ilLanguage $language;
33 
34  protected ilLogger $logger;
35 
39  public function __construct()
40  {
44  global $DIC;
45  if ($DIC->offsetExists('lng')) {
46  $this->language = $DIC['lng'];
47  }
48  if ($DIC->offsetExists('filesystem')) {
49  $this->filesystem = $DIC->filesystem()->temp();
50  }
51  if ($DIC->offsetExists('ilLoggerFactory')) {
52  $this->logger = $DIC->logger()->root();
53  }
54  }
55 
56  private function initDependencies(): void
57  {
58  }
59 
60  public function getId(): string
61  {
62  return "file_system_clean_temp_dir";
63  }
64 
65  public function getTitle(): string
66  {
67  return $this->language->txt('file_system_clean_temp_dir_cron');
68  }
69 
70  public function getDescription(): string
71  {
72  return $this->language->txt("file_system_clean_temp_dir_cron_info");
73  }
74 
75  public function hasAutoActivation(): bool
76  {
77  return false;
78  }
79 
80  public function hasFlexibleSchedule(): bool
81  {
82  return true;
83  }
84 
86  {
87  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
88  }
89 
90  public function getDefaultScheduleValue(): ?int
91  {
92  return 1;
93  }
94 
95  public function run(): ilCronJobResult
96  {
97  $this->initDependencies();
98  // only delete files and folders older than ten days to prevent issues with ongoing processes (e.g. zipping a folder)
99  $date = "until 10 day ago";
100 
101  // files are deleted before folders to prevent issues that would arise when trying to delete a (no longer existing) file in a deleted folder.
102  $files = $this->filesystem->finder()->in([""]);
103  $files = $files->files();
104  $files = $files->date($date);
105  $files = $files->getIterator();
106  $files->rewind();
107  $deleted_files = [];
108  while ($files->valid()) {
109  try {
110  $file_match = $files->current();
111  $path = $file_match->getPath();
112  if ($file_match->isFile()) {
113  $this->filesystem->delete($path);
114  $deleted_files[] = $path;
115  }
116  $files->next();
117  } catch (Throwable $t) {
118  $this->logger->error(
119  "Cron Job \"Clean temp directory\" could not delete " . $path
120  . "due to the following exception: " . $t->getMessage()
121  );
122  $files->next();
123  }
124  }
125 
126  // the folders are sorted based on their path length to ensure that nested folders are deleted first
127  // thereby preventing any issues due to deletion attempts on no longer existing folders.
128  $folders = $this->filesystem->finder()->in([""]);
129  $folders = $folders->directories();
130  $folders = $folders->date($date);
131  $folders = $folders->sort(fn (
132  Metadata $a,
133  Metadata $b
134  ): int => strlen($a->getPath()) - strlen($b->getPath()));
135  $folders = $folders->reverseSorting();
136  $folders = $folders->getIterator();
137 
138  $deleted_folders = [];
139 
140  $folders->rewind();
141  while ($folders->valid()) {
142  try {
143  $folder_match = $folders->current();
144  $path = $folder_match->getPath();
145  if ($folder_match->isDir()) {
146  $this->filesystem->deleteDir($path);
147  $deleted_folders[] = $path;
148  }
149  $folders->next();
150  } catch (Throwable $t) {
151  $this->logger->error(
152  "Cron Job \"Clean temp directory\" could not delete " . $path
153  . "due to the following exception: " . $t->getMessage()
154  );
155  $folders->next();
156  }
157  }
158 
159  $num_folders = count($deleted_folders);
160  $num_files = count($deleted_files);
161 
162  $result = new ilCronJobResult();
163  $result->setMessage($num_folders . " folders and " . $num_files . " files have been deleted.");
164  $result->setStatus(ilCronJobResult::STATUS_OK);
165  return $result;
166  }
167 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getPath()
The path to the file or directory.
Definition: Metadata.php:62
$path
Definition: ltiservices.php:30
global $DIC
Definition: shib_login.php:25
Class ilFileSystemCleanTempDirCron.
__construct(Container $dic, ilPlugin $plugin)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
language()
description: > Example for rendring a language glyph.
Definition: language.php:25