19 declare(strict_types=1);
57 $this->db = $db ?? $DIC->database();
62 $this->mail_ids_for_path_stmt = $this->db->prepare(
63 'SELECT COUNT(*) cnt FROM mail_attachment WHERE path = ?',
73 $attachment_paths = [];
75 $res = $this->db->query(
77 SELECT path, COUNT(mail_id) cnt_mail_ids 79 WHERE ' . $this->db->in(
81 $this->collector->mailIdsToDelete(),
88 while ($row = $this->db->fetchAssoc(
$res)) {
89 if ($i > 0 && $i % self::PING_THRESHOLD) {
93 $num_usages_total = (
int) $this->db->fetchAssoc(
95 $this->mail_ids_for_path_stmt,
99 $num_usages_within_deleted_mails = (
int) $row[
'cnt_mail_ids'];
101 if ($num_usages_within_deleted_mails >= $num_usages_total) {
102 $attachment_paths[] = $row[
'path'];
108 return $attachment_paths;
113 if ($this->delete_directory_callback !==
null) {
114 call_user_func($this->delete_directory_callback, $directory);
120 private function deleteAttachments():
void 125 foreach ($attachment_paths as
$path) {
126 if ($i > 0 && $i % self::PING_THRESHOLD) {
134 RecursiveIteratorIterator::CHILD_FIRST
137 foreach ($iter as $file) {
140 $path_name = $file->getPathname();
141 if ($file->isDir()) {
145 "Attachment directory '%s' deleted",
149 } elseif (is_file($path_name) && unlink($path_name)) {
152 "Attachment file '%s' deleted",
159 'Attachment file \'%s\' for mail_id could not be deleted due to missing file system permissions',
169 "Attachment directory '%s' deleted",
174 $this->
logger->warning($e->getMessage());
175 $this->
logger->warning($e->getTraceAsString());
181 $this->db->manipulate(
182 'DELETE FROM mail_attachment WHERE ' .
189 $this->db->manipulate(
190 'DELETE FROM mail WHERE ' .
197 if ((
int) $this->
settings->get(
'mail_notify_orphaned',
'0') >= 1) {
198 $this->db->manipulate(
199 'DELETE FROM mail_cron_orphaned WHERE ' .
203 $this->db->manipulate(
'DELETE FROM mail_cron_orphaned');
207 public function delete():
void 209 if ($this->collector->mailIdsToDelete() !== []) {
210 $this->deleteAttachments();
216 'Deleted mail_ids: %s',
217 implode(
', ', $this->collector->mailIdsToDelete())
224 'Deleted mail_cron_orphaned mail_ids: %s',
225 implode(
', ', $this->collector->mailIdsToDelete())
static getLogger(string $a_component_id)
Get component logger.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
readonly ilSetting $settings
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
determineDeletableAttachmentPaths()
deleteDirectory(string $directory)
readonly ilDBInterface $db
$delete_directory_callback
__construct(private readonly ilMailCronOrphanedMails $job, private readonly ExpiredOrOrphanedMailsCollector $collector, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ilLogger $logger=null, ?callable $delete_directory_callback=null)
readonly ilDBStatement $mail_ids_for_path_stmt
readonly ilLogger $logger