ILIAS  release_8 Revision v8.24
ExpiredOrOrphanedMailsCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ilSetting;
29
31{
32 private const PING_THRESHOLD = 500;
33
39 private array $mail_ids = [];
40
41 public function __construct(
43 ?ilDBInterface $db = null,
44 ?ilSetting $setting = null,
46 ) {
47 global $DIC;
48
49 $this->db = $db ?? $DIC->database();
50 $this->settings = $setting ?? $DIC->settings();
51 $this->clock = $clock ?? (new Factory())->clock()->system();
52
53 $this->job = $job;
54
55 $this->collect();
56 }
57
58 private function collect(): void
59 {
60 $mail_only_inbox_trash = (bool) $this->settings->get('mail_only_inbox_trash', '0');
61 $mail_expiration_warning_days = (int) $this->settings->get('mail_notify_orphaned', '0');
62
63 if ($mail_expiration_warning_days > 0) {
64 if ($mail_only_inbox_trash) {
65 // Only select determine mails which are now located in the inbox or trash folder
66 $res = $this->db->queryF(
67 "
68 SELECT mail_id FROM mail_cron_orphaned
69 LEFT JOIN mail_obj_data mdata ON mdata.obj_id = folder_id
70 WHERE ts_do_delete <= %s AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)
71 ",
73 [$this->clock->now()->getTimestamp(), 'inbox', 'trash']
74 );
75 } else {
76 // Select all determined emails independently of the folder
77 $res = $this->db->queryF(
78 "SELECT mail_id FROM mail_cron_orphaned WHERE ts_do_delete <= %s",
80 [$this->clock->now()->getTimestamp()]
81 );
82 }
83 } else {
84 // Mails should be deleted without notification
85 $mail_expiration_days = (int) $this->settings->get('mail_threshold', '0');
86 $left_interval_datetime = $this->clock->now()->modify('- ' . $mail_expiration_days . ' days');
87
89 $data = [$left_interval_datetime->format('Y-m-d 23:59:59')];
90
91 $mails_query = "
92 SELECT m.mail_id
93 FROM mail m
94 LEFT JOIN mail_obj_data mdata ON mdata.obj_id = m.folder_id
95 WHERE m.send_time <= %s
96 ";
97
98 if ($mail_only_inbox_trash) {
99 $mails_query .= " AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)";
100 array_push($types, ilDBConstants::T_TEXT, ilDBConstants::T_TEXT);
101 array_push($data, 'inbox', 'trash');
102 }
103
104 $res = $this->db->queryF($mails_query, $types, $data);
105 }
106
107 $i = 0;
108 while ($row = $this->db->fetchAssoc($res)) {
109 if ($i > 0 && $i % self::PING_THRESHOLD) {
110 $this->job->ping();
111 }
112
113 $this->addMailIdToDelete((int) $row['mail_id']);
114
115 ++$i;
116 }
117 }
118
119 private function addMailIdToDelete(int $mail_id): void
120 {
121 $this->mail_ids[] = $mail_id;
122 }
123
127 public function mailIdsToDelete(): array
128 {
129 return $this->mail_ids;
130 }
131}
Builds data types.
Definition: Factory.php:21
__construct(ilMailCronOrphanedMails $job, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ClockInterface $clock=null)
ilSetting $setting
Definition: class.ilias.php:54
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$i
Definition: metadata.php:41