ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ExpiredOrOrphanedMailsCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilSetting;
26 use ilDBConstants;
28 use ilDBInterface;
29 
31 {
32  private const PING_THRESHOLD = 500;
33 
35  private ilDBInterface $db;
39  private array $mail_ids = [];
40 
41  public function __construct(
43  ?ilDBInterface $db = null,
44  ?ilSetting $setting = null,
45  ?ClockInterface $clock = 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 
88  $types = [ilDBConstants::T_TIMESTAMP];
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 }
$res
Definition: ltiservices.php:69
__construct(ilMailCronOrphanedMails $job, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ClockInterface $clock=null)
global $DIC
Definition: feed.php:28
ilSetting $setting
Definition: class.ilias.php:54
$i
Definition: metadata.php:41